Package com.tnsai.quality.observability
Class OpenTelemetryTracer
java.lang.Object
com.tnsai.quality.observability.OpenTelemetryTracer
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);
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA scope wrapper for OpenTelemetry spans that implements AutoCloseable. -
Constructor Summary
ConstructorsConstructorDescriptionOpenTelemetryTracer(io.opentelemetry.api.trace.Tracer tracer) Creates a new OpenTelemetryTracer with the given OTel Tracer. -
Method Summary
Modifier and TypeMethodDescriptionfromCurrentContext(String operationName) Creates a TraceContext from the current OpenTelemetry context.startChildSpan(TraceContext parent, String operationName) Starts a child span under an existing TraceContext.Starts a new span with the given operation name.Starts a new span with the given operation name and kind.io.opentelemetry.api.trace.SpantoOTelSpan(TraceContext ctx) Creates an OpenTelemetry span from a TraceContext.voidExecutes a runnable within a span scope.<T> TExecutes a supplier within a span scope.
-
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
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 namekind- the span kind- Returns:
- a SpanScope that should be closed when the operation completes
-
startChildSpan
Starts a child span under an existing TraceContext.- Parameters:
parent- the parent TraceContextoperationName- the child operation name- Returns:
- a SpanScope for the child span
-
toOTelSpan
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
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
Executes a supplier within a span scope.- Type Parameters:
T- the return type- Parameters:
operationName- the operation namesupplier- the code to execute- Returns:
- the result of the supplier
-
trace
Executes a runnable within a span scope.- Parameters:
operationName- the operation namerunnable- the code to execute
-