OpenTelemetry Tracing API
The OpenTelemetry Tracing API provides language-level interfaces for creating and interacting with traces and spans. In Java, it lets application code record distributed operations while keeping instrumentation separate from the backend that stores or analyzes telemetry.
A trace represents the end-to-end path of a request through one or more services. A span represents a single operation within that trace, such as an HTTP request, database query, or message-processing step.
The Tracing API lets code obtain a `Tracer` and create spans. A span can carry attributes, events, status information, and links to other spans. Child spans can be created from the current `Context`, forming the parent-child structure used to reconstruct a trace.
OpenTelemetry separates the API from the SDK. Application and library code can use API interfaces such as `Tracer` and `Span`, while the SDK supplies the implementation and configuration for processing and exporting telemetry.
In distributed systems, trace context can be propagated between services. For example, an incoming HTTP request can establish the current span, and an outgoing request can inject the trace context so the receiving service creates a related span.
Key points
- A trace is composed of related spans representing operations.
- A `Tracer` creates spans; a `Span` records information about one operation.
- `Context` carries the current span so child operations can be associated with the correct parent.
- The API defines instrumentation interfaces, while the SDK provides implementation and processing.
- Trace context propagation connects spans across service boundaries.
Interview questions
What is the difference between a trace and a span in OpenTelemetry?
A trace represents the complete path of an operation through a distributed system. A span represents one operation along that path, such as handling an HTTP request or executing a database call. Spans can have parent-child relationships, allowing the trace to be reconstructed as a hierarchy. Each span can also contain attributes, events, status, and timing information.
How do you create a span with the OpenTelemetry Java API?
First obtain a `Tracer`, commonly from `GlobalOpenTelemetry` or dependency injection. Then use `spanBuilder` to create and start a span. Application code can add attributes or events while the operation runs. The span should be ended when the operation finishes, normally with a `try`/`finally` structure.
What is the role of Context in OpenTelemetry tracing?
`Context` provides a way to carry the current execution context, including the current span. When instrumentation creates a child span from the active context, the tracing system can associate the new operation with its parent. Context can also be propagated across asynchronous or distributed boundaries when the relevant instrumentation supports it. Correct context handling is therefore important for maintaining trace relationships.
What is the difference between the OpenTelemetry API and SDK?
The API defines the interfaces that application and library instrumentation uses to create and interact with telemetry. The SDK provides the implementation and mechanisms for processing and exporting telemetry. This separation allows instrumentation code to depend on stable API concepts without being tightly coupled to a particular exporter or backend. In Java, this commonly means instrumentation uses API classes while application configuration supplies the SDK.
How does distributed tracing connect spans created in different services?
The originating service records a span and propagates trace context with the outgoing request or message. The receiving service extracts that context and uses it when creating its server or consumer span. This establishes the parent or related relationship needed to connect operations across service boundaries. OpenTelemetry instrumentation commonly handles these propagation details for supported protocols and frameworks.