Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b51a6f8

Browse files
soundofspacelzchenocelotl
authored
Sort by label keys before generating labels key and value lists (open-telemetry#3698)
* sort by label keys * changelog * test * review * linting * Update contrib SHA --------- Co-authored-by: Leighton Chen <[email protected]> Co-authored-by: Diego Hurtado <[email protected]>
1 parent 73e207b commit b51a6f8

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ env:
1010
# Otherwise, set variable to the commit of your branch on
1111
# opentelemetry-python-contrib which is compatible with these Core repo
1212
# changes.
13-
CONTRIB_REPO_SHA: 3c2788469834aa4f5976e1644d757f43d60bc219
13+
CONTRIB_REPO_SHA: 9ce1c26d2732dfbdadbb492fc38c562dcd08ed2e
1414
# This is needed because we do not clone the core repo in contrib builds anymore.
1515
# When running contrib builds as part of core builds, we use actions/checkout@v2 which
1616
# does not set an environment variable (simply just runs tox), which is different when

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
([#3785](https://github.com/open-telemetry/opentelemetry-python/pull/3785))
2020
- Add capture the fully qualified type name for raised exceptions in spans
2121
([#3837](https://github.com/open-telemetry/opentelemetry-python/pull/3837))
22+
- Prometheus exporter sort label keys to prevent duplicate metrics when user input changes order
23+
([#3698](https://github.com/open-telemetry/opentelemetry-python/pull/3698))
2224

2325
## Version 1.24.0/0.45b0 (2024-03-28)
2426

exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def _translate_to_prometheus(
237237
label_keys = []
238238
label_values = []
239239

240-
for key, value in number_data_point.attributes.items():
240+
for key, value in sorted(number_data_point.attributes.items()):
241241
label_keys.append(self._sanitize(key))
242242
label_values.append(self._check_value(value))
243243

exporter/opentelemetry-exporter-prometheus/tests/test_prometheus_exporter.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,3 +436,24 @@ def test_target_info_sanitize(self):
436436
prometheus_metric.samples[0].labels["ratio"],
437437
"0.1",
438438
)
439+
440+
def test_label_order_does_not_matter(self):
441+
metric_reader = PrometheusMetricReader()
442+
provider = MeterProvider(metric_readers=[metric_reader])
443+
meter = provider.get_meter("getting-started", "0.1.2")
444+
counter = meter.create_counter("counter")
445+
446+
counter.add(1, {"cause": "cause1", "reason": "reason1"})
447+
counter.add(1, {"reason": "reason2", "cause": "cause2"})
448+
449+
prometheus_output = generate_latest().decode()
450+
451+
# All labels are mapped correctly
452+
self.assertIn('cause="cause1"', prometheus_output)
453+
self.assertIn('cause="cause2"', prometheus_output)
454+
self.assertIn('reason="reason1"', prometheus_output)
455+
self.assertIn('reason="reason2"', prometheus_output)
456+
457+
# Only one metric is generated
458+
metric_count = prometheus_output.count("# HELP counter_total")
459+
self.assertEqual(metric_count, 1)

0 commit comments

Comments
 (0)