Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fcollonval committed Sep 2, 2024
1 parent 682a176 commit 0a13b62
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from opentelemetry.sdk.metrics._internal.exemplar import (
AlwaysOffExemplarFilter,
AlwaysOnExemplarFilter,
TraceBasedExemplarFilter,
)
from opentelemetry.sdk.metrics.export import InMemoryMetricReader
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@
from opentelemetry.context import Context
from opentelemetry.metrics import Observation
from opentelemetry.sdk.metrics import Counter, MeterProvider, ObservableCounter
from opentelemetry.sdk.metrics._internal.exemplar import (
AlwaysOffExemplarFilter,
AlwaysOnExemplarFilter,
TraceBasedExemplarFilter,
)
from opentelemetry.sdk.metrics._internal.exemplar import AlwaysOnExemplarFilter
from opentelemetry.sdk.metrics.export import (
AggregationTemporality,
InMemoryMetricReader,
Expand Down
11 changes: 5 additions & 6 deletions opentelemetry-sdk/tests/metrics/test_aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
)
from opentelemetry.sdk.metrics._internal.exemplar import (
AlignedHistogramBucketExemplarReservoir,
ExemplarReservoirBuilder,
SimpleFixedSizeExemplarReservoir,
)
from opentelemetry.sdk.metrics._internal.instrument import (
Expand Down Expand Up @@ -660,15 +659,12 @@ def test_observable_gauge(self):
class TestExemplarsFromAggregations(TestCase):

def test_collection_simple_fixed_size_reservoir(self):
exemplar_reservoir_factory = lambda: SimpleFixedSizeExemplarReservoir(
size=3
)
synchronous_sum_aggregation = _SumAggregation(
Mock(),
True,
AggregationTemporality.DELTA,
0,
exemplar_reservoir_factory,
lambda: SimpleFixedSizeExemplarReservoir(size=3),
)

synchronous_sum_aggregation.aggregate(measurement(1))
Expand All @@ -679,7 +675,10 @@ def test_collection_simple_fixed_size_reservoir(self):
datapoint = synchronous_sum_aggregation.collect(
AggregationTemporality.CUMULATIVE, 0
)
self.assertEqual(len(datapoint.exemplars), 3)
# As the reservoir as multiple buckets, it may store up to
# 3 exemplars
self.assertGreater(len(datapoint.exemplars), 0)
self.assertLessEqual(len(datapoint.exemplars), 3)

def test_collection_simple_fixed_size_reservoir_with_default_reservoir(
self,
Expand Down
22 changes: 11 additions & 11 deletions opentelemetry-sdk/tests/metrics/test_exemplarreservoir.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
)
from opentelemetry.sdk.metrics._internal.exemplar import (
AlignedHistogramBucketExemplarReservoir,
ExemplarReservoir,
ExemplarReservoirFactory,
SimpleFixedSizeExemplarReservoir,
)
from opentelemetry.sdk.metrics._internal.view import _default_reservoir_factory
from opentelemetry.trace import INVALID_SPAN, SpanContext, TraceFlags
from opentelemetry.trace import SpanContext, TraceFlags


class TestSimpleFixedSizeExemplarReservoir(TestCase):
Expand Down Expand Up @@ -58,9 +56,10 @@ def test_filter_attributes(self):
reservoir.offer(
1, time_ns(), {"key1": "value1", "key2": "value2"}, ctx
)
exemplars = reservoir.collect({"key2": "value2", "key3": "value3"})
exemplars = reservoir.collect({"key2": "value2"})
self.assertEqual(len(exemplars), 1)
self.assertNotEqual("key1", exemplars[0].filtered_attributes)
self.assertIn("key1", exemplars[0].filtered_attributes)
self.assertNotIn("key2", exemplars[0].filtered_attributes)

def test_reset_after_collection(self):
reservoir = SimpleFixedSizeExemplarReservoir(4)
Expand Down Expand Up @@ -101,17 +100,18 @@ def test_measurement_in_buckets(self):
)
span = trace.NonRecordingSpan(span_context)
ctx = trace.set_span_in_context(span)
reservoir.offer(52, time_ns(), {"bucket": "5"}, ctx)
reservoir.offer(7, time_ns(), {"bucket": "3"}, ctx)
reservoir.offer(6, time_ns(), {"bucket": "3"}, ctx)
reservoir.offer(80, time_ns(), {"bucket": "5"}, ctx) # outliner
reservoir.offer(52, time_ns(), {"bucket": "4"}, ctx)
reservoir.offer(7, time_ns(), {"bucket": "1"}, ctx)
reservoir.offer(6, time_ns(), {"bucket": "1"}, ctx)

