Search in sources :

Example 1 with SpanContext

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

the class BinaryFormatImplTest method fromBinaryValue_MissingTraceOptionsOk.

@Test
public void fromBinaryValue_MissingTraceOptionsOk() throws SpanContextParseException {
    SpanContext extracted = binaryFormat.fromByteArray(new byte[] { 0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1, 97, 98, 99, 100, 101, 102, 103, 104 });
    assertThat(extracted.isValid()).isTrue();
    assertThat(extracted.getTraceOptions()).isEqualTo(TraceOptions.DEFAULT);
}
Also used : SpanContext(io.opencensus.trace.SpanContext) Test(org.junit.Test)

Example 2 with SpanContext

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

the class SpanBuilderImplTest method startRemoteSpan.

@Test
public void startRemoteSpan() {
    SpanContext spanContext = SpanContext.create(TraceId.generateRandomId(randomHandler.current()), SpanId.generateRandomId(randomHandler.current()), TraceOptions.DEFAULT);
    RecordEventsSpanImpl span = (RecordEventsSpanImpl) SpanBuilderImpl.createWithRemoteParent(SPAN_NAME, spanContext, spanBuilderOptions).setRecordEvents(true).startSpan();
    assertThat(span.getContext().isValid()).isTrue();
    assertThat(span.getContext().getTraceId()).isEqualTo(spanContext.getTraceId());
    assertThat(span.getContext().getTraceOptions().isSampled()).isTrue();
    SpanData spanData = span.toSpanData();
    assertThat(spanData.getParentSpanId()).isEqualTo(spanContext.getSpanId());
    assertThat(spanData.getHasRemoteParent()).isTrue();
}
Also used : SpanContext(io.opencensus.trace.SpanContext) SpanData(io.opencensus.trace.export.SpanData) Test(org.junit.Test)

Example 3 with SpanContext

use of io.opencensus.trace.SpanContext 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 4 with SpanContext

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

the class AppEngineCloudTraceContextUtilsTest method toFromSampledCloudTraceContext.

@Test
public void toFromSampledCloudTraceContext() {
    CloudTraceContext cloudTraceContext = new CloudTraceContext(// fae1c6346b9cf9a272cb6504b5a10dcc/123456789.
    new byte[] { (byte) 0x09, (byte) 0xa2, (byte) 0xf9, (byte) 0x9c, (byte) 0x6b, (byte) 0x34, (byte) 0xc6, (byte) 0xe1, (byte) 0xfa, (byte) 0x11, (byte) 0xcc, (byte) 0x0d, (byte) 0xa1, (byte) 0xb5, (byte) 0x04, (byte) 0x65, (byte) 0xcb, (byte) 0x72 }, Long.MIN_VALUE, // Trace enabled.
    1L);
    SpanContext spanContext = AppEngineCloudTraceContextUtils.fromCloudTraceContext(cloudTraceContext);
    assertThat(spanContext).isEqualTo(SpanContext.create(TraceId.fromLowerBase16("fae1c6346b9cf9a272cb6504b5a10dcc"), SpanId.fromLowerBase16("8000000000000000"), TraceOptions.builder().setIsSampled(true).build()));
    // CloudTraceContext does not implement equals, so need to check every argument.
    CloudTraceContext newCloudTraceContext = AppEngineCloudTraceContextUtils.toCloudTraceContext(spanContext);
    assertThat(newCloudTraceContext.getTraceId()).isEqualTo(cloudTraceContext.getTraceId());
    assertThat(newCloudTraceContext.getSpanId()).isEqualTo(cloudTraceContext.getSpanId());
    assertThat(newCloudTraceContext.getTraceMask()).isEqualTo(cloudTraceContext.getTraceMask());
}
Also used : SpanContext(io.opencensus.trace.SpanContext) CloudTraceContext(com.google.apphosting.api.CloudTraceContext) Test(org.junit.Test)

Example 5 with SpanContext

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

the class AppEngineCloudTraceContextUtilsTest method toFromNotSampledCloudTraceContext.

@Test
public void toFromNotSampledCloudTraceContext() {
    CloudTraceContext cloudTraceContext = new CloudTraceContext(// fae1c6346b9cf9a272cb6504b5a10dcc/123456789.
    new byte[] { (byte) 0x09, (byte) 0xa2, (byte) 0xf9, (byte) 0x9c, (byte) 0x6b, (byte) 0x34, (byte) 0xc6, (byte) 0xe1, (byte) 0xfa, (byte) 0x11, (byte) 0xcc, (byte) 0x0d, (byte) 0xa1, (byte) 0xb5, (byte) 0x04, (byte) 0x65, (byte) 0xcb, (byte) 0x72 }, Long.MIN_VALUE, // Trace disabled.
    0L);
    SpanContext spanContext = AppEngineCloudTraceContextUtils.fromCloudTraceContext(cloudTraceContext);
    assertThat(spanContext).isEqualTo(SpanContext.create(TraceId.fromLowerBase16("fae1c6346b9cf9a272cb6504b5a10dcc"), SpanId.fromLowerBase16("8000000000000000"), TraceOptions.builder().setIsSampled(false).build()));
    // CloudTraceContext does not implement equals, so need to check every argument.
    assertThat(cloudTraceContextEquals(AppEngineCloudTraceContextUtils.toCloudTraceContext(spanContext), cloudTraceContext)).isTrue();
}
Also used : SpanContext(io.opencensus.trace.SpanContext) CloudTraceContext(com.google.apphosting.api.CloudTraceContext) Test(org.junit.Test)

Aggregations

SpanContext (io.opencensus.trace.SpanContext)21 Test (org.junit.Test)8 Timestamp (io.opencensus.common.Timestamp)6 AttributeValue (io.opencensus.trace.AttributeValue)4 Status (io.opencensus.trace.Status)4 SpanData (io.opencensus.trace.export.SpanData)4 Span (io.opencensus.trace.Span)3 SpanId (io.opencensus.trace.SpanId)3 ArrayList (java.util.ArrayList)3 CloudTraceContext (com.google.apphosting.api.CloudTraceContext)2 Context (io.grpc.Context)2 Annotation (io.opencensus.trace.Annotation)2 SpanBuilder (io.opencensus.trace.SpanBuilder)2 Map (java.util.Map)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Span (com.google.devtools.cloudtrace.v2.Span)1 SpanName (com.google.devtools.cloudtrace.v2.SpanName)1 TruncatableString (com.google.devtools.cloudtrace.v2.TruncatableString)1 Metadata (io.grpc.Metadata)1 ServerStreamTracer (io.grpc.ServerStreamTracer)1