Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
matrix prometheus exporter
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
infra
matrix prometheus exporter
Commits
179c9cde
Commit
179c9cde
authored
7 months ago
by
psy
Browse files
Options
Downloads
Patches
Plain Diff
use config file instead of env vars, add config option for bind address
parent
03eda25c
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+3
-1
3 additions, 1 deletion
.gitignore
example_config.ini
+4
-0
4 additions, 0 deletions
example_config.ini
exporter.py
+69
-55
69 additions, 55 deletions
exporter.py
requirements.txt
+2
-1
2 additions, 1 deletion
requirements.txt
with
78 additions
and
57 deletions
.gitignore
+
3
−
1
View file @
179c9cde
.idea/
venv/
config.ini
__pycache__/
This diff is collapsed.
Click to expand it.
example_config.ini
0 → 100644
+
4
−
0
View file @
179c9cde
BIND_ADDR
=
::
PORT
=
8337
SYNAPSE_URL
=
ACCESS_TOKEN
=
\ No newline at end of file
This diff is collapsed.
Click to expand it.
exporter.py
+
69
−
55
View file @
179c9cde
#! /usr/bin/env python
import
os
import
requests
from
prometheus_client
import
start_http_server
,
Gauge
from
time
import
sleep
import
argparse
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_version
=
Gauge
(
'
matrix_channel_version_total
'
,
'
Channels per version
'
,
[
'
version
'
])
metric_channel_state_events
=
Gauge
(
'
matrix_channel_state_events_total
'
,
'
State events per room
'
,
[
'
room_id
'
,
'
canonical_alias
'
,
'
name
'
])
metric_channel_state_events
=
Gauge
(
'
matrix_channel_state_events_total
'
,
'
State events per room
'
,
[
'
room_id
'
,
'
canonical_alias
'
,
'
name
'
])
def
fetch_metrics
():
def
fetch_metrics
(
synapse_url
,
access_token
):
room_type_count
=
{
'
space
'
:
0
,
'
public
'
:
0
,
'
private
'
:
0
,
'
dm
'
:
0
}
room_version_count
=
{}
auth
=
{
'
Authorization
'
:
f
"
Bearer
{
config
[
'
token
'
]
}
"
'
Authorization
'
:
f
"
Bearer
{
access_
token
}
"
}
room_version_count
=
{}
rooms
=
requests
.
get
(
f
"
https://
{
config
[
'
synapse_url
'
]
}
/_synapse/admin/v1/rooms
"
,
headers
=
auth
).
json
()[
'
rooms
'
]
for
room
in
sorted
(
rooms
,
key
=
lambda
item
:
item
[
'
joined_members
'
],
reverse
=
True
):
rooms
=
requests
.
get
(
f
"
https://
{
synapse_url
}
/_synapse/admin/v1/rooms
"
,
headers
=
auth
).
json
()[
'
rooms
'
]
for
room
in
rooms
:
if
room
[
'
version
'
]
not
in
room_version_count
:
room_version_count
[
room
[
'
version
'
]]
=
0
...
...
@@ -59,18 +60,31 @@ def fetch_metrics():
metric_channel_types
.
labels
(
type
=
room_type
).
set
(
count
)
def
parse_config
(
config
):
config_parsed
=
{}
for
line
in
config
.
splitlines
():
if
not
line
.
startswith
(
'
#
'
)
and
len
(
line
)
>
0
:
parts
=
line
.
split
(
'
=
'
,
1
)
config_parsed
[
parts
[
0
].
strip
()]
=
parts
[
1
].
strip
()
return
config_parsed
if
__name__
==
'
__main__
'
:
config
=
{
'
port
'
:
int
(
os
.
environ
.
get
(
'
PORT
'
)),
'
token
'
:
os
.
environ
.
get
(
'
ACCESS_TOKEN
'
),
'
synapse_url
'
:
os
.
environ
.
get
(
'
SYNAPSE_URL
'
)
}
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
'
-c
'
,
'
--config
'
,
default
=
'
config.ini
'
,
type
=
argparse
.
FileType
(
'
r
'
))
args
=
parser
.
parse_args
()
config
=
parse_config
(
args
.
config
.
read
())
start_http_server
(
config
[
'
port
'
],
addr
=
'
::
'
)
start_http_server
(
int
(
config
[
'
PORT
'
]
)
,
addr
=
config
[
'
BIND_ADDR
'
]
)
while
True
:
try
:
fetch_metrics
()
fetch_metrics
(
config
[
'
SYNAPSE_URL
'
],
config
[
'
ACCESS_TOKEN
'
]
)
except
Exception
as
e
:
print
(
'
Exception:
\n
'
+
str
(
e
))
sleep
(
20
)
This diff is collapsed.
Click to expand it.
requirements.txt
+
2
−
1
View file @
179c9cde
requests
prometheus-client
argparse
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment