Class OpenTelemetryTracer

java.lang.Object
com.tnsai.quality.observability.OpenTelemetryTracer

public class OpenTelemetryTracer extends Object
Bridge between TnsAI's TraceContext and OpenTelemetry tracing.

This class provides seamless integration between the existing TraceContext abstraction and real OpenTelemetry spans. It allows gradual migration to full OpenTelemetry while maintaining backward compatibility.

Usage Example


 // Initialize with TnsAITelemetry
 TnsAITelemetry telemetry = TnsAITelemetry.builder()
     .serviceName("my-agent")
     .build();

 OpenTelemetryTracer otelTracer = new OpenTelemetryTracer(telemetry.getTracer());

 // Create spans using TraceContext API (bridges to OTel)
 try (SpanScope scope = otelTracer.startSpan("agent.process")) {
     // Your code here
     scope.setAttribute("agent.name", "my-agent");
 }

 // Or convert existing TraceContext to OTel span
 TraceContext ctx = TraceContext.newTrace("operation");
 Span span = otelTracer.toOTelSpan(ctx);
 
  • Constructor Details

    • OpenTelemetryTracer

      public OpenTelemetryTracer(io.opentelemetry.api.trace.Tracer tracer)
      Creates a new OpenTelemetryTracer with the given OTel Tracer.
      Parameters:
      tracer - the OpenTelemetry Tracer instance
  • Method Details

    • startSpan

      public OpenTelemetryTracer.SpanScope startSpan(String operationName)
      Starts a new span with the given operation name.
      Parameters:
      operationName - the operation name
      Returns:
      a SpanScope that should be closed when the operation completes
    • startSpan

      public OpenTelemetryTracer.SpanScope startSpan(String operationName, io.opentelemetry.api.trace.SpanKind kind)
      Starts a new span with the given operation name and kind.
      Parameters:
      operationName - the operation name
      kind - the span kind
      Returns:
      a SpanScope that should be closed when the operation completes
    • startChildSpan

      public OpenTelemetryTracer.SpanScope startChildSpan(TraceContext parent, String operationName)
      Starts a child span under an existing TraceContext.
      Parameters:
      parent - the parent TraceContext
      operationName - the child operation name
      Returns:
      a SpanScope for the child span
    • toOTelSpan

      public io.opentelemetry.api.trace.Span toOTelSpan(TraceContext ctx)
      Creates an OpenTelemetry span from a TraceContext.

      This is useful for converting existing TraceContext instances to OpenTelemetry spans for export.

      Parameters:
      ctx - the TraceContext
      Returns:
      the corresponding OpenTelemetry Span
    • fromCurrentContext

      public TraceContext fromCurrentContext(String operationName)
      Creates a TraceContext from the current OpenTelemetry context.

      This allows extracting trace information from incoming requests that carry OpenTelemetry context.

      Parameters:
      operationName - the operation name for the new TraceContext
      Returns:
      a TraceContext linked to the current OTel context
    • trace

      public <T> T trace(String operationName, Supplier<T> supplier)
      Executes a supplier within a span scope.
      Type Parameters:
      T - the return type
      Parameters:
      operationName - the operation name
      supplier - the code to execute
      Returns:
      the result of the supplier
    • trace

      public void trace(String operationName, Runnable runnable)
      Executes a runnable within a span scope.
      Parameters:
      operationName - the operation name
      runnable - the code to execute