Tracing Technicalities with Grafana Tempo
local graph
Chapter 6 — Tracing Technicalities with Grafana Tempo
Section titled “Chapter 6 — Tracing Technicalities with Grafana Tempo”Grafana Tempo is the third telemetry storage tool from Grafana that we’ll discuss; it provides the capability to store and query trace data. This chapter will introduce the Tempo query language (TraceQL). TraceQL can be used to select and filter traces generated by your applications to gather insights from across traces; the language is very similar to LogQL and PromQL but tailored to trace data. In this chapter, we will explore the major tracing protocols and how they can be used to output traces from applications; this will help you make informed choices on which protocol to use in an application, or which protocols to support when collecting data. We’ll then explore the architecture of Tempo to understand how it can fulfill the need for a scalable platform for tracing.
We will cover the following main topics in this chapter:
- Introducing Tempo and the TraceQL query language
- Exploring tracing protocols
- Understanding the Tempo architecture
Technical requirements
Section titled “Technical requirements”In this chapter, you will use the demo application and Grafana Cloud instance (set up in Chapter 3). You’ll find the code for the chapter in the GitHub repository at https://github.com/PacktPublishing/Observability-with-Grafana/tree/main/chapter6. You’ll find the Code in Action videos at
Updating the OpenTelemetry Demo application
Section titled “Updating the OpenTelemetry Demo application”For this chapter, we have provided an updated OTEL-Collector.yaml with additional tracing configuration. This updated configuration is in the GitHub repository in the chapter6 directory. Full details on the update process are available from the GitHub repository in README.md.
To apply this updated configuration to the OpenTelemetry Collector, follow these steps:
- Upgrade the Collector
$ helm upgrade --version '0.73.1' --values chapter6/OTEL-Collector.yaml --values OTEL-Creds.yaml owg open-telemetry/opentelemetry-collectorNAME: owg-otel-collectorLAST DEPLOYED: Sat Aug 19 12:42:36 2023NAMESPACE: defaultSTATUS: deployedREVISION: 4…
- Validate that the upgrade
Your traces will now have more labels, and will also produce service graphs and span metrics.$ kubectl get pods --selector=component=standalone-collectorNAME READY STATUS RESTARTS AGEowg-otel-collector-594fddd656-tfstk 1/1 Terminating 1 (70s ago) 2m8sowg-otel-collector-7b7fb876bd-vxgwg 1/1 Running 0 3s
Now that our local installation is updated, let’s begin by exploring the third query language, TraceQL.
Introducing Tempo and the TraceQL query language
Section titled “Introducing Tempo and the TraceQL query language”Tempo and TraceQL are the newest of the tools and query languages we will explore in depth in this book. Like LogQL, TraceQL was built using PromQL as an inspiration and offers developers and operators a familiar set of filtering, aggregation, and mathematical tools that aid in the observability flow between metrics, logs, and traces.
Let’s have a quick look at how Tempo sees trace data:
- Trace collection: Introduced in Chapter 2, a trace (or distributed trace) is a collection of data that represents a request propagating through a system. Traces are often collected from multiple applications. Spans are sent by each application to some form of collection architecture and, ultimately, to Tempo for storage and querying.
- Trace fields: The following diagram introduces a simplified structure of a trace, similar to the simplified structure of logs, seen in Chapter 4, and traces, seen in Chapter 5:

