Search in sources :

Example 6 with TracerProvider

use of io.opentelemetry.api.trace.TracerProvider 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)

Example 7 with TracerProvider

use of io.opentelemetry.api.trace.TracerProvider in project honeycomb-opentelemetry-java by honeycombio.

the class OpenTelemetryConfigurationTest method testConfiguration_tracerSettings.

@Test
@SuppressWarnings("unchecked")
public void testConfiguration_tracerSettings() {
    Sampler sampler = new DeterministicTraceSampler(5);
    OpenTelemetry openTelemetry = OpenTelemetryConfiguration.builder().setSampler(sampler).setApiKey("foobar").setDataset("dataset").addResourceAttribute("str", "str").addResourceAttribute("bool", true).addResourceAttribute("int", 123).addResourceAttribute("str-array", "str1", "str2", "str3").build();
    TracerProvider provider = openTelemetry.getTracerProvider();
    try {
        Method m = provider.getClass().getDeclaredMethod("unobfuscate");
        m.setAccessible(true);
        SdkTracerProvider sdkProvider = (SdkTracerProvider) m.invoke(provider, (Object[]) null);
        // verify sampler
        Sampler s = sdkProvider.getSampler();
        Assertions.assertEquals(sampler, s);
        Field providerField = pluckField(sdkProvider.getClass(), "sharedState");
        Object sharedState = providerField.get(sdkProvider);
        Field resourceField = pluckField(sharedState.getClass(), "resource");
        Resource r = (Resource) resourceField.get(sharedState);
        Attributes attrs = r.getAttributes();
        // verify resource attributes
        Assertions.assertEquals(DistroMetadata.VERSION_VALUE, attrs.get(AttributeKey.stringKey(DistroMetadata.VERSION_FIELD)));
        Assertions.assertEquals(DistroMetadata.RUNTIME_VERSION_VALUE, attrs.get(AttributeKey.stringKey(DistroMetadata.RUNTIME_VERSION_FIELD)));
        Assertions.assertEquals("str", attrs.get(AttributeKey.stringKey("str")));
        Assertions.assertEquals(true, attrs.get(AttributeKey.booleanKey("bool")));
        Assertions.assertEquals(123, attrs.get(AttributeKey.longKey("int")));
        Assertions.assertEquals(Arrays.asList("str1", "str2", "str3"), attrs.get(AttributeKey.stringArrayKey("str-array")));
        Field procesorField = pluckField(sharedState.getClass(), "activeSpanProcessor");
        Object sp = procesorField.get(sharedState);
        Field allSpanProcessors = pluckField(sp.getClass(), "spanProcessorsAll");
        List<SpanProcessor> processors = (List<SpanProcessor>) allSpanProcessors.get(sp);
        // verify configured span processors
        Assertions.assertInstanceOf(BaggageSpanProcessor.class, processors.get(0));
        Assertions.assertInstanceOf(BatchSpanProcessor.class, processors.get(1));
    } catch (Exception e) {
        Assertions.fail(e);
    }
}
Also used : Resource(io.opentelemetry.sdk.resources.Resource) Attributes(io.opentelemetry.api.common.Attributes) OpenTelemetry(io.opentelemetry.api.OpenTelemetry) GlobalOpenTelemetry(io.opentelemetry.api.GlobalOpenTelemetry) Method(java.lang.reflect.Method) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) SdkTracerProvider(io.opentelemetry.sdk.trace.SdkTracerProvider) TracerProvider(io.opentelemetry.api.trace.TracerProvider) Field(java.lang.reflect.Field) BatchSpanProcessor(io.opentelemetry.sdk.trace.export.BatchSpanProcessor) BaggageSpanProcessor(io.honeycomb.opentelemetry.sdk.trace.spanprocessors.BaggageSpanProcessor) SpanProcessor(io.opentelemetry.sdk.trace.SpanProcessor) Sampler(io.opentelemetry.sdk.trace.samplers.Sampler) DeterministicTraceSampler(io.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler) List(java.util.List) DeterministicTraceSampler(io.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler) Test(org.junit.jupiter.api.Test)

Aggregations

TracerProvider (io.opentelemetry.api.trace.TracerProvider)7 Test (org.junit.jupiter.api.Test)7 Attributes (io.opentelemetry.api.common.Attributes)5 SpanBuilder (io.opentelemetry.api.trace.SpanBuilder)5 Sampler (io.opentelemetry.sdk.trace.samplers.Sampler)3 List (java.util.List)3 SpanContext (io.opentelemetry.api.trace.SpanContext)2 SpanKind (io.opentelemetry.api.trace.SpanKind)2 Context (io.opentelemetry.context.Context)2 Resource (io.opentelemetry.sdk.resources.Resource)2 SdkTracerProvider (io.opentelemetry.sdk.trace.SdkTracerProvider)2 LinkData (io.opentelemetry.sdk.trace.data.LinkData)2 SpanData (io.opentelemetry.sdk.trace.data.SpanData)2 DeterministicTraceSampler (io.honeycomb.opentelemetry.sdk.trace.samplers.DeterministicTraceSampler)1 BaggageSpanProcessor (io.honeycomb.opentelemetry.sdk.trace.spanprocessors.BaggageSpanProcessor)1 GlobalOpenTelemetry (io.opentelemetry.api.GlobalOpenTelemetry)1 OpenTelemetry (io.opentelemetry.api.OpenTelemetry)1 AttributeKey (io.opentelemetry.api.common.AttributeKey)1 AttributeKey.booleanArrayKey (io.opentelemetry.api.common.AttributeKey.booleanArrayKey)1 AttributeKey.booleanKey (io.opentelemetry.api.common.AttributeKey.booleanKey)1