001package io.prometheus.metrics.tracer.initializer; 002 003import io.prometheus.metrics.tracer.agent.OpenTelemetryAgentSpanContext; 004import io.prometheus.metrics.tracer.common.SpanContext; 005import io.prometheus.metrics.tracer.otel.OpenTelemetrySpanContext; 006import java.util.concurrent.atomic.AtomicReference; 007import javax.annotation.Nullable; 008 009public class SpanContextSupplier { 010 011 private static final AtomicReference<SpanContext> spanContextRef = 012 new AtomicReference<SpanContext>(); 013 014 public static void setSpanContext(SpanContext spanContext) { 015 spanContextRef.set(spanContext); 016 } 017 018 public static boolean hasSpanContext() { 019 return getSpanContext() != null; 020 } 021 022 @Nullable 023 public static SpanContext getSpanContext() { 024 return spanContextRef.get(); 025 } 026 027 static { 028 try { 029 if (OpenTelemetrySpanContext.isAvailable()) { 030 spanContextRef.set(new OpenTelemetrySpanContext()); 031 } 032 } catch (NoClassDefFoundError ignored) { 033 // tracer_otel dependency not found 034 } catch (UnsupportedClassVersionError ignored) { 035 // OpenTelemetry requires Java 8, but client_java might run on Java 6. 036 } 037 try { 038 if (OpenTelemetryAgentSpanContext.isAvailable()) { 039 spanContextRef.set(new OpenTelemetryAgentSpanContext()); 040 } 041 } catch (NoClassDefFoundError ignored) { 042 // tracer_otel_agent dependency not found 043 } catch (UnsupportedClassVersionError ignored) { 044 // OpenTelemetry requires Java 8, but client_java might run on Java 6. 045 } 046 } 047}