package ru.mtuci.messenger.telemetry; import io.micrometer.tracing.Span; import io.micrometer.tracing.Tracer; import org.springframework.beans.factory.ObjectProvider; import org.springframework.stereotype.Component; @Component public class TraceCarrier { private final ObjectProvider tracers; public TraceCarrier(ObjectProvider tracers) { this.tracers = tracers; } public String currentTraceparent() { Tracer tracer = tracers.getIfAvailable(); if (tracer == null) { return null; } Span span = tracer.currentSpan(); if (span == null || span.context() == null) { return null; } String traceId = span.context().traceId(); String spanId = span.context().spanId(); if (traceId == null || spanId == null || traceId.length() != 32 || spanId.length() != 16) { return null; } if ("00000000000000000000000000000000".equals(traceId)) { return null; } boolean sampled = !Boolean.FALSE.equals(span.context().sampled()); return "00-" + traceId + "-" + spanId + "-" + (sampled ? "01" : "00"); } }