-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path__init__.py
74 lines (66 loc) · 2.16 KB
/
__init__.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
"""Prometheus metrics module."""
import sys
from flask import Flask
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from prometheus_client import multiprocess, make_wsgi_app, Info, Gauge
from prometheus_client import CollectorRegistry
GAUGES = [
'memory_rss',
'cpu_average',
'timestamp',
'uptime_seconds',
'total_users',
'total_nonbridged_users',
'daily_user_type_native',
'daily_user_type_guest',
'daily_user_type_bridged',
'total_room_count',
'daily_active_users',
'monthly_active_users',
'daily_active_rooms',
'daily_active_e2ee_rooms',
'daily_messages',
'daily_e2ee_messages',
'daily_sent_messages',
'daily_sent_e2ee_messages',
'r30v2_users_all',
'r30v2_users_android',
'r30v2_users_ios',
'r30v2_users_electron',
'r30v2_users_web',
'cache_factor',
'event_cache_size'
]
KEYS = [
'homeserver',
]
INFOS = KEYS + [
'server_context',
'python_version',
'database_engine',
'database_server_version',
'log_level',
]
class Prometheus:
"""Prometheus metrics class."""
def __init__(self, app: Flask = None):
self.registry = CollectorRegistry()
self.metadata = Info('app_build_info', 'Description of the build')
if app:
self.init_app(app)
def init_app(self, app: Flask):
"""Initialize Flask application."""
# See https://prometheus.github.io/client_python/instrumenting/labels/
app.config['LABELS_INITIALIZED'] = False # Set True once labels are initialized.
app.config['METRICS_PREFIX'] = 'synapse_usage_'
try:
multiprocess.MultiProcessCollector(self.registry)
except ValueError as error:
app.logger.error(error)
sys.exit(1)
app.wsgi_app = DispatcherMiddleware(app.wsgi_app, {'/metrics': make_wsgi_app()})
self.metadata.info({'version': app.config['VERSION'], 'build': app.config['BUILD']})
self.metrics = {
metric: Gauge(app.config['METRICS_PREFIX'] + metric, metric, KEYS) for metric in GAUGES
}
self.metrics['info'] = Gauge(app.config['METRICS_PREFIX'] + 'info', 'info', INFOS)