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);
}
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);
}
}
Aggregations