Search in sources :

Example 1 with InMemorySpanExporter

use of io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter in project opentelemetry-java by open-telemetry.

the class AutoConfiguredOpenTelemetrySdkTest method tracerProviderCustomizer.

@Test
void tracerProviderCustomizer() {
    InMemorySpanExporter spanExporter = InMemorySpanExporter.create();
    AutoConfiguredOpenTelemetrySdkBuilder autoConfiguration = AutoConfiguredOpenTelemetrySdk.builder().addTracerProviderCustomizer((tracerProviderBuilder, config) -> {
        tracerProviderBuilder.setResource(Resource.builder().put("cat", "meow").build());
        return tracerProviderBuilder.addSpanProcessor(SimpleSpanProcessor.create(spanExporter));
    }).addResourceCustomizer((resource, config) -> resource.merge(Resource.builder().put("cow", "moo").build())).addPropertiesSupplier(() -> Collections.singletonMap("otel.metrics.exporter", "none")).addPropertiesSupplier(() -> Collections.singletonMap("otel.traces.exporter", "none")).addPropertiesSupplier(() -> Collections.singletonMap("otel.logs.exporter", "none")).setResultAsGlobal(false);
    GlobalOpenTelemetry.set(OpenTelemetry.noop());
    AutoConfiguredOpenTelemetrySdk autoConfigured = autoConfiguration.build();
    assertThat(autoConfigured.getResource().getAttribute(stringKey("cow"))).isEqualTo("moo");
    OpenTelemetrySdk sdk = autoConfigured.getOpenTelemetrySdk();
    sdk.getTracerProvider().get("test").spanBuilder("test").startSpan().end();
    List<SpanData> spanItems = spanExporter.getFinishedSpanItems();
    assertThat(spanItems.size()).isEqualTo(1);
    SpanData spanData = spanItems.get(0);
    assertThat(spanData.getResource().getAttribute(stringKey("cat"))).isEqualTo("meow");
    // Ensures the export happened.
    sdk.getSdkTracerProvider().shutdown().join(10, TimeUnit.SECONDS);
}
Also used : InMemorySpanExporter(io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) BeforeEach(org.junit.jupiter.api.BeforeEach) LogProcessor(io.opentelemetry.sdk.logs.LogProcessor) SpanId(io.opentelemetry.api.trace.SpanId) W3CTraceContextPropagator(io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk) Resource(io.opentelemetry.sdk.resources.Resource) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) ResourceAttributes(io.opentelemetry.semconv.resource.attributes.ResourceAttributes) TextMapPropagator(io.opentelemetry.context.propagation.TextMapPropagator) MetricReader(io.opentelemetry.sdk.metrics.export.MetricReader) SimpleSpanProcessor(io.opentelemetry.sdk.trace.export.SimpleSpanProcessor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) ContextKey(io.opentelemetry.context.ContextKey) Context(io.opentelemetry.context.Context) IdGenerator(io.opentelemetry.sdk.trace.IdGenerator) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Span(io.opentelemetry.api.trace.Span) SdkLogEmitterProvider(io.opentelemetry.sdk.logs.SdkLogEmitterProvider) OpenTelemetry(io.opentelemetry.api.OpenTelemetry) SpanExporter(io.opentelemetry.sdk.trace.export.SpanExporter) Mockito.when(org.mockito.Mockito.when) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) Mockito.verify(org.mockito.Mockito.verify) GlobalOpenTelemetry(io.opentelemetry.api.GlobalOpenTelemetry) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) SdkMeterProvider(io.opentelemetry.sdk.metrics.SdkMeterProvider) Mockito(org.mockito.Mockito) List(java.util.List) InMemorySpanExporter(io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter) AttributeKey.stringKey(io.opentelemetry.api.common.AttributeKey.stringKey) TextMapGetter(io.opentelemetry.context.propagation.TextMapGetter) MetricReaderFactory(io.opentelemetry.sdk.metrics.export.MetricReaderFactory) TraceId(io.opentelemetry.api.trace.TraceId) SpanData(io.opentelemetry.sdk.trace.data.SpanData) Collections(java.util.Collections) CompletableResultCode(io.opentelemetry.sdk.common.CompletableResultCode) SpanData(io.opentelemetry.sdk.trace.data.SpanData) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk) Test(org.junit.jupiter.api.Test)

Example 2 with InMemorySpanExporter

use of io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter 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);
}
Also used : InMemorySpanExporter(io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk)

Example 3 with InMemorySpanExporter

use of io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter 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);
}
Also used : InMemorySpanExporter(io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) OpenTelemetrySdk(io.opentelemetry.sdk.OpenTelemetrySdk)

Aggregations

OpenTelemetrySdk (io.opentelemetry.sdk.OpenTelemetrySdk)3 InMemorySpanExporter (io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter)3 SdkTracerProvider (io.opentelemetry.sdk.trace.SdkTracerProvider)3 GlobalOpenTelemetry (io.opentelemetry.api.GlobalOpenTelemetry)1 OpenTelemetry (io.opentelemetry.api.OpenTelemetry)1 AttributeKey.stringKey (io.opentelemetry.api.common.AttributeKey.stringKey)1 Span (io.opentelemetry.api.trace.Span)1 SpanId (io.opentelemetry.api.trace.SpanId)1 TraceId (io.opentelemetry.api.trace.TraceId)1 W3CTraceContextPropagator (io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator)1 Context (io.opentelemetry.context.Context)1 ContextKey (io.opentelemetry.context.ContextKey)1 TextMapGetter (io.opentelemetry.context.propagation.TextMapGetter)1 TextMapPropagator (io.opentelemetry.context.propagation.TextMapPropagator)1 CompletableResultCode (io.opentelemetry.sdk.common.CompletableResultCode)1 LogProcessor (io.opentelemetry.sdk.logs.LogProcessor)1 SdkLogEmitterProvider (io.opentelemetry.sdk.logs.SdkLogEmitterProvider)1 SdkMeterProvider (io.opentelemetry.sdk.metrics.SdkMeterProvider)1 MetricReader (io.opentelemetry.sdk.metrics.export.MetricReader)1 MetricReaderFactory (io.opentelemetry.sdk.metrics.export.MetricReaderFactory)1