Figure 6.1 – A simplified view of a trace containing four spans
Back in Chapter 2, we introduced the common fields of a trace. In the preceding figure, we can see that all four spans have the same trace_id, which is the unique identifier of the whole trace. Each span has a unique identifier, the span_id. Each span also records where it came from, using the parent_id field. Finally, the start and end times are recorded. This simplified view does exclude several of the fields seen in the OpenTelemetry Protocol (OTLP), Zipkin, and Jaeger, which are used to capture a lot of contextual information. We will discuss these later in this chapter.
Now that we’ve seen the structure of trace data, let’s now explore the Tempo interface and how we can query data.
Exploring the Tempo features
Section titled “Exploring the Tempo features”In this section, we will introduce the major features of Tempo, the tracing platform available in Grafana, and its query language, TraceQL. In Chapters 4 and 5, we introduced the LogQL and PromQL languages, which focus on being able to select log or metric data and offer detailed functionality to perform a powerful analysis of the selected data. Currently, PromQL only offers the ability to select trace data. While there are powerful tools to select this data, there are no tools to perform an analysis. Such functionality is an eventual aim for the product, but we wanted to highlight the current state of Tempo at v2.3.x.
Let’s begin by exploring the user interface for Tempo and how it represents trace data.
The Tempo interface
Section titled “The Tempo interface”The main view used to explore data in Tempo is split into two parts, the query editor, and the trace view. In the following screenshot, the query editor is on the left and the trace view is on the right. When you first enter the view, you will only see the query editor. The trace view is opened when a trace ID or span ID is clicked on:

Figure 6.2 – The query editor (left) and the trace view (right)
The results panel is contextual. If we use TraceQL to run a search, it will return a list of traces and spans that match; this is shown on the left in the preceding screenshot. However, if we search for a specific trace ID, we will be shown the trace view on the right, where we can explore the spans in the trace.
While we are examining the query panel, let’s look at two of the different search modes we can use, as shown here:

Figure 6.3 – Search modes
The two search modes shown in the preceding screenshot are as follows:
- Basic Search mode: Here, you are presented with drop-down menus to select the traces you are interested in. This is especially useful for people who are new to Tempo and want to get data quickly, but we will not explore this search mode in this book. Be aware that this mode is due to be deprecated in Grafana 10.3.
- TraceQL mode: This allows you to use TraceQL to search in a very granular way for the data you need. This is the default search mode.
As well as these, three search modes are available:
- Loki Search mode: This should be familiar to you from Chapter 4; it is available in Tempo, so you can pivot between logs containing trace and span IDs and a full trace view very quickly.
- JSON File mode: This allows for a trace saved in the JSON format to be imported and viewed directly. Combined with the export functionality, this allows for the simple preservation and sharing of interesting traces. Exploring the data in an exported JSON file is a good exercise for understanding the underlying data structures used in tracing.
- Service Graph mode: One of the most powerful features of collecting distributed traces is the ability to visualize the connections between those services. This tool gives anyone a clear graphical representation of how applications in a system communicate with each other. This functionality leverages metrics and traces together to represent a system’s current state. The tool will also indicate erroring requests in red and successful requests in green.
The following screenshot shows the default view of a service graph:

