-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
snuba.py
129 lines (108 loc) · 3.71 KB
/
snuba.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from __future__ import annotations
from datetime import datetime
from typing import Dict, Optional, Sequence, Union
from django.core.cache import cache
from sentry import quotas
from sentry.sentry_metrics.client.base import GenericMetricsBackend
from sentry.sentry_metrics.use_case_id_registry import UseCaseID
from sentry.testutils.cases import BaseMetricsTestCase # NOQA:S007
def build_mri(metric_name: str, type: str, use_case_id: UseCaseID, unit: Optional[str]) -> str:
mri_unit = "none" if unit is None else unit
return f"{type}:{use_case_id.value}/{metric_name}@{mri_unit}"
def get_retention_from_org_id(org_id: int) -> int:
cache_key = f"sentry_metrics:org_retention_days:{org_id}"
cached_retention: Optional[int] = cache.get(cache_key)
if cached_retention is not None:
return cached_retention
else:
# the default in Snuba is 90 days, and if there is no
# org-configured retention stored, we use that default
retention = quotas.backend.get_event_retention(organization=org_id) or 90
cache.set(cache_key, retention)
return retention
class SnubaMetricsBackend(GenericMetricsBackend):
"""
This backend is meant for use in dev/testing
environments. It allows for producing metrics
to a Snuba HTTP endpoint, which will trigger
processing and ultimately, insertion, of the
metric into Clickhouse.
"""
def counter(
self,
use_case_id: UseCaseID,
org_id: int,
project_id: int,
metric_name: str,
value: Union[int, float],
tags: Dict[str, str],
unit: Optional[str],
) -> None:
"""
Emit a counter metric for internal use cases only.
"""
BaseMetricsTestCase.store_metric(
name=build_mri(metric_name, "c", use_case_id, unit),
tags=tags,
value=value,
org_id=org_id,
project_id=project_id,
type="counter",
timestamp=int(datetime.now().timestamp()),
use_case_id=use_case_id,
)
def set(
self,
use_case_id: UseCaseID,
org_id: int,
project_id: int,
metric_name: str,
value: Sequence[int],
tags: Dict[str, str],
unit: Optional[str],
) -> None:
"""
Emit a set metric for internal use cases only. Can support
a sequence of values.
"""
for val in value:
BaseMetricsTestCase.store_metric(
name=build_mri(metric_name, "s", use_case_id, unit),
tags=tags,
value=val,
org_id=org_id,
project_id=project_id,
type="set",
timestamp=int(datetime.now().timestamp()),
use_case_id=use_case_id,
)
def distribution(
self,
use_case_id: UseCaseID,
org_id: int,
project_id: int,
metric_name: str,
value: Sequence[Union[int, float]],
tags: Dict[str, str],
unit: Optional[str],
) -> None:
"""
Emit a distribution metric for internal use cases only. Can
support a sequence of values.
"""
for val in value:
BaseMetricsTestCase.store_metric(
name=build_mri(metric_name, "d", use_case_id, unit),
tags=tags,
value=val,
org_id=org_id,
project_id=project_id,
type="distribution",
timestamp=int(datetime.now().timestamp()),
use_case_id=use_case_id,
)
def close(self) -> None:
"""
Calling this is not required and is mostly for usage in tests
"""
pass