Lesson 1 of 2 Distributed Tracing with OpenTelemetry

Understanding OpenTelemetry

This lesson explains telemetry signals, traces and spans, context propagation over HTTP and messages, and sampling.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the three OpenTelemetry signals.
  • Explain spans, span kinds and attributes.
  • Describe W3C trace context propagation.
  • Explain why sampling must keep whole traces.

Why distributed tracing

A single user question to an AI assistant can pass through an API in Container Apps, a call to Azure Managed Redis, a vector query in Cosmos DB, a model call, a Service Bus message and a function that logs feedback. When the answer is slow or wrong, logs from each component show fragments, but not which fragments belong to the same request or where the time went. Distributed tracing records the whole journey as one connected trace.

OpenTelemetry is a vendor-neutral, open-source standard from the Cloud Native Computing Foundation for producing telemetry. It defines an API for instrumenting code, SDKs that process telemetry, instrumentation libraries for common frameworks, a wire protocol (OTLP), and exporters that send data to back ends such as Azure Monitor. Instrumenting once with OpenTelemetry avoids lock-in to one monitoring product.

Match the pairs

OpenTelemetry parts

Exam skills 4.2.1

Match each part of OpenTelemetry with what it does.

Log in to try this activity Interactive activities are free with an account.

Signals

OpenTelemetry defines three main signals, which Azure Monitor stores in Application Insights.

SignalWhat it recordsExample
TracesTimed operations (spans) and how they relateAn HTTP request with its database query and model call
MetricsNumeric measurements aggregated over timeRequests per second, queue length, token counts
LogsTimestamped records of eventsA warning that a document had no text to embed

The signals are linked: logs written while a span is active carry its trace and span IDs, so a log record can be viewed within the trace that produced it.

Sort into groups

Trace, metric or log?

Exam skills 4.2.1

Decide which OpenTelemetry signal records each item.

Log in to try this activity Interactive activities are free with an account.

Traces and spans

A trace is a tree of spans that share a 16-byte trace ID. Each span represents one operation and has a name, start and end times, a span ID, the ID of its parent span, a kind, attributes (key-value details such as http.response.status_code or db.system), events (timestamped notes, such as an exception), a status (unset, ok or error) and links to related spans in other traces. The root span has no parent.

Span kinds describe the role of an operation, and Application Insights maps them to its own record types.

  • SERVER: handling an incoming request; recorded as a request.
  • CLIENT: an outgoing call, such as an HTTP request or database query; recorded as a dependency.
  • PRODUCER and CONSUMER: sending and receiving messages; recorded as a dependency and a request respectively.
  • INTERNAL: work inside a process, such as chunking a document; recorded as a dependency of type InProc.
Match the pairs

Inside a span

Exam skills 4.2.1

Match each part of a span with what it holds.

Log in to try this activity Interactive activities are free with an account.

Context propagation

Spans in different processes join one trace only if the trace context travels with the call. Context propagation does this. For HTTP, the W3C Trace Context standard defines the traceparent header, which carries the version, trace ID, parent span ID and sampling flag, for example 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01, and an optional tracestate header. Instrumented HTTP clients add the header automatically, and instrumented servers read it and create child spans.

For messaging, the context is stored in message properties. The Azure SDKs, when tracing is enabled, add trace context to Service Bus and Event Hubs messages they send and link the receiving span to it, so a trace can continue from an API through a queue to a worker. Baggage propagates application-defined key-value pairs, such as a tenant ID, alongside the context.

  1. Start

    The API receives a request and starts a SERVER span with a new trace ID.

  2. Call

    An instrumented HTTP or SDK call creates a CLIENT span and sends traceparent with the request.

  3. Continue

    The next service reads traceparent and creates its SERVER span as a child.

  4. Message

    A PRODUCER span adds the context to a Service Bus message; the worker's CONSUMER span continues it.

  5. Assemble

    The back end groups all spans by trace ID into one end-to-end view.

The following diagram compares the trace without and with context propagation.

Joining a trace across a Service Bus queue. Without context propagation, the worker's processing starts a new trace. When the API sends through an instrumented client, the PRODUCER span writes the trace context into the message properties, and the worker extracts it, so its CONSUMER span joins the original trace.
Both services send to the same Application Insights resource.
Put in order

Following a trace across services

Exam skills 4.2.1

Put the steps of context propagation from an API through a queue in order.

Log in to try this activity Interactive activities are free with an account.

Sampling

Recording every span of every request can be expensive at high volume. Sampling keeps a representative subset. Head-based sampling decides at the start of a trace, for example keeping 10% of traces, and propagates the decision in the traceparent sampling flag so that all services keep or drop the same traces, producing complete traces rather than fragments. Azure Monitor's Python distro supports a fixed-percentage sampling ratio and rate-limited sampling, which caps traces per second. Application Insights adjusts counts in its charts for the sampling rate, so request rates remain accurate.

Sample whole traces

If each service sampled independently at random, a trace might keep the API's span but drop the worker's, leaving incomplete traces. Parent-based sampling, which honours the incoming sampling decision, keeps traces whole.

True or false

Sampling

Exam skills 4.2.1

Decide whether each statement about sampling is true.

Log in to try this activity Interactive activities are free with an account.
Worked example

A trace that stops at the queue

An API in Container Apps and a worker in Azure Functions both send OpenTelemetry data to the same Application Insights resource. The end-to-end view shows the API's request and its Service Bus send, but the worker's processing appears as a separate trace, so the team cannot see how long whole jobs take.

The trace context is not crossing the queue. The API must send messages through an instrumented Service Bus client, so that the PRODUCER span writes trace context into the message properties, and the worker must extract that context when it receives the message, which the Azure SDK's tracing integration and the Functions OpenTelemetry support do, so that its CONSUMER span joins or links to the original trace. Both services also need distinct service names so they appear separately on the application map.

Lowering the sampling ratio would drop data, not join it. Sending both services to different Application Insights resources would make correlation harder. Logging the order ID in both places helps searching but does not create a trace.

Check yourself

Which span kind does Application Insights record as a request, and which as a dependency?

Show answer

SERVER (and CONSUMER) spans become requests; CLIENT, PRODUCER and INTERNAL spans become dependencies.

Exam Tips

Remember for the exam

  • A trace is a tree of spans sharing a trace ID; each span has kind, attributes, events and status.
  • SERVER spans become requests; CLIENT, PRODUCER and INTERNAL spans become dependencies.
  • W3C traceparent carries context over HTTP; message properties carry it through queues.
  • Head-based, parent-based sampling keeps whole traces.