Skip to content
instrument-distributed-tracing logo

Instrument Distributed Tracing

instrument-distributed-tracing

Instrument applications with OpenTelemetry for distributed tracing, including auto and manual instrumentation, context propagation, sampling strategies, and integration with Jaeger or Tempo. Use when debugging latency issues in distributed systems, understanding request flow across microservices,...

pjt222/agent-almanac0installs36stars

SKILL.md

Full skill instructions

Instrument Distributed Tracing

Implement OpenTelemetry distributed tracing to track requests across microservices and identify performance bottlenecks.

When to Use

  • Debugging latency issues in distributed systems with multiple services
  • Understanding request flow and dependencies between microservices
  • Identifying slow database queries or external API calls within a transaction
  • Correlating traces with logs and metrics for root cause analysis
  • Measuring end-to-end latency from user request to response
  • Migrating from legacy tracing systems (Zipkin, Jaeger) to OpenTelemetry
  • Establishing SLO compliance through detailed latency percentile tracking

Inputs

  • Required: List of services to instrument (languages and frameworks)
  • Required: Tracing backend choice (Jaeger, Tempo, Zipkin, or vendor SaaS)
  • Optional: Existing instrumentation libraries (OpenTracing, Zipkin)
  • Optional: Sampling strategy requirements (percentage, rate limiting)
  • Optional: Custom span attributes for business-specific metadata

Procedure

See Extended Examples for complete configuration files and templates.

Step 1: Set Up Tracing Backend

Deploy Jaeger or Grafana Tempo to receive and store traces.

Option A: Jaeger all-in-one (development/​testing):

# docker-compose.yml
version: '3.8'
services:
  jaeger:
    image: jaegertracing/​all-in-one:1.51
    ports:
      - "5775:5775/​udp"   # Zipkin compact thrift
      - "6831:6831/​udp"   # Jaeger compact thrift
      - "6832:6832/​udp"   # Jaeger binary thrift
      - "5778:5778"       # Serve configs
      - "16686:16686"     # Jaeger UI
      - "14268:14268"     # Jaeger HTTP thrift
      - "14250:14250"     # Jaeger GRPC
      - "9411:9411"       # Zipkin compatible endpoint
    environment:
      - COLLECTOR_ZIPKIN_HOST_PORT=:9411
      - COLLECTOR_OTLP_ENABLED=true
    restart: unless-stopped

Option B: Grafana Tempo (production, scalable):

# docker-compose.yml
version: '3.8'
services:
  tempo:
    image: grafana/​tempo:2.3.0
    command: ["-config.file=/​etc/​tempo.yaml"]
    volumes:
      - ./​tempo.yaml:/​etc/​tempo.yaml
      - tempo-data:/​tmp/​tempo
    ports:
      - "3200:3200"   # Tempo HTTP
      - "4317:4317"   # OTLP gRPC
      - "4318:4318"   # OTLP HTTP
      - "9411:9411"   # Zipkin
    restart: unless-stopped

volumes:
  tempo-data:

Tempo configuration (tempo.yaml):

server:
  http_listen_port: 3200

distributor:
  receivers:
    jaeger:
# ... (see EXAMPLES.md for complete configuration)

For production with S3 storage:

storage:
  trace:
    backend: s3
    s3:
      bucket: tempo-traces
      endpoint: s3.amazonaws.com
      region: us-east-1
    wal:
      path: /​tmp/​tempo/​wal
    pool:
      max_workers: 100
      queue_depth: 10000

Expected: Tracing backend accessible, ready to receive traces via OTLP, Jaeger UI or Grafana shows "no traces" initially.

On failure:

  • Verify ports not already in use: netstat -tulpn | grep -E '(4317|16686|3200)'
  • Check container logs: docker logs jaeger or docker logs tempo
  • Test OTLP endpoint: curl http://localhost:4318/​v1/​traces -v
  • For Tempo: validate config syntax with tempo -config.file=/​etc/​tempo.yaml -verify-config

Step 2: Instrument Applications (Auto-Instrumentation)

Use OpenTelemetry auto-instrumentation for common frameworks to minimize code changes.

Python with Flask:

pip install opentelemetry-distro opentelemetry-exporter-otlp
opentelemetry-bootstrap -a install
# app.py
from flask import Flask
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
# ... (see EXAMPLES.md for complete configuration)

Go with Gin framework:

go get go.opentelemetry.io/​otel
go get go.opentelemetry.io/​otel/​exporters/​otlp/​otlptrace/​otlptracegrpc
go get go.opentelemetry.io/​otel/​sdk/​trace
go get go.opentelemetry.io/​contrib/​instrumentation/​github.com/​gin-gonic/​gin/​otelgin
package main

