Oslo Metrics
raw JSON → 0.15.1 verified Mon Apr 27 auth: no python
Oslo Metrics is a library for collecting and reporting metrics in OpenStack services. It provides a pluggable backend system for metrics such as counters, gauges, and histograms. The current version is 0.15.1, with a relatively stable release cadence.
pip install oslo-metrics Common errors
error ImportError: No module named oslo.metrics ↓
cause Using the deprecated dotted package name.
fix
Use 'import oslo_metrics' instead of 'import oslo.metrics'.
error ConfigOpts instance has no attribute 'register_opts' ↓
cause Incorrect configuration setup; register_opts is a method of ConfigOpts but requires proper options.
fix
Ensure you have loaded the necessary options: from oslo_config import cfg; cfg.CONF.register_opts(...).
error ValueError: Metric '...' is already registered ↓
cause Attempting to register a metric with a name that already exists in the client.
fix
Use unique metric names or check if metric exists before registering: if not client.has_metric('...'): client.register_metric(...).
Warnings
breaking In version 0.12.0, the API for creating metrics changed. Older 'Counter', 'Gauge', 'Histogram' classes were replaced with new metric types. Old code using the old classes will break. ↓
fix Update imports and usage to use new metric classes (e.g., from oslo_metrics import Counter).
gotcha Metrics client is a singleton per process. Calling get_metrics_client multiple times returns the same instance, which can lead to unintended sharing of state. ↓
fix Use a single client instance, or pass conf consistently.
deprecated The 'oslo.metrics' namespace (with dot) is deprecated in favor of 'oslo_metrics' (with underscore). Using the old dotted package name will cause ImportError in future releases. ↓
fix Replace 'import oslo.metrics' with 'import oslo_metrics'.
Imports
- MetricsClient
from oslo_metrics import MetricsClient - get_metrics_client wrong
from oslo_metrics.client import get_metrics_clientcorrectfrom oslo_metrics import get_metrics_client - Counter
from oslo_metrics import Counter
Quickstart
from oslo_metrics import get_metrics_client, Counter
from oslo_config import cfg
# Configuration (example using oslo.config)
conf = cfg.ConfigOpts()
conf.register_opts(cfg.CONF.opts)
client = get_metrics_client(conf)
# Create a counter metric
counter = Counter('requests.total', 'Total number of requests')
client.register_metric(counter)
# Increment counter
counter.increment()
# Flush metrics to backend
client.flush()