Skip to content

Commit 1ddfaa0

Browse files
authored
Merge branch 'main' into 2024/add/tokenTyping
2 parents 1c08e90 + 9a2e59d commit 1ddfaa0

File tree

8 files changed

+90
-26
lines changed

8 files changed

+90
-26
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
- Add type annotations to context's attach & detach
1111
([#4346](https://github.com/open-telemetry/opentelemetry-python/pull/4346))
12+
- prometheus-exporter: fix labels out of place for data points with different
13+
attribute sets
14+
([#4413](https://github.com/open-telemetry/opentelemetry-python/pull/4413))
15+
- Type indent parameter in to_json
16+
([#4402](https://github.com/open-telemetry/opentelemetry-python/pull/4402))
1217
- Tolerates exceptions when loading resource detectors via `OTEL_EXPERIMENTAL_RESOURCE_DETECTORS`
1318
([#4373](https://github.com/open-telemetry/opentelemetry-python/pull/4373))
1419
- opentelemetry-sdk: fix OTLP exporting of Histograms with explicit buckets advisory

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

Lines changed: 14 additions & 11 deletions
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

Lines changed: 56 additions & 0 deletions
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+
)

opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def __eq__(self, other: object) -> bool:
217217
return NotImplemented
218218
return self.__dict__ == other.__dict__
219219

220-
def to_json(self, indent=4) -> str:
220+
def to_json(self, indent: Optional[int] = 4) -> str:
221221
return json.dumps(
222222
{
223223
"body": self.body,

opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/point.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class NumberDataPoint:
3838
value: Union[int, float]
3939
exemplars: Sequence[Exemplar] = field(default_factory=list)
4040

41-
def to_json(self, indent=4) -> str:
41+
def to_json(self, indent: Optional[int] = 4) -> str:
4242
return dumps(asdict(self), indent=indent)
4343

4444

@@ -59,7 +59,7 @@ class HistogramDataPoint:
5959
max: float
6060
exemplars: Sequence[Exemplar] = field(default_factory=list)
6161

62-
def to_json(self, indent=4) -> str:
62+
def to_json(self, indent: Optional[int] = 4) -> str:
6363
return dumps(asdict(self), indent=indent)
6464

6565

@@ -90,7 +90,7 @@ class ExponentialHistogramDataPoint:
9090
max: float
9191
exemplars: Sequence[Exemplar] = field(default_factory=list)
9292

93-
def to_json(self, indent=4) -> str:
93+
def to_json(self, indent: Optional[int] = 4) -> str:
9494
return dumps(asdict(self), indent=indent)
9595

9696

@@ -105,7 +105,7 @@ class ExponentialHistogram:
105105
"opentelemetry.sdk.metrics.export.AggregationTemporality"
106106
)
107107

108-
def to_json(self, indent=4) -> str:
108+
def to_json(self, indent: Optional[int] = 4) -> str:
109109
return dumps(
110110
{
111111
"data_points": [
@@ -129,7 +129,7 @@ class Sum:
129129
)
130130
is_monotonic: bool
131131

132-
def to_json(self, indent=4) -> str:
132+
def to_json(self, indent: Optional[int] = 4) -> str:
133133
return dumps(
134134
{
135135
"data_points": [
@@ -151,7 +151,7 @@ class Gauge:
151151

152152
data_points: Sequence[NumberDataPoint]
153153

154-
def to_json(self, indent=4) -> str:
154+
def to_json(self, indent: Optional[int] = 4) -> str:
155155
return dumps(
156156
{
157157
"data_points": [
@@ -173,7 +173,7 @@ class Histogram:
173173
"opentelemetry.sdk.metrics.export.AggregationTemporality"
174174
)
175175

176-
def to_json(self, indent=4) -> str:
176+
def to_json(self, indent: Optional[int] = 4) -> str:
177177
return dumps(
178178
{
179179
"data_points": [
@@ -203,7 +203,7 @@ class Metric:
203203
unit: Optional[str]
204204
data: DataT
205205

206-
def to_json(self, indent=4) -> str:
206+
def to_json(self, indent: Optional[int] = 4) -> str:
207207
return dumps(
208208
{
209209
"name": self.name,
@@ -223,7 +223,7 @@ class ScopeMetrics:
223223
metrics: Sequence[Metric]
224224
schema_url: str
225225

226-
def to_json(self, indent=4) -> str:
226+
def to_json(self, indent: Optional[int] = 4) -> str:
227227
return dumps(
228228
{
229229
"scope": loads(self.scope.to_json(indent=indent)),
@@ -245,7 +245,7 @@ class ResourceMetrics:
245245
scope_metrics: Sequence[ScopeMetrics]
246246
schema_url: str
247247

248-
def to_json(self, indent=4) -> str:
248+
def to_json(self, indent: Optional[int] = 4) -> str:
249249
return dumps(
250250
{
251251
"resource": loads(self.resource.to_json(indent=indent)),
@@ -265,7 +265,7 @@ class MetricsData:
265265

266266
resource_metrics: Sequence[ResourceMetrics]
267267

268-
def to_json(self, indent=4) -> str:
268+
def to_json(self, indent: Optional[int] = 4) -> str:
269269
return dumps(
270270
{
271271
"resource_metrics": [

opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ def __hash__(self) -> int:
297297
f"{dumps(self._attributes.copy(), sort_keys=True)}|{self._schema_url}" # type: ignore
298298
)
299299

300-
def to_json(self, indent: int = 4) -> str:
300+
def to_json(self, indent: Optional[int] = 4) -> str:
301301
attributes: MutableMapping[str, AttributeValue] = dict(
302302
self._attributes
303303
)

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ def instrumentation_info(self) -> Optional[InstrumentationInfo]:
483483
def instrumentation_scope(self) -> Optional[InstrumentationScope]:
484484
return self._instrumentation_scope
485485

486-
def to_json(self, indent: int = 4):
486+
def to_json(self, indent: Optional[int] = 4):
487487
parent_id = None
488488
if self.parent is not None:
489489
parent_id = f"0x{trace_api.format_span_id(self.parent.span_id)}"

opentelemetry-sdk/src/opentelemetry/sdk/util/instrumentation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def name(self) -> str:
153153
def attributes(self) -> Attributes:
154154
return self._attributes
155155

156-
def to_json(self, indent=4) -> str:
156+
def to_json(self, indent: Optional[int] = 4) -> str:
157157
return dumps(
158158
{
159159
"name": self._name,

0 commit comments

Comments
 (0)