Lesson 10 / 25
Metric Instruments
Counters, histograms and gauges.
Choose the instrument by behaviour
A meter creates instruments: Counter (monotonic, such as requests processed), UpDownCounter (can decrease, such as items in a queue), Histogram (distributions such as latency) and Gauge (current values), plus asynchronous (observable) variants whose values are read by a callback at collection time (CPU usage, pool size). Measurements carry attributes, and the SDK aggregates them before export. Zero-code instrumentation already emits standard HTTP and runtime metrics.
Beyond traces
The same SDKs produce metrics and correlate logs with traces.
Creating metrics in Python
OpenTelemetry metrics API (a sketch).
from opentelemetry import metrics
meter = metrics.get_meter("orders.checkout")
orders = meter.create_counter("app.orders.placed", unit="{order}",
description="Orders placed")
latency = meter.create_histogram("app.checkout.duration", unit="s",
description="Checkout processing time")
def record(order, seconds):
orders.add(1, {"payment.method": order.payment_method})
latency.record(seconds, {"app.customer.tier": order.tier})Keep metric attributes bounded
Attributes multiply time series just as Prometheus labels do; never use IDs as metric attributes.
Quick check: Which instrument fits checkout latency?
- Histogram
- Counter
- UpDownCounter
- Resource
Answer
Histogram — Distributions need histograms.