Lesson 3 of 4 Analyzing Instrumentation

Inspecting Distributed Tracing with Application Insights

This lesson explains how traces connect services, and how to use them to find a cause.

Learning outcomes

After completing this lesson, you will be able to:

  • Define traces and spans.
  • Explain how trace context propagates.
  • Use Application Map.
  • Read an end-to-end transaction view.

Why tracing is needed

In a monolith, a stack trace shows what went wrong. In a distributed application, one request may touch a gateway, three services, a queue and a database, each on different machines. Logs from each service exist, but nothing joins them. Distributed tracing joins them by giving each request an identifier that travels with it.

How it works

A trace is the whole journey of one request. A span is one step in it, such as a database call, with a start time and duration. Spans have parent and child relationships, so the whole can be drawn as a tree.

Context travels between services in the W3C Trace Context headers, chiefly traceparent, which holds a trace ID and the current span ID. OpenTelemetry and Application Insights both use this standard, and they propagate it automatically for common libraries. In Application Insights, the trace ID appears as the operation ID, which is stored as OperationId in workspace tables. All telemetry items for one request share it.

Tracing works only where all services are instrumented. If one service in the chain is not, its part of the journey is missing, and the trace may seem broken. Asynchronous work through a queue needs the context to be passed in the message.

True or false

Traces, spans and context

Exam skills 5.2.3

Check how distributed tracing joins the steps of a request.

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

Application Map

Application Map draws components and their dependencies, as discovered from telemetry. Each node shows request rate, failures and duration, and each connecting line shows calls between them, so a red node or line is a good place to begin. Selecting a component opens its failures and performance. The cloud role name sets how a service is labelled, and each service needs its own name, otherwise different services merge into a single node.

The map shows the structure of a system across all requests, whereas the next view shows just one.

End-to-end transaction details

The end-to-end transaction details view, or transaction diagnostics, shows one transaction as a Gantt chart. Each row is a span, its position shows when it started, and its length shows how long it took. Failed rows are marked, and details and exceptions appear alongside.

Reading the chart is straightforward. A long bar that contains a long child bar means that the time was spent in the child. A long bar with no children means that the time was spent in that component's own code, or in an uninstrumented call. Many short sequential bars to one database suggest an N+1 query problem, in which code makes one call per item in a loop.

The following diagram sketches the Gantt chart of a slow checkout transaction in Application Insights.

End-to-end transaction details for a slow checkout. The Gantt chart shows the 8-second request with the checkout service's own work short and one long dependency span, such as a call to a payment API, which explains slow checkouts on healthy hosts.
The span durations are illustrative.
Scenario questions

Reading the Gantt chart

Exam skills 5.2.3

Interpret the end-to-end transaction view.

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

A method for investigating

  1. Find the symptom

    Use the Failures or Performance view to find the affected operation.

  2. Locate the component

    Use Application Map to see where failures or latency are concentrated.

  3. Pick a sample

    Select a slow or failed example request.

  4. Read the trace

    Use the Gantt chart to find the span responsible.

  5. Go to the logs

    Query logs by operation ID to see the detail.

The final step uses KQL, which the next lesson teaches. The following query lists everything recorded for one request.

union AppRequests, AppDependencies, AppExceptions, AppTraces
| where OperationId == "5f1b2c7a9e0d4d13a7c2b18e6f4d9a30"
| order by TimeGenerated asc
Put in order

Investigating a slow request

Exam skills 5.2.3

Arrange the steps of a distributed tracing investigation.

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

Finding a slow dependency

Users report that checkout takes 8 seconds. Host metrics for the checkout service are normal. Which tool should you use, and what would you expect to find?

Use the end-to-end transaction details for a slow checkout request. Healthy hosts suggest that the time goes to a dependency, and the Gantt chart would show a long span, perhaps a call to a payment API or a database. Application Map would help to find the likely component first. Scaling the service would not help with a slow dependency, and VM insights would only confirm what the host metrics already show.

Check yourself

Two services appear as one node on Application Map. What is the likely cause?

Show answer

They share a cloud role name, so each service needs its own.

Exam Tips

Remember for the exam

  • A trace is a whole request; a span is one step.
  • Operation ID links all telemetry for a request.
  • Application Map shows components, failures and latency.
  • The end-to-end transaction view shows a Gantt chart of one request.
  • Each service needs instrumentation and its own cloud role name.