RousselTMLEARNING

Article · OpenTelemetry: From Discovery to Expertise

Metrics in depth

1. The metrics model #

MeterProvider, meters, instruments.

From measurement to exported point #

Code records measurements on instruments created by a meter. The SDK aggregates them (sum, histogram, last value) per attribute combination, then a metric reader exports them periodically (60 s by default, OTEL_METRIC_EXPORT_INTERVAL).

from opentelemetry import metrics
meter = metrics.get_meter("shop.checkout", "1.4.0")
commandes = meter.create_counter("shop.orders", unit="{order}", description="Commandes validées")
duree = meter.create_histogram("http.server.request.duration", unit="s")
commandes.add(1, {"shop.payment.method": "card"})
duree.record(0.183, {"http.request.method": "POST", "http.route": "/orders", "http.response.status_code": 201})

Instruments #

Choosing an instrument
A wrong instrument choice gives wrong graphs: a queue is not a Counter.
InstrumentSynchronousAsynchronous (observable)Default aggregation
Counteradd() in the codeObservableCounter (read an existing total)monotonic sum
UpDownCounteradd(+1/-1)ObservableUpDownCounternon-monotonic sum
Histogramrecord()—histogram
Gaugeset()ObservableGauge (callback at each export)last value

2. Temporality and histograms #

Two notions that surprise people.

Cumulative or delta #

Temporality
Prometheus expects cumulative; some SaaS backends prefer delta.
export OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE=cumulative   # ou delta, lowmemory
Good to know: The Collector's deltatocumulative processor converts delta to cumulative before sending to Prometheus.

Histograms #

TypePrincipleAdvantage
Explicitfixed boundaries (by default 0, 5, 10, 25… 10000)compatible everywhere
Exponentialboundaries computed automatically from a scaleaccurate across the whole range without tuning; matches Prometheus native histograms
Common mistake: The default boundaries are « historical » milliseconds: for a duration in seconds (unit="s"), define suitable boundaries through a view (0.005, 0.01, 0.025… 10) or use the exponential histogram.

3. Views and cardinality #

Controlling what is produced.

Views #

from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.view import View, ExplicitBucketHistogramAggregation, DropAggregation
provider = MeterProvider(metric_readers=[reader], views=[
    View(instrument_name="http.server.request.duration",
         aggregation=ExplicitBucketHistogramAggregation([0.01, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5])),
    View(instrument_name="http.server.request.duration", attribute_keys={"http.request.method", "http.route", "http.response.status_code"}),
    View(instrument_name="db.client.connection.*", aggregation=DropAggregation()),   # supprimer des métriques inutiles
])

Cardinality #

Each distinct combination of attributes creates a series. 5 methods × 50 routes × 10 codes = 2,500 series per instance. A user.id attribute multiplies that number by the number of users: the backend saturates and the bill explodes.

Good practice: On metrics, only use bounded attributes (route, method, code, region). Identifiers go on spans and logs.

4. Exemplars and Prometheus #

Linking a metric to a trace.

Exemplars #

An exemplar is a specific measurement kept with its trace_id: on a p99 latency spike, you click the exemplar and open the exact trace of a slow request. The OTEL_METRICS_EXEMPLAR_FILTER=trace_based filter (default) only records measurements made within a sampled span.

To Prometheus #

OpenTelemetryPrometheus
shop.orders (Counter, unit {order})shop_orders_total
http.server.request.duration (Histogram, unit s)http_server_request_duration_seconds_bucket, _sum, _count
attribute http.routelabel http_route
resource service.namejob / instance labels and the target_info metric
# Prometheus 3 accepte l'OTLP nativement
prometheus --web.enable-otlp-receiver      # point d'entrée : /api/v1/otlp/v1/metrics
# ou dans le Collector : exportateur prometheusremotewrite, ou prometheus (scrape)

📄 OpenTelemetry metrics: instruments, temporality, histograms and exemplars →

My progress

  • ⏱ Estimated time : ≈ 6 min

Sign in to track your progress and take notes. Sign in

💬 Recent discussions about this course

Ask a question about this lesson →See all discussions about this course

Recommended Articles

OpenTelemetry traces in depth: spans, status, events, links and sampling

Master the trace signal: anatomy of a distributed trace, span and SpanKind, attributes, status an...

Correlating traces, metrics, logs and profiles with OpenTelemetry

Link the four signals to diagnose quickly: shared resource, trace_id in logs, exemplars on metric...

OpenTelemetry and Dynatrace: OTLP, collector and instrumentation

Integrate OpenTelemetry with Dynatrace: OTLP endpoint and tokens, Dynatrace OTel Collector, trace...

Capstone project: the four signals of the OpenTelemetry Demo in the Grafana stack

A capstone project spanning the observability courses: deploy the OpenTelemetry Demo, send metric...

OpenTelemetry (OTel) best practices: the guide to modern observability

Master OpenTelemetry with our expert advice: implementing the Collector, following semantic conve...

Tempo: generating metrics from traces (service graph, span metrics, exemplars)

Enable Tempo's metrics-generator: service graphs, RED span metrics, remote write to Prometheus or...

Metric types in Prometheus

Discover in detail the four metric types supported by Prometheus (Counter, Gauge, Histogram, Summ...

OTLP explained: understanding the OpenTelemetry protocol

The OTLP protocol explained simply. Understand the differences between gRPC and HTTP for exportin...

OpenTelemetry: automatic vs manual instrumentation

Understand the differences between automatic and manual instrumentation with OpenTelemetry, their...

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!