API Reference

MetricFlow provides three core metric types and a set of global functions for initialization and lifecycle management.

Initialization

mf = metricflow.init(
    exporter="stdout",     # Exporter backend
    interval=10,           # Export interval in seconds
    prefix="myapp",        # Optional metric name prefix
)
Parameter Type Default Description
exporter str "stdout" Exporter backend name
interval int 10 Export interval in seconds
prefix str "" Prefix for all metric names
labels dict {} Default labels for all metrics

Counter

A monotonically increasing value. Use for counting events like requests, errors, or completions.

counter = mf.counter(
    "request_count",
    description="Total HTTP requests",
    labels=["method", "status"]
)
Method Description
counter.inc(**labels) Increment by 1
counter.add(value, **labels) Increment by given value
counter.get(**labels) Get current value
counter.inc(method="GET", status="200")
counter.add(5, method="POST", status="201")

Gauge

A value that can go up or down. Use for measurements like temperature, memory usage, or active connections.

gauge = mf.gauge(
    "active_connections",
    description="Currently active connections",
    labels=["pool"]
)
Method Description
gauge.set(value, **labels) Set to specific value
gauge.inc(**labels) Increment by 1
gauge.dec(**labels) Decrement by 1
gauge.get(**labels) Get current value

Histogram

Samples observations and counts them in configurable buckets. Use for measuring distributions like request latencies or response sizes.

hist = mf.histogram(
    "request_duration_seconds",
    description="Request latency distribution",
    labels=["endpoint"],
    buckets=[0.01, 0.05, 0.1, 0.5, 1.0, 5.0]
)
Method Description
hist.observe(value, **labels) Record an observation
hist.get_count(**labels) Get total observation count
hist.get_sum(**labels) Get sum of all observations

Global Functions

Function Description
metricflow.init(**kwargs) Initialize MetricFlow instance
mf.flush() Force export all pending metrics
mf.shutdown() Gracefully shutdown, flush remaining metrics
mf.get_metrics() Return all registered metrics as dict