Skip to content

Commit

Permalink
[exporter/otlp-proto-grpc] add histogram (open-telemetry#2429)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
Alex Boten authored Feb 3, 2022
1 parent 57a3b17 commit cade607
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 13 deletions.
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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



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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,18 @@ def _translate_data(
pt.as_double = metric.point.value
pbmetric.gauge.data_points.append(pt)
elif isinstance(metric.point, Histogram):
# TODO: implement histogram
pbmetric.histogram = pb2.Histogram(
data_points=[],
pt = pb2.HistogramDataPoint(
attributes=self._translate_attributes(metric.attributes),
time_unix_nano=metric.point.time_unix_nano,
start_time_unix_nano=metric.point.start_time_unix_nano,
count=sum(metric.point.bucket_counts),
bucket_counts=metric.point.bucket_counts,
explicit_bounds=metric.point.explicit_bounds,
)
pbmetric.histogram.aggregation_temporality = (
metric.point.aggregation_temporality
)
pbmetric.histogram.data_points.append(pt)
elif isinstance(metric.point, Sum):
pt = pb2.NumberDataPoint(
attributes=self._translate_attributes(metric.attributes),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from opentelemetry.sdk._metrics.point import (
AggregationTemporality,
Gauge,
Histogram,
Metric,
Sum,
)
Expand Down Expand Up @@ -152,9 +153,20 @@ def setUp(self):

self.metrics = {
"sum_int": _generate_sum("sum_int", 33),
"sum_float": _generate_sum("sum_float", 2.98),
"sum_double": _generate_sum("sum_double", 2.98),
"gauge_int": _generate_gauge("gauge_int", 9000),
"gauge_float": _generate_gauge("gauge_float", 52.028),
"gauge_double": _generate_gauge("gauge_double", 52.028),
"histogram": _generate_metric(
"histogram",
Histogram(
time_unix_nano=1641946016139533244,
start_time_unix_nano=1641946016139533244,
bucket_counts=[1, 4],
sum=67,
explicit_bounds=[10.0, 20.0],
aggregation_temporality=AggregationTemporality.DELTA,
),
),
}

def tearDown(self):
Expand Down Expand Up @@ -367,7 +379,7 @@ def test_translate_sum_int(self):
actual = self.exporter._translate_data([self.metrics["sum_int"]])
self.assertEqual(expected, actual)

def test_translate_sum_float(self):
def test_translate_sum_double(self):
expected = ExportMetricsServiceRequest(
resource_metrics=[
pb2.ResourceMetrics(
Expand All @@ -386,7 +398,7 @@ def test_translate_sum_float(self):
),
metrics=[
pb2.Metric(
name="sum_float",
name="sum_double",
unit="s",
description="foo",
sum=pb2.Sum(
Expand Down Expand Up @@ -422,7 +434,7 @@ def test_translate_sum_float(self):
]
)
# pylint: disable=protected-access
actual = self.exporter._translate_data([self.metrics["sum_float"]])
actual = self.exporter._translate_data([self.metrics["sum_double"]])
self.assertEqual(expected, actual)

def test_translate_gauge_int(self):
Expand Down Expand Up @@ -480,7 +492,7 @@ def test_translate_gauge_int(self):
actual = self.exporter._translate_data([self.metrics["gauge_int"]])
self.assertEqual(expected, actual)

def test_translate_gauge_float(self):
def test_translate_gauge_double(self):
expected = ExportMetricsServiceRequest(
resource_metrics=[
pb2.ResourceMetrics(
Expand All @@ -499,7 +511,7 @@ def test_translate_gauge_float(self):
),
metrics=[
pb2.Metric(
name="gauge_float",
name="gauge_double",
unit="s",
description="foo",
gauge=pb2.Gauge(
Expand Down Expand Up @@ -532,5 +544,66 @@ def test_translate_gauge_float(self):
]
)
# pylint: disable=protected-access
actual = self.exporter._translate_data([self.metrics["gauge_float"]])
actual = self.exporter._translate_data([self.metrics["gauge_double"]])
self.assertEqual(expected, actual)

def test_translate_histogram(self):
expected = ExportMetricsServiceRequest(
resource_metrics=[
pb2.ResourceMetrics(
resource=OTLPResource(
attributes=[
KeyValue(key="a", value=AnyValue(int_value=1)),
KeyValue(
key="b", value=AnyValue(bool_value=False)
),
]
),
instrumentation_library_metrics=[
pb2.InstrumentationLibraryMetrics(
instrumentation_library=InstrumentationLibrary(
name="first_name", version="first_version"
),
metrics=[
pb2.Metric(
name="histogram",
unit="s",
description="foo",
histogram=pb2.Histogram(
data_points=[
pb2.HistogramDataPoint(
attributes=[
KeyValue(
key="a",
value=AnyValue(
int_value=1
),
),
KeyValue(
key="b",
value=AnyValue(
bool_value=True
),
),
],
start_time_unix_nano=1641946016139533244,
time_unix_nano=1641946016139533244,
count=5,
bucket_counts=[1, 4],
explicit_bounds=[10.0, 20.0],
exemplars=[],
flags=pb2.DataPointFlags.FLAG_NONE,
)
],
aggregation_temporality=AggregationTemporality.DELTA,
),
)
],
)
],
)
]
)
# pylint: disable=protected-access
actual = self.exporter._translate_data([self.metrics["histogram"]])
self.assertEqual(expected, actual)

0 comments on commit cade607

Please sign in to comment.