Changed code to support older Python versions
This commit is contained in:
parent
eb92d2d36f
commit
582458cdd0
5027 changed files with 794942 additions and 4 deletions
81
venv/lib/python3.11/site-packages/sentry_sdk/metrics.py
Normal file
81
venv/lib/python3.11/site-packages/sentry_sdk/metrics.py
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
"""
|
||||
NOTE: This file contains experimental code that may be changed or removed at any
|
||||
time without prior notice.
|
||||
"""
|
||||
|
||||
import time
|
||||
from typing import Any, Optional, TYPE_CHECKING, Union
|
||||
|
||||
import sentry_sdk
|
||||
from sentry_sdk.utils import safe_repr
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from sentry_sdk._types import Metric, MetricType
|
||||
|
||||
|
||||
def _capture_metric(
|
||||
name, # type: str
|
||||
metric_type, # type: MetricType
|
||||
value, # type: float
|
||||
unit=None, # type: Optional[str]
|
||||
attributes=None, # type: Optional[dict[str, Any]]
|
||||
):
|
||||
# type: (...) -> None
|
||||
client = sentry_sdk.get_client()
|
||||
|
||||
attrs = {} # type: dict[str, Union[str, bool, float, int]]
|
||||
if attributes:
|
||||
for k, v in attributes.items():
|
||||
attrs[k] = (
|
||||
v
|
||||
if (
|
||||
isinstance(v, str)
|
||||
or isinstance(v, int)
|
||||
or isinstance(v, bool)
|
||||
or isinstance(v, float)
|
||||
)
|
||||
else safe_repr(v)
|
||||
)
|
||||
|
||||
metric = {
|
||||
"timestamp": time.time(),
|
||||
"trace_id": None,
|
||||
"span_id": None,
|
||||
"name": name,
|
||||
"type": metric_type,
|
||||
"value": float(value),
|
||||
"unit": unit,
|
||||
"attributes": attrs,
|
||||
} # type: Metric
|
||||
|
||||
client._capture_metric(metric)
|
||||
|
||||
|
||||
def count(
|
||||
name, # type: str
|
||||
value, # type: float
|
||||
unit=None, # type: Optional[str]
|
||||
attributes=None, # type: Optional[dict[str, Any]]
|
||||
):
|
||||
# type: (...) -> None
|
||||
_capture_metric(name, "counter", value, unit, attributes)
|
||||
|
||||
|
||||
def gauge(
|
||||
name, # type: str
|
||||
value, # type: float
|
||||
unit=None, # type: Optional[str]
|
||||
attributes=None, # type: Optional[dict[str, Any]]
|
||||
):
|
||||
# type: (...) -> None
|
||||
_capture_metric(name, "gauge", value, unit, attributes)
|
||||
|
||||
|
||||
def distribution(
|
||||
name, # type: str
|
||||
value, # type: float
|
||||
unit=None, # type: Optional[str]
|
||||
attributes=None, # type: Optional[dict[str, Any]]
|
||||
):
|
||||
# type: (...) -> None
|
||||
_capture_metric(name, "distribution", value, unit, attributes)
|
||||
Loading…
Add table
Add a link
Reference in a new issue