Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a5cec85

Browse files
author
Alex Boten
authored
Merge branch 'main' into codeboten/document-noop
2 parents 8b1f8b2 + b7f9157 commit a5cec85

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/_metrics/aggregation.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ def collect(self) -> Optional[_PointVarT]:
5454
pass
5555

5656

57+
class _DropAggregation(_Aggregation):
58+
def aggregate(self, measurement: Measurement) -> None:
59+
pass
60+
61+
def collect(self) -> Optional[_PointVarT]:
62+
pass
63+
64+
5765
class _SumAggregation(_Aggregation[Sum]):
5866
def __init__(
5967
self,
@@ -378,3 +386,10 @@ def _create_aggregation(self, instrument: Instrument) -> _Aggregation:
378386
class LastValueAggregation(_AggregationFactory):
379387
def _create_aggregation(self, instrument: Instrument) -> _Aggregation:
380388
return _LastValueAggregation()
389+
390+
391+
class DropAggregation(_AggregationFactory):
392+
"""Using this aggregation will make all measurements be ignored."""
393+
394+
def _create_aggregation(self, instrument: Instrument) -> _Aggregation:
395+
return _DropAggregation()

opentelemetry-sdk/tests/metrics/test_metric_reader_storage.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414

1515
from unittest.mock import Mock, patch
1616

17+
from opentelemetry.sdk._metrics.aggregation import DropAggregation
18+
from opentelemetry.sdk._metrics.instrument import Counter
1719
from opentelemetry.sdk._metrics.measurement import Measurement
1820
from opentelemetry.sdk._metrics.metric_reader_storage import (
1921
MetricReaderStorage,
2022
)
2123
from opentelemetry.sdk._metrics.point import AggregationTemporality
2224
from opentelemetry.sdk._metrics.sdk_configuration import SdkConfiguration
25+
from opentelemetry.sdk._metrics.view import View
2326
from opentelemetry.test.concurrency_test import ConcurrencyTestBase, MockFunc
2427

2528

@@ -217,3 +220,24 @@ def test_default_view_disabled(self, MockViewInstrumentMatch: Mock):
217220
MockViewInstrumentMatch.call_args_list.clear()
218221
storage.consume_measurement(Measurement(1, instrument2))
219222
self.assertEqual(len(MockViewInstrumentMatch.call_args_list), 0)
223+
224+
def test_drop_aggregation(self):
225+
226+
counter = Counter("name", Mock(), Mock())
227+
metric_reader_storage = MetricReaderStorage(
228+
SdkConfiguration(
229+
resource=Mock(),
230+
metric_readers=(),
231+
views=(
232+
View(
233+
instrument_name="name", aggregation=DropAggregation()
234+
),
235+
),
236+
enable_default_view=False,
237+
)
238+
)
239+
metric_reader_storage.consume_measurement(Measurement(1, counter))
240+
241+
self.assertEqual(
242+
[], metric_reader_storage.collect(AggregationTemporality.DELTA)
243+
)

opentelemetry-sdk/tests/metrics/test_view_instrument_match.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
from opentelemetry.sdk._metrics._view_instrument_match import (
1919
_ViewInstrumentMatch,
2020
)
21+
from opentelemetry.sdk._metrics.aggregation import (
22+
DropAggregation,
23+
_DropAggregation,
24+
)
2125
from opentelemetry.sdk._metrics.measurement import Measurement
2226
from opentelemetry.sdk._metrics.point import AggregationTemporality, Metric
2327
from opentelemetry.sdk._metrics.sdk_configuration import SdkConfiguration
@@ -129,6 +133,28 @@ def test_consume_measurement(self):
129133
{frozenset({}): self.mock_created_aggregation},
130134
)
131135

136+
# Test that a drop aggregation is handled in the same way as any
137+
# other aggregation.
138+
drop_aggregation = DropAggregation()
139+
140+
view_instrument_match = _ViewInstrumentMatch(
141+
view=View(
142+
instrument_name="instrument1",
143+
name="name",
144+
aggregation=drop_aggregation,
145+
attribute_keys={},
146+
),
147+
instrument=instrument1,
148+
sdk_config=sdk_config,
149+
)
150+
view_instrument_match.consume_measurement(
151+
Measurement(value=0, instrument=instrument1, attributes=None)
152+
)
153+
self.assertIsInstance(
154+
view_instrument_match._attributes_aggregation[frozenset({})],
155+
_DropAggregation,
156+
)
157+
132158
def test_collect(self):
133159
instrument1 = Mock(
134160
name="instrument1", description="description", unit="unit"

0 commit comments

Comments
 (0)