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);
}
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();
}
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());
}
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());
}
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();
}
Aggregations