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 #
| Instrument | Synchronous | Asynchronous (observable) | Default aggregation |
|---|---|---|---|
| Counter | add() in the code | ObservableCounter (read an existing total) | monotonic sum |
| UpDownCounter | add(+1/-1) | ObservableUpDownCounter | non-monotonic sum |
| Histogram | record() | — | histogram |
| Gauge | set() | ObservableGauge (callback at each export) | last value |
2. Temporality and histograms #
Two notions that surprise people.
Cumulative or delta #
export OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE=cumulative # ou delta, lowmemoryGood to know: The Collector's deltatocumulative processor converts delta to cumulative before sending to Prometheus.Histograms #
| Type | Principle | Advantage |
|---|---|---|
| Explicit | fixed boundaries (by default 0, 5, 10, 25… 10000) | compatible everywhere |
| Exponential | boundaries computed automatically from a scale | accurate 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 #
| OpenTelemetry | Prometheus |
|---|---|
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.route | label http_route |
resource service.name | job / 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)My progress
Sign in to track your progress and take notes. Sign in
💬 Recent discussions about this course
- Quels attributs utiliser pour nommer mes spans HTTP (semconv) ?✓ solved · 1 reply(ies)
- OpenTelemetry Collector : mes traces arrivent mais pas mes métriques✓ solved · 2 reply(ies)
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...