Skip to content

Commit

Permalink
exporter/prometheus_remote_write: sort labels before exporting (#2940)
Browse files Browse the repository at this point in the history
  • Loading branch information
xrmx authored Oct 30, 2024
1 parent a940fc4 commit 54c7ee8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2922](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2922))
- `opentelemetry-instrumentation-celery` Don't detach context without a None token
([#2927](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2927))
- `opentelemetry-exporter-prometheus-remote-write`: sort labels before exporting
([#2940](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2940))
- `opentelemetry-instrumentation-dbapi` sqlcommenter key values created from PostgreSQL, MySQL systems
([#2897](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2897))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import re
from collections import defaultdict
from itertools import chain
from typing import Dict, Sequence
from typing import Dict, Mapping, Sequence

import requests
import snappy
Expand Down Expand Up @@ -253,12 +253,14 @@ def _parse_metric(
return self._convert_to_timeseries(sample_sets, resource_labels)

def _convert_to_timeseries(
self, sample_sets: Sequence[tuple], resource_labels: Sequence
self, sample_sets: Mapping[tuple, Sequence], resource_labels: Sequence
) -> Sequence[TimeSeries]:
timeseries = []
for labels, samples in sample_sets.items():
ts = TimeSeries()
for label_name, label_value in chain(resource_labels, labels):
for label_name, label_value in sorted(
chain(resource_labels, labels)
):
# Previous implementation did not str() the names...
ts.labels.append(self._label(label_name, str(label_value)))
for value, timestamp in samples:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
PrometheusRemoteWriteMetricsExporter,
)
from opentelemetry.exporter.prometheus_remote_write.gen.types_pb2 import ( # pylint: disable=E0611
Label,
Sample,
TimeSeries,
)
from opentelemetry.sdk.metrics.export import (
Expand Down Expand Up @@ -155,6 +157,38 @@ def test_parse_metric(metric, prom_rw):
assert sample.value in values


def test_convert_to_timeseries(prom_rw):
resource_labels = (("service_name", "foo"), ("bool_value", True))
sample_sets = {
(("foo", "bar"), ("baz", 42), ("__name__", "test_histogram_tu")): [
(1, 1641946016139)
],
(("baz", "42"), ("foo", "bar")): [(4, 1641946016139)],
}
timeseries = prom_rw._convert_to_timeseries(sample_sets, resource_labels)
assert timeseries == [
TimeSeries(
labels=[
Label(name="__name__", value="test_histogram_tu"),
Label(name="baz", value="42"),
Label(name="bool_value", value="True"),
Label(name="foo", value="bar"),
Label(name="service_name", value="foo"),
],
samples=[Sample(value=1, timestamp=1641946016139)],
),
TimeSeries(
labels=[
Label(name="baz", value="42"),
Label(name="bool_value", value="True"),
Label(name="foo", value="bar"),
Label(name="service_name", value="foo"),
],
samples=[Sample(value=4, timestamp=1641946016139)],
),
]


class TestValidation(unittest.TestCase):
# Test cases to ensure exporter parameter validation works as intended
def test_valid_standard_param(self):
Expand Down

0 comments on commit 54c7ee8

Please sign in to comment.