Figure 6.4 – Service graphs
As well as representing the connections between services, the preceding screenshot shows the request rates and average latency of the responses. Above the service graph, the Requests, Errors, and Duration (RED) metrics are shown. We will discuss these metrics in greater detail in Chapter 9.
At the time of writing, this aspect of OpenTelemetry and Tempo is under active development, and the authors are looking forward to the features that are coming.
Now that we have seen the interface for Tempo, let us understand how to use TraceQL to query trace data.
Exploring the Tempo Query language
Section titled “Exploring the Tempo Query language”Like Prometheus and Loki, Tempo offers a query language, TraceQL. Now that you are familiar with the interface of Tempo and the structure of traces, let’s explore the features of TraceQL.
Field types
Section titled “Field types”TraceQL uses two field types, intrinsic fields and attribute fields. Let’s look at these in detail:
- Intrinsic fields: These are the fundamental information of spans and traces. These are used to show information in the trace view. The intrinsic fields are as follows:
status: The value could beerror,ok, or unset (null)statusMessage: Optional text to clarify the statusduration: The time between the start and end of the spanname: The operation or span namekind: The value could beserver,client,producer,consumer,internal, orunspecified, which is a fallback valuetraceDuration: Number of milliseconds between the start and end of all spans in the tracerootName: The name of the first span of the tracerootServiceName: The name of the first service of the trace
- Attribute fields: These are the customizable fields that have been added to a span, either by the application or the collection tooling. Attribute fields are either span fields or resource fields, which is a distinction derived from the implementation of OpenTelemetry:
- Span attributes are the fields added to the span by the submitting application; examples include
span.http.methodandspan.app.ads.ad_response_type.- Resource attributes, conversely, represent the entity producing telemetry; examples include
resource.container.idandresource.k8s.node.name. For efficient querying, it is best practice to always includespan.andresource.in an attribute query. However, it is possible to use a leading.to query when you are unsure whether a field is a span or resource – for example,.http.methodor.k8s.node.name. When looking at an individual span, you can see the fields available under Span Attributes and Resource Attributes. This expanded view of a single trace shows the fields that are contained in the span:
- Resource attributes, conversely, represent the entity producing telemetry; examples include
- Span attributes are the fields added to the span by the submitting application; examples include
Figure 6.5 – Attributes for a span
Now that you have a good grasp of the fields available when searching traces in Tempo, let’s have a look at how to search for traces and spans.
Selecting traces and spans
Section titled “Selecting traces and spans”TraceQL offers tools to select data to show in a dashboard, or just to explore the current state of the system. These are described in the following table:
| Name | Syntax | Operators | Scope |
|---|---|---|---|
| Field selector | {field = " value"} | =, !=, >, >=, <, <=, =~, !~ | Selects spans on the value of a field |
| Field expressions | {field1="value1" && field2="value2"} | &&, || | Selects spans on the values of multiple fields |
| Logical operators | {field1="value1} && { field1="value2} | &&, || | Selects spans where a logical check between sets of spans is true. This can check multiple fields. |
| Structural operators | {field1="value1"} > { field2="value2} | >, >>, ~ | Searches for spans in the second filter where they are related to the first filter. These are explained in more detail after this table. |
Table 6.1 – The selection operators available in TraceQL
Structural operators offer the ability to carry out queries that take account of where conditions are met upstream (parent) or downstream (child) in a trace. Let’s look at some examples:
>or the child operator refers to the direct child, such asThe preceding line would search for any span from the product catalog service, where the frontend service was the immediate parent.{.service.name="frontend"} > {.service.name="productcatalogservice"}>>or the descendent operator refers to any descendent, such as the following:This would search for any span from the cart service where the frontend service was a parent in the trace but could have passed through another service first, such as the checkout service.{.service.name="frontend"} >> {.service.name="cartservice"}~or the sibling operator refers to any spans that share the same parent, such asThis would search for any span that visited the frontend multiple times. In the demo application, the frontend service would be the parent.{.service.name="frontend"} ~ {.service.name="frontend"}
These operators allow us to select data. TraceQL also allows tools to carry out aggregation and mathematical functions on trace data.
Aggregators and arithmetic
Section titled “Aggregators and arithmetic”Aggregators and mathematical functions allow for more complex queries. These can display information aggregated across all traces. Some of these are described in the following table:
| Name | Syntax | Operators | Scope |
|---|---|---|---|
| Count aggregator | | count() > 10 | count() | Refines the returned spans by the total count of spans in the span set |
| Numeric aggregators | | avg(duration) > 20ms | avg(), max(), min(), and sum() | Refines the returned spans by the field in the span set |
| Arithmetic operators | {field1 < field2 * 10} | +, -, *, /, and ^ | Performs arithmetic on numeric fields |
Table 6.2 – Aggregation and mathematical operators in TraceQL
It is worth noting that TraceQL is in active development at the time of writing, so this list of operators is expected to grow.
Now that you have seen how to search trace data, let’s discuss the important topic of moving seamlessly between data types to get a full picture.
Pivoting between data types
Section titled “Pivoting between data types”When correctly instrumented, an application will produce data that can be used to move between traces, logs, and metrics to truly understand what is happening.
Let’s consider the following span, where an error was seen in checkoutservice. This could be a problematic error in a real shop, as it suggests a customer got to the checkout and was unable to complete their sale for some reason:

Figure 6.6 – Finding the logs for an error
The query interface for Tempo offers a helpful link, Logs for this span, which will open a Loki query. This functionality uses the service_name and service_namespace fields from the trace to query Loki. In a similar way, services can inject the trace context (traceId and spanId) into their log output where available. Loki can then be configured to provide contextual linking to Tempo, to see the trace view. Finally, as mentioned in Chapter 5, metrics can present exemplars, which allow users to see a sample trace from a metric graph.
We’ve explored the ways of seeing the data produced by applications. In the next section, we will understand the different protocols that are available to produce trace data for Grafana Tempo.
Exploring tracing protocols
Section titled “Exploring tracing protocols”In Chapter 2, we introduced the three main tracing protocols, OTLP, Zipkin, and Jaeger. In this section, we will explore some of the features of these protocols, how well-supported they are, and how to use them in the software services that you write. We will also discuss the different headers used by these protocols to propagate context to other services. A tracing protocol is made up of a set of headers that are added to the HTTP requests made by an instrumented application. These headers are what propagate the information of individual spans to downstream services. Once all of these spans are collected, they form a fully distributed trace.
What are the main tracing protocols?
Section titled “What are the main tracing protocols?”First, let’s look at the features and support of the main tracing protocols – OTLP, Zipkin, and Jaeger.
OTLP tracing offers support for C++,.NET, Erlang, Go, Java, JavaScript, PHP, Python, Ruby, Rust, and Swift. There is good support for OTLP in popular development frameworks such as Spring, Django, ASP.NET, and Gin. With this wide support, it is best practice to search the documentation for your framework of choice on how to instrument an application; in most cases, instrumentation can be as simple as adding a few lines of dependencies.
Tracing is an inherently distributed process, and there have been several standards to propagate trace fields. This means that applications may need to use different HTTP or gRPC headers when handling traces, depending on other applications in their operating environment. OTLP provides native support for W3C TraceContext, B3, and Jaeger propagation headers, as well as support for W3C baggage headers, used to propagate other context information. The support of B3 and Jaeger headers means that applications instrumented with Zipkin and Jaeger libraries are natively supported. However, other trace headers such as AWS’s X-Ray protocol are not maintained as part of the mainline distribution. If these protocols are used, it is recommended to use the relevant vendor’s distribution of OpenTelemetry – for example, the AWS Distro for OpenTelemetry when X-Ray is used in a monitored environment (https://aws.amazon.com/otel/).
In the data collection space, OTLP trace data has good support from the OpenTelemetry Collector, Grafana Agent, FluentBit via a plugin, and Telegraf via a plugin.
Zipkin
Section titled “Zipkin”Zipkin offers support for C#, Go, Java, JavaScript, Ruby, Scalar, and PHP via supported libraries, and C++, C, Clojure, Elixir, Lua, and Scala, via community-supported libraries. As with OTLP, there is also good support for Zipkin in popular development frameworks, so it is good practice to check the framework documentation when instrumenting applications.
Zipkin only natively supports the B3 propagation headers. However, as frameworks offer pluggable support for different trace protocols, support for alternative propagation headers is probably easy to implement in an application.
When it comes to data collection, Zipkin is supported by the OpenTelemetry Collector, Grafana Agent, and the native tools created by Zipkin.
Jaeger
Section titled “Jaeger”We have included Jaeger for historical reasons here, but it is not recommended for adoption. Jaeger was originally developed by Uber. Before January 2022, Jaeger offered SDKs for Java, Python, Node.js, Go, C#, and C++. These SDKs supported the OpenTracing APIs. OpenTelemetry was formed by the OpenTracing and OpenCensus projects merging. Jaeger now recommends the use of the OpenTelemetry SDKs for instrumenting applications. For applications already using the Jaeger client libraries, migration guides have been provided by OpenTelemetry: https://opentelemetry.io/docs/migration/opentracing/.
Jaeger libraries supported the Jaeger, Zipkin, and W3C TraceContext headers, but they had no support for any other propagation formats.
There was not wide support in data collectors for Jaeger while it was actively supported; the intended way to use the protocol was to collect data in a Jaeger backend locally in an environment. The OpenTelemetry Collector and Grafana Agent do offer receivers for Jaeger traces and allow you to collect these traces as applications migrate to the OpenTelemetry protocol.
Now that you are familiar with the tracing protocols, let’s look at the headers that are used to propagate information between services that use distributed tracing.
Context propagation
Section titled “Context propagation”Distributed tracing is relatively new in web technologies, with the World Wide Web Consortium (W3C) making Trace Context a recommended standard in November 2021, while the Baggage format is still currently in a working draft state. Tracing records information in two distinct ways:
- Traces and spans are sent to a collection agent by each application
- Applications also share data using HTTP or gRPC headers, which are picked up by the receiving application
As tracing is a new technology, a couple of unofficial standard formats were used before the official W3C Trace Context headers were decided on. To provide some historical context on tracing, we’ll explore the following formats:
- Jaeger/Uber headers
- Zipkin B3 headers
- W3C Trace Context headers
- W3C baggage headers
Jaeger/Uber headers
Section titled “Jaeger/Uber headers”Jaeger libraries historically used the following header formats; we’ve included these for historical reference, as these should be considered deprecated in favor of W3C Trace Context.
The two HTTP headers used in Jaeger are uber-trace-id and uberctx, which look like this:
uber-trace-id: {trace-id}:{span-id}:{parent-span-id}:{flags}uberctx-{baggage-key}: {baggage-value}An example of the uber-trace-id header is as follows:
uber-trace-id: 269daf90c4589ce1:5c44cd976d8f8cd9:39e8e549de678267:0x01Let’s break this down the various fields in the uber-trace-id header:
trace-id: This field is a 64-bit or 128-bit random number and is hex-encoded. In the example this is269daf90c4589ce1.span-idandparent-span-id: These are 64-bit random numbers and are hex-encoded. These are5c44cd976d8f8cd9and39e8e549de678267, respectively.flags: This field is used to convey additional information, such as whether the trace is being sampled. In this example, its value is0x01.
In the uberctx baggage header, the fields are as follows:
baggage-key: This is a unique string that is used to name the header.baggage-value: This is a string that will be percent-encoded. Baggage as a concept will be explored further in the W3C baggage section.
Zipkin B3 headers
Section titled “Zipkin B3 headers”Zipkin libraries use B3 headers; unlike the Uber headers, Zipkin has historically separated each field into its own header, as shown in the following snippet:
X-B3-TraceId: {TraceId}X-B3-ParentSpanId: {ParentSpanId}X-B3-SpanId: {SpanId}X-B3-Sampled: {bool}X-B3-Flags: 1 OR header absentb3: {TraceId}-{SpanId}-{SamplingState}-{ParentSpanId}Let’s break these headers down:
X-B3-TraceId: Similar to the Jaeger format,TraceIdis 64-bit or 128-bit hex-encoded. Here is an example of this header as it wouldX-B3-TraceId: 68720d6346a16000531430804ce28f9cX-B3-ParentSpanIdandX-B3-SpanId: These are 64-bit and hex-encoded.X-B3-Sampled: This has a value of either1or0, although early implementations may usetrueorfalse.X-B3-Flags: This header is used to propagate debug decisions.b3: Zipkin predated the introduction of the W3C Trace Context standards. To aid in the transition to the newly agreed standard, Zipkin introduced theb3header. Later versions of Zipkin can propagate using both these headers and the W3C Trace Context headers for interoperability. Theb3header exactly matches thetracestateheader used in W3C Trace Context and represents the other headers combined into one mapping.
W3C Trace Context
Section titled “W3C Trace Context”W3C specifies a standard pair of headers, as shown here:
traceparent: {version}-{trace-id}-{parent-id}-{trace-flags}tracestate: vendor specific trace informationLet’s break these headers down into their constituents:
- The various fields in
traceparentare as follows:- The
trace-idis a hex-encoded 16-byte array (128-bit).parent-idis a hex-encoded 8-byte array (64-bit); this field is equivalent tospan-idorSpanIdin Jaeger and B3, respectively, and represents the span ID used by the service that generated the header. It differs from the B3ParentSpanId, as this can be used to represent a service further upstream that initiated a traced process.- The
versionfield is another hex-encoded 8-bit field; it represents the version of the standard being used. Currently, only version 00 exists. trace-flagsis another hex-encoded 8-bit field. In version 00 of the W3C standard, the only available flag is one to denote whether sampling is occurring or not.
- The
tracestateis used to encode vendor-specific information. Whiletraceparentis a fixed format and required by any vendor adopting the standard,tracestateis available for vendors to ensure that trace data is propagated while giving space for them to use and encode that data as desired. The only requirement regarding this header field is that the contents will be a comma-separated list of key-value pairs.
W3C baggage
Section titled “W3C baggage”Baggage is a related but different concept to a trace. Baggage headers contain contextual data that is passed between applications. These headers can share specific fields from one application and a downstream application. For example, we might have a top-level concept of tenantId, but when an application makes a request to a downstream application, that application may not need to know about tenantId for it to process the request. A baggage header allows us to propagate this tenantId field to the downstream application. The downstream application can then use this field in its observability instrumentation, while not polluting its data model with an unrelated field. This effectively separates observability concerns from application concerns. It’s important to note that data contained in baggage headers can be exposed to anyone inspecting network traffic, so it should not be used to share sensitive information.
W3C baggage headers look like this:
baggage: key1=value1,key2=value2;property1;propertyKey=propertyValue,…All fields must be percent-encoded; the full header must have 64 members or fewer, and it has a maximum size of 8,192 bytes. Using baggage gives systems a standardized way to propagate contextual information.
We’ve now discussed how trace data is produced by applications and how it is shared, both with a collection agent and other applications. Let’s take some time to look at how data is processed and stored by Tempo.
Understanding the Tempo architecture
Section titled “Understanding the Tempo architecture”Like Loki and Mimir, Tempo leverages object stores such as Amazon S3, Google Cloud Storage, and Microsoft Azure Blob Storage. With the horizontal scalability of components in both the read and write pathways, Tempo has a fantastic ability to scale as data volumes increase.
The following diagram shows the architecture used by Tempo:

Figure 6.7 – The Tempo architecture
The write pathway for Tempo consists of the following:
- Distributor: The distributor is responsible for accepting spans and routing them to the correct instance of the ingester service, based on the trace ID of the span.
- Ingester: The ingester is responsible for grouping spans into traces, batching multiple traces into blocks, and writing bloom filters and indexes for querying. Once a block is complete, the ingester also flushes the data to the backend.
- Metrics generator: The metrics generator is an optional component; it receives spans from the distributor and uses them to produce service graphs and span metrics (such as the rate and the error duration). These are then written to a metrics backend.
The read pathway has these components:
- Query frontend: The frontend is responsible for receiving a query and splitting it into smaller shards, based on the blocks (created by the ingester) that will be read to return the requested data. These shards are then queued with queriers.
- Querier: This component is responsible for finding the requested data, either from the backend if the block has been flushed, or directly from the ingester if the block is still being collected.
The compactor, which is a standalone component, is responsible for optimizing the use of the backend storage.
Now that we’re done exploring the system architecture of Tempo, you have seen all the major components of the tool, and how distributed tracing using Tempo can help provide great visibility of the components of the systems you run.
Summary
Section titled “Summary”In this chapter, you learned how to use TraceQL to query trace data stored in Tempo, which will help you build queries for dashboards using this rich data source. You have explored the Tempo user interface, so you will be confident in moving around the interface. Combined with the skills learned in Chapters 4 and 5 you will be confident in moving between log, metric, and trace data in Grafana to be able to observe the systems you work with.
We took a detailed look at the different protocols and libraries you can use when instrumenting an application, and we saw the levels of support those tools have across different programming languages. We also explored the HTTP headers that are used to propagate trace data between applications. This will help you choose the best way to instrument an application and how to work with applications that are already instrumented with tracing.
Finally, we looked at the Tempo architecture and how it can horizontally scale to support your organization with however many traces you need to sample. With this knowledge, you will understand how to operate a Tempo installation and monitor the various components.
In the next chapter, we will conclude Part 2 of the book by showing you how to collect data from your infrastructure layers, whether that is a cloud provider such as AWS, Azure, or GCP, or a Kubernetes cluster.