Skip to content

Commit e646361

Browse files
committed
feat(metrics): Move minimetrics code to the SDK
1 parent 7b72efd commit e646361

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

sentry_sdk/_types.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
from typing import Any
1414
from typing import Callable
1515
from typing import Dict
16+
from typing import Iterable
17+
from typing import List
18+
from typing import Mapping
1619
from typing import Optional
20+
from typing import Sequence
1721
from typing import Tuple
1822
from typing import Type
1923
from typing import Union
@@ -87,3 +91,52 @@
8791
MeasurementUnit = Union[DurationUnit, InformationUnit, FractionUnit, str]
8892

8993
ProfilerMode = Literal["sleep", "thread", "gevent", "unknown"]
94+
95+
# Unit of the metrics.
96+
MetricUnit = Literal[
97+
"none",
98+
"nanosecond",
99+
"microsecond",
100+
"millisecond",
101+
"second",
102+
"minute",
103+
"hour",
104+
"day",
105+
"week",
106+
"bit",
107+
"byte",
108+
"kilobyte",
109+
"kibibyte",
110+
"mebibyte",
111+
"gigabyte",
112+
"terabyte",
113+
"tebibyte",
114+
"petabyte",
115+
"pebibyte",
116+
"exabyte",
117+
"exbibyte",
118+
"ratio",
119+
"percent",
120+
]
121+
122+
# Type of the metric.
123+
MetricType = Literal["d", "s", "g", "c"]
124+
125+
# Value of the metric.
126+
MetricValue = Union[int, float, str]
127+
128+
# Tag key of a metric.
129+
MetricTagKey = str
130+
131+
# Internal representation of tags as a tuple of tuples (this is done in order to allow for the same key to exist
132+
# multiple times).
133+
MetricTagsInternal = Tuple[Tuple[MetricTagKey, str], ...]
134+
135+
# External representation of tags as a dictionary.
136+
MetricTagValue = Union[str, List[str], Tuple[str, ...]]
137+
MetricTags = Mapping[MetricTagKey, MetricTagValue]
138+
139+
# Value inside the generator for the metric value.
140+
FlushedMetricValue = Union[int, float]
141+
142+
BucketKey = Tuple[MetricType, str, MetricUnit, MetricTagsInternal]

sentry_sdk/client.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,15 @@ def _capture_envelope(envelope):
229229

230230
self.session_flusher = SessionFlusher(capture_func=_capture_envelope)
231231

232+
if self.options.get("_experiments", {}).get("enable_metrics"):
233+
from sentry_sdk.metrics import MetricsAggregator
234+
235+
self.metrics_aggregator = MetricsAggregator(
236+
capture_func=_capture_envelope
237+
)
238+
else:
239+
self.metrics_aggregator = None
240+
232241
max_request_body_size = ("always", "never", "small", "medium")
233242
if self.options["max_request_body_size"] not in max_request_body_size:
234243
raise ValueError(
@@ -610,6 +619,8 @@ def close(
610619
if self.transport is not None:
611620
self.flush(timeout=timeout, callback=callback)
612621
self.session_flusher.kill()
622+
if self.metrics_aggregator is not None:
623+
self.metrics_aggregator.kill()
613624
if self.monitor:
614625
self.monitor.kill()
615626
self.transport.kill()
@@ -632,6 +643,8 @@ def flush(
632643
if timeout is None:
633644
timeout = self.options["shutdown_timeout"]
634645
self.session_flusher.flush()
646+
if self.metrics_aggregator is not None:
647+
self.metrics_aggregator.flush()
635648
self.transport.flush(timeout=timeout, callback=callback)
636649

637650
def __enter__(self):

sentry_sdk/consts.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"profiler_mode": Optional[ProfilerMode],
4242
"otel_powered_performance": Optional[bool],
4343
"transport_zlib_compression_level": Optional[int],
44+
"enable_metrics": Optional[bool],
4445
},
4546
total=False,
4647
)

sentry_sdk/envelope.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ def data_category(self):
260260
return "internal"
261261
elif ty == "profile":
262262
return "profile"
263+
elif ty == "statsd":
264+
return "statsd"
263265
else:
264266
return "default"
265267

0 commit comments

Comments
 (0)