Structuring OpenTelemetry Observability
I'm planning to create a guide for building an OpenTelemetry-first observability stack. My focus will be on schema alignment, which includes semantic conventions and various attributes. I’ll also touch on baggage rules, sampling strategies like head and tail sampling, and how to generate service maps from metrics and traces.
I’ll structure the guide into sections covering principles, schema, baggage, sampling, and configurations, with reference architecture and governance. It’s crucial to ensure attribute mapping and maintain service consistency throughout. I'll include minimal configuration examples to aid clarity.Creating an OpenTelemetry Guide
I’m working on crafting a concise guide with headings and bullet points in markdown. I’ll emphasize that being "OpenTelemetry-first" means instrumenting with OTel SDKs and OTLP. The OTel Collector will serve as the central hub, with backend options like Tempo or Jaeger for traces, Prometheus or Mimir for metrics, and Loki or Elastic for logs.
I'll also outline schema and governance aspects to ensure compatibility across various backends and recommend a low cardinality attribute schema for service.name to maintain uniqueness.
Reference architecture (OpenTelemetry-first)
Rule: everything speaks OTLP; vendor/backends are downstream adapters.
Recommended flow
- Apps instrumented with OTel SDKs (traces + metrics + logs) → OTLP (gRPC preferred).
- OpenTelemetry Collector (agent or sidecar) per node/cluster does enrichment + local protections (limits, PII guards).
- OpenTelemetry Collector (gateway) does cross-service policy (tail sampling, routing, multi-tenant auth) and exports to:
- Traces: Tempo/Jaeger/Zipkin/Elastic APM…
- Metrics: Prometheus remote_write / Mimir / Cortex…
- Logs: Loki / Elastic / Splunk / etc.
This keeps instrumentation stable while letting backends change.
1) Schema alignment (semantic conventions + governance)
A. Non‑negotiable identity fields
These must be consistent across traces/metrics/logs:
Resource attributes
service.name (unique, stable; do not include instance/host)
service.namespace (optional but recommended for org/domain boundaries)
service.version (from build)
deployment.environment (e.g., prod, staging)
cloud.*, k8s.*, host.* (from detectors)
Trace/Span attributes (key ones for service maps + filtering)
span.kind (SERVER/CLIENT/PRODUCER/CONSUMER/INTERNAL)
- RPC/HTTP/db messaging semantic conventions:
- HTTP:
http.request.method, url.scheme, server.address, http.response.status_code
- RPC:
rpc.system, rpc.service, rpc.method
- Messaging:
messaging.system, messaging.destination.name, messaging.operation
- DB:
db.system, db.operation.name, db.collection.name / db.namespace
Logs
- Use OTel Logs with
trace_id/span_id correlation.
- Prefer structured fields that mirror span attributes (same keys).
B. Naming rules (avoid fractured service maps)
service.name: lowercase, - separated, no env suffix (env goes in deployment.environment)
- Good:
checkout, auth-api
- Avoid:
checkout-prod, CheckoutService, checkout_v2
- Use
service.namespace for grouping, not prefixes in service.name.
C. Cardinality guardrails (so metrics/logs don’t explode)
- Allowed high-cardinality only in traces, not metrics.
- Metrics label allowlist (typical):
service.name, service.namespace, deployment.environment, http.request.method, http.response.status_code (bounded), rpc.system, messaging.system.
- Explicitly ban/strip in metrics/log labels: user IDs, emails, request IDs, full URLs, IPs, session tokens.
D. Enforce alignment in the Collector (transform + filter)
Use Collector processors to normalize and repair telemetry:
- resource detection: add
k8s.*, cloud.*
- transform: rename legacy keys → semantic conventions
- attributes: drop known-bad keys; hash/redact where required
- routing: ensure prod/staging separation
2) Baggage rules (what is allowed to propagate)
Baggage is powerful and dangerous: it propagates everywhere and can become a privacy/cost incident.
A. Strict allowlist
Define a small set of keys that are permitted end-to-end:
tenant.id (bounded, not PII)
region (bounded)
feature.flag (bounded)
request.class (e.g., interactive|batch)
experiment.id (bounded)
Hard ban in baggage:
- Any PII (email, phone, name), auth tokens, session cookies, full URLs, payment data.
B. Size and lifetime limits
- Enforce max baggage size (e.g., <= 1–2 KB total; <= 10–20 entries).
- Disallow unbounded values (no UUID-per-request keys unless explicitly required).
- Prefer span attributes for rich context; baggage should be “routing hints,” not data.
C. Use baggage to control sampling and routing (carefully)
Good use: request.class=batch can drive sampling policies.
Bad use: user.id=... (privacy + cardinality).
3) Sampling gates (where to decide, and how)
Sampling is best treated as multiple gates:
Gate 0 — “Never drop” instrumentation rules (in SDK)
Always record spans for:
- Errors/exceptions
- Very slow requests (SDK supports span processors; otherwise rely on tail sampling)
Gate 1 — Head sampling (SDK / ingress)
Purpose: reduce volume early and cheaply.
- Use parent-based + traceidratio by default.
- Optionally increase rate for critical entrypoints.
Pros: cheapest.
Cons: can miss rare bad traces unless complemented with tail sampling.
Gate 2 — Tail sampling (Collector gateway)
Purpose: keep “interesting” traces (errors, latency, specific routes/tenants).
Typical policies:
- Keep if
status_code=ERROR
- Keep if duration > threshold (e.g., p95 target)
- Keep if route/operation is critical
- Keep if
deployment.environment=prod and request.class=interactive
- Rate-limit noisy endpoints
Tail sampling requirements:
- Collector needs enough memory and time window (e.g., 10–30s decision wait).
- Ensure consistent
trace_id propagation across services.
Gate 3 — Storage/export sampling
If a backend is expensive, optionally downsample again after you’ve computed RED metrics / exemplars.
Practical default policy set
- Head: 5–20% parent-based ratio in prod; 100% in dev.
- Tail:
- Keep 100% errors
- Keep 100% latency > N ms (per service/endpoint)
- Keep 1–5% baseline otherwise
- Ensure sampling consistency for service maps: don’t sample so hard that edges disappear; prefer keeping server spans at a higher rate than internal spans if you must.
4) Service maps (how to make them accurate)
Service maps are derived from trace edges. They break when identity or peer attributes are inconsistent.
A. Required fields for edges
For HTTP/gRPC client spans:
span.kind=CLIENT
- Destination attributes:
- Prefer
server.address + server.port (or net.peer.name legacy)
- And/or
peer.service (when you know logical service name)
For server spans:
span.kind=SERVER
service.name must be correct and stable.
B. Normalize “peer service” naming
If you use peer.service, enforce:
- it matches the callee’s
service.name (or a controlled alias table)
- no hostnames, no per-pod names
C. Kubernetes and mesh considerations
- If you have a service mesh, avoid double-instrumentation confusion:
- Decide whether edges come from app spans, mesh spans, or both.
- If both, use span naming conventions and filters to prevent duplicate edges.
D. Span naming conventions (helps topology + UX)
- HTTP server span name:
HTTP {method} {route} (bounded route templates, not raw URLs)
- RPC span name:
{service}/{method}
- Messaging:
{destination} {operation}
Collector configuration sketch (gateway)
Illustrative only—adapt to your backend(s):
receivers:
otlp:
protocols:
grpc: {}
http: {}
processors:
memory_limiter:
check_interval: 1s
limit_percentage: 80
spike_limit_percentage: 25
resource:
attributes:
- key: deployment.environment
from_attribute: env
action: upsert
resourcedetection:
detectors: [env, system, k8snode, k8scluster, gce, ec2]
timeout: 2s
override: false
attributes/baggage_allowlist:
actions:
- key: baggage.user.id
action: delete
- key: baggage.email
action: delete
transform/schema:
error_mode: ignore
trace_statements:
- context: span
statements:
# Example: map legacy http.method -> http.request.method
- set(attributes["http.request.method"], attributes["http.method"]) where attributes["http.request.method"] == nil and attributes["http.method"] != nil
- delete_key(attributes, "http.method")
tail_sampling:
decision_wait: 20s
num_traces: 50000
policies:
- name: keep-errors
type: status_code
status_code: { status_codes: [ERROR] }
- name: keep-slow
type: latency
latency: { threshold_ms: 1500 }
- name: baseline
type: probabilistic
probabilistic: { sampling_percentage: 5 }
exporters:
otlp/traces:
endpoint: tempo:4317
tls: { insecure: true }
prometheusremotewrite:
endpoint: http://mimir:9009/api/v1/push
otlphttp/logs:
endpoint: http://loki:3100/otlp
service:
pipelines:
traces:
receivers: [otlp]
processors: [memory_limiter, resourcedetection, resource, attributes/baggage_allowlist, transform/schema, tail_sampling]
exporters: [otlp/traces]
metrics:
receivers: [otlp]
processors: [memory_limiter, resourcedetection, resource, transform/schema]
exporters: [prometheusremotewrite]
logs:
receivers: [otlp]
processors: [memory_limiter, resourcedetection, resource, transform/schema]
exporters: [otlphttp/logs]
Operational checklist (what makes this “OpenTelemetry-first”)
- Schema contract: published attribute dictionary + naming rules + banned keys + cardinality guidance.
- Collector-as-control-plane: all normalization, enrichment, PII controls, and sampling policies live centrally.
- Sampling is explicit: documented gates, default rates, and “keep” policies for errors/latency.
- Baggage is governed: allowlist + size limits + privacy policy.
- Service map readiness: consistent
service.name, correct span kinds, stable route templates, normalized peer attributes.
- Backends are replaceable: OTLP in, vendor out.
If you tell me your environment (Kubernetes vs VMs, languages, and chosen backends), I can produce:
- a concrete schema contract (attribute allow/deny lists),
- a tailored tail-sampling policy set (per endpoint/service),
- and an end-to-end collector deployment layout (agent + gateway) with sizing guidance.