-
Notifications
You must be signed in to change notification settings - Fork 606
Expand file tree
/
Copy pathmetrics.py
More file actions
62 lines (49 loc) · 1.42 KB
/
metrics.py
File metadata and controls
62 lines (49 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import time
from typing import Any, Optional, TYPE_CHECKING
import sentry_sdk
from sentry_sdk.utils import format_attribute
if TYPE_CHECKING:
from sentry_sdk._types import Attributes, Metric, MetricType
def _capture_metric(
name: str,
metric_type: "MetricType",
value: float,
unit: "Optional[str]" = None,
attributes: "Optional[Attributes]" = None,
) -> None:
attrs: "Attributes" = {}
if attributes:
for k, v in attributes.items():
attrs[k] = format_attribute(v)
metric: "Metric" = {
"timestamp": time.time(),
"trace_id": None,
"span_id": None,
"name": name,
"type": metric_type,
"value": float(value),
"unit": unit,
"attributes": attrs,
}
sentry_sdk.get_current_scope()._capture_metric(metric)
def count(
name: str,
value: float,
unit: "Optional[str]" = None,
attributes: "Optional[dict[str, Any]]" = None,
) -> None:
_capture_metric(name, "counter", value, unit, attributes)
def gauge(
name: str,
value: float,
unit: "Optional[str]" = None,
attributes: "Optional[dict[str, Any]]" = None,
) -> None:
_capture_metric(name, "gauge", value, unit, attributes)
def distribution(
name: str,
value: float,
unit: "Optional[str]" = None,
attributes: "Optional[dict[str, Any]]" = None,
) -> None:
_capture_metric(name, "distribution", value, unit, attributes)