Skip to content

Commit 7e1f42f

Browse files
committed
opentelemetry-api: add advisory parameters to Histograms
1 parent 72fad82 commit 7e1f42f

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

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

Lines changed: 10 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

@@ -218,6 +218,7 @@ def schema_url(self) -> Optional[str]:
218218
"""
219219
return self._schema_url
220220

221+
# TODO: should we add advisory here too?
221222
def _is_instrument_registered(
222223
self, name: str, type_: type, unit: str, description: str
223224
) -> Tuple[bool, str]:
@@ -379,6 +380,7 @@ def create_histogram(
379380
name: str,
380381
unit: str = "",
381382
description: str = "",
383+
advisory: MetricsInstrumentAdvisory = None,
382384
) -> Histogram:
383385
"""Creates a :class:`~opentelemetry.metrics.Histogram` instrument
384386
@@ -526,13 +528,14 @@ def create_histogram(
526528
name: str,
527529
unit: str = "",
528530
description: str = "",
531+
advisory: MetricsInstrumentAdvisory = None,
529532
) -> Histogram:
530533
with self._lock:
531534
if self._real_meter:
532535
return self._real_meter.create_histogram(
533-
name, unit, description
536+
name, unit, description, advisory
534537
)
535-
proxy = _ProxyHistogram(name, unit, description)
538+
proxy = _ProxyHistogram(name, unit, description, advisory)
536539
self._instruments.append(proxy)
537540
return proxy
538541

@@ -686,6 +689,7 @@ def create_histogram(
686689
name: str,
687690
unit: str = "",
688691
description: str = "",
692+
advisory: MetricsInstrumentAdvisory = None,
689693
) -> Histogram:
690694
"""Returns a no-op Histogram."""
691695
if self._is_instrument_registered(
@@ -699,7 +703,9 @@ def create_histogram(
699703
unit,
700704
description,
701705
)
702-
return NoOpHistogram(name, unit=unit, description=description)
706+
return NoOpHistogram(
707+
name, unit=unit, description=description, advisory=advisory
708+
)
703709

704710
def create_observable_gauge(
705711
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
1615
from typing import 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.
@@ -56,3 +55,5 @@
5655
],
5756
...,
5857
]
58+
59+
MetricsInstrumentAdvisory = Optional[Mapping[str, AnyValue]]

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)