use of io.opencensus.trace.Span in project instrumentation-java by census-instrumentation.
the class BasicOperationsBenchmark method createSpanWithCurrentSpan.
/**
* Create a child span from the current span.
*/
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Span createSpanWithCurrentSpan(Data data) {
Span span = data.tracer.spanBuilder("ChildSpanFromCurrent").setRecordEvents(data.recorded).setSampler(data.sampled ? Samplers.alwaysSample() : Samplers.neverSample()).startSpan();
span.end();
return span;
}
use of io.opencensus.trace.Span in project instrumentation-java by census-instrumentation.
the class SpanOperationsBenchmark method addAnnotationWithAttributes.
/**
* Add an annotation with attributes.
*/
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Span addAnnotationWithAttributes(Data data) {
Span span = data.annotationSpanAttributes;
span.addAnnotation(ANNOTATION_DESCRIPTION, data.attributeMap);
return span;
}
use of io.opencensus.trace.Span in project instrumentation-java by census-instrumentation.
the class ContextUtilsTest method testGetCurrentSpan_ContextSetToNull.
@Test
public void testGetCurrentSpan_ContextSetToNull() {
ContextHandle orig = ContextHandleUtils.withValue(ContextHandleUtils.currentContext(), null).attach();
try {
Span span = ContextHandleUtils.getValue(ContextHandleUtils.currentContext());
// ContextUtils.getValue always returns non-null.
assertThat(span).isEqualTo(BlankSpan.INSTANCE);
} finally {
ContextHandleUtils.currentContext().detach(orig);
}
}
use of io.opencensus.trace.Span in project instrumentation-java by census-instrumentation.
the class MetricReader method readAndExport.
/**
* Reads the metrics from the {@link MetricProducerManager} and exports them to the {@code
* metricExporter}.
*
* @param metricExporter the exporter called to export the metrics read.
* @since 0.19
*/
public void readAndExport(MetricExporter metricExporter) {
Span span = tracer.spanBuilder(spanName).setRecordEvents(true).setSampler(probabilitySampler).startSpan();
Scope scope = tracer.withSpan(span);
try {
ArrayList<Metric> metricsList = new ArrayList<>();
for (MetricProducer metricProducer : metricProducerManager.getAllMetricProducer()) {
metricsList.addAll(metricProducer.getMetrics());
}
metricExporter.export(metricsList);
} catch (Throwable e) {
logger.log(Level.WARNING, "Exception thrown by the metrics exporter.", e);
span.setStatus(Status.UNKNOWN.withDescription("Exception when export metrics: " + exceptionMessage(e)));
} finally {
scope.close();
span.end();
}
}
use of io.opencensus.trace.Span in project instrumentation-java by census-instrumentation.
the class MultiSpansTracing method doWork.
private static void doWork() {
Span rootSpan = tracer.spanBuilderWithExplicitParent("MyRootSpan", null).startSpan();
rootSpan.addAnnotation("Annotation to the root Span before child is created.");
Span childSpan = tracer.spanBuilderWithExplicitParent("MyChildSpan", rootSpan).startSpan();
childSpan.addAnnotation("Annotation to the child Span");
childSpan.end();
rootSpan.addAnnotation("Annotation to the root Span after child is ended.");
rootSpan.end();
}
Aggregations