use of io.opentelemetry.sdk.trace.SdkTracerProvider in project opentelemetry-java by open-telemetry.
the class SimpleSpanProcessorTest method tracerSdk_NotSampled_Span.
@Test
void tracerSdk_NotSampled_Span() {
WaitingSpanExporter waitingSpanExporter = new WaitingSpanExporter(1, CompletableResultCode.ofSuccess());
SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder().addSpanProcessor(SimpleSpanProcessor.create(waitingSpanExporter)).setSampler(mockSampler).build();
when(mockSampler.shouldSample(any(), any(), any(), any(), any(), anyList())).thenReturn(SamplingResult.drop());
try {
Tracer tracer = sdkTracerProvider.get(getClass().getName());
tracer.spanBuilder(SPAN_NAME).startSpan();
tracer.spanBuilder(SPAN_NAME).startSpan();
when(mockSampler.shouldSample(any(), any(), any(), any(), any(), anyList())).thenReturn(SamplingResult.recordAndSample());
Span span = tracer.spanBuilder(SPAN_NAME).startSpan();
span.end();
// Spans are recorded and exported in the same order as they are ended, we test that a non
// sampled span is not exported by creating and ending a sampled span after a non sampled span
// and checking that the first exported span is the sampled span (the non sampled did not get
// exported).
List<SpanData> exported = waitingSpanExporter.waitForExport();
// Need to check this because otherwise the variable span1 is unused, other option is to not
// have a span1 variable.
assertThat(exported).containsExactly(((ReadableSpan) span).toSpanData());
} finally {
sdkTracerProvider.shutdown();
}
}
use of io.opentelemetry.sdk.trace.SdkTracerProvider in project opentelemetry-java by open-telemetry.
the class OpenTelemetrySdkTest method testConfiguration_tracerSettings.
@Test
void testConfiguration_tracerSettings() {
Resource resource = Resource.create(Attributes.builder().put("cat", "meow").build());
IdGenerator idGenerator = mock(IdGenerator.class);
SpanLimits spanLimits = SpanLimits.getDefault();
OpenTelemetrySdk openTelemetry = OpenTelemetrySdk.builder().setTracerProvider(SdkTracerProvider.builder().setClock(clock).setResource(resource).setIdGenerator(idGenerator).setSpanLimits(spanLimits).build()).build();
TracerProvider unobfuscatedTracerProvider = ((OpenTelemetrySdk.ObfuscatedTracerProvider) openTelemetry.getTracerProvider()).unobfuscate();
assertThat(unobfuscatedTracerProvider).isInstanceOfSatisfying(SdkTracerProvider.class, sdkTracerProvider -> assertThat(sdkTracerProvider.getSpanLimits()).isEqualTo(spanLimits));
// Since TracerProvider is in a different package, the only alternative to this reflective
// approach would be to make the fields public for testing which is worse than this.
assertThat(unobfuscatedTracerProvider).extracting("sharedState").hasFieldOrPropertyWithValue("clock", clock).hasFieldOrPropertyWithValue("resource", resource).hasFieldOrPropertyWithValue("idGenerator", idGenerator);
}
use of io.opentelemetry.sdk.trace.SdkTracerProvider 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());
}
use of io.opentelemetry.sdk.trace.SdkTracerProvider in project opentelemetry-java by open-telemetry.
the class OpenTelemetryRule method create.
/**
* Returns a {@link OpenTelemetryRule} with a default SDK initialized with an in-memory span
* exporter and W3C trace context propagation.
*/
public static OpenTelemetryRule create() {
InMemorySpanExporter spanExporter = InMemorySpanExporter.create();
SdkTracerProvider tracerProvider = SdkTracerProvider.builder().addSpanProcessor(SimpleSpanProcessor.create(spanExporter)).build();
OpenTelemetrySdk openTelemetry = OpenTelemetrySdk.builder().setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance())).setTracerProvider(tracerProvider).build();
return new OpenTelemetryRule(openTelemetry, spanExporter);
}
use of io.opentelemetry.sdk.trace.SdkTracerProvider in project opentelemetry-java by open-telemetry.
the class OpenTelemetryExtension method create.
/**
* Returns a {@link OpenTelemetryExtension} with a default SDK initialized with an in-memory span
* exporter and W3C trace context propagation.
*/
public static OpenTelemetryExtension create() {
InMemorySpanExporter spanExporter = InMemorySpanExporter.create();
SdkTracerProvider tracerProvider = SdkTracerProvider.builder().addSpanProcessor(SimpleSpanProcessor.create(spanExporter)).build();
OpenTelemetrySdk openTelemetry = OpenTelemetrySdk.builder().setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance())).setTracerProvider(tracerProvider).build();
return new OpenTelemetryExtension(openTelemetry, spanExporter);
}
Aggregations