import (
    "context"
    "github.com/​gin-gonic/​gin"
    "go.opentelemetry.io/​otel"
# ... (see EXAMPLES.md for complete configuration)

Node.js with Express:

npm install @opentelemetry/​api \
            @opentelemetry/​sdk-node \
            @opentelemetry/​auto-instrumentations-node \
            @opentelemetry/​exporter-trace-otlp-grpc
// tracing.js
const { NodeSDK } = require('@opentelemetry/​sdk-node');
const { OTLPTraceExporter } = require('@opentelemetry/​exporter-trace-otlp-grpc');
const { getNodeAutoInstrumentations } = require('@opentelemetry/​auto-instrumentations-node');
const { Resource } = require('@opentelemetry/​resources');
const { SemanticResourceAttributes } = require('@opentelemetry/​semantic-conventions');
# ... (see EXAMPLES.md for complete configuration)

Expected: Traces from instrumented services appear in Jaeger UI or Grafana, HTTP requests automatically create spans.

On failure:

  • Check exporter endpoint is reachable from application
  • Verify environment variables: OTEL_EXPORTER_OTLP_ENDPOINT=http://tempo:4317
  • Enable debug logging: OTEL_LOG_LEVEL=debug (Python), OTEL_LOG_LEVEL=DEBUG (Node.js)
  • Test with simple span: manually create a span to verify export pipeline
  • Check for version conflicts between OpenTelemetry packages

Step 3: Add Manual Instrumentation

Create custom spans for business logic, database queries, and external calls.

Python manual spans:

from opentelemetry import trace

tracer = trace.get_tracer(__name__)

def process_order(order_id):
    # Create a span for the entire operation
# ... (see EXAMPLES.md for complete configuration)

Go manual spans:

import (
    "context"
    "go.opentelemetry.io/​otel"
    "go.opentelemetry.io/​otel/​attribute"
    "go.opentelemetry.io/​otel/​codes"
    "go.opentelemetry.io/​otel/​trace"
# ... (see EXAMPLES.md for complete configuration)

Span attributes best practices:

  • Use semantic conventions: http.method, http.status_code, db.system, db.statement
  • Add business context: user.id, order.id, product.category
  • Include resource identifiers: instance.id, region, availability_zone
  • Record errors: span.RecordError(err) and span.SetStatus(codes.Error, message)
  • Add events for significant milestones: span.AddEvent("cache_miss")

Expected: Custom spans appear in trace view, parent-child relationships correct, attributes visible in span details, errors highlighted.

On failure:

  • Verify context propagation: parent span context passed to child
  • Check span names are descriptive and follow naming conventions
  • Ensure spans are ended (use defer span.End() in Go, with blocks in Python)
  • Review attribute types: strings, ints, bools, floats only
  • Validate semantic conventions: use standard attribute names where applicable

Step 4: Implement Context Propagation

Ensure trace context flows across service boundaries and async operations.

HTTP headers propagation (W3C Trace Context):

# Client side (Python with requests)
import requests
from opentelemetry import trace
from opentelemetry.propagate import inject

tracer = trace.get_tracer(__name__)
# ... (see EXAMPLES.md for complete configuration)
// Server side (Go with Gin)
import (
    "go.opentelemetry.io/​otel"
    "go.opentelemetry.io/​otel/​propagation"
)

# ... (see EXAMPLES.md for complete configuration)

Message queue propagation (Kafka):

# Producer
from opentelemetry.propagate import inject
from kafka import KafkaProducer

producer = KafkaProducer(bootstrap_servers=['kafka:9092'])

# ... (see EXAMPLES.md for complete configuration)
# Consumer
from opentelemetry.propagate import extract

def process_message(msg):
    # Extract trace context from Kafka headers
    headers = {k: v.decode('utf-8') for k, v in msg.headers}
    ctx = extract(headers)

    # Continue the trace
    with tracer.start_as_current_span("process_order_event", context=ctx):
        order_id = json.loads(msg.value)['order_id']
        handle_order(order_id)

Async operations (Python asyncio):

import asyncio
from opentelemetry import trace, context

async def async_operation():
    # Capture current context
    token = context.attach(context.get_current())
    try:
        with tracer.start_as_current_span("async_database_query"):
            await asyncio.sleep(0.1)  # Simulated async work
            return "result"
    finally:
        context.detach(token)

Expected: Traces span multiple services, trace IDs consistent across service boundaries, parent-child relationships preserved.

On failure:

  • Verify W3C Trace Context propagator configured: otel.propagation.set_global_textmap(TraceContextTextMapPropagator())
  • Check headers are passed in HTTP requests
  • For Kafka: ensure headers supported by broker version (v0.11+)
  • Debug with header inspection: log traceparent header value
  • Use trace visualization to identify broken trace links

Step 5: Configure Sampling Strategies

Implement sampling to reduce trace volume and cost while maintaining visibility.

Sampling strategies:

from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.sampling import (
    ParentBased,
    TraceIdRatioBased,
    StaticSampler,
    Decision
# ... (see EXAMPLES.md for complete configuration)

Tail-based sampling with Tempo:

Configure in tempo.yaml:

overrides:
  defaults:
    metrics_generator:
      processors: [service-graphs, span-metrics]
      storage:
        path: /​tmp/​tempo/​generator/​wal
        remote_write:
          - url: http://prometheus:9090/​api/​v1/​write
            send_exemplars: true

    # Tail sampling (requires tempo-query)
    ingestion_rate_limit_bytes: 5000000
    ingestion_burst_size_bytes: 10000000

Use Grafana Tempo's TraceQL for dynamic sampling:

# Sample traces with errors
{ status = error }

# Sample slow traces (>1s)
{ duration > 1s }

# Sample specific services
{ resource.service.name = "checkout-service" }

Expected: Trace volume reduced to target percentage, error traces always sampled, sampling decision visible in trace metadata.

On failure:

  • Verify sampler applied before tracer provider initialization
  • Check sampling decision attribute in exported spans
  • For tail sampling: ensure sufficient buffering (ingestion_burst_size_bytes)
  • Monitor dropped traces: otel_traces_dropped_total metric
  • Test with synthetic high-volume traffic to validate sampling rate

Step 6: Correlate Traces with Metrics and Logs

Link traces to metrics and logs for unified observability.

Add trace IDs to logs (Python):

import logging
from opentelemetry import trace

# Custom log formatter with trace context
class TraceFormatter(logging.Formatter):
    def format(self, record):
# ... (see EXAMPLES.md for complete configuration)

Generate metrics from traces (Tempo):

# tempo.yaml
metrics_generator:
  registry:
    external_labels:
      cluster: production
  storage:
# ... (see EXAMPLES.md for complete configuration)

This generates Prometheus metrics:

  • traces_service_graph_request_total - request count between services
  • traces_span_metrics_duration_seconds - span duration histogram
  • traces_spanmetrics_calls_total - span call counts

Query traces from metrics (Grafana):

Add exemplar support to Prometheus datasource in Grafana:

datasources:
  - name: Prometheus
    type: prometheus
    url: http://prometheus:9090
    jsonData:
      exemplarTraceIdDestinations:
        - name: trace_id
          datasourceName: Tempo

In Grafana dashboard, enable exemplars:

{
  "fieldConfig": {
    "defaults": {
      "custom": {
        "showExemplars": true
      }
    }
  }
}

Expected: Clicking metric exemplars opens trace, logs show trace IDs, traces link to logs, unified debugging across signals.

On failure:

  • Verify exemplar support enabled in Prometheus (requires v2.26+)
  • Check trace ID format matches (32-char hex)
  • Ensure metrics generator enabled in Tempo config
  • Validate remote write endpoint accessible from Tempo
  • Test exemplar queries: histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m])) and on() exemplar

