# OpenTelemetry-First Observability Stack
# ======================================
# This stack is designed around OpenTelemetry (OTel) Collector as the central hub.
# Key Focus Areas:
# - **Schema Alignment**: Enforce OTEL Semantic Conventions (v1.25.0) using `transform` processor.
# - Renames/validates attributes (e.g., `http.status_code` -> `http.response.status_code`).
# - **Baggage Rules**: `baggage` processor for propagation/clearing (e.g., propagate `user.id`, clear sensitive `debug`).
# - **Sampling Gates**:
# - Head sampling: `probabilistic_sampler` (e.g., 5% sampling rate).
# - Tail sampling: `tail_sampling` with policies (e.g., always sample errors, latency >1s, rate-limited).
# - **Service Maps**: `spanmetrics` + `servicegraph` processors generate Prometheus metrics for service graphs.
# - Visualize in Grafana (service dependency graphs).
#
# Architecture:
# 1. **Apps**: Instrument with OTel SDKs (e.g., auto-instrumentation for Java/JS/Python).
# - Set env: OTEL_TRACES_EXPORTER=otlp, OTEL_METRICS_EXPORTER=otlp, OTEL_LOGS_EXPORTER=otlp
# - OTEL_RESOURCE_ATTRIBUTES=service.name=my-service,service.version=1.0
# - Schema: OTEL_SDK_EXPERIMENTAL_OCI_SCHEMA_URL=https://opentelemetry.io/schemas/1.25.0
# 2. **OTel Collector (Gateway)**: Receives OTLP, processes, exports.
# 3. **Backends**:
# - Traces: Jaeger (or Tempo)
# - Metrics: Prometheus + Grafana (for service maps)
# - Logs: Loki
# 4. **Deployment**: Docker Compose (below) or Kubernetes (OpenTelemetry Operator).
#
# Quickstart: docker-compose up
Docker Compose Deployment (docker-compose.yml)
version: '3.8'
services:
# OTel Collector (Gateway)
otel-collector:
image: otelcol contrib:latest # otel/opentelemetry-collector-contrib
command: ["--config=/etc/otel-collector-config.yaml"]
volumes:
- ./otel-collector-config.yaml:/etc/otel-collector-config.yaml
ports:
- "4317:4317" # OTLP gRPC
- "4318:4318" # OTLP HTTP
- "8888:8888" # Prometheus scrape
- "8889:8889" # Prometheus remote_write
depends_on:
- jaeger
- prometheus
- loki
# Jaeger (Traces + Service Maps fallback)
jaeger:
image: jaegertracing/all-in-one:latest
ports:
- "16686:16686" # UI
environment:
- COLLECTOR_OTLP_ENABLED=true
# Prometheus (Metrics + Service Graphs)
prometheus:
image: prom/prometheus:latest
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"
# Grafana (Dashboards + Service Maps)
grafana:
image: grafana/grafana:latest
ports:
- "3000:3000"
environment:
- GF_AUTH_ANONYMOUS_ENABLED=true
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
volumes:
- grafana-storage:/var/lib/grafana
depends_on:
- prometheus
- loki
# Loki (Logs)
loki:
image: grafana/loki:latest
ports:
- "3100:3100"
volumes:
grafana-storage:
Prometheus Config (prometheus.yml)
global:
scrape_interval: 15s
scrape_configs:
- job_name: otel-collector
static_configs:
- targets: [otel-collector:8888] # Scrapes spanmetrics/servicegraph metrics
- job_name: otel-collector-remote
remote_write:
- url: http://otel-collector:8889 # Optional remote_write
Grafana Setup
- Login: http://localhost:3000 (admin/admin)
- Add datasources:
- Service Maps: Import Grafana dashboard ID
12622 (Service Dependency Graph) or 15558 (OTel Service Graph).
- Queries use
service_graph metrics from servicegraph processor.
OTel Collector Config (otel-collector-config.yaml)
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318
processors:
# Schema Alignment: Transform attributes to OTEL SemConv v1.25.0
transform/schema:
error_mode: propagate
trace_statements:
- context: span
statements:
# Align HTTP attributes
- set(attributes["http.response.status_code"], attributes["http.status_code"])
- delete_key(attributes, "http.status_code")
# Align DB attributes (example)
- set(attributes["db.system"], attributes["db.type"])
- delete_key(attributes, "db.type")
# Enforce resource schema
- set(resource.attributes["service.version"], resource.attributes["service_version"]) if exists(resource.attributes, "service_version") else nil()
- delete_key(resource.attributes, "service_version")
- context: resource
statements:
- set(attributes["deployment.environment"], "production") where attributes["k8s.namespace.name"] == "prod"
# Baggage Rules: Propagate user.id/correlation.id, clear sensitive/debug
baggage:
rule:
- baggage_key: user.id
action: propagate
- baggage_key: correlation.id
action: propagate
- baggage_key: debug
action: delete_match
- baggage_key: ssn # Sensitive example
action: delete
# Sampling Gates: Head Sampling (probabilistic, 5% rate)
probabilistic_sampler:
sampling_percentage: 5 # Adjust per service via resource attributes
# Tail Sampling: Always sample errors/latency, rate-limit others
tail_sampling:
policies:
- name: errors
type: status_code
status_code:
status_codes: [ERROR]
- name: latency
type: latency
latency:
threshold_ms: 1000
- name: operation
type: operation
operation: "high-traffic-operation" # Custom
- name: rate_limit
type: rate_limiting_group
rate_limiting_group:
span_p99_from_fallback: 500 # ms
group: purchases
decision_wait: 10s
num_traces: 10000
schedule_delay: 500ms
# Service Maps: Generate metrics from spans
spanmetrics:
metrics_exporter: prometheus # Exports to Prometheus receiver
servicegraph:
add_service_graph: true # Generates service_graph metrics (client/server edges)
batch: # Batches for efficiency
send_batch_size: 1000
timeout: 5s
# Optional: Resource detection (K8s, host, etc.)
resourcedetection:
detectors: [env, system, os, process, k8s]
timeout: 2s
override: false
exporters:
# Traces to Jaeger
jaeger:
endpoint: "jaeger:14250"
tls:
insecure: true
# Metrics to Prometheus (scrape + remote_write)
prometheus:
endpoint: "0.0.0.0:8888"
prometheusremotewrite:
endpoint: "0.0.0.0:8889"
# Logs to Loki
loki:
endpoint: "http://loki:3100/loki/api/v1/push"
# Debug (local dev)
logging:
loglevel: info
service:
pipelines:
traces:
receivers: [otlp]
processors: [transform/schema, baggage, probabilistic_sampler, tail_sampling, spanmetrics, servicegraph, batch]
exporters: [jaeger, logging]
metrics:
receivers: [otlp, spanmetrics, servicegraph] # Include generated metrics
processors: [batch]
exporters: [prometheus, prometheusremotewrite, logging]
logs:
receivers: [otlp]
processors: [batch]
exporters: [loki, logging]
Validation & Testing
- Start Stack:
docker-compose up
- Instrument Sample App (e.g., Python Flask with OTel auto-instr):
pip install opentelemetry-distro opentelemetry-exporter-otlp
otel-cli --traces_exporter otlp --service_name test-app --otlp_endpoint http://localhost:4318 python app.py
- Generate Traffic:
curl http://localhost:8000 (your app).
- Verify:
- Schema: Jaeger UI → Trace → Attributes (should see
http.response.status_code).
- Baggage: Add
baggage user.id=123 in trace context → Propagates.
- Sampling: Jaeger → Increase traffic → See ~5% traces, 100% errors.
- Service Maps: Grafana → Explore → Service Graph dashboard → See edges (e.g., frontend→backend).
- Scale:
- Kubernetes: Use OpenTelemetry Collector Operator + DaemonSet agents.
- Per-service sampling: Use
parentbased_sampler or attributes in collector.
Advanced Tweaks
- Dynamic Config: Use
zpages extension for runtime changes (--config=... --set=extensions.zpages).
- Vendor Export: Replace exporters (e.g.,
datadog, newrelic).
- Logs Correlation: Add
spanlog bridge in instrumentation.
This stack is production-ready, scalable, and 100% OTel-native. Questions? Provide your app details for custom instrumentation. 🔍