Skip to content
Snippets Groups Projects
Commit c8ed2cfe authored by psy's avatar psy
Browse files

save state as json, add absolute gauge, initialize counters

parent 82d54434
No related branches found
No related tags found
No related merge requests found
...@@ -3,12 +3,14 @@ import requests ...@@ -3,12 +3,14 @@ import requests
from prometheus_client import start_http_server, Counter, Gauge from prometheus_client import start_http_server, Counter, Gauge
from time import sleep from time import sleep
import argparse import argparse
import json
metric_members = Gauge('matrix_channel_members_total', 'Members joined in room', ['room_id', 'canonical_alias', 'name']) metric_members = Gauge('matrix_channel_members_total', 'Members joined in room', ['room_id', 'canonical_alias', 'name'])
metric_channel_types = Gauge('matrix_channel_types_total', 'Channels per type', ['type']) metric_channel_types = Gauge('matrix_channel_types_total', 'Channels per type', ['type'])
metric_channel_version = Gauge('matrix_channel_version_total', 'Channels per version', ['version']) metric_channel_version = Gauge('matrix_channel_version_total', 'Channels per version', ['version'])
metric_channel_state_events = Gauge('matrix_channel_state_events_total', 'State events per room', metric_channel_state_events = Gauge('matrix_channel_state_events_total', 'State events per room',
['room_id', 'canonical_alias', 'name']) ['room_id', 'canonical_alias', 'name'])
metric_channel_messages_absolute = Gauge('matrix_channel_messages_absolute', 'Messages per channel absolute', ['room_id', 'canonical_alias', 'name', 'type'])
metric_channel_messages = Counter('matrix_channel_messages_total', 'Messages per channel', ['room_id', 'canonical_alias', 'name', 'type']) metric_channel_messages = Counter('matrix_channel_messages_total', 'Messages per channel', ['room_id', 'canonical_alias', 'name', 'type'])
...@@ -22,21 +24,35 @@ def fetch_metrics(synapse_url, access_token): ...@@ -22,21 +24,35 @@ def fetch_metrics(synapse_url, access_token):
update_room_metrics(rooms) update_room_metrics(rooms)
for room in rooms: for room in rooms:
statefile = f"state/{room['room_id']}.end" statefile = f"state/{room['room_id']}.state"
url = f"{synapse_url}/_synapse/admin/v1/rooms/{room['room_id']}/messages" url = f"{synapse_url}/_synapse/admin/v1/rooms/{room['room_id']}/messages"
messages = []
end = None state = {}
try: try:
with open(statefile, 'r') as file: with open(statefile, 'r') as file:
end = file.read() state = json.load(file)
except OSError: except OSError:
print(f"No state file for room {room['room_id']} ({room.get('name', '')}) found. Fetching messages could take a while.") print(f"No state file for room {room['room_id']} ({room.get('name', '')}) found. Fetching messages could take a while.")
state['messagecount'] = {}
for type in state['messagecount']:
metric_channel_messages.labels(
room_id=room['room_id'],
canonical_alias=room['canonical_alias'] if room['canonical_alias'] else '',
name=room['name'] if room['name'] else '',
type=type)
metric_channel_messages_absolute.labels(
room_id=room['room_id'],
canonical_alias=room['canonical_alias'] if room['canonical_alias'] else '',
name=room['name'] if room['name'] else '',
type=type).set(state['messagecount'][type])
messages = []
while True: while True:
params = {} params = {}
if end is not None: if 'end' in state:
params['from'] = end params['from'] = state['end']
result = requests.get(url, params=params, headers=auth).json() result = requests.get(url, params=params, headers=auth).json()
...@@ -44,7 +60,7 @@ def fetch_metrics(synapse_url, access_token): ...@@ -44,7 +60,7 @@ def fetch_metrics(synapse_url, access_token):
messages.extend(result['chunk']) messages.extend(result['chunk'])
if 'end' in result: if 'end' in result:
end = result['end'] state['end'] = result['end']
else: else:
break break
...@@ -55,8 +71,19 @@ def fetch_metrics(synapse_url, access_token): ...@@ -55,8 +71,19 @@ def fetch_metrics(synapse_url, access_token):
name=room['name'] if room['name'] else '', name=room['name'] if room['name'] else '',
type=message['type']).inc() type=message['type']).inc()
metric_channel_messages_absolute.labels(
room_id=room['room_id'],
canonical_alias=room['canonical_alias'] if room['canonical_alias'] else '',
name=room['name'] if room['name'] else '',
type=message['type']).inc()
if message['type'] not in state['messagecount']:
state['messagecount'][message['type']] = 0
state['messagecount'][message['type']] += 1
with open(statefile, 'w') as file: with open(statefile, 'w') as file:
file.write(end) json.dump(state, file)
def update_room_metrics(rooms): def update_room_metrics(rooms):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment