-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmonitor.py
More file actions
62 lines (52 loc) · 2.41 KB
/
monitor.py
File metadata and controls
62 lines (52 loc) · 2.41 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
import aiohttp
from typing import Iterator, AsyncGenerator
from django.conf import settings
from django.template import Template, Context
query_last_metrics_promql_tpl = Template("""
last_over_time({__name__=~"{{metrics}}",namespace="{{namespace}}"}[{{duration}}])
""")
async def query_prom(url, params) -> list[tuple[dict[str, str], int]]:
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params) as response:
if response.status != 200:
return []
response_json = await response.json()
if response_json['status'] != 'success':
return []
return response_json['data']['result']
async def last_metrics(namespace) -> AsyncGenerator[Iterator, str]:
if not settings.DRYCC_METRICS_CONFIG:
return
url = f"{settings.DRYCC_VICTORIAMETRICS_URL}/api/v1/query"
promql = query_last_metrics_promql_tpl.render(Context({
"metrics": '|'.join(settings.DRYCC_METRICS_CONFIG.keys()),
"namespace": namespace,
"duration": settings.DRYCC_METRICS_INTERVAL
}))
for item in await query_prom(url, {"query": promql, "start": int(time.time() - 60)}):
yield '%s{%s} %s\n' % (
item['metric']['__name__'],
','.join([
f'{key}="{value}"' for key, value in item['metric'].items()
if key in settings.DRYCC_METRICS_CONFIG[item['metric']['__name__']]
]),
item['value'][1]
)
async def query_volume_usage(namespaces: Iterator[str], start: int, stop: int
) -> list[tuple[dict[str, str], int]]:
if not settings.DRYCC_VICTORIAMETRICS_URL:
return []
url = f"{settings.DRYCC_VICTORIAMETRICS_URL}/api/v1/query"
promql = Template(settings.DRYCC_VOLUME_USAGE_TEMPLATE).render(Context({
"namespaces": "|".join(namespaces)
}))
return await query_prom(url, {"query": promql, "start": start, "end": stop})
async def query_network_usage(namespaces: Iterator[str], start: int, stop: int
) -> list[tuple[dict[str, str], int]]:
url = f"{settings.DRYCC_VICTORIAMETRICS_URL}/api/v1/query"
promql = Template(settings.DRYCC_NETWORK_USAGE_TEMPLATE).render(Context({
"namespaces": "|".join(namespaces),
"duration": f"{stop-start}s"
}))
return await query_prom(url, {"query": promql, "start": start, "end": stop})