-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmetrics.py
More file actions
103 lines (85 loc) · 2.77 KB
/
metrics.py
File metadata and controls
103 lines (85 loc) · 2.77 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
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
import enum
import prometheus_client
class Metrics(enum.Enum):
WAV_FILE_GENERATION_SECONDS = (
"wav_file_generation_seconds",
"Time taken to generate the phone script wav file in seconds",
prometheus_client.Summary,
)
API_RESPONSE_CODES = (
"leetcode_api_response_codes",
"Response codes of requests made to LeetCode GraphQL API",
prometheus_client.Counter,
["code"],
)
API_LATENCY = (
"leetcode_api_latency",
"Latency / response time of requests made to LeetCode GraphQL API",
prometheus_client.Summary
)
SIGN_LAST_UPDATED = (
"sign_last_updated",
"Timestamp of when the sign was last updated",
prometheus_client.Gauge,
)
SIGN_UPDATE_ERROR = (
"sign_update_error",
"Gauge of the success of updating the sign: 0 for success and 1 for failure",
prometheus_client.Gauge,
)
NULL_USERS_FOUND = (
"null_users_found",
"Number of times LeetCode GraphQL API returns a null / nonexistent user",
prometheus_client.Counter,
["username"]
)
ENDPOINT_HITS = (
"endpoint_hits",
"Count of each HTTP Response code",
prometheus_client.Counter,
["path", "code"]
)
LEETCODE_API_ERROR = (
"leetcode_api_error",
"Gauge of the success of API calls to LeetCode: 0 for success and 1 for failure",
prometheus_client.Gauge,
)
WAV_LAST_GENERATED = (
"wav_last_generated",
"Timestamp of when the phone script wav file was last generated",
prometheus_client.Gauge,
)
WAV_LAST_SENT = (
"wav_last_sent",
"Timestamp of when the phone script wav file was last sent",
prometheus_client.Gauge,
)
WAV_GENERATION_ERROR = (
"wav_generation_error",
"Gauge of the success of generating the phone script wav file: 0 for success and 1 for failure",
prometheus_client.Gauge,
)
def __init__(self, title, description, prometheus_type, label=()):
self.title = title
self.description = description
self.prometheus_type = prometheus_type
self.labels = label
class MetricsHandler:
_instance = None
def __init__(self):
raise RuntimeError("Call MetricsHandler.instance() instead")
def init(self) -> None:
for metric in Metrics:
setattr(
self,
metric.title,
metric.prometheus_type(
metric.title, metric.description, labelnames=metric.labels
),
)
@classmethod
def instance(cls):
if cls._instance is None:
cls._instance = cls.__new__(cls)
cls.init(cls)
return cls._instance