RousselTMLEARNING

Observability

TraceQL: the trace query language

Master TraceQL: span selectors, resource and span attributes, operators, duration comparisons, structural operators, aggregations and select.

1. Span selectors #

Empty braces are valid, but expensive.

Attributes and scopes #

Structure of a TraceQL query
Levels are composed from left to right.
{ resource.service.name = "checkout" }                       # attribut de ressource
{ span.http.status_code = 500 }                                   # attribut de span
{ .http.status_code = 500 }                                       # portée implicite (span puis ressource)
{ name = "POST /payer" }                                          # nom du span
{ status = error }                                                # statut : error | ok | unset
{ kind = server }                                                 # kind : server, client, producer...
{ duration > 2s }                                                 # durée du span
{ traceDuration > 5s }                                            # durée de la trace complète
{ resource.service.name =~ "api-.*" && span.http.method != "GET" }  # regex, &&, ||
Good practice: Use qualified attribute names (span. / resource.): searches are faster and unambiguous.

2. Structural operators #

Linking spans together.

Parent, child and descendant #

# Appels de paiement en erreur sous le service checkout (descendant)
{ resource.service.name = "checkout" } >> { resource.service.name = "payment" && status = error }

# Enfant direct
{ resource.service.name = "frontend" } > { resource.service.name = "checkout" }

# Spans frères (sibling)
{ span.db.system = "postgresql" } ~ { span.db.system = "redis" }

# Remontée : trouver l'ancêtre d'un span lent
{ duration > 1s } << { resource.service.name = "gateway" }
OperatorMeaning
>>A is an ancestor of B
>A is the direct parent of B
~A and B are siblings
<< / <Reversed versions

3. Aggregations and selection #

Analyzing the result.

TraceQL pipeline #

# Traces avec plus de 10 requêtes SQL (problème N+1 probable)
{ span.db.system = "postgresql" } | count() > 10

# Traces dont la durée moyenne des spans HTTP dépasse 500 ms
{ span.http.method != "" } | avg(duration) > 500ms

# Afficher uniquement certains attributs dans les résultats
{ status = error } | select(span.http.url, resource.service.name, span.exception.message)

# Regrouper
{ resource.service.name = "api" } | by(span.http.route) | count() > 5

Through the API and the Tempo CLI #

curl -G -s http://localhost:3200/api/search \
  --data-urlencode 'q={ resource.service.name = "checkout" && status = error }' \
  --data-urlencode 'limit=20' --data-urlencode 'start='$(date -d '1 hour ago' +%s) --data-urlencode 'end='$(date +%s) | jq '.traces[].traceID'

curl -s http://localhost:3200/api/search/tags | jq                 # attributs disponibles
curl -s http://localhost:3200/api/search/tag/service.name/values | jq

4. Ready-to-use diagnostic queries #

A small catalog.

Catalog #

GoalTraceQL query
Recent errors of a service{ resource.service.name = "checkout" && status = error }
Slow requests (> 2 s){ kind = server && duration > 2s }
Outgoing HTTP 5xx calls{ kind = client && span.http.status_code >= 500 }
Excessive retries{ name = "http.retry" } | count() > 3
Traces for one user{ span.user.id = "42" }

Related articles

The LGTM stack (Loki, Grafana, Tempo, Mimir): architecture and setup

Grafana Labs' LGTM stack: Loki for logs, Grafana for visualization, Tempo for traces, Mimir for metrics (and …

Grafana Tempo: the distributed tracing backend at scale

Understand distributed tracing and Grafana Tempo: traces, spans, TraceID, architecture (distributor, ingester…

Installing Grafana Tempo (Docker Compose, binary and Helm)

Install Grafana Tempo: the tempo.yaml configuration, Docker Compose with Grafana and Prometheus, OTLP ports, …

Keep learning

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!