use of io.opentelemetry.sdk.trace.SdkTracerProviderBuilder in project wildfly by wildfly.
the class OpenTelemetryHolder method build.
private OpenTelemetry build(OpenTelemetryConfig config) {
String serviceName = (config.serviceName != null) ? config.serviceName : DEFAULT_SERVICE_NAME;
final SdkTracerProviderBuilder tracerProviderBuilder = SdkTracerProvider.builder().addSpanProcessor(getSpanProcessor(config)).setResource(Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, serviceName)));
if (config.sampler != null) {
tracerProviderBuilder.setSampler(getSampler(config));
}
try {
return OpenTelemetrySdk.builder().setTracerProvider(tracerProviderBuilder.build()).setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance())).buildAndRegisterGlobal();
} catch (IllegalStateException ex) {
return GlobalOpenTelemetry.get();
}
}
use of io.opentelemetry.sdk.trace.SdkTracerProviderBuilder in project opentelemetry-java by open-telemetry.
the class TracerProviderConfigurationTest method configureTracerProvider.
@Test
void configureTracerProvider() {
Map<String, String> properties = new HashMap<>();
properties.put("otel.bsp.schedule.delay", "100000");
properties.put("otel.traces.sampler", "always_off");
properties.put("otel.traces.exporter", "none");
Resource resource = Resource.create(Attributes.builder().put("cat", "meow").build());
// We don't have any exporters on classpath for this test so check no-op case. Exporter cases
// are verified in other test sets like testFullConfig.
SdkTracerProviderBuilder tracerProviderBuilder = SdkTracerProvider.builder().setResource(resource);
TracerProviderConfiguration.configureTracerProvider(tracerProviderBuilder, DefaultConfigProperties.createForTest(properties), TracerProviderConfiguration.class.getClassLoader(), MeterProvider.noop(), (a, unused) -> a, (a, unused) -> a);
SdkTracerProvider tracerProvider = tracerProviderBuilder.build();
try {
assertThat(tracerProvider.getSampler()).isEqualTo(Sampler.alwaysOff());
assertThat(tracerProvider).extracting("sharedState").satisfies(sharedState -> {
assertThat(sharedState).extracting("resource").isEqualTo(resource);
assertThat(sharedState).extracting("activeSpanProcessor").isEqualTo(SpanProcessor.composite());
});
} finally {
tracerProvider.shutdown();
}
}
use of io.opentelemetry.sdk.trace.SdkTracerProviderBuilder in project opentelemetry-java by open-telemetry.
the class AutoConfiguredOpenTelemetrySdkBuilder method build.
/**
* Returns a new {@link AutoConfiguredOpenTelemetrySdk} holding components auto-configured using
* the settings of this {@link AutoConfiguredOpenTelemetrySdkBuilder}.
*/
public AutoConfiguredOpenTelemetrySdk build() {
if (!customized) {
customized = true;
mergeSdkTracerProviderConfigurer();
for (AutoConfigurationCustomizerProvider customizer : ServiceLoader.load(AutoConfigurationCustomizerProvider.class, serviceClassLoader)) {
customizer.customize(this);
}
}
ConfigProperties config = getConfig();
Resource resource = ResourceConfiguration.configureResource(config, serviceClassLoader, resourceCustomizer);
SdkMeterProviderBuilder meterProviderBuilder = SdkMeterProvider.builder();
meterProviderBuilder.setResource(resource);
MeterProviderConfiguration.configureMeterProvider(meterProviderBuilder, config, serviceClassLoader, metricExporterCustomizer);
meterProviderBuilder = meterProviderCustomizer.apply(meterProviderBuilder, config);
SdkMeterProvider meterProvider = meterProviderBuilder.build();
SdkTracerProviderBuilder tracerProviderBuilder = SdkTracerProvider.builder();
tracerProviderBuilder.setResource(resource);
TracerProviderConfiguration.configureTracerProvider(tracerProviderBuilder, config, serviceClassLoader, meterProvider, spanExporterCustomizer, samplerCustomizer);
tracerProviderBuilder = tracerProviderCustomizer.apply(tracerProviderBuilder, config);
SdkTracerProvider tracerProvider = tracerProviderBuilder.build();
SdkLogEmitterProviderBuilder logEmitterProviderBuilder = SdkLogEmitterProvider.builder();
logEmitterProviderBuilder.setResource(resource);
LogEmitterProviderConfiguration.configureLogEmitterProvider(logEmitterProviderBuilder, config, meterProvider, logExporterCustomizer);
logEmitterProviderBuilder = logEmitterProviderCustomizer.apply(logEmitterProviderBuilder, config);
SdkLogEmitterProvider logEmitterProvider = logEmitterProviderBuilder.build();
if (registerShutdownHook) {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
List<CompletableResultCode> shutdown = new ArrayList<>();
shutdown.add(tracerProvider.shutdown());
shutdown.add(meterProvider.shutdown());
shutdown.add(logEmitterProvider.shutdown());
CompletableResultCode.ofAll(shutdown).join(10, TimeUnit.SECONDS);
}));
}
ContextPropagators propagators = PropagatorConfiguration.configurePropagators(config, serviceClassLoader, propagatorCustomizer);
OpenTelemetrySdkBuilder sdkBuilder = OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).setLogEmitterProvider(logEmitterProvider).setMeterProvider(meterProvider).setPropagators(propagators);
OpenTelemetrySdk openTelemetrySdk = sdkBuilder.build();
if (setResultAsGlobal) {
GlobalOpenTelemetry.set(openTelemetrySdk);
logger.log(Level.FINE, "Global OpenTelemetrySdk set to {0} by autoconfiguration", openTelemetrySdk);
}
return AutoConfiguredOpenTelemetrySdk.create(openTelemetrySdk, resource, config);
}
Aggregations