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

Draft fix of prometheus inconsistent label bug #3416

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def _receive_metrics(
timeout_millis: float = 10_000,
**kwargs,
) -> None:
print(metrics_data.to_json())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgotten print statement, shouldn't be in production code

if metrics_data is None:
return
self._collector.add_metrics_data(metrics_data)
Expand Down Expand Up @@ -217,6 +218,7 @@ def _translate_to_prometheus(
metrics.append(metric)

for metric in metrics:
label_keyss = []
Copy link
Contributor

@nstawski nstawski Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend using a more descriptive name such as labels_keys_list and label_values_list

label_valuess = []
values = []

Expand Down Expand Up @@ -246,6 +248,7 @@ def _translate_to_prometheus(
)
)

label_keyss.append(label_keys)
label_valuess.append(label_values)
if isinstance(number_data_point, HistogramDataPoint):
values.append(
Expand All @@ -260,8 +263,8 @@ def _translate_to_prometheus(
else:
values.append(number_data_point.value)

for pre_metric_family_id, label_values, value in zip(
pre_metric_family_ids, label_valuess, values
for pre_metric_family_id, label_keys, label_values, value in zip(
pre_metric_family_ids, label_keyss, label_valuess, values
):
is_non_monotonic_sum = (
isinstance(metric.data, Sum)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
HistogramDataPoint,
Metric,
MetricsData,
NumberDataPoint,
ResourceMetrics,
ScopeMetrics,
Sum,
)
from opentelemetry.sdk.resources import Resource
from opentelemetry.test.metrictestutil import (
Expand Down Expand Up @@ -321,7 +323,6 @@ def test_list_labels(self):
self.assertEqual(prometheus_metric.samples[0].labels["os"], "Unix")

def test_check_value(self):

collector = _CustomCollector()

self.assertEqual(collector._check_value(1), "1")
Expand All @@ -335,7 +336,6 @@ def test_check_value(self):
self.assertEqual(collector._check_value(None), "null")

def test_multiple_collection_calls(self):

metric_reader = PrometheusMetricReader()
provider = MeterProvider(metric_readers=[metric_reader])
meter = provider.get_meter("getting-started", "0.1.2")
Expand Down Expand Up @@ -389,3 +389,64 @@ def test_target_info_disabled(self):
)
self.assertNotIn("os", prometheus_metric.samples[0].labels)
self.assertNotIn("histo", prometheus_metric.samples[0].labels)

def test_metric_with_inconsistent_label_keys(self):
# Metric has two data points with different label keys
metric = Metric(
name="inconsistent_labels",
description="This metric has two different label key sets",
unit="s",
data=Sum(
data_points=[
NumberDataPoint(
attributes={"first_attr": "abc"},
start_time_unix_nano=1641946015139533244,
time_unix_nano=1641946016139533244,
value=1,
),
NumberDataPoint(
attributes={"different": "abc", "attributes": "123"},
start_time_unix_nano=1641946015139533244,
time_unix_nano=1641946016139533244,
value=2,
),
],
aggregation_temporality=AggregationTemporality.CUMULATIVE,
is_monotonic=True,
),
)

metrics_data = MetricsData(
resource_metrics=[
ResourceMetrics(
resource=Mock(),
scope_metrics=[
ScopeMetrics(
scope=Mock(),
metrics=[metric],
schema_url="schema_url",
)
],
schema_url="schema_url",
)
]
)

collector = _CustomCollector(disable_target_info=True)
collector.add_metrics_data(metrics_data)
result_bytes = generate_latest(collector)
result = result_bytes.decode("utf-8")
print("\n", result, "\n")
self.assertEqual(
result,
dedent(
"""\
# HELP inconsistent_labels_s_total This metric has two different label key sets
# TYPE inconsistent_labels_s_total counter
inconsistent_labels_s_total{first_attr="abc"} 1.0
# HELP inconsistent_labels_s_total This metric has two different label key sets
# TYPE inconsistent_labels_s_total counter
inconsistent_labels_s_total{attributes="123",different="abc"} 2.0
"""
),
)