Validation

  • Tracing backend receives spans from all instrumented services
  • Traces show correct parent-child relationships across services
  • Span attributes include semantic conventions and business context
  • Context propagates correctly across HTTP calls and message queues
  • Sampling strategy reduces trace volume to target percentage
  • Error traces always sampled (if using error-aware sampling)
  • Trace IDs appear in application logs with correct format
  • Grafana shows traces linked from metrics via exemplars
  • Log panels have data links to trace viewer
  • Trace retention matches configured storage policy

Common Pitfalls

  • Context not propagated: Forgetting to pass context to downstream calls breaks traces. Always pass context explicitly.
  • Spans never ended: Missing defer span.End() (Go) or with blocks (Python) causes spans to remain open and memory leaks.
  • Over-instrumentation: Creating spans for every function causes trace bloat. Focus on service boundaries, database calls, and external APIs.
  • Missing error recording: Not calling span.RecordError() loses valuable debugging information. Always record errors in spans.
  • High cardinality attributes: Using unbounded values (user IDs, request bodies) as span attributes causes storage issues. Use sampling or aggregate labels.
  • Incorrect span kind: Using wrong span kind (CLIENT vs SERVER vs INTERNAL) affects service graph generation. Follow semantic conventions.
  • Sampling before context: Sampling decisions must respect parent trace context. Use ParentBased sampler to honor upstream sampling.

