Skip to content

OpenTelemetry Metric Instrument Not Recording Fix

DodaTech Updated 2026-06-24 3 min read

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

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. 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

### What's the difference between Counter and UpDownCounter?

Counter is monotonically increasing — it can only go up. Use it for total requests, errors, or bytes sent. UpDownCounter can increase or decrease — use it for queue sizes, active connections, or available memory.

How often are metrics exported?

The PeriodicExportingMetricReader exports at a configurable interval (default 60s, typically set to 5-30s for production). The export interval affects metric granularity in dashboards. Shorter intervals mean more data points but higher overhead.

Can I use both metrics and tracing together?

Yes, they are independent signals in OpenTelemetry. Use metrics for service-level indicators (request rate, error rate, latency) and tracing for individual request debugging. Both can be exported to the same collector via OTLP.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro