Search in sources :

Example 11 with Span

use of io.opencensus.trace.Span in project instrumentation-java by census-instrumentation.

the class SpanBuilderImplTest method startChildSpan_WithSpecifiedSampler.

@Test
public void startChildSpan_WithSpecifiedSampler() {
    Span rootSpan = SpanBuilderImpl.createWithParent(SPAN_NAME, null, spanBuilderOptions).setSampler(Samplers.alwaysSample()).startSpan();
    assertThat(rootSpan.getContext().isValid()).isTrue();
    assertThat(rootSpan.getContext().getTraceOptions().isSampled()).isTrue();
    // Apply the given sampler for child spans.
    Span childSpan = SpanBuilderImpl.createWithParent(SPAN_NAME, rootSpan, spanBuilderOptions).setSampler(Samplers.neverSample()).startSpan();
    assertThat(childSpan.getContext().isValid()).isTrue();
    assertThat(childSpan.getContext().getTraceId()).isEqualTo(rootSpan.getContext().getTraceId());
    assertThat(childSpan.getContext().getTraceOptions().isSampled()).isFalse();
}
Also used : Span(io.opencensus.trace.Span) Test(org.junit.Test)

Example 12 with Span

use of io.opencensus.trace.Span in project instrumentation-java by census-instrumentation.

the class HttpClientHandler method handleStart.

/**
 * Instrument a request for tracing and stats before it is sent.
 *
 * <p>This method will create a span in current context to represent the HTTP call. The created
 * span will be serialized and propagated to the server.
 *
 * <p>The generated span will NOT be set as current context. User can control when to enter the
 * scope of this span. Use {@link AbstractHttpHandler#getSpanFromContext} to retrieve the span.
 *
 * @param parent the parent {@link Span}. {@code null} indicates using current span.
 * @param carrier the entity that holds the HTTP information.
 * @param request the request entity.
 * @return the {@link HttpRequestContext} that contains stats and trace data associated with the
 *     request.
 * @since 0.19
 */
public HttpRequestContext handleStart(@Nullable Span parent, C carrier, Q request) {
    checkNotNull(carrier, "carrier");
    checkNotNull(request, "request");
    if (parent == null) {
        parent = tracer.getCurrentSpan();
    }
    String spanName = getSpanName(request, extractor);
    SpanBuilder builder = tracer.spanBuilderWithExplicitParent(spanName, parent);
    Span span = builder.setSpanKind(Kind.CLIENT).startSpan();
    if (span.getOptions().contains(Options.RECORD_EVENTS)) {
        addSpanRequestAttributes(span, request, extractor);
    }
    // inject propagation header
    SpanContext spanContext = span.getContext();
    if (!spanContext.equals(SpanContext.INVALID)) {
        textFormat.inject(spanContext, carrier, setter);
    }
    return getNewContext(span, tagger.getCurrentTagContext());
}
Also used : SpanBuilder(io.opencensus.trace.SpanBuilder) SpanContext(io.opencensus.trace.SpanContext) Span(io.opencensus.trace.Span)

Example 13 with Span

use of io.opencensus.trace.Span in project instrumentation-java by census-instrumentation.

the class StartEndSpanBenchmark method startEndSampledChildSpan.

/**
 * This benchmark attempts to measure performance of start/end for a sampled child {@code Span}.
 */
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Span startEndSampledChildSpan(Data data) {
    Span span = data.tracer.spanBuilderWithExplicitParent(SPAN_NAME, data.rootSpan).setSampler(Samplers.alwaysSample()).startSpan();
    span.end();
    return span;
}
Also used : Span(io.opencensus.trace.Span) BlankSpan(io.opencensus.trace.BlankSpan) BenchmarkMode(org.openjdk.jmh.annotations.BenchmarkMode) Benchmark(org.openjdk.jmh.annotations.Benchmark) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit)

Example 14 with Span

use of io.opencensus.trace.Span in project instrumentation-java by census-instrumentation.

the class StartEndSpanBenchmark method startEndSampledRootSpan.

/**
 * This benchmark attempts to measure performance of start/end for a sampled root {@code Span}.
 */
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Span startEndSampledRootSpan(Data data) {
    Span span = data.tracer.spanBuilder(SPAN_NAME).setSampler(Samplers.alwaysSample()).startSpan();
    span.end();
    return span;
}
Also used : Span(io.opencensus.trace.Span) BlankSpan(io.opencensus.trace.BlankSpan) BenchmarkMode(org.openjdk.jmh.annotations.BenchmarkMode) Benchmark(org.openjdk.jmh.annotations.Benchmark) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit)

Example 15 with Span

use of io.opencensus.trace.Span in project instrumentation-java by census-instrumentation.

the class StartEndSpanBenchmark method startEndNonSampledChildSpan.

/**
 * This benchmark attempts to measure performance of start/end for a non-sampled child {@code
 * Span}.
 */
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Span startEndNonSampledChildSpan(Data data) {
    Span span = data.tracer.spanBuilderWithExplicitParent(SPAN_NAME, data.rootSpan).setSampler(Samplers.neverSample()).startSpan();
    span.end();
    return span;
}
Also used : Span(io.opencensus.trace.Span) BlankSpan(io.opencensus.trace.BlankSpan) BenchmarkMode(org.openjdk.jmh.annotations.BenchmarkMode) Benchmark(org.openjdk.jmh.annotations.Benchmark) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit)

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