use of io.opencensus.trace.TraceId in project instrumentation-java by census-instrumentation.
the class SpanBuilderImpl method startSpanInternal.
private Span startSpanInternal(@Nullable SpanContext parentContext, @Nullable Boolean hasRemoteParent, String name, @Nullable Sampler sampler, List<Span> parentLinks, @Nullable Boolean recordEvents, @Nullable Kind kind, @Nullable Span parentSpan) {
TraceParams activeTraceParams = options.traceConfig.getActiveTraceParams();
Random random = options.randomHandler.current();
TraceId traceId;
SpanId spanId = SpanId.generateRandomId(random);
SpanId parentSpanId = null;
// TODO(bdrutu): Handle tracestate correctly not just propagate.
Tracestate tracestate = TRACESTATE_DEFAULT;
if (parentContext == null || !parentContext.isValid()) {
// New root span.
traceId = TraceId.generateRandomId(random);
// This is a root span so no remote or local parent.
hasRemoteParent = null;
} else {
// New child span.
traceId = parentContext.getTraceId();
parentSpanId = parentContext.getSpanId();
tracestate = parentContext.getTracestate();
}
TraceOptions traceOptions = makeSamplingDecision(parentContext, hasRemoteParent, name, sampler, parentLinks, traceId, spanId, activeTraceParams) ? SAMPLED_TRACE_OPTIONS : NOT_SAMPLED_TRACE_OPTIONS;
if (traceOptions.isSampled() || Boolean.TRUE.equals(recordEvents)) {
// Pass the timestamp converter from the parent to ensure that the recorded events are in
// the right order. Implementation uses System.nanoTime() which is monotonically increasing.
TimestampConverter timestampConverter = null;
if (parentSpan instanceof RecordEventsSpanImpl) {
RecordEventsSpanImpl parentRecordEventsSpan = (RecordEventsSpanImpl) parentSpan;
timestampConverter = parentRecordEventsSpan.getTimestampConverter();
parentRecordEventsSpan.addChild();
}
Span span = RecordEventsSpanImpl.startSpan(SpanContext.create(traceId, spanId, traceOptions, tracestate), name, kind, parentSpanId, hasRemoteParent, activeTraceParams, options.startEndHandler, timestampConverter, options.clock);
linkSpans(span, parentLinks);
return span;
} else {
return NoRecordEventsSpanImpl.create(SpanContext.create(traceId, spanId, traceOptions, tracestate));
}
}
use of io.opencensus.trace.TraceId in project instrumentation-java by census-instrumentation.
the class B3Format method extract.
@Override
public <C> /*>>> extends @NonNull Object*/
SpanContext extract(C carrier, Getter<C> getter) throws SpanContextParseException {
checkNotNull(carrier, "carrier");
checkNotNull(getter, "getter");
try {
TraceId traceId;
String traceIdStr = getter.get(carrier, X_B3_TRACE_ID);
if (traceIdStr != null) {
if (traceIdStr.length() == TraceId.SIZE) {
// This is an 8-byte traceID.
traceIdStr = UPPER_TRACE_ID + traceIdStr;
}
traceId = TraceId.fromLowerBase16(traceIdStr);
} else {
throw new SpanContextParseException("Missing X_B3_TRACE_ID.");
}
SpanId spanId;
String spanIdStr = getter.get(carrier, X_B3_SPAN_ID);
if (spanIdStr != null) {
spanId = SpanId.fromLowerBase16(spanIdStr);
} else {
throw new SpanContextParseException("Missing X_B3_SPAN_ID.");
}
TraceOptions traceOptions = TraceOptions.DEFAULT;
if (SAMPLED_VALUE.equals(getter.get(carrier, X_B3_SAMPLED)) || FLAGS_VALUE.equals(getter.get(carrier, X_B3_FLAGS))) {
traceOptions = TraceOptions.builder().setIsSampled(true).build();
}
return SpanContext.create(traceId, spanId, traceOptions, TRACESTATE_DEFAULT);
} catch (IllegalArgumentException e) {
throw new SpanContextParseException("Invalid input.", e);
}
}
use of io.opencensus.trace.TraceId in project instrumentation-java by census-instrumentation.
the class SpanBuilderImplTest method startRemoteChildSpan_WithProbabilitySamplerDefaultSampler.
@Test
public void startRemoteChildSpan_WithProbabilitySamplerDefaultSampler() {
when(traceConfig.getActiveTraceParams()).thenReturn(TraceParams.DEFAULT);
// This traceId will not be sampled by the ProbabilitySampler because the first 8 bytes as long
// is not less than probability * Long.MAX_VALUE;
TraceId traceId = TraceId.fromBytes(new byte[] { (byte) 0x8F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0, 0, 0, 0, 0, 0, 0, 0 });
// If parent is sampled then the remote child must be sampled.
Span childSpan = SpanBuilderImpl.createWithRemoteParent(SPAN_NAME, SpanContext.create(traceId, SpanId.generateRandomId(randomHandler.current()), TraceOptions.builder().setIsSampled(true).build()), spanBuilderOptions).startSpan();
assertThat(childSpan.getContext().isValid()).isTrue();
assertThat(childSpan.getContext().getTraceId()).isEqualTo(traceId);
assertThat(childSpan.getContext().getTraceOptions().isSampled()).isTrue();
childSpan.end();
assertThat(traceConfig.getActiveTraceParams()).isEqualTo(TraceParams.DEFAULT);
// If parent is not sampled then the remote child must be not sampled.
childSpan = SpanBuilderImpl.createWithRemoteParent(SPAN_NAME, SpanContext.create(traceId, SpanId.generateRandomId(randomHandler.current()), TraceOptions.DEFAULT), spanBuilderOptions).startSpan();
assertThat(childSpan.getContext().isValid()).isTrue();
assertThat(childSpan.getContext().getTraceId()).isEqualTo(traceId);
assertThat(childSpan.getContext().getTraceOptions().isSampled()).isFalse();
childSpan.end();
}
Aggregations