Search in sources :

Example 41 with SpanContext

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;
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext) Baggage(io.opentelemetry.api.baggage.Baggage)

Example 42 with SpanContext

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();
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext) Test(org.junit.jupiter.api.Test)

Example 43 with SpanContext

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));
}
Also used : W3CTraceContextEncoding.decodeTraceState(io.opentelemetry.api.trace.propagation.internal.W3CTraceContextEncoding.decodeTraceState) W3CTraceContextEncoding.encodeTraceState(io.opentelemetry.api.trace.propagation.internal.W3CTraceContextEncoding.encodeTraceState) TraceState(io.opentelemetry.api.trace.TraceState) SpanContext(io.opentelemetry.api.trace.SpanContext)

Example 44 with SpanContext

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());
}
Also used : ExportTraceServiceRequest(io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest) SpanContext(io.opentelemetry.api.trace.SpanContext) InstrumentationLibrarySpans(io.opentelemetry.proto.trace.v1.InstrumentationLibrarySpans) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) ResourceSpans(io.opentelemetry.proto.trace.v1.ResourceSpans) Span(io.opentelemetry.api.trace.Span) Link(io.opentelemetry.proto.trace.v1.Span.Link)

Example 45 with SpanContext

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());
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext) Matcher(java.util.regex.Matcher) MatchResult(java.util.regex.MatchResult)

Aggregations

SpanContext (io.opentelemetry.api.trace.SpanContext)62 Test (org.junit.jupiter.api.Test)33 Span (io.opentelemetry.api.trace.Span)31 Context (io.opentelemetry.context.Context)31 SpanKind (io.opentelemetry.api.trace.SpanKind)13 TraceState (io.opentelemetry.api.trace.TraceState)12 W3CTraceContextPropagator (io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator)12 Collectors (java.util.stream.Collectors)12 Attributes (io.opentelemetry.api.common.Attributes)11 TraceFlags (io.opentelemetry.api.trace.TraceFlags)10 SemanticAttributes (io.opentelemetry.semconv.trace.attributes.SemanticAttributes)10 Instant (java.time.Instant)10 SpanId (io.opentelemetry.api.trace.SpanId)9 TraceId (io.opentelemetry.api.trace.TraceId)9 LinkData (io.opentelemetry.sdk.trace.data.LinkData)9 Mockito.when (org.mockito.Mockito.when)9 AttributesBuilder (io.opentelemetry.api.common.AttributesBuilder)8 ContextKey (io.opentelemetry.context.ContextKey)7 TextMapGetter (io.opentelemetry.context.propagation.TextMapGetter)7 InstrumentationVersion (io.opentelemetry.instrumentation.api.InstrumentationVersion)7