OpenTelemetry Metric Instrument Not Recording Fix
In this tutorial, you'll learn about OpenTelemetry Metric Instrument Not Recording Fix. We cover key concepts, practical examples, and best practices.
Your OpenTelemetry metric instrument reports zero or no data — the counter never increments, the histogram has no values, or the gauge stays at default. The meter is not initialized, the instrument is not bound to the meter, or the exporter is not configured for metrics.
The Problem
from opentelemetry import metrics
# WRONG: no meter created
counter = metrics.create_counter("api.requests")
counter.add(1)
AttributeError: module 'opentelemetry.metrics' has no attribute 'create_counter'
Meters and instruments must be obtained through the metrics API, not called as module functions.
Step-by-Step Fix
1. Create a meter and instruments
from opentelemetry import metrics
meter = metrics.get_meter(__name__)
request_counter = meter.create_counter(
name="api.requests",
description="Total API requests",
unit="1",
)
def handle_request():
request_counter.add(1, {"endpoint": "/users"})
2. Use different instrument types
from opentelemetry import metrics
meter = metrics.get_meter(__name__)
# Counter — monotonically increasing sum
request_count = meter.create_counter(
name="requests.total",
description="Total number of requests",
unit="1",
)
# Histogram — distribution of values
request_duration = meter.create_histogram(
name="request.duration",
description="Request duration in milliseconds",
unit="ms",
)
# UpDownCounter — can increase or decrease
active_requests = meter.create_up_down_counter(
name="requests.active",
description="Number of active requests",
unit="1",
)
# Gauge — point-in-time value
memory_usage = meter.create_gauge(
name="memory.usage",
description="Current memory usage",
unit="bytes",
)
3. Record values with attributes
from opentelemetry import metrics
import time
meter = metrics.get_meter(__name__)
request_duration = meter.create_histogram("request.duration", unit="ms")
def process_request(endpoint):
start = time.time()
try:
# Process request
result = do_work()
status = "success"
except Exception:
status = "error"
raise
finally:
duration = (time.time() - start) * 1000
request_duration.record(
duration,
{
"endpoint": endpoint,
"status": status,
},
)
4. Configure OTLP metric export
from opentelemetry import metrics
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
exporter = OTLPMetricExporter(endpoint="http://localhost:4317", insecure=True)
reader = PeriodicExportingMetricReader(exporter, export_interval_millis=5000)
provider = MeterProvider(metric_readers=[reader])
metrics.set_meter_provider(provider)
meter = metrics.get_meter(__name__)
request_counter = meter.create_counter("api.requests")
def handle_request():
request_counter.add(1)
5. Export to Prometheus
from opentelemetry import metrics
from opentelemetry.exporter.prometheus import PrometheusMetricReader
from opentelemetry.sdk.metrics import MeterProvider
from prometheus_client import start_http_server
reader = PrometheusMetricReader()
provider = MeterProvider(metric_readers=[reader])
metrics.set_meter_provider(provider)
start_http_server(8000)
meter = metrics.get_meter(__name__)
request_counter = meter.create_counter("api_requests_total", unit="1")
def handle_request():
request_counter.add(1)
Expected output:
# HELP api_requests_total Total API requests
# TYPE api_requests_total counter
api_requests_total{endpoint="/users"} 42
Prevention Tips
- Create instruments once at module level, not per-request
- Use semantic convention names for metric instruments
- Add relevant attributes for filtering and grouping
- Configure metric readers for periodic export
- Verify metric export with a test script before production
Common Mistakes with metric instrument
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
These mistakes appear frequently in real-world OPENTELEMETRY code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro