Skip to content

Commit

Permalink
Add drop aggregation (#2544)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl authored Mar 24, 2022
1 parent 95aeecc commit b7f9157
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
15 changes: 15 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/_metrics/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ def collect(self) -> Optional[_PointVarT]:
pass


class _DropAggregation(_Aggregation):
def aggregate(self, measurement: Measurement) -> None:
pass

def collect(self) -> Optional[_PointVarT]:
pass


class _SumAggregation(_Aggregation[Sum]):
def __init__(
self,
Expand Down Expand Up @@ -378,3 +386,10 @@ def _create_aggregation(self, instrument: Instrument) -> _Aggregation:
class LastValueAggregation(_AggregationFactory):
def _create_aggregation(self, instrument: Instrument) -> _Aggregation:
return _LastValueAggregation()


class DropAggregation(_AggregationFactory):
"""Using this aggregation will make all measurements be ignored."""

def _create_aggregation(self, instrument: Instrument) -> _Aggregation:
return _DropAggregation()
24 changes: 24 additions & 0 deletions opentelemetry-sdk/tests/metrics/test_metric_reader_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@

from unittest.mock import Mock, patch

from opentelemetry.sdk._metrics.aggregation import DropAggregation
from opentelemetry.sdk._metrics.instrument import Counter
from opentelemetry.sdk._metrics.measurement import Measurement
from opentelemetry.sdk._metrics.metric_reader_storage import (
MetricReaderStorage,
)
from opentelemetry.sdk._metrics.point import AggregationTemporality
from opentelemetry.sdk._metrics.sdk_configuration import SdkConfiguration
from opentelemetry.sdk._metrics.view import View
from opentelemetry.test.concurrency_test import ConcurrencyTestBase, MockFunc


Expand Down Expand Up @@ -217,3 +220,24 @@ def test_default_view_disabled(self, MockViewInstrumentMatch: Mock):
MockViewInstrumentMatch.call_args_list.clear()
storage.consume_measurement(Measurement(1, instrument2))
self.assertEqual(len(MockViewInstrumentMatch.call_args_list), 0)

def test_drop_aggregation(self):

counter = Counter("name", Mock(), Mock())
metric_reader_storage = MetricReaderStorage(
SdkConfiguration(
resource=Mock(),
metric_readers=(),
views=(
View(
instrument_name="name", aggregation=DropAggregation()
),
),
enable_default_view=False,
)
)
metric_reader_storage.consume_measurement(Measurement(1, counter))

self.assertEqual(
[], metric_reader_storage.collect(AggregationTemporality.DELTA)
)
26 changes: 26 additions & 0 deletions opentelemetry-sdk/tests/metrics/test_view_instrument_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
from opentelemetry.sdk._metrics._view_instrument_match import (
_ViewInstrumentMatch,
)
from opentelemetry.sdk._metrics.aggregation import (
DropAggregation,
_DropAggregation,
)
from opentelemetry.sdk._metrics.measurement import Measurement
from opentelemetry.sdk._metrics.point import AggregationTemporality, Metric
from opentelemetry.sdk._metrics.sdk_configuration import SdkConfiguration
Expand Down Expand Up @@ -129,6 +133,28 @@ def test_consume_measurement(self):
{frozenset({}): self.mock_created_aggregation},
)

# Test that a drop aggregation is handled in the same way as any
# other aggregation.
drop_aggregation = DropAggregation()

view_instrument_match = _ViewInstrumentMatch(
view=View(
instrument_name="instrument1",
name="name",
aggregation=drop_aggregation,
attribute_keys={},
),
instrument=instrument1,
sdk_config=sdk_config,
)
view_instrument_match.consume_measurement(
Measurement(value=0, instrument=instrument1, attributes=None)
)
self.assertIsInstance(
view_instrument_match._attributes_aggregation[frozenset({})],
_DropAggregation,
)

def test_collect(self):
instrument1 = Mock(
name="instrument1", description="description", unit="unit"
Expand Down

0 comments on commit b7f9157

Please sign in to comment.