Skip to content

Commit fcf4f51

Browse files
committed
Change interface to a dataclass instead of a typed dict
1 parent 103f285 commit fcf4f51

File tree

9 files changed

+65
-27
lines changed

9 files changed

+65
-27
lines changed

docs/examples/metrics/instruments/example.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
)
1212
from opentelemetry.sdk.metrics import MeterProvider
1313
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
14+
from opentelemetry.util.types import MetricsHistogramAdvisory
1415

1516
exporter = OTLPMetricExporter(insecure=True)
1617
reader = PeriodicExportingMetricReader(exporter)
@@ -59,10 +60,12 @@ def observable_gauge_func(options: CallbackOptions) -> Iterable[Observation]:
5960
histogram.record(99.9)
6061

6162

62-
# Histogram with explicit_bucket_boundaries advisory
63+
# Histogram with explicit bucket boundaries advisory
6364
histogram = meter.create_histogram(
6465
"histogram_with_advisory",
65-
advisory={"explicit_bucket_boundaries": [0.0, 1.0, 2.0]},
66+
advisory=MetricsHistogramAdvisory(
67+
explicit_bucket_boundaries=[0.0, 1.0, 2.0]
68+
),
6669
)
6770
histogram.record(99.9)
6871

opentelemetry-api/src/opentelemetry/metrics/_internal/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@
7575
)
7676
from opentelemetry.util._once import Once
7777
from opentelemetry.util._providers import _load_provider
78-
from opentelemetry.util.types import Attributes, MetricsInstrumentAdvisory
78+
from opentelemetry.util.types import (
79+
Attributes,
80+
MetricsHistogramAdvisory,
81+
MetricsInstrumentAdvisory,
82+
)
7983

8084
_logger = getLogger(__name__)
8185

@@ -411,7 +415,7 @@ def create_histogram(
411415
name: str,
412416
unit: str = "",
413417
description: str = "",
414-
advisory: Optional[MetricsInstrumentAdvisory] = None,
418+
advisory: Optional[MetricsHistogramAdvisory] = None,
415419
) -> Histogram:
416420
"""Creates a :class:`~opentelemetry.metrics.Histogram` instrument
417421
@@ -565,7 +569,7 @@ def create_histogram(
565569
name: str,
566570
unit: str = "",
567571
description: str = "",
568-
advisory: Optional[MetricsInstrumentAdvisory] = None,
572+
advisory: Optional[MetricsHistogramAdvisory] = None,
569573
) -> Histogram:
570574
with self._lock:
571575
if self._real_meter:
@@ -745,7 +749,7 @@ def create_histogram(
745749
name: str,
746750
unit: str = "",
747751
description: str = "",
748-
advisory: Optional[MetricsInstrumentAdvisory] = None,
752+
advisory: Optional[MetricsHistogramAdvisory] = None,
749753
) -> Histogram:
750754
"""Returns a no-op Histogram."""
751755
_, status = self._register_instrument(

opentelemetry-api/src/opentelemetry/metrics/_internal/instrument.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@
3535
from opentelemetry import metrics
3636
from opentelemetry.context import Context
3737
from opentelemetry.metrics._internal.observation import Observation
38-
from opentelemetry.util.types import Attributes, MetricsInstrumentAdvisory
38+
from opentelemetry.util.types import (
39+
Attributes,
40+
MetricsHistogramAdvisory,
41+
MetricsInstrumentAdvisory,
42+
)
3943

4044
_logger = getLogger(__name__)
4145

@@ -380,7 +384,7 @@ def __init__(
380384
name: str,
381385
unit: str = "",
382386
description: str = "",
383-
advisory: Optional[MetricsInstrumentAdvisory] = None,
387+
advisory: Optional[MetricsHistogramAdvisory] = None,
384388
) -> None:
385389
self._name = name
386390
self._unit = unit

opentelemetry-api/src/opentelemetry/util/types.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import Mapping, Optional, Sequence, Tuple, TypedDict, Union
15+
from dataclasses import dataclass
16+
from typing import Mapping, Optional, Sequence, Tuple, Union
1617

1718
# This is the implementation of the "Any" type as specified by the specifications of OpenTelemetry data model for logs.
1819
# For more details, refer to the OTel specification:
@@ -57,5 +58,16 @@
5758
]
5859

5960

60-
class MetricsInstrumentAdvisory(TypedDict):
61+
@dataclass
62+
class MetricsHistogramAdvisory:
6163
explicit_bucket_boundaries: Optional[Sequence[float]]
64+
65+
66+
@dataclass
67+
class MetricsCommonAdvisory:
68+
pass
69+
70+
71+
MetricsInstrumentAdvisory = Union[
72+
MetricsCommonAdvisory, MetricsHistogramAdvisory
73+
]

opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@
6464
from opentelemetry.sdk.resources import Resource
6565
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
6666
from opentelemetry.util._once import Once
67-
from opentelemetry.util.types import Attributes, MetricsInstrumentAdvisory
67+
from opentelemetry.util.types import (
68+
Attributes,
69+
MetricsHistogramAdvisory,
70+
)
6871

6972
_logger = getLogger(__name__)
7073

@@ -215,12 +218,12 @@ def create_histogram(
215218
name: str,
216219
unit: str = "",
217220
description: str = "",
218-
advisory: Optional[MetricsInstrumentAdvisory] = None,
221+
advisory: Optional[MetricsHistogramAdvisory] = None,
219222
) -> APIHistogram:
220223
if advisory is not None:
221224
invalid_advisory = False
222225
try:
223-
boundaries = advisory["explicit_bucket_boundaries"]
226+
boundaries = advisory.explicit_bucket_boundaries
224227
invalid_advisory = not (
225228
boundaries
226229
and all(isinstance(e, float) for e in boundaries)

opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,7 @@ def _create_aggregation(
12841284

12851285
if isinstance(instrument, Histogram):
12861286
boundaries: Optional[Sequence[float]] = (
1287-
instrument._advisory.get("explicit_bucket_boundaries")
1287+
instrument._advisory.explicit_bucket_boundaries
12881288
if instrument._advisory is not None
12891289
else None
12901290
)

opentelemetry-sdk/tests/metrics/integration_test/test_histogram_advisory_explicit_buckets.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
ExplicitBucketHistogramAggregation,
2121
View,
2222
)
23+
from opentelemetry.util.types import MetricsHistogramAdvisory
2324

2425

2526
class TestHistogramAdvisory(TestCase):
@@ -31,7 +32,9 @@ def test_default(self):
3132
meter = meter_provider.get_meter("testmeter")
3233
histogram = meter.create_histogram(
3334
"testhistogram",
34-
advisory={"explicit_bucket_boundaries": [1.0, 2.0, 3.0]},
35+
advisory=MetricsHistogramAdvisory(
36+
explicit_bucket_boundaries=[1.0, 2.0, 3.0]
37+
),
3538
)
3639
histogram.record(1, {"label": "value"})
3740
histogram.record(2, {"label": "value"})
@@ -59,7 +62,9 @@ def test_view_default_aggregation(self):
5962
meter = meter_provider.get_meter("testmeter")
6063
histogram = meter.create_histogram(
6164
"testhistogram",
62-
advisory={"explicit_bucket_boundaries": [1.0, 2.0, 3.0]},
65+
advisory=MetricsHistogramAdvisory(
66+
explicit_bucket_boundaries=[1.0, 2.0, 3.0]
67+
),
6368
)
6469
histogram.record(1, {"label": "value"})
6570
histogram.record(2, {"label": "value"})
@@ -92,7 +97,9 @@ def test_view_overrides_buckets(self):
9297
meter = meter_provider.get_meter("testmeter")
9398
histogram = meter.create_histogram(
9499
"testhistogram",
95-
advisory={"explicit_bucket_boundaries": [1.0, 2.0, 3.0]},
100+
advisory=MetricsHistogramAdvisory(
101+
explicit_bucket_boundaries=[1.0, 2.0, 3.0]
102+
),
96103
)
97104
histogram.record(1, {"label": "value"})
98105
histogram.record(2, {"label": "value"})

opentelemetry-sdk/tests/metrics/test_aggregation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
LastValueAggregation,
5252
SumAggregation,
5353
)
54-
from opentelemetry.util.types import Attributes
54+
from opentelemetry.util.types import Attributes, MetricsHistogramAdvisory
5555

5656

5757
def measurement(
@@ -635,7 +635,9 @@ def test_histogram_with_advisory(self):
635635
"name",
636636
Mock(),
637637
Mock(),
638-
advisory={"explicit_bucket_boundaries": boundaries},
638+
advisory=MetricsHistogramAdvisory(
639+
explicit_bucket_boundaries=boundaries
640+
),
639641
),
640642
Mock(),
641643
_default_reservoir_factory,

opentelemetry-sdk/tests/metrics/test_metrics.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
from opentelemetry.sdk.resources import Resource
4747
from opentelemetry.test import TestCase
4848
from opentelemetry.test.concurrency_test import ConcurrencyTestBase, MockFunc
49+
from opentelemetry.util.types import MetricsHistogramAdvisory
4950

5051

5152
class DummyMetricReader(MetricReader):
@@ -542,24 +543,26 @@ def test_create_histogram_with_advisory(self):
542543
"name",
543544
unit="unit",
544545
description="description",
545-
advisory={"explicit_bucket_boundaries": [0.0, 1.0, 2.0]},
546+
advisory=MetricsHistogramAdvisory(
547+
explicit_bucket_boundaries=[0.0, 1.0, 2.0]
548+
),
546549
)
547550

548551
self.assertIsInstance(histogram, Histogram)
549552
self.assertEqual(histogram.name, "name")
550553
self.assertEqual(
551554
histogram._advisory,
552-
{"explicit_bucket_boundaries": [0.0, 1.0, 2.0]},
555+
MetricsHistogramAdvisory(
556+
explicit_bucket_boundaries=[0.0, 1.0, 2.0]
557+
),
553558
)
554559

555560
def test_create_histogram_advisory_validation(self):
556561
advisories = [
557-
{"explicit_bucket_boundaries": None},
558-
{"explicit_bucket_boundaries": []},
559-
{},
560-
[],
561-
{"explicit_bucket_boundaries": [1]},
562-
{"explicit_bucket_boundaries": ["1"]},
562+
MetricsHistogramAdvisory(explicit_bucket_boundaries=None),
563+
MetricsHistogramAdvisory(explicit_bucket_boundaries=[]),
564+
MetricsHistogramAdvisory(explicit_bucket_boundaries=[1]),
565+
MetricsHistogramAdvisory(explicit_bucket_boundaries=["1"]),
563566
]
564567
for advisory in advisories:
565568
with self.subTest(advisory=advisory):

0 commit comments

Comments
 (0)