Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove ability to set a global metric prefix for prometheus exporter #3137

Merged
merged 9 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
- Remove the ability to set a global metric prefix for Prometheus exporter
([#3137](https://github.com/open-telemetry/opentelemetry-python/pull/3137))
- Adds environment variables for log exporter
([#3037](https://github.com/open-telemetry/opentelemetry-python/pull/3037))
- Add attribute name to type warning message.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,9 @@ def _convert_buckets(


class PrometheusMetricReader(MetricReader):
"""Prometheus metric exporter for OpenTelemetry.
"""Prometheus metric exporter for OpenTelemetry."""

Args:
prefix: single-word application prefix relevant to the domain
the metric belongs to.
"""

def __init__(self, prefix: str = "") -> None:
def __init__(self) -> None:

super().__init__(
preferred_temporality={
Expand All @@ -133,7 +128,7 @@ def __init__(self, prefix: str = "") -> None:
ObservableGauge: AggregationTemporality.CUMULATIVE,
}
)
self._collector = _CustomCollector(prefix)
self._collector = _CustomCollector()
REGISTRY.register(self._collector)
self._collector._callback = self.collect

Expand All @@ -158,8 +153,7 @@ class _CustomCollector:
https://github.com/prometheus/client_python#custom-collectors
"""

def __init__(self, prefix: str = ""):
self._prefix = prefix
def __init__(self):
self._callback = None
self._metrics_datas = deque()
self._non_letters_digits_underscore_re = compile(
Expand Down Expand Up @@ -210,8 +204,6 @@ def _translate_to_prometheus(
pre_metric_family_ids = []

metric_name = ""
if self._prefix != "":
metric_name = self._prefix + "_"
metric_name += self._sanitize(metric.name)

metric_description = metric.description or ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ def setUp(self):
def test_constructor(self):
"""Test the constructor."""
with self._registry_register_patch:
exporter = PrometheusMetricReader(prefix="testprefix")
self.assertEqual(exporter._collector._prefix, "testprefix")
_ = PrometheusMetricReader()
self.assertTrue(self._mock_registry_register.called)

def test_shutdown(self):
Expand Down Expand Up @@ -102,21 +101,21 @@ def test_histogram_to_prometheus(self):
]
)

collector = _CustomCollector("testprefix")
collector = _CustomCollector()
collector.add_metrics_data(metrics_data)
result_bytes = generate_latest(collector)
result = result_bytes.decode("utf-8")
self.assertEqual(
result,
dedent(
"""\
# HELP testprefix_test_name_s foo
# TYPE testprefix_test_name_s histogram
testprefix_test_name_s_bucket{histo="1",le="123.0"} 1.0
testprefix_test_name_s_bucket{histo="1",le="456.0"} 4.0
testprefix_test_name_s_bucket{histo="1",le="+Inf"} 6.0
testprefix_test_name_s_count{histo="1"} 6.0
testprefix_test_name_s_sum{histo="1"} 579.0
# HELP test_name_s foo
# TYPE test_name_s histogram
test_name_s_bucket{histo="1",le="123.0"} 1.0
test_name_s_bucket{histo="1",le="456.0"} 4.0
test_name_s_bucket{histo="1",le="+Inf"} 6.0
test_name_s_count{histo="1"} 6.0
test_name_s_sum{histo="1"} 579.0
"""
),
)
Expand Down Expand Up @@ -147,14 +146,12 @@ def test_sum_to_prometheus(self):
]
)

collector = _CustomCollector("testprefix")
collector = _CustomCollector()
collector.add_metrics_data(metrics_data)

for prometheus_metric in collector.collect():
self.assertEqual(type(prometheus_metric), CounterMetricFamily)
self.assertEqual(
prometheus_metric.name, "testprefix_test_sum_testunit"
)
self.assertEqual(prometheus_metric.name, "test_sum_testunit")
self.assertEqual(prometheus_metric.documentation, "testdesc")
self.assertTrue(len(prometheus_metric.samples) == 1)
self.assertEqual(prometheus_metric.samples[0].value, 123)
Expand Down Expand Up @@ -192,14 +189,12 @@ def test_gauge_to_prometheus(self):
]
)

collector = _CustomCollector("testprefix")
collector = _CustomCollector()
collector.add_metrics_data(metrics_data)

for prometheus_metric in collector.collect():
self.assertEqual(type(prometheus_metric), GaugeMetricFamily)
self.assertEqual(
prometheus_metric.name, "testprefix_test_gauge_testunit"
)
self.assertEqual(prometheus_metric.name, "test_gauge_testunit")
self.assertEqual(prometheus_metric.documentation, "testdesc")
self.assertTrue(len(prometheus_metric.samples) == 1)
self.assertEqual(prometheus_metric.samples[0].value, 123)
Expand All @@ -217,13 +212,13 @@ def test_invalid_metric(self):
description="testdesc",
unit="testunit",
)
collector = _CustomCollector("testprefix")
collector = _CustomCollector()
collector.add_metrics_data([record])
collector.collect()
self.assertLogs("opentelemetry.exporter.prometheus", level="WARNING")

def test_sanitize(self):
collector = _CustomCollector("testprefix")
collector = _CustomCollector()
self.assertEqual(
collector._sanitize("1!2@3#4$5%6^7&8*9(0)_-"),
"1_2_3_4_5_6_7_8_9_0___",
Expand Down Expand Up @@ -256,14 +251,12 @@ def test_list_labels(self):
)
]
)
collector = _CustomCollector("testprefix")
collector = _CustomCollector()
collector.add_metrics_data(metrics_data)

for prometheus_metric in collector.collect():
self.assertEqual(type(prometheus_metric), GaugeMetricFamily)
self.assertEqual(
prometheus_metric.name, "testprefix_test_gauge_testunit"
)
self.assertEqual(prometheus_metric.name, "test_gauge_testunit")
self.assertEqual(prometheus_metric.documentation, "testdesc")
self.assertTrue(len(prometheus_metric.samples) == 1)
self.assertEqual(prometheus_metric.samples[0].value, 123)
Expand All @@ -276,7 +269,7 @@ def test_list_labels(self):

def test_check_value(self):

collector = _CustomCollector("")
collector = _CustomCollector()

self.assertEqual(collector._check_value(1), "1")
self.assertEqual(collector._check_value(1.0), "1.0")
Expand All @@ -290,7 +283,7 @@ def test_check_value(self):

def test_multiple_collection_calls(self):

metric_reader = PrometheusMetricReader(prefix="prefix")
metric_reader = PrometheusMetricReader()
provider = MeterProvider(metric_readers=[metric_reader])
meter = provider.get_meter("getting-started", "0.1.2")
counter = meter.create_counter("counter")
Expand Down