Skip to content

Commit

Permalink
Add add_distribution_value method
Browse files Browse the repository at this point in the history
Add an `add_distribution_value` custom instrumentation metrics
method, in the same line as the ones that currently exist for Ruby,
Elixir and Node.js.
  • Loading branch information
unflxw committed Mar 8, 2024
1 parent 00ddcdc commit 37c85c1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/appsignal/__init__.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -36,6 +36,7 @@
"send_error_with_context",
"increment_counter",
"set_gauge",
"add_distribution_value"
]


Expand Down
21 changes: 20 additions & 1 deletion src/appsignal/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]] = {}


Expand Down

0 comments on commit 37c85c1

Please sign in to comment.