Skip to content

Commit

Permalink
Add mark label to connected devices metric
Browse files Browse the repository at this point in the history
+ added "langate_" prefix to metrics
  • Loading branch information
SkytAsul committed Jan 25, 2025
1 parent 39ee5b5 commit 5361239
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions backend/langate/modules/netcontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
DELETE_REQUESTS = ["disconnect_user"]
PUT_REQUESTS = ["set_mark"]

connected_devices_gauge = prometheus.Gauge("connected_devices", "Amount of connected devices")
connected_devices_gauge = prometheus.Gauge("langate_connected_devices", "Amount of connected devices", labelnames=["mark"])
mark_table = {} # used to keep track of mark for MAC addresses between requests

class Netcontrol:
"""
Expand Down Expand Up @@ -69,7 +70,8 @@ def connect_user(self, mac: str, mark: int, name: str) -> None:
self.logger.info(f"Connecting user with MAC address {mac} ({name})...")
try:
self.request("connect_user", {"mac": mac, "mark": mark, "name": name})
connected_devices_gauge.inc()
mark_table[mac] = mark
connected_devices_gauge.labels(str(mark)).inc()
except:
raise

Expand All @@ -80,7 +82,9 @@ def disconnect_user(self, mac: str) -> None:
self.logger.info(f"Disconnecting user with MAC address {mac}...")
try:
self.request("disconnect_user", {"mac": mac})
connected_devices_gauge.dec()
if mac in mark_table:
old_mark = mark_table.pop(mac)
connected_devices_gauge.labels(str(old_mark)).dec()
except:
raise

Expand All @@ -90,6 +94,11 @@ def set_mark(self, mac: str, mark: int) -> None:
"""
self.logger.info(f"Setting mark of user with MAC address {mac} to {mark}...")
self.request("set_mark", {"mac": mac, "mark": mark})
if mac in mark_table:
old_mark = mark_table[mac]
connected_devices_gauge.labels(str(old_mark)).dec()
mark_table[mac] = mark
connected_devices_gauge.labels(str(mark)).inc()

def __init__(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion backend/langate/user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import prometheus_client as prometheus

users_gauge = prometheus.Gauge("users", "Total amount of users registered.")
users_gauge = prometheus.Gauge("langate_users", "Total amount of users registered.")

class UserManager(BaseUserManager):
"""
Expand Down

0 comments on commit 5361239

Please sign in to comment.