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

Validate add/record operations for instruments #2394

Merged
merged 2 commits into from
Jan 20, 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
19 changes: 16 additions & 3 deletions opentelemetry-sdk/src/opentelemetry/sdk/_metrics/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

# pylint: disable=too-many-ancestors

import logging
from typing import Dict, Generator, Iterable, Union

from opentelemetry._metrics.instrument import CallbackT
Expand All @@ -33,6 +34,8 @@
from opentelemetry.sdk._metrics.measurement_consumer import MeasurementConsumer
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo

_logger = logging.getLogger(__name__)


class _Synchronous:
def __init__(
Expand Down Expand Up @@ -87,8 +90,10 @@ def add(
self, amount: Union[int, float], attributes: Dict[str, str] = None
):
if amount < 0:
raise Exception("amount must be non negative")

_logger.warning(
"Add amount must be non-negative on Counter %s.", self.name
)
return
self._measurement_consumer.consume_measurement(
Measurement(amount, attributes)
)
Expand All @@ -112,7 +117,15 @@ class ObservableUpDownCounter(_Asynchronous, APIObservableUpDownCounter):


class Histogram(_Synchronous, APIHistogram):
def record(self, amount, attributes=None):
def record(
self, amount: Union[int, float], attributes: Dict[str, str] = None
):
if amount < 0:
_logger.warning(
"Record amount must be non-negative on Histogram %s.",
self.name,
)
return
self._measurement_consumer.consume_measurement(
Measurement(amount, attributes)
)
Expand Down
45 changes: 45 additions & 0 deletions opentelemetry-sdk/tests/metrics/test_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,43 @@
from unittest.mock import Mock

from opentelemetry.sdk._metrics.instrument import (
Counter,
Histogram,
ObservableCounter,
ObservableGauge,
ObservableUpDownCounter,
UpDownCounter,
)


class TestCounter(TestCase):
def test_add(self):
mc = Mock()
counter = Counter("name", Mock(), mc)
counter.add(1.0)
mc.consume_measurement.assert_called_once()

def test_add_non_monotonic(self):
mc = Mock()
counter = Counter("name", Mock(), mc)
counter.add(-1.0)
mc.consume_measurement.assert_not_called()


class TestUpDownCounter(TestCase):
def test_add(self):
mc = Mock()
counter = UpDownCounter("name", Mock(), mc)
counter.add(1.0)
mc.consume_measurement.assert_called_once()

def test_add_non_monotonic(self):
mc = Mock()
counter = UpDownCounter("name", Mock(), mc)
counter.add(-1.0)
mc.consume_measurement.assert_called_once()


class TestObservableGauge(TestCase):
def test_callable_callback(self):
def callback():
Expand Down Expand Up @@ -82,3 +113,17 @@ def callback():
)

self.assertEqual(observable_up_down_counter.callback(), [1, 2, 3])


class TestHistogram(TestCase):
def test_record(self):
mc = Mock()
hist = Histogram("name", Mock(), mc)
hist.record(1.0)
mc.consume_measurement.assert_called_once()

def test_record_non_monotonic(self):
mc = Mock()
hist = Histogram("name", Mock(), mc)
hist.record(-1.0)
mc.consume_measurement.assert_not_called()