use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.
the class JaegerPropagator method extract.
@Override
public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
if (context == null) {
return Context.root();
}
if (getter == null) {
return context;
}
SpanContext spanContext = getSpanContextFromHeader(carrier, getter);
if (spanContext.isValid()) {
context = context.with(Span.wrap(spanContext));
}
Baggage baggage = getBaggageFromHeader(carrier, getter);
if (baggage != null) {
context = context.with(baggage);
}
return context;
}
use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.
the class ImmutableSpanContextTest method testSkipIdValidationAndInvalidTraceId.
@Test
public void testSkipIdValidationAndInvalidTraceId() {
SpanContext spanContext = ImmutableSpanContext.create(TraceId.getInvalid(), SPAN_ID, TraceFlags.getDefault(), TraceState.getDefault(), false, true);
assertThat(spanContext.isValid()).isTrue();
}
use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.
the class W3CTraceContextPropagator method inject.
@Override
public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> setter) {
if (context == null || setter == null) {
return;
}
SpanContext spanContext = Span.fromContext(context).getSpanContext();
if (!spanContext.isValid()) {
return;
}
char[] chars = TemporaryBuffers.chars(TRACEPARENT_HEADER_SIZE);
chars[0] = VERSION.charAt(0);
chars[1] = VERSION.charAt(1);
chars[2] = TRACEPARENT_DELIMITER;
String traceId = spanContext.getTraceId();
traceId.getChars(0, traceId.length(), chars, TRACE_ID_OFFSET);
chars[SPAN_ID_OFFSET - 1] = TRACEPARENT_DELIMITER;
String spanId = spanContext.getSpanId();
spanId.getChars(0, spanId.length(), chars, SPAN_ID_OFFSET);
chars[TRACE_OPTION_OFFSET - 1] = TRACEPARENT_DELIMITER;
String traceFlagsHex = spanContext.getTraceFlags().asHex();
chars[TRACE_OPTION_OFFSET] = traceFlagsHex.charAt(0);
chars[TRACE_OPTION_OFFSET + 1] = traceFlagsHex.charAt(1);
setter.set(carrier, TRACE_PARENT, new String(chars, 0, TRACEPARENT_HEADER_SIZE));
TraceState traceState = spanContext.getTraceState();
if (traceState.isEmpty()) {
// No need to add an empty "tracestate" header.
return;
}
setter.set(carrier, TRACE_STATE, encodeTraceState(traceState));
}
use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.
the class OtlpExporterIntegrationTest method testTraceExport.
private static void testTraceExport(SpanExporter spanExporter) {
SdkTracerProvider tracerProvider = SdkTracerProvider.builder().addSpanProcessor(SimpleSpanProcessor.create(spanExporter)).setResource(RESOURCE).build();
SpanContext linkContext = SpanContext.create(IdGenerator.random().generateTraceId(), IdGenerator.random().generateSpanId(), TraceFlags.getDefault(), TraceState.getDefault());
Span span = tracerProvider.get(OtlpExporterIntegrationTest.class.getName()).spanBuilder("my span name").addLink(linkContext).startSpan();
span.setAttribute("key", "value");
span.addEvent("event");
span.end();
await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> assertThat(grpcServer.traceRequests).hasSize(1));
ExportTraceServiceRequest request = grpcServer.traceRequests.get(0);
assertThat(request.getResourceSpansCount()).isEqualTo(1);
ResourceSpans resourceSpans = request.getResourceSpans(0);
assertThat(resourceSpans.getResource().getAttributesList()).contains(KeyValue.newBuilder().setKey(ResourceAttributes.SERVICE_NAME.getKey()).setValue(AnyValue.newBuilder().setStringValue("integration test").build()).build());
assertThat(resourceSpans.getInstrumentationLibrarySpansCount()).isEqualTo(1);
InstrumentationLibrarySpans ilSpans = resourceSpans.getInstrumentationLibrarySpans(0);
assertThat(ilSpans.getInstrumentationLibrary().getName()).isEqualTo(OtlpExporterIntegrationTest.class.getName());
assertThat(ilSpans.getSpansCount()).isEqualTo(1);
io.opentelemetry.proto.trace.v1.Span protoSpan = ilSpans.getSpans(0);
assertThat(protoSpan.getTraceId().toByteArray()).isEqualTo(span.getSpanContext().getTraceIdBytes());
assertThat(protoSpan.getSpanId().toByteArray()).isEqualTo(span.getSpanContext().getSpanIdBytes());
assertThat(protoSpan.getName()).isEqualTo("my span name");
assertThat(protoSpan.getAttributesList()).isEqualTo(Collections.singletonList(KeyValue.newBuilder().setKey("key").setValue(AnyValue.newBuilder().setStringValue("value").build()).build()));
assertThat(protoSpan.getEventsCount()).isEqualTo(1);
assertThat(protoSpan.getEvents(0).getName()).isEqualTo("event");
assertThat(protoSpan.getLinksCount()).isEqualTo(1);
Link link = protoSpan.getLinks(0);
assertThat(link.getTraceId().toByteArray()).isEqualTo(linkContext.getTraceIdBytes());
assertThat(link.getSpanId().toByteArray()).isEqualTo(linkContext.getSpanIdBytes());
}
use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.
the class MetricAdapter method mapExemplar.
private static ExemplarData mapExemplar(Exemplar exemplar) {
// Look for trace/span id.
SpanContext spanContext = SpanContext.getInvalid();
if (exemplar.getAttachments().containsKey("SpanContext")) {
// We need to use `io.opencensus.contrib.exemplar.util.AttachmentValueSpanContext`
// The `toString` will be the following:
// SpanContext{traceId=TraceId{traceId=(id))}, spanId=SpanId{spanId=(id), ...}
// We *attempt* parse it rather than pull in yet another dependency.
String spanContextToString = exemplar.getAttachments().get("SpanContext").getValue();
Matcher m = OPENCENSUS_TRACE_ATTACHMENT_PATTERN.matcher(spanContextToString);
if (m.matches()) {
MatchResult mr = m.toMatchResult();
String traceId = mr.group(1);
String spanId = mr.group(2);
spanContext = SpanContext.create(traceId, spanId, TraceFlags.getDefault(), TraceState.getDefault());
}
}
return DoubleExemplarData.create(Attributes.empty(), mapTimestamp(exemplar.getTimestamp()), spanContext, exemplar.getValue());
}
Aggregations