This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
124 lines (109 loc) · 4.03 KB
/
main.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
import argparse
import logging
import random
import time
import requests
from pythonjsonlogger import jsonlogger
from settings import settings
logger = logging.getLogger()
logHandler = logging.StreamHandler()
logFmt = jsonlogger.JsonFormatter(timestamp=True)
logHandler.setFormatter(logFmt)
logger.addHandler(logHandler)
def rabbit_metrics() -> None:
prom_ips = settings.prom_ips.split(",")
headers = {"Content-Type": "application/json"}
params = {"query": "sum(rate(rabbitmq_queue_messages_ack_total[2m])) * 120"}
servers_available = len(prom_ips)
for ip in prom_ips:
logging_extras = {"prometheus_ip": ip}
try:
logging.warning("Attempting to get RabbitMQ Metrics", extra=logging_extras)
r = requests.get(f"http://{ip}:9090/api/v1/query", params=params, headers=headers).json()
break
except Exception:
servers_available -= 1
if servers_available == 0:
logging.warning("RabbitMQ Metric collection attempts exhausted, request failed", extra=logging_extras)
return None
else:
logging.warning("RabbitMQ Metric collection failed, attempting next server", extra=logging_extras)
continue
data = {
"name": "RabbitMQ Queue data",
"id": "lm78zp8xzyys",
"timestamp": int(float(r["data"]["result"][0]["value"][0])),
"value": int(float(r["data"]["result"][0]["value"][1])),
}
logging.warning("Gathered RabbitMQ metrics successfully", extra={"data": data})
post_to_stauspage(timestamp=data["timestamp"], value=data["value"], id=data["id"], name=data["name"])
def fake_metrics() -> None:
data = {
"hermes": {
"name": "Hermes RPM",
"id": "4qfyp4zp6fhp",
"timestamp": int(time.time()),
"value": random.randint(98, 132),
},
"cluster": {
"name": "Cluster RPS",
"id": "ylx0y4wxm07c",
"timestamp": int(time.time()),
"value": random.randint(212, 282),
},
}
logging.warning("Generated fake data successfully", extra={"data": data})
post_to_stauspage(
timestamp=data["hermes"]["timestamp"],
value=data["hermes"]["value"],
id=data["hermes"]["id"],
name=data["hermes"]["name"],
)
post_to_stauspage(
timestamp=data["cluster"]["timestamp"],
value=data["cluster"]["value"],
id=data["cluster"]["id"],
name=data["cluster"]["name"],
)
def post_to_stauspage(timestamp: int, value: int, id: str, name: str) -> None:
url = f"https://api.statuspage.io/v1/pages/{settings.sp_page_id}/metrics/{id}/data"
data = {"data": {"timestamp": timestamp, "value": value}}
headers = {
"Content-Type": "application/json",
"Authorization": "OAuth " + settings.sp_api_key,
}
for i in range(settings.retry_count):
attempt = i + 1
log_extras = {"retry_count": attempt, "retry_max": settings.retry_count, "metric_name": name}
try:
logging.warning("Sending Metric to Statuspage", extra=log_extras)
requests.post(url, headers=headers, json=data).raise_for_status()
break
except Exception:
if attempt < settings.retry_count:
logging.warning("Retrying Sending Metric to Statuspage", extra=log_extras)
continue
else:
logging.warning("Giving up Sending Metric to Statuspage")
break
def main():
arg = argparse.ArgumentParser()
arg.add_argument(
"--rabbit",
action="store_true",
help="Fetch total number of ack'd messages in RabbitMQ",
)
arg.add_argument(
"--generate",
action="store_true",
help="Generate fake data for Hermes RPM and Cluster RPS",
)
args = arg.parse_args()
if args.rabbit:
rabbit_metrics()
elif args.generate:
fake_metrics()
else:
print("Run '--help' for more info")
if __name__ == "__main__":
main()