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

Skip to content

Commit cade607

Browse files
author
Alex Boten
authored
[exporter/otlp-proto-grpc] add histogram (open-telemetry#2429)
* [exporter/otlp-proto-grpc] add histogram Adding support for Histogram data point types. Note this doesn't support the sum of the histogram yet. * update changelog * update changelog * fix test
1 parent 57a3b17 commit cade607

File tree

3 files changed

+92
-13
lines changed

3 files changed

+92
-13
lines changed

CHANGELOG.md

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

1010
## [1.9.1-0.28b1](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.9.1-0.28b1) - 2022-01-29
1111

12-
13-
1412
- Update opentelemetry-proto to v0.12.0. Note that this update removes deprecated status codes.
1513
([#2415](https://github.com/open-telemetry/opentelemetry-python/pull/2415))
1614

exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/_metric_exporter/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,18 @@ def _translate_data(
132132
pt.as_double = metric.point.value
133133
pbmetric.gauge.data_points.append(pt)
134134
elif isinstance(metric.point, Histogram):
135-
# TODO: implement histogram
136-
pbmetric.histogram = pb2.Histogram(
137-
data_points=[],
135+
pt = pb2.HistogramDataPoint(
136+
attributes=self._translate_attributes(metric.attributes),
137+
time_unix_nano=metric.point.time_unix_nano,
138+
start_time_unix_nano=metric.point.start_time_unix_nano,
139+
count=sum(metric.point.bucket_counts),
140+
bucket_counts=metric.point.bucket_counts,
141+
explicit_bounds=metric.point.explicit_bounds,
142+
)
143+
pbmetric.histogram.aggregation_temporality = (
144+
metric.point.aggregation_temporality
138145
)
146+
pbmetric.histogram.data_points.append(pt)
139147
elif isinstance(metric.point, Sum):
140148
pt = pb2.NumberDataPoint(
141149
attributes=self._translate_attributes(metric.attributes),

exporter/opentelemetry-exporter-otlp-proto-grpc/tests/metrics/test_otlp_metrics_exporter.py

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
from opentelemetry.sdk._metrics.point import (
4747
AggregationTemporality,
4848
Gauge,
49+
Histogram,
4950
Metric,
5051
Sum,
5152
)
@@ -152,9 +153,20 @@ def setUp(self):
152153

153154
self.metrics = {
154155
"sum_int": _generate_sum("sum_int", 33),
155-
"sum_float": _generate_sum("sum_float", 2.98),
156+
"sum_double": _generate_sum("sum_double", 2.98),
156157
"gauge_int": _generate_gauge("gauge_int", 9000),
157-
"gauge_float": _generate_gauge("gauge_float", 52.028),
158+
"gauge_double": _generate_gauge("gauge_double", 52.028),
159+
"histogram": _generate_metric(
160+
"histogram",
161+
Histogram(
162+
time_unix_nano=1641946016139533244,
163+
start_time_unix_nano=1641946016139533244,
164+
bucket_counts=[1, 4],
165+
sum=67,
166+
explicit_bounds=[10.0, 20.0],
167+
aggregation_temporality=AggregationTemporality.DELTA,
168+
),
169+
),
158170
}
159171

160172
def tearDown(self):
@@ -367,7 +379,7 @@ def test_translate_sum_int(self):
367379
actual = self.exporter._translate_data([self.metrics["sum_int"]])
368380
self.assertEqual(expected, actual)
369381

370-
def test_translate_sum_float(self):
382+
def test_translate_sum_double(self):
371383
expected = ExportMetricsServiceRequest(
372384
resource_metrics=[
373385
pb2.ResourceMetrics(
@@ -386,7 +398,7 @@ def test_translate_sum_float(self):
386398
),
387399
metrics=[
388400
pb2.Metric(
389-
name="sum_float",
401+
name="sum_double",
390402
unit="s",
391403
description="foo",
392404
sum=pb2.Sum(
@@ -422,7 +434,7 @@ def test_translate_sum_float(self):
422434
]
423435
)
424436
# pylint: disable=protected-access
425-
actual = self.exporter._translate_data([self.metrics["sum_float"]])
437+
actual = self.exporter._translate_data([self.metrics["sum_double"]])
426438
self.assertEqual(expected, actual)
427439

428440
def test_translate_gauge_int(self):
@@ -480,7 +492,7 @@ def test_translate_gauge_int(self):
480492
actual = self.exporter._translate_data([self.metrics["gauge_int"]])
481493
self.assertEqual(expected, actual)
482494

483-
def test_translate_gauge_float(self):
495+
def test_translate_gauge_double(self):
484496
expected = ExportMetricsServiceRequest(
485497
resource_metrics=[
486498
pb2.ResourceMetrics(
@@ -499,7 +511,7 @@ def test_translate_gauge_float(self):
499511
),
500512
metrics=[
501513
pb2.Metric(
502-
name="gauge_float",
514+
name="gauge_double",
503515
unit="s",
504516
description="foo",
505517
gauge=pb2.Gauge(
@@ -532,5 +544,66 @@ def test_translate_gauge_float(self):
532544
]
533545
)
534546
# pylint: disable=protected-access
535-
actual = self.exporter._translate_data([self.metrics["gauge_float"]])
547+
actual = self.exporter._translate_data([self.metrics["gauge_double"]])
548+
self.assertEqual(expected, actual)
549+
550+
def test_translate_histogram(self):
551+
expected = ExportMetricsServiceRequest(
552+
resource_metrics=[
553+
pb2.ResourceMetrics(
554+
resource=OTLPResource(
555+
attributes=[
556+
KeyValue(key="a", value=AnyValue(int_value=1)),
557+
KeyValue(
558+
key="b", value=AnyValue(bool_value=False)
559+
),
560+
]
561+
),
562+
instrumentation_library_metrics=[
563+
pb2.InstrumentationLibraryMetrics(
564+
instrumentation_library=InstrumentationLibrary(
565+
name="first_name", version="first_version"
566+
),
567+
metrics=[
568+
pb2.Metric(
569+
name="histogram",
570+
unit="s",
571+
description="foo",
572+
histogram=pb2.Histogram(
573+
data_points=[
574+
pb2.HistogramDataPoint(
575+
attributes=[
576+
KeyValue(
577+
key="a",
578+
value=AnyValue(
579+
int_value=1
580+
),
581+
),
582+
KeyValue(
583+
key="b",
584+
value=AnyValue(
585+
bool_value=True
586+
),
587+
),
588+
],
589+
start_time_unix_nano=1641946016139533244,
590+
time_unix_nano=1641946016139533244,
591+
count=5,
592+
bucket_counts=[1, 4],
593+
explicit_bounds=[10.0, 20.0],
594+
exemplars=[],
595+
flags=pb2.DataPointFlags.FLAG_NONE,
596+
)
597+
],
598+
aggregation_temporality=AggregationTemporality.DELTA,
599+
),
600+
)
601+
],
602+
)
603+
],
604+
)
605+
]
606+
)
607+
# pylint: disable=protected-access
608+
actual = self.exporter._translate_data([self.metrics["histogram"]])
536609
self.assertEqual(expected, actual)

0 commit comments

Comments
 (0)