forked from MirrorHub/synapse
factor out metrics from __init__ to app/homeserver
This commit is contained in:
parent
251e6c1210
commit
9b13817e06
2 changed files with 32 additions and 25 deletions
|
@ -18,6 +18,7 @@ import logging
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from prometheus_client import Gauge
|
||||||
from six import iteritems
|
from six import iteritems
|
||||||
|
|
||||||
from twisted.application import service
|
from twisted.application import service
|
||||||
|
@ -299,7 +300,12 @@ class SynapseHomeServer(HomeServer):
|
||||||
except IncorrectDatabaseSetup as e:
|
except IncorrectDatabaseSetup as e:
|
||||||
quit_with_error(e.message)
|
quit_with_error(e.message)
|
||||||
|
|
||||||
|
# Gauges to expose monthly active user control metrics
|
||||||
|
current_mau_gauge = Gauge("synapse_admin_current_mau", "Current MAU")
|
||||||
|
max_mau_value_gauge = Gauge("synapse_admin_max_mau_value", "MAU Limit")
|
||||||
|
limit_usage_by_mau_gauge = Gauge(
|
||||||
|
"synapse_admin_limit_usage_by_mau", "MAU Limiting enabled"
|
||||||
|
)
|
||||||
def setup(config_options):
|
def setup(config_options):
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
|
@ -512,6 +518,18 @@ def run(hs):
|
||||||
# table will decrease
|
# table will decrease
|
||||||
clock.looping_call(generate_user_daily_visit_stats, 5 * 60 * 1000)
|
clock.looping_call(generate_user_daily_visit_stats, 5 * 60 * 1000)
|
||||||
|
|
||||||
|
def generate_monthly_active_users():
|
||||||
|
count = 0
|
||||||
|
if hs.config.limit_usage_by_mau:
|
||||||
|
count = hs.get_datastore().count_monthly_users()
|
||||||
|
logger.info("NJ count is %d" % (count,))
|
||||||
|
current_mau_gauge.set(float(count))
|
||||||
|
max_mau_value_gauge.set(float(hs.config.max_mau_value))
|
||||||
|
limit_usage_by_mau_gauge.set(float(hs.config.limit_usage_by_mau))
|
||||||
|
|
||||||
|
generate_monthly_active_users()
|
||||||
|
clock.looping_call(generate_monthly_active_users, 5 * 60 * 1000)
|
||||||
|
|
||||||
if hs.config.report_stats:
|
if hs.config.report_stats:
|
||||||
logger.info("Scheduling stats reporting for 3 hour intervals")
|
logger.info("Scheduling stats reporting for 3 hour intervals")
|
||||||
clock.looping_call(start_phone_stats_home, 3 * 60 * 60 * 1000)
|
clock.looping_call(start_phone_stats_home, 3 * 60 * 60 * 1000)
|
||||||
|
|
|
@ -19,7 +19,6 @@ import logging
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from dateutil import tz
|
from dateutil import tz
|
||||||
from prometheus_client import Gauge
|
|
||||||
|
|
||||||
from synapse.api.constants import PresenceState
|
from synapse.api.constants import PresenceState
|
||||||
from synapse.storage.devices import DeviceStore
|
from synapse.storage.devices import DeviceStore
|
||||||
|
@ -61,14 +60,6 @@ from .util.id_generators import ChainedIdGenerator, IdGenerator, StreamIdGenerat
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
# Gauges to expose monthly active user control metrics
|
|
||||||
current_mau_gauge = Gauge("synapse_admin_current_mau", "Current MAU")
|
|
||||||
max_mau_value_gauge = Gauge("synapse_admin_max_mau_value", "MAU Limit")
|
|
||||||
limit_usage_by_mau_gauge = Gauge(
|
|
||||||
"synapse_admin_limit_usage_by_mau", "MAU Limiting enabled"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class DataStore(RoomMemberStore, RoomStore,
|
class DataStore(RoomMemberStore, RoomStore,
|
||||||
RegistrationStore, StreamStore, ProfileStore,
|
RegistrationStore, StreamStore, ProfileStore,
|
||||||
PresenceStore, TransactionStore,
|
PresenceStore, TransactionStore,
|
||||||
|
@ -102,6 +93,7 @@ class DataStore(RoomMemberStore, RoomStore,
|
||||||
self._clock = hs.get_clock()
|
self._clock = hs.get_clock()
|
||||||
self.database_engine = hs.database_engine
|
self.database_engine = hs.database_engine
|
||||||
|
|
||||||
|
self.db_conn = db_conn
|
||||||
self._stream_id_gen = StreamIdGenerator(
|
self._stream_id_gen = StreamIdGenerator(
|
||||||
db_conn, "events", "stream_ordering",
|
db_conn, "events", "stream_ordering",
|
||||||
extra_tables=[("local_invites", "stream_id")]
|
extra_tables=[("local_invites", "stream_id")]
|
||||||
|
@ -282,22 +274,19 @@ class DataStore(RoomMemberStore, RoomStore,
|
||||||
returns:
|
returns:
|
||||||
int: count of current monthly active users
|
int: count of current monthly active users
|
||||||
"""
|
"""
|
||||||
def _count_monthly_users(txn):
|
|
||||||
thirty_days_ago = int(self._clock.time_msec()) - (1000 * 60 * 60 * 24 * 30)
|
|
||||||
sql = """
|
|
||||||
SELECT COUNT(*) FROM user_ips
|
|
||||||
WHERE last_seen > ?
|
|
||||||
"""
|
|
||||||
txn.execute(sql, (thirty_days_ago,))
|
|
||||||
count, = txn.fetchone()
|
|
||||||
|
|
||||||
self._current_mau = count
|
thirty_days_ago = int(self._clock.time_msec()) - (1000 * 60 * 60 * 24 * 30)
|
||||||
current_mau_gauge.set(self._current_mau)
|
sql = """
|
||||||
max_mau_value_gauge.set(self.hs.config.max_mau_value)
|
SELECT COALESCE(count(*), 0) FROM (
|
||||||
limit_usage_by_mau_gauge.set(self.hs.config.limit_usage_by_mau)
|
SELECT user_id FROM user_ips
|
||||||
logger.info("calling mau stats")
|
WHERE last_seen > ?
|
||||||
return count
|
GROUP BY user_id
|
||||||
return self.runInteraction("count_monthly_users", _count_monthly_users)
|
) u
|
||||||
|
"""
|
||||||
|
txn = self.db_conn.cursor()
|
||||||
|
txn.execute(sql, (thirty_days_ago,))
|
||||||
|
count, = txn.fetchone()
|
||||||
|
return count
|
||||||
|
|
||||||
|
|
||||||
def count_r30_users(self):
|
def count_r30_users(self):
|
||||||
|
|
Loading…
Reference in a new issue