Library

Volume 1 · CI/CD, DevOps & Observability

Notebook
Sign in

Section 33

OpenTelemetry (architecture, SDK, collector, exporters — code examples)

480 words · about 2 min

33.1 OTel Architecture

┌──────────────────────────────────────────────────────────┐
│                     Application                           │
│  ┌────────────────────────────────────────────────────┐  │
│  │  OTel SDK (Auto-instrumentation + Manual spans)    │  │
│  │  • TracerProvider    • MeterProvider               │  │
│  │  • LoggerProvider    • Propagators                 │  │
│  └────────────────────────┬───────────────────────────┘  │
└───────────────────────────┼──────────────────────────────┘
                            │ OTLP (OpenTelemetry Protocol)
                            ▼
              ┌──────────────────────────┐
              │    OTel Collector         │
              │  ┌────────┐ ┌─────────┐  │
              │  │Receivers│ │Processors│  │
              │  │(OTLP,   │ │(Batch,   │  │
              │  │ Kafka,  │ │ Filter,  │  │
              │  │ Prom)   │ │ Sample)  │  │
              │  └────────┘ └─────────┘  │
              │  ┌─────────────────────┐  │
              │  │     Exporters        │  │
              │  │(Jaeger, Prometheus,  │  │
              │  │ Loki, Azure, Datadog) │  │
              │  └─────────────────────┘  │
              └──────────────────────────┘

Benefits: vendor-neutral (swap backends without code changes), unified SDK across metrics/logs/traces, auto-instrumentation libraries for common frameworks, correlation of traces↔logs↔metrics via trace_id.

33.2 SDK Example — Manual Instrumentation (Python)

from opentelemetry import trace, metrics
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
span_processor = BatchSpanProcessor(OTLPSpanExporter(endpoint="otel-collector:4317"))
trace.get_tracer_provider().add_span_processor(span_processor)

meter = metrics.get_meter(__name__)
request_counter = meter.create_counter("http_requests_total")

@tracer.start_as_current_span("process_claim")
def process_claim(claim_id):
    span = trace.get_current_span()
    span.set_attribute("claim.id", claim_id)
    request_counter.add(1, {"endpoint": "/claims"})
    with tracer.start_as_current_span("db_lookup"):
        result = db.fetch(claim_id)
    return result

33.3 Collector Configuration

receivers:
  otlp:
    protocols:
      grpc: {}
      http: {}
  prometheus:
    config:
      scrape_configs:
        - job_name: 'app'
          scrape_interval: 15s

processors:
  batch: {}
  filter:
    error_mode: ignore
    traces:
      span:
        - 'attributes["http.route"] == "/health"'
  tail_sampling:
    policies:
      - name: errors
        type: status_code
        status_code: { status_codes: [ERROR] }
      - name: slow-traces
        type: latency
        latency: { threshold_ms: 1000 }
      - name: probabilistic
        type: probabilistic
        probabilistic: { sampling_percentage: 1 }

exporters:
  otlp/jaeger:
    endpoint: jaeger:4317
  prometheusremotewrite:
    endpoint: http://prometheus:9090/api/v1/write
  loki:
    endpoint: http://loki:3100/loki/api/v1/push

service:
  pipelines:
    traces:
      receivers: [otlp]
      processors: [filter, tail_sampling, batch]
      exporters: [otlp/jaeger]
    metrics:
      receivers: [otlp, prometheus]
      processors: [batch]
      exporters: [prometheusremotewrite]
    logs:
      receivers: [otlp]
      processors: [batch]
      exporters: [loki]

33.4 Q&A

Q: Why OpenTelemetry over vendor SDKs (Datadog agent, New Relic agent)? Vendor lock-in avoidance — a single instrumentation layer lets you switch backends (Jaeger→Tempo, or add Datadog) purely via Collector exporter config, no application code changes. It’s also the only standard with W3C Trace Context interoperability across heterogeneous stacks (mixed .NET/Python/Node microservices).

Q: How do you keep trace volume/cost under control at scale? Combine head-based sampling (cheap, decided at SDK) with tail-based sampling at the Collector (expensive but smart — guarantees you always keep error and slow traces even if head sampling would have dropped them).