Search in sources :

Example 11 with SpanContext

use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.

the class B3PropagatorInjectorSingleHeader method inject.

@Override
public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> setter) {
    if (context == null) {
        return;
    }
    if (setter == null) {
        return;
    }
    SpanContext spanContext = Span.fromContext(context).getSpanContext();
    if (!spanContext.isValid()) {
        return;
    }
    char[] chars = TemporaryBuffers.chars(COMBINED_HEADER_SIZE);
    String traceId = spanContext.getTraceId();
    traceId.getChars(0, traceId.length(), chars, 0);
    chars[SPAN_ID_OFFSET - 1] = B3Propagator.COMBINED_HEADER_DELIMITER_CHAR;
    String spanId = spanContext.getSpanId();
    spanId.getChars(0, SpanId.getLength(), chars, SPAN_ID_OFFSET);
    chars[SAMPLED_FLAG_OFFSET - 1] = B3Propagator.COMBINED_HEADER_DELIMITER_CHAR;
    if (Boolean.TRUE.equals(context.get(B3Propagator.DEBUG_CONTEXT_KEY))) {
        chars[SAMPLED_FLAG_OFFSET] = B3Propagator.DEBUG_SAMPLED;
    } else {
        chars[SAMPLED_FLAG_OFFSET] = spanContext.isSampled() ? B3Propagator.IS_SAMPLED : B3Propagator.NOT_SAMPLED;
    }
    setter.set(carrier, COMBINED_HEADER, new String(chars, 0, COMBINED_HEADER_SIZE));
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext)

Example 12 with SpanContext

use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.

the class LogDataBuilderTest method canSetSpanContext.

@Test
void canSetSpanContext() {
    LogDataBuilder builder = LogDataBuilder.create(resource, libraryInfo);
    SpanContext spanContext = mock(SpanContext.class);
    LogData result = builder.setSpanContext(spanContext).build();
    assertSame(spanContext, result.getSpanContext());
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext) Test(org.junit.jupiter.api.Test)

Example 13 with SpanContext

use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java by open-telemetry.

the class LogMarshaler method create.

// name field to be removed
@SuppressWarnings("deprecation")
static LogMarshaler create(io.opentelemetry.sdk.logs.data.LogData logData) {
    KeyValueMarshaler[] attributeMarshalers = KeyValueMarshaler.createRepeated(logData.getAttributes());
    // For now, map all the bodies to String AnyValue.
    StringAnyValueMarshaler anyValueMarshaler = new StringAnyValueMarshaler(MarshalerUtil.toBytes(logData.getBody().asString()));
    SpanContext spanContext = logData.getSpanContext();
    return new LogMarshaler(logData.getEpochNanos(), toProtoSeverityNumber(logData.getSeverity()), MarshalerUtil.toBytes(logData.getSeverityText()), MarshalerUtil.toBytes(logData.getName()), anyValueMarshaler, attributeMarshalers, // TODO (trask) implement droppedAttributesCount in LogRecord
    0, spanContext.getTraceFlags(), spanContext.getTraceId().equals(INVALID_TRACE_ID) ? null : spanContext.getTraceId(), spanContext.getSpanId().equals(INVALID_SPAN_ID) ? null : spanContext.getSpanId());
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext) StringAnyValueMarshaler(io.opentelemetry.exporter.internal.otlp.StringAnyValueMarshaler) KeyValueMarshaler(io.opentelemetry.exporter.internal.otlp.KeyValueMarshaler)

Example 14 with SpanContext

use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java-instrumentation by open-telemetry.

the class CamelPropagationUtilTest method shouldNotFailExtractingNullAwsParentForSqsEndpoint.

@Test
public void shouldNotFailExtractingNullAwsParentForSqsEndpoint() {
    // given
    Endpoint endpoint = new SqsEndpoint("", new SqsComponent(), new SqsConfiguration());
    Map<String, Object> exchangeHeaders = Collections.singletonMap("AWSTraceHeader", null);
    // when
    Context parent = CamelPropagationUtil.extractParent(exchangeHeaders, endpoint);
    // then
    Span parentSpan = Span.fromContext(parent);
    SpanContext parentSpanContext = parentSpan.getSpanContext();
    assertThat(parentSpanContext.isValid()).isEqualTo(false);
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) SqsComponent(org.apache.camel.component.aws.sqs.SqsComponent) SpanContext(io.opentelemetry.api.trace.SpanContext) HttpEndpoint(org.apache.camel.component.http.HttpEndpoint) SqsEndpoint(org.apache.camel.component.aws.sqs.SqsEndpoint) Endpoint(org.apache.camel.Endpoint) SqsConfiguration(org.apache.camel.component.aws.sqs.SqsConfiguration) SqsEndpoint(org.apache.camel.component.aws.sqs.SqsEndpoint) Span(io.opentelemetry.api.trace.Span) Test(org.junit.jupiter.api.Test)

Example 15 with SpanContext

use of io.opentelemetry.api.trace.SpanContext in project opentelemetry-java-instrumentation by open-telemetry.

the class CamelPropagationUtilTest method shouldNotFailExtractingNullHttpParentForHttpEndpoint.

@Test
public void shouldNotFailExtractingNullHttpParentForHttpEndpoint() throws Exception {
    // given
    Endpoint endpoint = new HttpEndpoint("", new HttpComponent(), URI.create(""));
    Map<String, Object> exchangeHeaders = Collections.singletonMap("uber-trace-id", null);
    // when
    Context parent = CamelPropagationUtil.extractParent(exchangeHeaders, endpoint);
    // then
    Span parentSpan = Span.fromContext(parent);
    SpanContext parentSpanContext = parentSpan.getSpanContext();
    assertThat(parentSpanContext.isValid()).isEqualTo(false);
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) SpanContext(io.opentelemetry.api.trace.SpanContext) HttpEndpoint(org.apache.camel.component.http.HttpEndpoint) SqsEndpoint(org.apache.camel.component.aws.sqs.SqsEndpoint) Endpoint(org.apache.camel.Endpoint) HttpComponent(org.apache.camel.component.http.HttpComponent) HttpEndpoint(org.apache.camel.component.http.HttpEndpoint) Span(io.opentelemetry.api.trace.Span) Test(org.junit.jupiter.api.Test)

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