Skip to content

Commit

Permalink
opentelemetry-api: add advisory parameters to Histograms
Browse files Browse the repository at this point in the history
  • Loading branch information
xrmx committed Dec 17, 2024
1 parent 35337ce commit b676189
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
)
from opentelemetry.util._once import Once
from opentelemetry.util._providers import _load_provider
from opentelemetry.util.types import Attributes
from opentelemetry.util.types import Attributes, MetricsInstrumentAdvisory

_logger = getLogger(__name__)

Expand Down Expand Up @@ -379,6 +379,7 @@ def create_histogram(
name: str,
unit: str = "",
description: str = "",
advisory: MetricsInstrumentAdvisory = None,
) -> Histogram:
"""Creates a :class:`~opentelemetry.metrics.Histogram` instrument
Expand Down Expand Up @@ -526,13 +527,14 @@ def create_histogram(
name: str,
unit: str = "",
description: str = "",
advisory: MetricsInstrumentAdvisory = None,
) -> Histogram:
with self._lock:
if self._real_meter:
return self._real_meter.create_histogram(
name, unit, description
name, unit, description, advisory
)
proxy = _ProxyHistogram(name, unit, description)
proxy = _ProxyHistogram(name, unit, description, advisory)
self._instruments.append(proxy)
return proxy

Expand Down Expand Up @@ -686,6 +688,7 @@ def create_histogram(
name: str,
unit: str = "",
description: str = "",
advisory: MetricsInstrumentAdvisory = None,
) -> Histogram:
"""Returns a no-op Histogram."""
if self._is_instrument_registered(
Expand All @@ -699,7 +702,9 @@ def create_histogram(
unit,
description,
)
return NoOpHistogram(name, unit=unit, description=description)
return NoOpHistogram(
name, unit=unit, description=description, advisory=advisory
)

def create_observable_gauge(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from opentelemetry import metrics
from opentelemetry.context import Context
from opentelemetry.metrics._internal.observation import Observation
from opentelemetry.util.types import Attributes
from opentelemetry.util.types import Attributes, MetricsInstrumentAdvisory

_logger = getLogger(__name__)

Expand Down Expand Up @@ -330,6 +330,16 @@ class Histogram(Synchronous):
histograms, summaries, and percentile.
"""

@abstractmethod
def __init__(
self,
name: str,
unit: str = "",
description: str = "",
advisory: MetricsInstrumentAdvisory = None,
) -> None:
pass

@abstractmethod
def record(
self,
Expand All @@ -348,8 +358,11 @@ def __init__(
name: str,
unit: str = "",
description: str = "",
advisory: MetricsInstrumentAdvisory = None,
) -> None:
super().__init__(name, unit=unit, description=description)
super().__init__(
name, unit=unit, description=description, advisory=advisory
)

def record(
self,
Expand All @@ -361,6 +374,20 @@ def record(


class _ProxyHistogram(_ProxyInstrument[Histogram], Histogram):
# pylint: disable=super-init-not-called
def __init__(
self,
name: str,
unit: str = "",
description: str = "",
advisory: MetricsInstrumentAdvisory = None,
) -> None:
self._name = name
self._unit = unit
self._description = description
self._advisory = advisory
self._real_instrument: Optional[InstrumentT] = None

def record(
self,
amount: Union[int, float],
Expand All @@ -372,7 +399,7 @@ def record(

def _create_real_instrument(self, meter: "metrics.Meter") -> Histogram:
return meter.create_histogram(
self._name, self._unit, self._description
self._name, self._unit, self._description, self._advisory
)


Expand Down
8 changes: 6 additions & 2 deletions opentelemetry-api/src/opentelemetry/util/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.


from typing import Mapping, Optional, Sequence, Tuple, Union
from typing import Literal, Mapping, Optional, Sequence, Tuple, Union

# This is the implementation of the "Any" type as specified by the specifications of OpenTelemetry data model for logs.
# For more details, refer to the OTel specification:
Expand Down Expand Up @@ -56,3 +55,8 @@
],
...,
]

MetricsInstrumentAdvisoryKey = Literal["ExplicitBucketBoundaries"]
MetricsInstrumentAdvisory = Optional[
Mapping[MetricsInstrumentAdvisoryKey, AnyValue]
]
2 changes: 1 addition & 1 deletion opentelemetry-api/tests/metrics/test_meter.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def create_observable_counter(
name, callbacks, unit=unit, description=description
)

def create_histogram(self, name, unit="", description=""):
def create_histogram(self, name, unit="", description="", advisory=None):
super().create_histogram(name, unit=unit, description=description)

def create_gauge(self, name, unit="", description=""):
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-api/tests/metrics/test_meter_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def test_proxy_meter(self):
name, unit, description
)
real_meter.create_histogram.assert_called_once_with(
name, unit, description
name, unit, description, None
)
real_meter.create_gauge.assert_called_once_with(
name, unit, description
Expand Down

0 comments on commit b676189

Please sign in to comment.