OpenTelemetry - What It Is, Why It Became the Standard and How to Instrument Traces
1. Recap: Metrics and Logs Ready, Now Connect the Dots
The mini apps in this series already expose metrics (articles 2 and 3) and send logs to Loki (article 4). What’s still missing is the third pillar seen in article 1: traces, which show the path of a specific request and exactly where the time was spent. This article introduces OpenTelemetry, the current standard for generating this kind of data, and instruments both mini apps to produce real traces.
2. What OpenTelemetry Is
OpenTelemetry (often abbreviated “OTel”) is a CNCF project that defines a set of APIs, SDKs and a transport protocol (OTLP — OpenTelemetry Protocol) for generating, processing and exporting telemetry data — metrics, logs and, the focus of this article, traces — in a way that’s neutral regarding which backend will actually store and visualize that data.
In practice, this means the application is instrumented once, against the OpenTelemetry API, and decides later, through configuration, where to export to — Prometheus, Loki, Jaeger, a commercial vendor, or, as this series will do in the next article, an intermediate OpenTelemetry collector. Switching observability backends no longer requires rewriting the instrumentation.
3. Why It Became the Dominant Standard
Before OpenTelemetry, there were two competing, incompatible projects trying to solve the same problem: OpenTracing (focused on traces) and OpenCensus (traces and metrics, led by Google). Every instrumentation library had to pick one of the two, fragmenting the ecosystem — a library instrumented for OpenTracing didn’t talk to a backend designed for OpenCensus without translation layers.
In 2019, the two projects merged into OpenTelemetry, under the CNCF (the same foundation that hosts Kubernetes and Prometheus). This solved the fragmentation and, more importantly, gave the project vendor neutrality: no observability vendor controls the standard, which led practically every major player (Datadog, New Relic, Grafana, AWS, Google Cloud, Azure) to support OTLP as a native ingestion protocol. Today, instrumenting with OpenTelemetry is the option that least locks the application into a specific vendor.
4. Tracing Concepts: Trace, Span and Context
Revisiting and expanding on what article 1 introduced:
- Span — the basic unit: it represents an operation with a start, an end and a duration (an HTTP call, a query, a calculation). It carries a name, attributes (arbitrary key-value pairs) and, optionally, point-in-time events within its time range.
- Trace — the set of all spans generated to serve a single request end to end, connected to each other by a shared
trace id. - Parent/child span — a span can start other spans “inside” it (for example, the HTTP request’s span starts a child span for the database query it triggers). This nesting relationship is what makes it possible to rebuild the timing tree seen in article 1.
- Context propagation — when a request crosses a process boundary (an HTTP call between two services), the current
trace idandspan idtravel as HTTP headers (the standard format istraceparent, defined by the W3C Trace Context specification), letting the next service continue the same trace instead of starting a new one.
5. Instrumenting the Python Mini App
The official Python SDK needs to be installed (it’s not part of the standard library, unlike what the mini apps have done so far for metrics and logs — generating spans by hand without an SDK is possible, but reimplementing sampling, context and propagation manually defeats the purpose of using a standard built precisely for this):
pip install opentelemetry-sdk
Manual instrumentation, creating one span per request:
# tracing.py
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
BatchSpanProcessor,
ConsoleSpanExporter,
)
provider = TracerProvider()
provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(provider)
tracer = trace.get_tracer("python-app")
Wiring it into article 2’s app.py, wrapping request handling in a span:
from tracing import tracer
# inside do_GET, in the branch that isn't /metrics
with tracer.start_as_current_span("handle_request") as span:
span.set_attribute("http.path", self.path)
request_count += 1
log_to_loki(f"request received at {self.path}")
span.set_attribute("http.status_code", 200)
For now, the exporter used is ConsoleSpanExporter, which just prints each span to stdout — enough to confirm the instrumentation is generating data, without depending on any additional infrastructure yet. Running the application and making a request, the terminal should show a JSON block with trace_id, span_id, name and start_time/end_time.
6. Instrumenting the PHP Mini App
The PHP equivalent uses the official SDK, installed via Composer:
composer require open-telemetry/sdk open-telemetry/exporter-otlp
<?php
// tracing.php
require __DIR__ . '/vendor/autoload.php';
use OpenTelemetry\SDK\Trace\TracerProvider;
use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor;
use OpenTelemetry\SDK\Trace\SpanExporter\ConsoleSpanExporter;
$exporter = new ConsoleSpanExporter();
$tracerProvider = new TracerProvider(
new SimpleSpanProcessor($exporter)
);
$tracer = $tracerProvider->getTracer('php-app');
Wiring it into article 3’s app.php, around processing an order:
require __DIR__ . '/tracing.php';
$span = $tracer->spanBuilder('process_order')->startSpan();
$scope = $span->activate();
try {
// ... existing order processing logic ...
$span->setAttribute('order.duration_ms', $state['durations'][count($state['durations']) - 1] * 1000);
} finally {
$span->end();
$scope->detach();
}
The try/finally pattern ensures the span is ended even if processing throws an exception halfway through — a span that never ends (because an end() is never reached) is a common manual instrumentation mistake, and it pollutes the tracing backend with “stuck” spans.
7. Conclusion and Next Steps
We saw what OpenTelemetry is, why the 2019 merger of OpenTracing and OpenCensus made it the neutral instrumentation standard, the concepts of span, trace and context propagation, and manually instrumented both mini apps to generate spans — for now, only visible in each application’s console. In the next article, the whole stack is unified in a single docker-compose.yml: the mini apps swap ConsoleSpanExporter for a real OTLP exporter, pointing at an OpenTelemetry collector, alongside the already configured Prometheus, Loki and Grafana.
References: