Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Add `registry` keyword argument to `PrometheusMetricReader` to allow passing a custom Prometheus registry
([#5055](https://github.com/open-telemetry/opentelemetry-python/pull/5055))

- Enabled the flake8-tidy-import plugins rules for the ruff linter. These rules throw warnings for relative imports in the modules.
([#5019](https://github.com/open-telemetry/opentelemetry-python/pull/5019))
- `opentelemetry-sdk`: Fix `AttributeError` in `ExplicitBucketHistogramAggregation` when applied to non-Histogram instruments without explicit boundaries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ class PrometheusMetricReader(MetricReader):
"""Prometheus metric exporter for OpenTelemetry."""

def __init__(
self, disable_target_info: bool = False, prefix: str = ""
self,
disable_target_info: bool = False,
prefix: str = "",
registry=REGISTRY,
) -> None:
super().__init__(
preferred_temporality={
Expand All @@ -151,7 +154,8 @@ def __init__(
self._collector = _CustomCollector(
disable_target_info=disable_target_info, prefix=prefix
)
REGISTRY.register(self._collector)
self._registry = registry
self._registry.register(self._collector)
self._collector._callback = self.collect
self._prefix = prefix

Expand All @@ -166,7 +170,7 @@ def _receive_metrics(
self._collector.add_metrics_data(metrics_data)

def shutdown(self, timeout_millis: float = 30_000, **kwargs) -> None:
REGISTRY.unregister(self._collector)
self._registry.unregister(self._collector)


class _CustomCollector:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from unittest import TestCase
from unittest.mock import Mock, patch

from prometheus_client import generate_latest
from prometheus_client import CollectorRegistry, generate_latest
from prometheus_client.core import (
CounterMetricFamily,
GaugeMetricFamily,
Expand Down Expand Up @@ -55,6 +55,13 @@ def setUp(self):
side_effect=self._mock_registry_register,
)

def test_custom_registry(self):
custom_registry = CollectorRegistry()
reader = PrometheusMetricReader(registry=custom_registry)
# global REGISTRY should NOT be used
self._mock_registry_register.assert_not_called()
reader.shutdown()

def verify_text_format(
self, metric: Metric, expect_prometheus_text: str, prefix: str = ""
) -> None:
Expand Down