Search in sources :

Example 1 with IdGenerator

use of io.opentelemetry.sdk.trace.IdGenerator in project opentelemetry-java by open-telemetry.

the class AutoConfiguredOpenTelemetrySdkTest method builder_addTracerProviderCustomizer.

@Test
void builder_addTracerProviderCustomizer() {
    when(idGenerator.generateTraceId()).thenReturn(TraceId.fromLongs(9999999999L, 9999999999L));
    when(idGenerator.generateSpanId()).thenReturn(SpanId.fromLong(9999999999L));
    Span span = builder.addTracerProviderCustomizer((tracerProviderBuilder, config) -> tracerProviderBuilder.setIdGenerator(idGenerator)).build().getOpenTelemetrySdk().getTracer("test").spanBuilder("name").startSpan();
    assertThat(span.getSpanContext().getSpanId()).isEqualTo(SpanId.fromLong(9999999999L));
    assertThat(span.getSpanContext().getTraceId()).isEqualTo(TraceId.fromLongs(9999999999L, 9999999999L));
    span.end();
}
Also used : 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) Span(io.opentelemetry.api.trace.Span) Test(org.junit.jupiter.api.Test)

Example 2 with IdGenerator

use of io.opentelemetry.sdk.trace.IdGenerator 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);
}
Also used : Resource(io.opentelemetry.sdk.resources.Resource) SpanLimits(io.opentelemetry.sdk.trace.SpanLimits) IdGenerator(io.opentelemetry.sdk.trace.IdGenerator) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) TracerProvider(io.opentelemetry.api.trace.TracerProvider) Test(org.junit.jupiter.api.Test)

Aggregations

Resource (io.opentelemetry.sdk.resources.Resource)2 IdGenerator (io.opentelemetry.sdk.trace.IdGenerator)2 SdkTracerProvider (io.opentelemetry.sdk.trace.SdkTracerProvider)2 Test (org.junit.jupiter.api.Test)2 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 TracerProvider (io.opentelemetry.api.trace.TracerProvider)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 OpenTelemetrySdk (io.opentelemetry.sdk.OpenTelemetrySdk)1 CompletableResultCode (io.opentelemetry.sdk.common.CompletableResultCode)1 LogProcessor (io.opentelemetry.sdk.logs.LogProcessor)1 SdkLogEmitterProvider (io.opentelemetry.sdk.logs.SdkLogEmitterProvider)1