diff --git a/src/appsignal/__init__.py b/src/appsignal/__init__.py index b1aa1df..496d89d 100644 --- a/src/appsignal/__init__.py +++ b/src/appsignal/__init__.py @@ -1,5 +1,5 @@ from .client import Client as Appsignal -from .metrics import increment_counter, set_gauge +from .metrics import increment_counter, set_gauge, add_distribution_value from .tracing import ( send_error, send_error_with_context, @@ -36,6 +36,7 @@ "send_error_with_context", "increment_counter", "set_gauge", + "add_distribution_value" ] diff --git a/src/appsignal/metrics.py b/src/appsignal/metrics.py index 3437d9c..5970e87 100644 --- a/src/appsignal/metrics.py +++ b/src/appsignal/metrics.py @@ -2,7 +2,13 @@ from typing import TYPE_CHECKING, Any, Iterable -from opentelemetry.metrics import CallbackOptions, Observation, UpDownCounter, get_meter +from opentelemetry.metrics import ( + CallbackOptions, + Histogram, + Observation, + UpDownCounter, + get_meter, +) if TYPE_CHECKING: @@ -23,6 +29,19 @@ def increment_counter(name: str, value: int | float, tags: Tags = None) -> None: counter.add(value, tags) +_histograms: dict[str, Histogram] = {} + + +def add_distribution_value(name: str, value: int | float, tags: Tags = None) -> None: + if name in _histograms: + histogram = _histograms[name] + else: + histogram = _meter.create_histogram(name) + _histograms[name] = histogram + + histogram.record(value, tags) + + _gauges: dict[str, dict[TagsKey, int | float]] = {}