javainstrumentationopen-telemetry

OpenTelemetry configuration without AutoConfiguredOpenTelemetrySdk


I need to configure OpenTelemetry without using AutoConfiguredOpenTelemetrySdk. How can I achieve the same functionality of the code below:

 OpenTelemetry openTelemetry = AutoConfiguredOpenTelemetrySdk.builder()
                        .addPropertiesCustomizer(x -> telemetryProperties)
                        .addResourceCustomizer(this::customizeResource)
                        .setServiceClassLoader(Thread.currentThread().getContextClassLoader())
                        .setResultAsGlobal(false)
                        .registerShutdownHook(false)
                        .build()
                        .getOpenTelemetrySdk();

Thank you!


Solution

  • The AutoConfiguredOpenTelemetrySdk is just a wrapper around the builder classes for the SDK and its component pieces. You can use those builder classes directly.

    You can find the documentation for this type of setup here: https://opentelemetry.io/docs/instrumentation/java/manual/#sdk-configuration

    // Create an instance of the OpenTelemetrySdk class.
    OpenTelemetrySdk openTelemetrySdk = OpenTelemetrySdk.builder()
        .setTracerProvider(
            SdkTracerProvider.builder()
                // Customize the resource if desired.
                .setResource(Resource.create()
                    .merge(this.customizeResource(Resource.getDefault()))
                )
                .build()
        )
        .build();
    
    // Create an instance of the OpenTelemetry interface using the OpenTelemetrySdk.
    OpenTelemetry openTelemetry = openTelemetrySdk.getOpenTelemetry();