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);
}
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;
}
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();
}
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();
}
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();
}
Aggregations