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

Prometheus Exporter string representation for target_info labels #3659

Merged
merged 5 commits into from
Feb 9, 2024
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 @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Prometheus Exporter string representation for target_info labels
([#3659](https://github.com/open-telemetry/opentelemetry-python/pull/3659))
- Logs: ObservedTimestamp field is missing in console exporter output
([#3564](https://github.com/open-telemetry/opentelemetry-python/pull/3564))
- Fix explicit bucket histogram aggregation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,8 @@ def _create_info_metric(
"""Create an Info Metric Family with list of attributes"""
# sanitize the attribute names according to Prometheus rule
attributes = {
self._sanitize(key): value for key, value in attributes.items()
self._sanitize(key): self._check_value(value)
for key, value in attributes.items()
}
info = InfoMetricFamily(name, description, labels=attributes)
info.add_metric(labels=list(attributes.keys()), value=attributes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,30 +351,33 @@ def test_target_info_enabled_by_default(self):
metric_reader = PrometheusMetricReader()
provider = MeterProvider(
metric_readers=[metric_reader],
resource=Resource({"os": "Unix", "histo": 1}),
resource=Resource({"os": "Unix", "version": "1.2.3"}),
)
meter = provider.get_meter("getting-started", "0.1.2")
counter = meter.create_counter("counter")
counter.add(1)
result = list(metric_reader._collector.collect())

for prometheus_metric in result[:0]:
self.assertEqual(type(prometheus_metric), InfoMetricFamily)
self.assertEqual(prometheus_metric.name, "target")
self.assertEqual(
prometheus_metric.documentation, "Target metadata"
)
self.assertTrue(len(prometheus_metric.samples) == 1)
self.assertEqual(prometheus_metric.samples[0].value, 1)
self.assertTrue(len(prometheus_metric.samples[0].labels) == 2)
self.assertEqual(prometheus_metric.samples[0].labels["os"], "Unix")
self.assertEqual(prometheus_metric.samples[0].labels["histo"], "1")
self.assertEqual(len(result), 2)

prometheus_metric = result[0]

self.assertEqual(type(prometheus_metric), InfoMetricFamily)
self.assertEqual(prometheus_metric.name, "target")
self.assertEqual(prometheus_metric.documentation, "Target metadata")
self.assertTrue(len(prometheus_metric.samples) == 1)
self.assertEqual(prometheus_metric.samples[0].value, 1)
self.assertTrue(len(prometheus_metric.samples[0].labels) == 2)
self.assertEqual(prometheus_metric.samples[0].labels["os"], "Unix")
self.assertEqual(
prometheus_metric.samples[0].labels["version"], "1.2.3"
)

def test_target_info_disabled(self):
metric_reader = PrometheusMetricReader(disable_target_info=True)
provider = MeterProvider(
metric_readers=[metric_reader],
resource=Resource({"os": "Unix", "histo": 1}),
resource=Resource({"os": "Unix", "version": "1.2.3"}),
)
meter = provider.get_meter("getting-started", "0.1.2")
counter = meter.create_counter("counter")
Expand All @@ -388,7 +391,7 @@ def test_target_info_disabled(self):
prometheus_metric.documentation, "Target metadata"
)
self.assertNotIn("os", prometheus_metric.samples[0].labels)
self.assertNotIn("histo", prometheus_metric.samples[0].labels)
self.assertNotIn("version", prometheus_metric.samples[0].labels)

def test_target_info_sanitize(self):
metric_reader = PrometheusMetricReader()
Expand All @@ -398,6 +401,8 @@ def test_target_info_sanitize(self):
{
"system.os": "Unix",
"system.name": "Prometheus Target Sanitize",
"histo": 1,
"ratio": 0.1,
}
),
)
Expand All @@ -411,7 +416,7 @@ def test_target_info_sanitize(self):
self.assertEqual(prometheus_metric.documentation, "Target metadata")
self.assertTrue(len(prometheus_metric.samples) == 1)
self.assertEqual(prometheus_metric.samples[0].value, 1)
self.assertTrue(len(prometheus_metric.samples[0].labels) == 2)
self.assertTrue(len(prometheus_metric.samples[0].labels) == 4)
self.assertTrue("system_os" in prometheus_metric.samples[0].labels)
self.assertEqual(
prometheus_metric.samples[0].labels["system_os"], "Unix"
Expand All @@ -421,3 +426,13 @@ def test_target_info_sanitize(self):
prometheus_metric.samples[0].labels["system_name"],
"Prometheus Target Sanitize",
)
self.assertTrue("histo" in prometheus_metric.samples[0].labels)
self.assertEqual(
prometheus_metric.samples[0].labels["histo"],
"1",
)
self.assertTrue("ratio" in prometheus_metric.samples[0].labels)
self.assertEqual(
prometheus_metric.samples[0].labels["ratio"],
"0.1",
)
Loading