exemplars = reservoir.collect({"bucket": "3"})
exemplars = reservoir.collect({"bucket": "1"})

self.assertEqual(len(exemplars), 2)
self.assertEqual(len(exemplars), 3)
self.assertEqual(exemplars[0].value, 6)
self.assertEqual(exemplars[1].value, 52)
self.assertEqual(exemplars[2].value, 80) # outliner
self.assertEqual(len(exemplars[0].filtered_attributes), 0)
self.assertNotEqual(exemplars[1].filtered_attributes, {"bucket": "5"})

def test_last_measurement_in_bucket(self):
reservoir = AlignedHistogramBucketExemplarReservoir([0, 5, 10, 25])
Expand Down
30 changes: 18 additions & 12 deletions opentelemetry-sdk/tests/metrics/test_view_instrument_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# pylint: disable=protected-access

from time import time_ns
from typing import Any, Callable, Optional, Sequence, Set, Type
from typing import Callable, Sequence, Type
from unittest import TestCase
from unittest.mock import MagicMock, Mock

Expand All @@ -24,17 +24,13 @@
_ViewInstrumentMatch,
)
from opentelemetry.sdk.metrics._internal.aggregation import (
Aggregation,
DefaultAggregation,
_Aggregation,
_DropAggregation,
_ExplicitBucketHistogramAggregation,
_ExponentialBucketHistogramAggregation,
_LastValueAggregation,
)
from opentelemetry.sdk.metrics._internal.exemplar import (
AlignedHistogramBucketExemplarReservoir,
ExemplarReservoir,
ExemplarReservoirBuilder,
SimpleFixedSizeExemplarReservoir,
)
Expand Down Expand Up @@ -65,7 +61,8 @@ def factory(
)
else:
return lambda **kwargs: SimpleFixedSizeExemplarReservoir(
size=size, **kwargs
size=size,
**{k: v for k, v in kwargs.items() if k != "size"},
)

return factory
Expand All @@ -74,7 +71,6 @@ def factory(
class Test_ViewInstrumentMatch(TestCase): # pylint: disable=invalid-name
@classmethod
def setUpClass(cls):

cls.mock_aggregation_factory = Mock()
cls.mock_created_aggregation = (
cls.mock_aggregation_factory._create_aggregation()
Expand Down Expand Up @@ -387,7 +383,6 @@ def test_setting_aggregation(self):


class TestSimpleFixedSizeExemplarReservoir(TestCase):

def test_consume_measurement_with_custom_reservoir_factory(self):
simple_fixed_size_factory = generalized_reservoir_factory(size=10)

Expand Down Expand Up @@ -507,14 +502,14 @@ def test_consume_measurement_with_exemplars(self):
self.assertEqual(len(data_points), 1)

# Verify that exemplars have been correctly stored and collected
self.assertEqual(len(data_points[0].exemplars), 2)
# As the default reservoir as only one bucket, it will retain
# the last measurement as exemplar
self.assertEqual(len(data_points[0].exemplars), 1)

self.assertEqual(data_points[0].exemplars[0].value, 4.0)
self.assertEqual(data_points[0].exemplars[1].value, 5.0)
self.assertEqual(data_points[0].exemplars[0].value, 5.0)


class TestAlignedHistogramBucketExemplarReservoir(TestCase):

def test_consume_measurement_with_custom_reservoir_factory(self):
# Custom factory for AlignedHistogramBucketExemplarReservoir with specific boundaries
histogram_reservoir_factory = generalized_reservoir_factory(
Expand Down Expand Up @@ -582,6 +577,16 @@ def test_consume_measurement_with_custom_reservoir_factory(self):
)
)

# view_instrument_match.consume_measurement(
# Measurement(
# value=30.0, # Should go into the outliners bucket
# time_unix_nano=time_ns(),
# instrument=instrument1,
# context=Context(),
# attributes={"attribute3": "value3"},
# )
# )

# Collect the data points
data_points = view_instrument_match.collect(
AggregationTemporality.CUMULATIVE, 0
Expand All @@ -604,3 +609,4 @@ def test_consume_measurement_with_custom_reservoir_factory(self):
self.assertEqual(
data_points[2].exemplars[0].value, 15.0
) # Third bucket
# self.assertEqual(data_points[2].exemplars[0].value, 30.0) # Outliner bucket

0 comments on commit 0a13b62

Please sign in to comment.