use of io.opentelemetry.api.trace.SpanKind in project opentelemetry-java by open-telemetry.
the class SdkSpanBuilderTest method addAttributes_OnlyViaSampler.
@Test
void addAttributes_OnlyViaSampler() {
Sampler sampler = new Sampler() {
@Override
public SamplingResult shouldSample(Context parentContext, String traceId, String name, SpanKind spanKind, Attributes attributes, List<LinkData> parentLinks) {
return SamplingResult.create(SamplingDecision.RECORD_AND_SAMPLE, Attributes.builder().put("cat", "meow").build());
}
@Override
public String getDescription() {
return "test";
}
};
TracerProvider tracerProvider = SdkTracerProvider.builder().setSampler(sampler).build();
// Verify methods do not crash.
SpanBuilder spanBuilder = tracerProvider.get("test").spanBuilder(SPAN_NAME);
SdkSpan span = (SdkSpan) spanBuilder.startSpan();
span.end();
assertThat(span.toSpanData().getAttributes().size()).isEqualTo(1);
assertThat(span.toSpanData().getAttributes().get(stringKey("cat"))).isEqualTo("meow");
}
use of io.opentelemetry.api.trace.SpanKind in project opentelemetry-java-instrumentation by open-telemetry.
the class Instrumenter method shouldStart.
/**
* Returns whether instrumentation should be applied for the {@link REQUEST}. If {@code true},
* call {@link #start(Context, Object)} and {@link #end(Context, Object, Object, Throwable)}
* around the operation being instrumented, or if {@code false} execute the operation directly
* without calling those methods.
*/
public boolean shouldStart(Context parentContext, REQUEST request) {
if (disabled) {
return false;
}
SpanKind spanKind = spanKindExtractor.extract(request);
boolean suppressed = spanSuppressionStrategy.shouldSuppress(parentContext, spanKind);
if (suppressed) {
supportability.recordSuppressedSpan(spanKind, instrumentationName);
}
return !suppressed;
}
use of io.opentelemetry.api.trace.SpanKind in project opentelemetry-java-instrumentation by open-telemetry.
the class SupportabilityMetrics method report.
// visible for testing
void report() {
suppressionCounters.forEach((instrumentationName, countsByKind) -> {
for (SpanKind kind : SpanKind.values()) {
long value = countsByKind.getAndReset(kind);
if (value > 0) {
reporter.accept("Suppressed Spans by '" + instrumentationName + "' (" + kind + ") : " + value);
}
}
});
counters.forEach((counterName, counter) -> {
long value = counter.getAndSet(0);
if (value > 0) {
reporter.accept("Counter '" + counterName + "' : " + value);
}
});
}
use of io.opentelemetry.api.trace.SpanKind in project opentelemetry-java by open-telemetry.
the class SdkSpanTest method testAsSpanData.
@Test
void testAsSpanData() {
String name = "GreatSpan";
SpanKind kind = SpanKind.SERVER;
String traceId = this.traceId;
String spanId = this.spanId;
String parentSpanId = this.parentSpanId;
SpanLimits spanLimits = SpanLimits.getDefault();
SpanProcessor spanProcessor = NoopSpanProcessor.getInstance();
TestClock clock = TestClock.create();
Resource resource = this.resource;
Attributes attributes = TestUtils.generateRandomAttributes();
AttributesMap attributesWithCapacity = new AttributesMap(32, Integer.MAX_VALUE);
attributes.forEach((key, value) -> attributesWithCapacity.put((AttributeKey) key, value));
Attributes event1Attributes = TestUtils.generateRandomAttributes();
Attributes event2Attributes = TestUtils.generateRandomAttributes();
SpanContext context = SpanContext.create(traceId, spanId, TraceFlags.getDefault(), TraceState.getDefault());
LinkData link1 = LinkData.create(context, TestUtils.generateRandomAttributes());
SdkSpan readableSpan = SdkSpan.startSpan(context, name, instrumentationLibraryInfo, kind, parentSpanId != null ? Span.wrap(SpanContext.create(traceId, parentSpanId, TraceFlags.getDefault(), TraceState.getDefault())) : Span.getInvalid(), Context.root(), spanLimits, spanProcessor, clock, resource, attributesWithCapacity, Collections.singletonList(link1), 1, 0);
long startEpochNanos = clock.now();
clock.advance(Duration.ofMillis(4));
long firstEventEpochNanos = clock.now();
readableSpan.addEvent("event1", event1Attributes);
clock.advance(Duration.ofMillis(6));
long secondEventTimeNanos = clock.now();
readableSpan.addEvent("event2", event2Attributes);
clock.advance(Duration.ofMillis(100));
readableSpan.end();
long endEpochNanos = clock.now();
List<EventData> events = Arrays.asList(EventData.create(firstEventEpochNanos, "event1", event1Attributes, event1Attributes.size()), EventData.create(secondEventTimeNanos, "event2", event2Attributes, event2Attributes.size()));
SpanData result = readableSpan.toSpanData();
verifySpanData(result, attributesWithCapacity, events, Collections.singletonList(link1), name, startEpochNanos, endEpochNanos, StatusData.unset(), /* hasEnded= */
true);
assertThat(result.getTotalRecordedLinks()).isEqualTo(1);
assertThat(result.getSpanContext().isSampled()).isEqualTo(false);
}
use of io.opentelemetry.api.trace.SpanKind in project opentelemetry-java by open-telemetry.
the class SdkSpanBuilderTest method sampler_updatedTraceState.
@Test
void sampler_updatedTraceState() {
String samplerAttributeName = "sampler-attribute";
AttributeKey<String> samplerAttributeKey = stringKey(samplerAttributeName);
SdkSpan span = (SdkSpan) SdkTracerProvider.builder().setSampler(new Sampler() {
@Override
public SamplingResult shouldSample(Context parentContext, String traceId, String name, SpanKind spanKind, Attributes attributes, List<LinkData> parentLinks) {
return new SamplingResult() {
@Override
public SamplingDecision getDecision() {
return SamplingDecision.RECORD_AND_SAMPLE;
}
@Override
public Attributes getAttributes() {
return Attributes.empty();
}
@Override
public TraceState getUpdatedTraceState(TraceState parentTraceState) {
return parentTraceState.toBuilder().put("newkey", "newValue").build();
}
};
}
@Override
public String getDescription() {
return "test sampler";
}
}).build().get("test").spanBuilder(SPAN_NAME).setAttribute(samplerAttributeKey, "none").startSpan();
try {
assertThat(span.getSpanContext().isSampled()).isTrue();
assertThat(span.toSpanData().getAttributes().get(samplerAttributeKey)).isNotNull();
assertThat(span.toSpanData().getSpanContext().getTraceState()).isEqualTo(TraceState.builder().put("newkey", "newValue").build());
} finally {
span.end();
}
}
Aggregations