Search in sources :

Example 1 with SpanContext

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

the class SpanMarshaler method create.

static SpanMarshaler create(SpanData span) {
    String traceId = span.getSpanContext().getTraceId();
    String spanId = span.getSpanContext().getSpanId();
    byte[] operationNameUtf8 = MarshalerUtil.toBytes(span.getName());
    TimeMarshaler startTime = TimeMarshaler.create(span.getStartEpochNanos());
    TimeMarshaler duration = TimeMarshaler.create(span.getEndEpochNanos() - span.getStartEpochNanos());
    List<KeyValueMarshaler> tags = KeyValueMarshaler.createRepeated(span.getAttributes());
    int droppedAttributes = span.getTotalAttributeCount() - span.getAttributes().size();
    if (droppedAttributes > 0) {
        tags.add(KeyValueMarshaler.create(KEY_DROPPED_ATTRIBUTES_COUNT, (long) droppedAttributes));
    }
    LogMarshaler[] logs = LogMarshaler.createRepeated(span.getEvents());
    int droppedEvents = span.getTotalRecordedEvents() - span.getEvents().size();
    if (droppedEvents > 0) {
        tags.add(KeyValueMarshaler.create(KEY_DROPPED_EVENTS_COUNT, (long) droppedEvents));
    }
    List<SpanRefMarshaler> references = SpanRefMarshaler.createRepeated(span.getLinks());
    // add the parent span
    SpanContext parentSpanContext = span.getParentSpanContext();
    if (parentSpanContext.isValid()) {
        references.add(SpanRefMarshaler.create(parentSpanContext));
    }
    if (span.getKind() != SpanKind.INTERNAL) {
        tags.add(KeyValueMarshaler.create(KEY_SPAN_KIND, span.getKind().name().toLowerCase(Locale.ROOT)));
    }
    if (!span.getStatus().getDescription().isEmpty()) {
        tags.add(KeyValueMarshaler.create(KEY_SPAN_STATUS_MESSAGE, span.getStatus().getDescription()));
    }
    if (span.getStatus().getStatusCode() != StatusCode.UNSET) {
        tags.add(KeyValueMarshaler.create(KEY_SPAN_STATUS_CODE, span.getStatus().getStatusCode().name()));
    }
    tags.add(KeyValueMarshaler.create(KEY_INSTRUMENTATION_LIBRARY_NAME, span.getInstrumentationLibraryInfo().getName()));
    if (span.getInstrumentationLibraryInfo().getVersion() != null) {
        tags.add(KeyValueMarshaler.create(KEY_INSTRUMENTATION_LIBRARY_VERSION, span.getInstrumentationLibraryInfo().getVersion()));
    }
    if (span.getStatus().getStatusCode() == StatusCode.ERROR) {
        tags.add(KeyValueMarshaler.create(KEY_ERROR, true));
    }
    return new SpanMarshaler(traceId, spanId, operationNameUtf8, startTime, duration, tags, logs, references);
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext)

Example 2 with SpanContext

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

the class W3CTraceContextPropagatorBenchmark method measureExtractCreateInject.

/**
 * Benchmark for measuring HttpTraceContext extract.
 */
@Benchmark
@BenchmarkMode({ Mode.AverageTime })
@Fork(1)
@Measurement(iterations = 15, time = 1)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 1)
@OperationsPerInvocation(COUNT)
@Nullable
public Context measureExtractCreateInject() {
    Context result = null;
    for (int i = 0; i < COUNT; i++) {
        result = w3cTraceContextPropagator.extract(Context.root(), carriers.get(i), getter);
        SpanContext current = Span.fromContext(result).getSpanContext();
        SpanContext clientSpanContext = SpanContext.create(current.getTraceId(), current.getSpanId(), current.getTraceFlags(), current.getTraceState());
        result = Span.wrap(clientSpanContext).storeInContext(result);
        w3cTraceContextPropagator.inject(result, carrier, setter);
        if (carrier.size() != 1) {
            throw new IllegalStateException("Fail test");
        }
        carrier.clear();
    }
    return result;
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) SpanContext(io.opentelemetry.api.trace.SpanContext) Measurement(org.openjdk.jmh.annotations.Measurement) Warmup(org.openjdk.jmh.annotations.Warmup) Fork(org.openjdk.jmh.annotations.Fork) BenchmarkMode(org.openjdk.jmh.annotations.BenchmarkMode) Benchmark(org.openjdk.jmh.annotations.Benchmark) OperationsPerInvocation(org.openjdk.jmh.annotations.OperationsPerInvocation) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit) Nullable(javax.annotation.Nullable)

Example 3 with SpanContext

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

the class ImmutableSpanContextTest method testWithIdValidationAndInvalidSpanId.

@Test
public void testWithIdValidationAndInvalidSpanId() {
    SpanContext spanContext = ImmutableSpanContext.create(TRACE_ID, SpanId.getInvalid(), TraceFlags.getDefault(), TraceState.getDefault(), false, false);
    assertThat(spanContext.isValid()).isFalse();
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext) Test(org.junit.jupiter.api.Test)

Example 4 with SpanContext

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

the class ImmutableSpanContextTest method testSkipIdValidationAndValidIds.

@Test
public void testSkipIdValidationAndValidIds() {
    SpanContext spanContext = ImmutableSpanContext.create(TRACE_ID, 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 5 with SpanContext

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

the class ImmutableSpanContextTest method testWithIdValidationAndInvalidTraceId.

@Test
public void testWithIdValidationAndInvalidTraceId() {
    SpanContext spanContext = ImmutableSpanContext.create(TraceId.getInvalid(), SPAN_ID, TraceFlags.getDefault(), TraceState.getDefault(), false, false);
    assertThat(spanContext.isValid()).isFalse();
}
Also used : SpanContext(io.opentelemetry.api.trace.SpanContext) 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