QA Interview Hub · Knowledge base · Quizzes · Українська

Logs and observability › Observability: logs, metrics, traces

Distributed tracing, trace ids and OpenTelemetry

A distributed trace links all operations of one request with a shared trace id that services pass along in a header, such as the W3C traceparent. A trace consists of spans forming a tree. OpenTelemetry is a vendor-neutral standard and set of SDKs for traces, metrics and logs. The trace id is written to logs. Because of sampling, a missing trace does not prove the request never happened.

Logs · Tracing

An e2e test failed on the "Pay" step. Which of ten services broke?

One click on "Pay" starts a chain: gateway, checkout, payments, an external payment provider, a database, a notification queue. The test sees only the final 500. Searching for the cause across each service’s logs means matching entries by time and hoping the clocks did not drift. A distributed trace solves this: all operations of one request are linked by a common identifier. This lesson covers how it is built, what traceparent is, what OpenTelemetry is and how to use it from tests.

How it works and why. A trace is the whole path of a request, a span is one operation (an HTTP call, a DB query) with a start, a duration, attributes and a status. Spans form a tree: each has its parent’s id. For services to know they belong to one trace, the context is passed in request headers. The W3C Trace Context standard defines the traceparent header:

traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
             version-trace id (32 hex)-parent span id (16 hex)-flags

The flag 01 means "sampled": the caller may have recorded the trace, and this decision is passed on. An all-zero trace id or span id is invalid. The extra tracestate header carries vendor data. Older systems use their own X-Request-Id, X-Correlation-Id or the B3 format; the key is that this identifier lands in every log entry (a traceId field), then you go from a trace to logs and back in one click.

A trace as a span chart

api-gateway 3.1 scheckout 2.9 spayments 2.6 sgateway call 2.2 s: timeout
Figure 1. The bar width is the span duration. It shows that all the time goes into the gateway call, which ends with an error, while the other services merely waited.

OpenTelemetry (OTel). This is an open, vendor-neutral standard and a set of SDKs for traces, metrics and logs. The parts: APIs and SDKs for languages (manual and automatic instrumentation of HTTP clients, databases, frameworks), the OTLP transfer protocol (gRPC or HTTP) and the Collector: an intermediate service that receives data, processes it (filters, masks fields, samples) and forwards it to a backend (Jaeger, Tempo, Datadog and others). The maturity of the signals differs across languages, so before using logs or metrics in a project check the language’s documentation.

Example: a test puts the trace id into the report. The test generates a traceparent itself and adds it to the API request. A server instrumented with OTel continues this trace. If the test fails, the trace id is in the report, and it opens the full path of the request:

import { randomBytes } from 'node:crypto';

test('an order is created', async ({ request }, testInfo) => {
  const traceId = randomBytes(16).toString('hex');
  const spanId = randomBytes(8).toString('hex');
  const traceparent = '00-' + traceId + '-' + spanId + '-01';

  const res = await request.post('/api/orders', {
    headers: { traceparent },
    data: { sku: 'A-1', qty: 1 },
  });
  // the trace id goes into the report: it is the key for the trace store
  testInfo.annotations.push({ type: 'trace-id', description: traceId });
  expect(res.status()).toBe(201);
});

Why this beats searching by time: the id is unambiguous, so other people’s requests on a shared stand do not land in the results. The flag -01 asks to record the trace; services with a sampler that honours the parent’s decision keep it even if they normally sample only a small share. A trap: some gateways at the edge of the system drop or replace incoming tracing headers, and then the test’s trace id does not match the real one. Check that the response or the logs really contain this id.

Sampling. Recording every trace is expensive. Head sampling decides at the start of a request (for example keep 5%), which is cheap but may drop exactly the failing request. Tail sampling decides after completion and can keep all error and slow traces, but requires buffering in the Collector. So for a rare failure "no trace" does not mean "no request".

Limits and alternatives. Context disappears where a service did not pass it on: an asynchronous queue without header propagation, a custom HTTP client without instrumentation, a thread that lost the context. Then the trace breaks in two and the "bottleneck" is not visible. Without traces, correlation ids in logs remain: worse, but it works. Traces do not replace metrics: they show individual requests, not the overall picture.

Typical mistakes.

  • Not writing the trace id into logs: the trace exists, but there is nothing to jump to the logs by.
  • Hoping queues and background jobs pass context on their own: it is propagated explicitly.
  • Treating a missing trace as proof the request did not happen: maybe the sampler did not select it.
  • Using an all-zero or a made-up short trace id: the header is invalid and the server ignores it.

Check yourself

A test added a traceparent header, but the trace store has no request with this trace id. Name three possible causes.

Answer

A gateway at the entry dropped or replaced the header; the server is not instrumented or does not continue incoming context; a sampler dropped the trace (head sampling that ignores the flag); the header is invalid (wrong length or zeros). First check whether the id reaches at least the service’s logs.

Why is tail sampling better than head sampling for investigating failures, and what does it cost?

Answer

The decision is made after the request completes, so all error and slow traces can be kept and normal ones dropped. The cost: the Collector must buffer all spans until the trace finishes, which means memory and more complex infrastructure.

In an interview. Junior: A trace is a request’s path through services, made of spans. The identifier is passed in a header such as traceparent, and by it you find all entries of that request in the logs. Middle/Senior: I know the traceparent format and that the sampled flag passes the recording decision on. In tests I generate a trace id, add it to the report and check that it reaches the logs. I understand sampling (head versus tail) and that context is lost on queues and unsupported clients, so I verify that separately.

More articles in this chapter