Search in sources :

Example 1 with Span

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;
}
Also used : Span(io.opencensus.trace.Span) BenchmarkMode(org.openjdk.jmh.annotations.BenchmarkMode) Benchmark(org.openjdk.jmh.annotations.Benchmark) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit)

Example 2 with 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;
}
Also used : Span(io.opencensus.trace.Span) BenchmarkMode(org.openjdk.jmh.annotations.BenchmarkMode) Benchmark(org.openjdk.jmh.annotations.Benchmark) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit)

Example 3 with 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);
    }
}
Also used : ContextHandle(io.opencensus.trace.ContextHandle) BlankSpan(io.opencensus.trace.BlankSpan) Span(io.opencensus.trace.Span) Test(org.junit.Test)

Example 4 with Span

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();
    }
}
Also used : Scope(io.opencensus.common.Scope) ArrayList(java.util.ArrayList) MetricProducer(io.opencensus.metrics.export.MetricProducer) Metric(io.opencensus.metrics.export.Metric) Span(io.opencensus.trace.Span)

Example 5 with Span

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();
}
Also used : Span(io.opencensus.trace.Span)

Aggregations

Span (io.opencensus.trace.Span)47 Test (org.junit.Test)17 Benchmark (org.openjdk.jmh.annotations.Benchmark)14 BenchmarkMode (org.openjdk.jmh.annotations.BenchmarkMode)14 OutputTimeUnit (org.openjdk.jmh.annotations.OutputTimeUnit)14 Scope (io.opencensus.common.Scope)8 BlankSpan (io.opencensus.trace.BlankSpan)8 ArrayList (java.util.ArrayList)4 SpanBuilder (io.opencensus.trace.SpanBuilder)3 SpanContext (io.opencensus.trace.SpanContext)3 ApiException (com.google.api.gax.rpc.ApiException)2 ByteString (com.google.protobuf.ByteString)2 HttpRequestContext (io.opencensus.contrib.http.HttpRequestContext)2 Metric (io.opencensus.metrics.export.Metric)2 TagContext (io.opencensus.tags.TagContext)2 AttributeValue (io.opencensus.trace.AttributeValue)2 SpanId (io.opencensus.trace.SpanId)2 TraceId (io.opencensus.trace.TraceId)2 TraceConfig (io.opencensus.trace.config.TraceConfig)2 TraceParams (io.opencensus.trace.config.TraceParams)2