All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ca.bitcoco.jsk.operation.TraceConfig Maven / Gradle / Ivy

package ca.bitcoco.jsk.operation;

import ca.bitcoco.jsk.globalConfig.Versioning;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;
import java.util.UUID;


@Configuration
public class TraceConfig {


    Duration BatchTimeOut = Duration.ofMillis(500);


    String SERVICE_NAME_LABEL = "service.name";
    String SERVICE_INSTANCE_ID = "service.id";


    public OpenTelemetrySdk openTelemetrySdk(String endpoint, String apiKey) {
        return OpenTelemetrySdk.builder()
                .setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
                .setTracerProvider(tracerProvider(endpoint, apiKey))
                .build();
    }

    public Resource resource(String serviceName) {
        return Resource.getDefault()
                .merge(Resource.create(
                        Attributes.builder()
                                .put(SERVICE_NAME_LABEL, serviceName)
                                .put(SERVICE_INSTANCE_ID, UUID.randomUUID().toString())
                                .build()
                ));
    }

    public SdkTracerProvider tracerProvider(String endpoint, String apiKey) {
        SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
                .setResource(resource(Versioning.serviceName))
                .addSpanProcessor(
                        BatchSpanProcessor.builder(
                                OtlpGrpcSpanExporter.builder()
                                        .setEndpoint(endpoint)
                                        .addHeader("api-key", apiKey)
                                        .build()
                        ).build()
                )
                .build();
        Runtime.getRuntime().addShutdownHook(new Thread(sdkTracerProvider::close));

        return sdkTracerProvider;
    }


    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(name = "bitcoco.jsk.operation.tracing.enabled", havingValue = "true")
    public OpenTelemetry configTracing(@Value("${bitcoco.jsk.operation.OTLP_agent_host:localhost}") String agentHost,
                                       @Value("${bitcoco.jsk.operation.OTLP_agent_port:4317}") String agentPort
    ) {
        try {
            String endpoint = "http://" + agentHost + ":" + agentPort;
            OpenTelemetry openTelemetry = openTelemetrySdk(endpoint, "1234");
            GlobalOpenTelemetry.set(openTelemetry);

            return openTelemetry;
        } catch (Exception e) {
            LogManager.getLogger(this).log(Level.ERROR, "OTLP connect failed");
            throw e;
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy