Skip to content

Commit e12971a

Browse files
committed
opentelemetry-api: add advisory parameters to Histograms
1 parent 35337ce commit e12971a

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
)
7575
from opentelemetry.util._once import Once
7676
from opentelemetry.util._providers import _load_provider
77-
from opentelemetry.util.types import Attributes
77+
from opentelemetry.util.types import Attributes, MetricsInstrumentAdvisory
7878

7979
_logger = getLogger(__name__)
8080

@@ -379,6 +379,7 @@ def create_histogram(
379379
name: str,
380380
unit: str = "",
381381
description: str = "",
382+
advisory: MetricsInstrumentAdvisory = None,
382383
) -> Histogram:
383384
"""Creates a :class:`~opentelemetry.metrics.Histogram` instrument
384385
@@ -526,13 +527,14 @@ def create_histogram(
526527
name: str,
527528
unit: str = "",
528529
description: str = "",
530+
advisory: MetricsInstrumentAdvisory = None,
529531
) -> Histogram:
530532
with self._lock:
531533
if self._real_meter:
532534
return self._real_meter.create_histogram(
533-
name, unit, description
535+
name, unit, description, advisory
534536
)
535-
proxy = _ProxyHistogram(name, unit, description)
537+
proxy = _ProxyHistogram(name, unit, description, advisory)
536538
self._instruments.append(proxy)
537539
return proxy
538540

@@ -686,6 +688,7 @@ def create_histogram(
686688
name: str,
687689
unit: str = "",
688690
description: str = "",
691+
advisory: MetricsInstrumentAdvisory = None,
689692
) -> Histogram:
690693
"""Returns a no-op Histogram."""
691694
if self._is_instrument_registered(
@@ -699,7 +702,9 @@ def create_histogram(
699702
unit,
700703
description,
701704
)
702-
return NoOpHistogram(name, unit=unit, description=description)
705+
return NoOpHistogram(
706+
name, unit=unit, description=description, advisory=advisory
707+
)
703708

704709
def create_observable_gauge(
705710
self,

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
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
38+
from opentelemetry.util.types import Attributes, MetricsInstrumentAdvisory
3939

4040
_logger = getLogger(__name__)
4141

@@ -330,6 +330,16 @@ class Histogram(Synchronous):
330330
histograms, summaries, and percentile.
331331
"""
332332

333+
@abstractmethod
334+
def __init__(
335+
self,
336+
name: str,
337+
unit: str = "",
338+
description: str = "",
339+
advisory: MetricsInstrumentAdvisory = None,
340+
) -> None:
341+
pass
342+
333343
@abstractmethod
334344
def record(
335345
self,
@@ -348,8 +358,11 @@ def __init__(
348358
name: str,
349359
unit: str = "",
350360
description: str = "",
361+
advisory: MetricsInstrumentAdvisory = None,
351362
) -> None:
352-
super().__init__(name, unit=unit, description=description)
363+
super().__init__(
364+
name, unit=unit, description=description, advisory=advisory
365+
)
353366

354367
def record(
355368
self,
@@ -361,6 +374,19 @@ def record(
361374

362375

363376
class _ProxyHistogram(_ProxyInstrument[Histogram], Histogram):
377+
def __init__(
378+
self,
379+
name: str,
380+
unit: str = "",
381+
description: str = "",
382+
advisory: MetricsInstrumentAdvisory = None,
383+
) -> None:
384+
self._name = name
385+
self._unit = unit
386+
self._description = description
387+
self._advisory = advisory
388+
self._real_instrument: Optional[InstrumentT] = None
389+
364390
def record(
365391
self,
366392
amount: Union[int, float],
@@ -372,7 +398,7 @@ def record(
372398

373399
def _create_real_instrument(self, meter: "metrics.Meter") -> Histogram:
374400
return meter.create_histogram(
375-
self._name, self._unit, self._description
401+
self._name, self._unit, self._description, self._advisory
376402
)
377403

378404

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

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

15-
16-
from typing import Mapping, Optional, Sequence, Tuple, Union
15+
from typing import Literal, Mapping, Optional, Sequence, Tuple, Union
1716

1817
# This is the implementation of the "Any" type as specified by the specifications of OpenTelemetry data model for logs.
1918
# For more details, refer to the OTel specification:
@@ -56,3 +55,8 @@
5655
],
5756
...,
5857
]
58+
59+
MetricsInstrumentAdvisoryKey = Literal["ExplicitBucketBoundaries"]
60+
MetricsInstrumentAdvisory = Optional[
61+
Mapping[MetricsInstrumentAdvisoryKey, AnyValue]
62+
]

opentelemetry-api/tests/metrics/test_meter_provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def test_proxy_meter(self):
251251
name, unit, description
252252
)
253253
real_meter.create_histogram.assert_called_once_with(
254-
name, unit, description
254+
name, unit, description, None
255255
)
256256
real_meter.create_gauge.assert_called_once_with(
257257
name, unit, description

0 commit comments

Comments
 (0)