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

Add meter to observers #1425

Merged
merged 8 commits into from
Nov 26, 2020
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
3 changes: 3 additions & 0 deletions exporter/opentelemetry-exporter-otlp/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- Add meter reference to observers
([#1425](https://github.com/open-telemetry/opentelemetry-python/pull/1425))

## Version 0.16b0

Released 2020-11-25
Expand Down
249 changes: 223 additions & 26 deletions exporter/opentelemetry-exporter-otlp/tests/test_otlp_metric_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import os
from collections import OrderedDict
from unittest import TestCase
from unittest.mock import patch
from unittest.mock import Mock, patch

from grpc import ChannelCredentials

Expand All @@ -41,7 +41,13 @@
from opentelemetry.proto.resource.v1.resource_pb2 import (
Resource as OTLPResource,
)
from opentelemetry.sdk.metrics import Counter, MeterProvider
from opentelemetry.sdk.metrics import (
Counter,
MeterProvider,
SumObserver,
UpDownCounter,
UpDownSumObserver,
)
from opentelemetry.sdk.metrics.export import ExportRecord
from opentelemetry.sdk.metrics.export.aggregate import SumAggregator
from opentelemetry.sdk.resources import Resource as SDKResource
Expand All @@ -50,26 +56,12 @@


class TestOTLPMetricExporter(TestCase):
@patch("opentelemetry.sdk.metrics.export.aggregate.time_ns")
def setUp(self, mock_time_ns): # pylint: disable=arguments-differ
mock_time_ns.configure_mock(**{"return_value": 1})
def setUp(self): # pylint: disable=arguments-differ
self.exporter = OTLPMetricsExporter(insecure=True)
resource = SDKResource(OrderedDict([("a", 1), ("b", False)]))

self.counter_export_record = ExportRecord(
Counter(
"c",
"d",
"e",
int,
MeterProvider(resource=resource,).get_meter("name", "version"),
("f",),
),
[("g", "h")],
SumAggregator(),
resource,
self.resource = SDKResource(OrderedDict([("a", 1), ("b", False)]))
self.meter = MeterProvider(resource=self.resource,).get_meter(
"name", "version"
)

Configuration._reset() # pylint: disable=protected-access

def tearDown(self):
Expand Down Expand Up @@ -102,12 +94,20 @@ def test_no_credentials_error(self):
with self.assertRaises(ValueError):
OTLPMetricsExporter()

def test_translate_metrics(self):
# pylint: disable=no-member
@patch("opentelemetry.sdk.metrics.export.aggregate.time_ns")
def test_translate_counter_export_record(self, mock_time_ns):
mock_time_ns.configure_mock(**{"return_value": 1})

counter_export_record = ExportRecord(
Counter("c", "d", "e", int, self.meter, ("f",),),
[("g", "h")],
SumAggregator(),
self.resource,
)

self.counter_export_record.aggregator.checkpoint = 1
self.counter_export_record.aggregator.initial_checkpoint_timestamp = 1
self.counter_export_record.aggregator.last_update_timestamp = 1
counter_export_record.aggregator.checkpoint = 1
counter_export_record.aggregator.initial_checkpoint_timestamp = 1
counter_export_record.aggregator.last_update_timestamp = 1

expected = ExportMetricsServiceRequest(
resource_metrics=[
Expand Down Expand Up @@ -157,6 +157,203 @@ def test_translate_metrics(self):
)

# pylint: disable=protected-access
actual = self.exporter._translate_data([self.counter_export_record])
actual = self.exporter._translate_data([counter_export_record])

self.assertEqual(expected, actual)

@patch("opentelemetry.sdk.metrics.export.aggregate.time_ns")
def test_translate_sum_observer_export_record(self, mock_time_ns):
mock_time_ns.configure_mock(**{"return_value": 1})
counter_export_record = ExportRecord(
SumObserver(Mock(), "c", "d", "e", int, self.meter, ("f",),),
[("g", "h")],
SumAggregator(),
self.resource,
)

counter_export_record.aggregator.checkpoint = 1
counter_export_record.aggregator.initial_checkpoint_timestamp = 1
counter_export_record.aggregator.last_update_timestamp = 1

expected = ExportMetricsServiceRequest(
resource_metrics=[
ResourceMetrics(
resource=OTLPResource(
attributes=[
KeyValue(key="a", value=AnyValue(int_value=1)),
KeyValue(
key="b", value=AnyValue(bool_value=False)
),
]
),
instrumentation_library_metrics=[
InstrumentationLibraryMetrics(
instrumentation_library=InstrumentationLibrary(
name="name", version="version",
),
metrics=[
OTLPMetric(
name="c",
description="d",
unit="e",
int_sum=IntSum(
data_points=[
IntDataPoint(
labels=[
StringKeyValue(
key="g", value="h"
)
],
value=1,
time_unix_nano=1,
start_time_unix_nano=1,
)
],
aggregation_temporality=(
AggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE
),
is_monotonic=True,
),
)
],
)
],
)
]
)

# pylint: disable=protected-access
actual = self.exporter._translate_data([counter_export_record])

self.assertEqual(expected, actual)

@patch("opentelemetry.sdk.metrics.export.aggregate.time_ns")
def test_translate_updowncounter_export_record(self, mock_time_ns):
mock_time_ns.configure_mock(**{"return_value": 1})

counter_export_record = ExportRecord(
UpDownCounter("c", "d", "e", int, self.meter),
[("g", "h")],
SumAggregator(),
self.resource,
)

counter_export_record.aggregator.checkpoint = 1
counter_export_record.aggregator.initial_checkpoint_timestamp = 1
counter_export_record.aggregator.last_update_timestamp = 1

expected = ExportMetricsServiceRequest(
resource_metrics=[
ResourceMetrics(
resource=OTLPResource(
attributes=[
KeyValue(key="a", value=AnyValue(int_value=1)),
KeyValue(
key="b", value=AnyValue(bool_value=False)
),
]
),
instrumentation_library_metrics=[
InstrumentationLibraryMetrics(
instrumentation_library=InstrumentationLibrary(
name="name", version="version",
),
metrics=[
OTLPMetric(
name="c",
description="d",
unit="e",
int_sum=IntSum(
data_points=[
IntDataPoint(
labels=[
StringKeyValue(
key="g", value="h"
)
],
value=1,
time_unix_nano=1,
start_time_unix_nano=1,
)
],
aggregation_temporality=(
AggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE
),
),
)
],
)
],
)
]
)

# pylint: disable=protected-access
actual = self.exporter._translate_data([counter_export_record])

self.assertEqual(expected, actual)

@patch("opentelemetry.sdk.metrics.export.aggregate.time_ns")
def test_translate_updownsum_observer_export_record(self, mock_time_ns):
mock_time_ns.configure_mock(**{"return_value": 1})
counter_export_record = ExportRecord(
UpDownSumObserver(Mock(), "c", "d", "e", int, self.meter, ("f",),),
[("g", "h")],
SumAggregator(),
self.resource,
)

counter_export_record.aggregator.checkpoint = 1
counter_export_record.aggregator.initial_checkpoint_timestamp = 1
counter_export_record.aggregator.last_update_timestamp = 1

expected = ExportMetricsServiceRequest(
resource_metrics=[
ResourceMetrics(
resource=OTLPResource(
attributes=[
KeyValue(key="a", value=AnyValue(int_value=1)),
KeyValue(
key="b", value=AnyValue(bool_value=False)
),
]
),
instrumentation_library_metrics=[
InstrumentationLibraryMetrics(
instrumentation_library=InstrumentationLibrary(
name="name", version="version",
),
metrics=[
OTLPMetric(
name="c",
description="d",
unit="e",
int_sum=IntSum(
data_points=[
IntDataPoint(
labels=[
StringKeyValue(
key="g", value="h"
)
],
value=1,
time_unix_nano=1,
start_time_unix_nano=1,
)
],
aggregation_temporality=(
AggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE
),
),
)
],
)
],
)
]
)

# pylint: disable=protected-access
actual = self.exporter._translate_data([counter_export_record])

self.assertEqual(expected, actual)
3 changes: 3 additions & 0 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- Add meter reference to observers
([#1425](https://github.com/open-telemetry/opentelemetry-python/pull/1425))

## Version 0.16b0

Released 2020-11-25
Expand Down
29 changes: 26 additions & 3 deletions opentelemetry-sdk/src/opentelemetry/sdk/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def __init__(
description: str,
unit: str,
value_type: Type[metrics_api.ValueT],
meter: "Accumulator",
ocelotl marked this conversation as resolved.
Show resolved Hide resolved
label_keys: Sequence[str] = (),
enabled: bool = True,
):
Expand All @@ -249,6 +250,7 @@ def __init__(
self.description = description
self.unit = unit
self.value_type = value_type
self.meter = meter
self.label_keys = label_keys
self.enabled = enabled

Expand Down Expand Up @@ -477,7 +479,14 @@ def register_sumobserver(
enabled: bool = True,
) -> metrics_api.SumObserver:
ob = SumObserver(
callback, name, description, unit, value_type, label_keys, enabled
callback,
name,
description,
unit,
value_type,
self,
label_keys,
enabled,
)
with self.observers_lock:
self.observers.add(ob)
Expand All @@ -494,7 +503,14 @@ def register_updownsumobserver(
enabled: bool = True,
) -> metrics_api.UpDownSumObserver:
ob = UpDownSumObserver(
callback, name, description, unit, value_type, label_keys, enabled
callback,
name,
description,
unit,
value_type,
self,
label_keys,
enabled,
)
with self.observers_lock:
self.observers.add(ob)
Expand All @@ -511,7 +527,14 @@ def register_valueobserver(
enabled: bool = True,
) -> metrics_api.ValueObserver:
ob = ValueObserver(
callback, name, description, unit, value_type, label_keys, enabled
callback,
name,
description,
unit,
value_type,
self,
label_keys,
enabled,
)
with self.observers_lock:
self.observers.add(ob)
Expand Down
Loading