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

Adding implementation for Gauge #2408

Merged
merged 3 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2370](https://github.com/open-telemetry/opentelemetry-python/pull/2370))
- [api] Rename `_DefaultMeter` and `_DefaultMeterProvider` to `NoOpMeter` and `NoOpMeterProvider`.
([#2383](https://github.com/open-telemetry/opentelemetry-python/pull/2383))
- [exporter/opentelemetry-exporter-otlp-proto-grpc] Add Gauge to OTLPMetricExporter
([#2408](https://github.com/open-telemetry/opentelemetry-python/pull/2408))

## [1.8.0-0.27b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.8.0-0.27b0) - 2021-12-17

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,15 @@ def _translate_data(
unit=metric.unit,
)
if isinstance(metric.point, Gauge):
# TODO: implement gauge
pbmetric.gauge = pb2.Gauge(
data_points=[],
pt = pb2.NumberDataPoint(
attributes=self._translate_attributes(metric.attributes),
time_unix_nano=metric.point.time_unix_nano,
)
if isinstance(metric.point.value, int):
pt.as_int = metric.point.value
else:
pt.as_double = metric.point.value
pbmetric.gauge.data_points.append(pt)
elif isinstance(metric.point, Histogram):
# TODO: implement histogram
pbmetric.histogram = pb2.Histogram(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,14 @@
InstrumentationLibrary,
KeyValue,
)
from opentelemetry.proto.metrics.v1.metrics_pb2 import (
InstrumentationLibraryMetrics,
)
from opentelemetry.proto.metrics.v1.metrics_pb2 import Metric as OTLPMetric
from opentelemetry.proto.metrics.v1.metrics_pb2 import (
NumberDataPoint as OTLPNumberDataPoint,
)
from opentelemetry.proto.metrics.v1.metrics_pb2 import ResourceMetrics
from opentelemetry.proto.metrics.v1.metrics_pb2 import Sum as OTLPSum
from opentelemetry.proto.metrics.v1 import metrics_pb2 as pb2
ocelotl marked this conversation as resolved.
Show resolved Hide resolved
from opentelemetry.proto.resource.v1.resource_pb2 import (
Resource as OTLPResource,
)
from opentelemetry.sdk._metrics.export import MetricExportResult
from opentelemetry.sdk._metrics.point import (
AggregationTemporality,
Gauge,
Metric,
Sum,
)
Expand Down Expand Up @@ -136,6 +129,16 @@ def _generate_sum(name, val) -> Sum:
)


def _generate_gauge(name, val) -> Gauge:
return _generate_metric(
name,
Gauge(
time_unix_nano=1641946016139533244,
value=val,
),
)


class TestOTLPMetricExporter(TestCase):
def setUp(self):

Expand All @@ -150,6 +153,8 @@ def setUp(self):
self.metrics = {
"sum_int": _generate_sum("sum_int", 33),
"sum_float": _generate_sum("sum_float", 2.98),
"gauge_int": _generate_gauge("gauge_int", 9000),
"gauge_float": _generate_gauge("gauge_float", 52.028),
}

def tearDown(self):
Expand Down Expand Up @@ -307,7 +312,7 @@ def test_failure(self):
def test_translate_sum_int(self):
expected = ExportMetricsServiceRequest(
resource_metrics=[
ResourceMetrics(
pb2.ResourceMetrics(
resource=OTLPResource(
attributes=[
KeyValue(key="a", value=AnyValue(int_value=1)),
Expand All @@ -317,18 +322,18 @@ def test_translate_sum_int(self):
]
),
instrumentation_library_metrics=[
InstrumentationLibraryMetrics(
pb2.InstrumentationLibraryMetrics(
instrumentation_library=InstrumentationLibrary(
name="first_name", version="first_version"
),
metrics=[
OTLPMetric(
pb2.Metric(
name="sum_int",
unit="s",
description="foo",
sum=OTLPSum(
sum=pb2.Sum(
data_points=[
OTLPNumberDataPoint(
pb2.NumberDataPoint(
attributes=[
KeyValue(
key="a",
Expand Down Expand Up @@ -365,7 +370,7 @@ def test_translate_sum_int(self):
def test_translate_sum_float(self):
expected = ExportMetricsServiceRequest(
resource_metrics=[
ResourceMetrics(
pb2.ResourceMetrics(
resource=OTLPResource(
attributes=[
KeyValue(key="a", value=AnyValue(int_value=1)),
Expand All @@ -375,18 +380,18 @@ def test_translate_sum_float(self):
]
),
instrumentation_library_metrics=[
InstrumentationLibraryMetrics(
pb2.InstrumentationLibraryMetrics(
instrumentation_library=InstrumentationLibrary(
name="first_name", version="first_version"
),
metrics=[
OTLPMetric(
pb2.Metric(
name="sum_float",
unit="s",
description="foo",
sum=OTLPSum(
sum=pb2.Sum(
data_points=[
OTLPNumberDataPoint(
pb2.NumberDataPoint(
attributes=[
KeyValue(
key="a",
Expand Down Expand Up @@ -419,3 +424,113 @@ def test_translate_sum_float(self):
# pylint: disable=protected-access
actual = self.exporter._translate_data([self.metrics["sum_float"]])
self.assertEqual(expected, actual)

def test_translate_gauge_int(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="gauge_int",
unit="s",
description="foo",
gauge=pb2.Gauge(
data_points=[
pb2.NumberDataPoint(
attributes=[
KeyValue(
key="a",
value=AnyValue(
int_value=1
),
),
KeyValue(
key="b",
value=AnyValue(
bool_value=True
),
),
],
time_unix_nano=1641946016139533244,
as_int=9000,
)
],
),
)
],
)
],
)
]
)
# pylint: disable=protected-access
actual = self.exporter._translate_data([self.metrics["gauge_int"]])
self.assertEqual(expected, actual)

def test_translate_gauge_float(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="gauge_float",
unit="s",
description="foo",
gauge=pb2.Gauge(
data_points=[
pb2.NumberDataPoint(
attributes=[
KeyValue(
key="a",
value=AnyValue(
int_value=1
),
),
KeyValue(
key="b",
value=AnyValue(
bool_value=True
),
),
],
time_unix_nano=1641946016139533244,
as_double=52.028,
)
],
),
)
],
)
],
)
]
)
# pylint: disable=protected-access
actual = self.exporter._translate_data([self.metrics["gauge_float"]])
self.assertEqual(expected, actual)