Related Skills

  • correlate-observability-signals - Unified debugging with metrics, logs, and traces linked by trace IDs
  • setup-prometheus-monitoring - Generate metrics from traces using Tempo metrics generator
  • configure-log-aggregation - Add trace IDs to logs for correlation with distributed traces
  • build-grafana-dashboards - Visualize trace-derived metrics and exemplar links in dashboards

More skills from pjt222

evolve-agent logo
pjt222/agent-almanac

evolve-agent

Evolve an existing agent definition by refining its persona in-place or creating an advanced variant. Covers assessing the current agent against best practices, gathering evolution requirements, choosing scope (refinement vs. variant), applying changes to skills, tools, capabilities, and limitati...

36 0
View
version-ml-data logo
pjt222/agent-almanac

version-ml-data

Version machine learning datasets using DVC (Data Version Control) with remote storage backends, build reproducible data pipelines with dependency tracking, integrate with Git workflows, and ensure data lineage for model reproducibility. Use when versioning large datasets that do not fit in Git, ...

36 0
View
create-2d-composition logo
pjt222/agent-almanac

create-2d-composition

Compose 2D graphics programmatically using SVG generation, diagram layout algorithms, image compositing, and batch processing workflows. Use when generating diagrams, flowcharts, or infographics programmatically, creating reproducible scientific figures, automating production of badges or visual ...

36 0
View
manage-kubernetes-secrets logo
pjt222/agent-almanac

manage-kubernetes-secrets

Implement secure secrets management in Kubernetes using SealedSecrets for GitOps, External Secrets Operator for cloud secret managers, and rotation strategies. Handle TLS certificates, API keys, and credentials with encryption at rest and RBAC controls. Use when storing sensitive configuration fo...

36 0
View
appraise-gemstone logo
pjt222/agent-almanac

appraise-gemstone

Appraise gemstone value using the four Cs (color, clarity, cut, carat), origin assessment, treatment detection, and market factor analysis. Advisory educational guidance only — not a certified appraisal. Use when understanding factors that determine a gemstone's value, pre-screening stones before...

36 0
View
center logo
pjt222/agent-almanac

center

AI dynamic reasoning balance — maintaining grounded reasoning under cognitive pressure, smooth chain-of-thought coordination, and weight-shifting cognitive load across subsystems. Use at the beginning of a complex task requiring multiple coordinated reasoning threads, after a sudden context shift...

36 0
View
build-tcg-deck logo
pjt222/agent-almanac

build-tcg-deck

Build a competitive or casual trading card game deck. Covers archetype selection, mana/energy curve analysis, win condition identification, meta-game positioning, and sideboard construction for Pokemon TCG, Magic: The Gathering, Flesh and Blood, and other TCGs. Use when building a new deck for a ...

36 0
View
setup-prometheus-monitoring logo
pjt222/agent-almanac

setup-prometheus-monitoring

Configure Prometheus for time-series metrics collection, including scrape configurations, service discovery, recording rules, and federation patterns for multi-cluster deployments. Use when setting up centralized metrics collection for microservices, implementing time-series monitoring for applic...

36 0
View
assess-holistic-health logo
pjt222/agent-almanac

assess-holistic-health

Conduct temperament-based health assessment from Hildegard von Bingen's Causae et Curae. Evaluates the four temperaments (sanguine, choleric, melancholic, phlegmatic), elemental correspondences (air, fire, earth, water), and provides dietary and lifestyle recommendations for rebalancing. Use when...

36 0
View
implement-gitops-workflow logo
pjt222/agent-almanac

implement-gitops-workflow

Implement GitOps continuous delivery using Argo CD or Flux with app-of-apps pattern, automated sync policies, drift detection, and multi-environment promotion. Manage Kubernetes deployments declaratively from Git with automated reconciliation. Use when implementing declarative infrastructure mana...

36 0
View
assess-ip-landscape logo
pjt222/agent-almanac

assess-ip-landscape

Map the intellectual property landscape for a technology domain or product area. Covers patent cluster analysis, white space identification, competitor IP portfolio assessment, freedom-to-operate preliminary screening, and strategic IP positioning recommendations. Use before starting R&D in a new...

36 0
View
configure-log-aggregation logo
pjt222/agent-almanac

configure-log-aggregation

Set up centralized log aggregation with Loki and Promtail (or ELK stack), including log parsing, label extraction, retention policies, and integration with metrics for correlation. Use when consolidating logs from multiple services into a searchable system, replacing local log files with centrali...

36 0
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View