Skip to content

Commit 9a2e59d

Browse files
authored
prometheus-exporter: fix labels out of place for data points with different attribute sets (#4413)
* show tests fail at CI without the change Signed-off-by: emdneto <[email protected]> * now it should work Signed-off-by: emdneto <[email protected]> * refactor variables Signed-off-by: emdneto <[email protected]> * add changelog Signed-off-by: emdneto <[email protected]> --------- Signed-off-by: emdneto <[email protected]>
1 parent c44df70 commit 9a2e59d

File tree

3 files changed

+73
-11
lines changed

3 files changed

+73
-11
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- prometheus-exporter: fix labels out of place for data points with different
11+
attribute sets
12+
([#4413](https://github.com/open-telemetry/opentelemetry-python/pull/4413))
1013
- Type indent parameter in to_json
1114
([#4402](https://github.com/open-telemetry/opentelemetry-python/pull/4402))
1215
- Tolerates exceptions when loading resource detectors via `OTEL_EXPERIMENTAL_RESOURCE_DETECTORS`

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

+14-11
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,14 @@ def _translate_to_prometheus(
224224
metrics.append(metric)
225225

226226
for metric in metrics:
227-
label_valuess = []
227+
label_values_data_points = []
228+
label_keys_data_points = []
228229
values = []
229230

230-
pre_metric_family_ids = []
231+
per_metric_family_ids = []
231232

232233
metric_name = sanitize_full_name(metric.name)
233-
234234
metric_description = metric.description or ""
235-
236235
metric_unit = map_unit(metric.unit)
237236

238237
for number_data_point in metric.data.data_points:
@@ -243,7 +242,7 @@ def _translate_to_prometheus(
243242
label_keys.append(sanitize_attribute(key))
244243
label_values.append(self._check_value(value))
245244

246-
pre_metric_family_ids.append(
245+
per_metric_family_ids.append(
247246
"|".join(
248247
[
249248
metric_name,
@@ -254,7 +253,8 @@ def _translate_to_prometheus(
254253
)
255254
)
256255

257-
label_valuess.append(label_values)
256+
label_values_data_points.append(label_values)
257+
label_keys_data_points.append(label_keys)
258258
if isinstance(number_data_point, HistogramDataPoint):
259259
values.append(
260260
{
@@ -268,8 +268,11 @@ def _translate_to_prometheus(
268268
else:
269269
values.append(number_data_point.value)
270270

271-
for pre_metric_family_id, label_values, value in zip(
272-
pre_metric_family_ids, label_valuess, values
271+
for per_metric_family_id, label_keys, label_values, value in zip(
272+
per_metric_family_ids,
273+
label_keys_data_points,
274+
label_values_data_points,
275+
values,
273276
):
274277
is_non_monotonic_sum = (
275278
isinstance(metric.data, Sum)
@@ -291,7 +294,7 @@ def _translate_to_prometheus(
291294
and not should_convert_sum_to_gauge
292295
):
293296
metric_family_id = "|".join(
294-
[pre_metric_family_id, CounterMetricFamily.__name__]
297+
[per_metric_family_id, CounterMetricFamily.__name__]
295298
)
296299

297300
if metric_family_id not in metric_family_id_metric_family:
@@ -311,7 +314,7 @@ def _translate_to_prometheus(
311314
or should_convert_sum_to_gauge
312315
):
313316
metric_family_id = "|".join(
314-
[pre_metric_family_id, GaugeMetricFamily.__name__]
317+
[per_metric_family_id, GaugeMetricFamily.__name__]
315318
)
316319

317320
if (
@@ -331,7 +334,7 @@ def _translate_to_prometheus(
331334
].add_metric(labels=label_values, value=value)
332335
elif isinstance(metric.data, Histogram):
333336
metric_family_id = "|".join(
334-
[pre_metric_family_id, HistogramMetricFamily.__name__]
337+
[per_metric_family_id, HistogramMetricFamily.__name__]
335338
)
336339

337340
if (

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

+56
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,59 @@ def test_semconv(self):
638638
"""
639639
),
640640
)
641+
642+
def test_multiple_data_points_with_different_label_sets(self):
643+
hist_point_1 = HistogramDataPoint(
644+
attributes={"http_target": "/foobar", "net_host_port": 8080},
645+
start_time_unix_nano=1641946016139533244,
646+
time_unix_nano=1641946016139533244,
647+
count=6,
648+
sum=579.0,
649+
bucket_counts=[1, 3, 2],
650+
explicit_bounds=[123.0, 456.0],
651+
min=1,
652+
max=457,
653+
)
654+
hist_point_2 = HistogramDataPoint(
655+
attributes={"net_host_port": 8080},
656+
start_time_unix_nano=1641946016139533245,
657+
time_unix_nano=1641946016139533245,
658+
count=7,
659+
sum=579.0,
660+
bucket_counts=[1, 3, 3],
661+
explicit_bounds=[123.0, 456.0],
662+
min=1,
663+
max=457,
664+
)
665+
666+
metric = Metric(
667+
name="http.server.request.duration",
668+
description="test multiple label sets",
669+
unit="s",
670+
data=Histogram(
671+
data_points=[hist_point_1, hist_point_2],
672+
aggregation_temporality=AggregationTemporality.CUMULATIVE,
673+
),
674+
)
675+
676+
self.verify_text_format(
677+
metric,
678+
dedent(
679+
"""\
680+
# HELP http_server_request_duration_seconds test multiple label sets
681+
# TYPE http_server_request_duration_seconds histogram
682+
http_server_request_duration_seconds_bucket{http_target="/foobar",le="123.0",net_host_port="8080"} 1.0
683+
http_server_request_duration_seconds_bucket{http_target="/foobar",le="456.0",net_host_port="8080"} 4.0
684+
http_server_request_duration_seconds_bucket{http_target="/foobar",le="+Inf",net_host_port="8080"} 6.0
685+
http_server_request_duration_seconds_count{http_target="/foobar",net_host_port="8080"} 6.0
686+
http_server_request_duration_seconds_sum{http_target="/foobar",net_host_port="8080"} 579.0
687+
# HELP http_server_request_duration_seconds test multiple label sets
688+
# TYPE http_server_request_duration_seconds histogram
689+
http_server_request_duration_seconds_bucket{le="123.0",net_host_port="8080"} 1.0
690+
http_server_request_duration_seconds_bucket{le="456.0",net_host_port="8080"} 4.0
691+
http_server_request_duration_seconds_bucket{le="+Inf",net_host_port="8080"} 7.0
692+
http_server_request_duration_seconds_count{net_host_port="8080"} 7.0
693+
http_server_request_duration_seconds_sum{net_host_port="8080"} 579.0
694+
"""
695+
),
696+
)

0 commit comments

Comments
 (0)