2020-10-02 08:23:15 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
# Copyright 2020 The Matrix.org Foundation C.I.C.
|
2023-11-21 15:29:58 -05:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2020-10-02 08:23:15 -04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
import logging
|
|
|
|
import math
|
|
|
|
import resource
|
|
|
|
import sys
|
2023-02-10 18:29:00 -05:00
|
|
|
from typing import TYPE_CHECKING, List, Mapping, Sized, Tuple
|
2020-10-02 08:23:15 -04:00
|
|
|
|
|
|
|
from prometheus_client import Gauge
|
|
|
|
|
2020-10-09 07:37:51 -04:00
|
|
|
from synapse.metrics.background_process_metrics import wrap_as_background_process
|
2021-11-10 15:06:54 -05:00
|
|
|
from synapse.types import JsonDict
|
2020-10-02 08:23:15 -04:00
|
|
|
|
2021-10-22 13:15:41 -04:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
2020-10-02 08:23:15 -04:00
|
|
|
logger = logging.getLogger("synapse.app.homeserver")
|
|
|
|
|
|
|
|
# Contains the list of processes we will be monitoring
|
|
|
|
# currently either 0 or 1
|
2021-11-10 15:06:54 -05:00
|
|
|
_stats_process: List[Tuple[int, "resource.struct_rusage"]] = []
|
2020-10-02 08:23:15 -04:00
|
|
|
|
|
|
|
# Gauges to expose monthly active user control metrics
|
2022-09-08 10:01:42 -04:00
|
|
|
current_mau_gauge = Gauge("synapse_admin_mau_current", "Current MAU")
|
2020-10-02 08:23:15 -04:00
|
|
|
current_mau_by_service_gauge = Gauge(
|
|
|
|
"synapse_admin_mau_current_mau_by_service",
|
|
|
|
"Current MAU by service",
|
|
|
|
["app_service"],
|
|
|
|
)
|
2022-09-08 10:01:42 -04:00
|
|
|
max_mau_gauge = Gauge("synapse_admin_mau_max", "MAU Limit")
|
2020-10-02 08:23:15 -04:00
|
|
|
registered_reserved_users_mau_gauge = Gauge(
|
2022-09-08 10:01:42 -04:00
|
|
|
"synapse_admin_mau_registered_reserved_users",
|
2020-10-02 08:23:15 -04:00
|
|
|
"Registered users with reserved threepids",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-10-07 11:27:56 -04:00
|
|
|
@wrap_as_background_process("phone_stats_home")
|
2021-11-10 15:06:54 -05:00
|
|
|
async def phone_stats_home(
|
|
|
|
hs: "HomeServer",
|
|
|
|
stats: JsonDict,
|
|
|
|
stats_process: List[Tuple[int, "resource.struct_rusage"]] = _stats_process,
|
|
|
|
) -> None:
|
2022-09-05 06:26:43 -04:00
|
|
|
"""Collect usage statistics and send them to the configured endpoint.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
hs: the HomeServer object to use for gathering usage data.
|
|
|
|
stats: the dict in which to store the statistics sent to the configured
|
|
|
|
endpoint. Mostly used in tests to figure out the data that is supposed to
|
|
|
|
be sent.
|
|
|
|
stats_process: statistics about resource usage of the process.
|
|
|
|
"""
|
|
|
|
|
2020-10-02 08:23:15 -04:00
|
|
|
logger.info("Gathering stats for reporting")
|
|
|
|
now = int(hs.get_clock().time())
|
2021-11-10 15:06:54 -05:00
|
|
|
# Ensure the homeserver has started.
|
|
|
|
assert hs.start_time is not None
|
2020-10-02 08:23:15 -04:00
|
|
|
uptime = int(now - hs.start_time)
|
|
|
|
if uptime < 0:
|
|
|
|
uptime = 0
|
|
|
|
|
|
|
|
#
|
|
|
|
# Performance statistics. Keep this early in the function to maintain reliability of `test_performance_100` test.
|
|
|
|
#
|
|
|
|
old = stats_process[0]
|
|
|
|
new = (now, resource.getrusage(resource.RUSAGE_SELF))
|
|
|
|
stats_process[0] = new
|
|
|
|
|
|
|
|
# Get RSS in bytes
|
|
|
|
stats["memory_rss"] = new[1].ru_maxrss
|
|
|
|
|
|
|
|
# Get CPU time in % of a single core, not % of all cores
|
|
|
|
used_cpu_time = (new[1].ru_utime + new[1].ru_stime) - (
|
|
|
|
old[1].ru_utime + old[1].ru_stime
|
|
|
|
)
|
|
|
|
if used_cpu_time == 0 or new[0] == old[0]:
|
|
|
|
stats["cpu_average"] = 0
|
|
|
|
else:
|
|
|
|
stats["cpu_average"] = math.floor(used_cpu_time / (new[0] - old[0]) * 100)
|
|
|
|
|
|
|
|
#
|
|
|
|
# General statistics
|
|
|
|
#
|
|
|
|
|
2022-02-23 06:04:02 -05:00
|
|
|
store = hs.get_datastores().main
|
2022-09-05 06:26:43 -04:00
|
|
|
common_metrics = await hs.get_common_usage_metrics_manager().get_metrics()
|
2021-07-19 14:31:17 -04:00
|
|
|
|
2021-09-13 13:07:12 -04:00
|
|
|
stats["homeserver"] = hs.config.server.server_name
|
2021-09-29 06:44:15 -04:00
|
|
|
stats["server_context"] = hs.config.server.server_context
|
2020-10-02 08:23:15 -04:00
|
|
|
stats["timestamp"] = now
|
|
|
|
stats["uptime_seconds"] = uptime
|
|
|
|
version = sys.version_info
|
|
|
|
stats["python_version"] = "{}.{}.{}".format(
|
|
|
|
version.major, version.minor, version.micro
|
|
|
|
)
|
2021-07-19 14:31:17 -04:00
|
|
|
stats["total_users"] = await store.count_all_users()
|
2020-10-02 08:23:15 -04:00
|
|
|
|
2021-07-19 14:31:17 -04:00
|
|
|
total_nonbridged_users = await store.count_nonbridged_users()
|
2020-10-02 08:23:15 -04:00
|
|
|
stats["total_nonbridged_users"] = total_nonbridged_users
|
|
|
|
|
2021-07-19 14:31:17 -04:00
|
|
|
daily_user_type_results = await store.count_daily_user_type()
|
2020-10-02 08:23:15 -04:00
|
|
|
for name, count in daily_user_type_results.items():
|
|
|
|
stats["daily_user_type_" + name] = count
|
|
|
|
|
2021-07-19 14:31:17 -04:00
|
|
|
room_count = await store.get_room_count()
|
2020-10-02 08:23:15 -04:00
|
|
|
stats["total_room_count"] = room_count
|
|
|
|
|
2022-09-05 06:26:43 -04:00
|
|
|
stats["daily_active_users"] = common_metrics.daily_active_users
|
2021-07-19 14:31:17 -04:00
|
|
|
stats["monthly_active_users"] = await store.count_monthly_users()
|
|
|
|
daily_active_e2ee_rooms = await store.count_daily_active_e2ee_rooms()
|
2021-02-01 12:06:22 -05:00
|
|
|
stats["daily_active_e2ee_rooms"] = daily_active_e2ee_rooms
|
2021-07-19 14:31:17 -04:00
|
|
|
stats["daily_e2ee_messages"] = await store.count_daily_e2ee_messages()
|
|
|
|
daily_sent_e2ee_messages = await store.count_daily_sent_e2ee_messages()
|
2021-02-01 12:06:22 -05:00
|
|
|
stats["daily_sent_e2ee_messages"] = daily_sent_e2ee_messages
|
2021-07-19 14:31:17 -04:00
|
|
|
stats["daily_active_rooms"] = await store.count_daily_active_rooms()
|
|
|
|
stats["daily_messages"] = await store.count_daily_messages()
|
|
|
|
daily_sent_messages = await store.count_daily_sent_messages()
|
2021-02-01 12:06:22 -05:00
|
|
|
stats["daily_sent_messages"] = daily_sent_messages
|
2020-10-02 08:23:15 -04:00
|
|
|
|
2021-07-27 10:25:39 -04:00
|
|
|
r30v2_results = await store.count_r30v2_users()
|
2021-07-19 11:11:34 -04:00
|
|
|
for name, count in r30v2_results.items():
|
|
|
|
stats["r30v2_users_" + name] = count
|
|
|
|
|
2020-10-02 08:23:15 -04:00
|
|
|
stats["cache_factor"] = hs.config.caches.global_factor
|
|
|
|
stats["event_cache_size"] = hs.config.caches.event_cache_size
|
|
|
|
|
|
|
|
#
|
|
|
|
# Database version
|
|
|
|
#
|
|
|
|
|
|
|
|
# This only reports info about the *main* database.
|
2021-07-19 14:31:17 -04:00
|
|
|
stats["database_engine"] = store.db_pool.engine.module.__name__
|
|
|
|
stats["database_server_version"] = store.db_pool.engine.server_version
|
2020-10-02 08:23:15 -04:00
|
|
|
|
2020-10-07 11:13:38 -04:00
|
|
|
#
|
|
|
|
# Logging configuration
|
|
|
|
#
|
|
|
|
synapse_logger = logging.getLogger("synapse")
|
|
|
|
log_level = synapse_logger.getEffectiveLevel()
|
|
|
|
stats["log_level"] = logging.getLevelName(log_level)
|
|
|
|
|
2021-09-23 12:03:01 -04:00
|
|
|
logger.info(
|
|
|
|
"Reporting stats to %s: %s" % (hs.config.metrics.report_stats_endpoint, stats)
|
|
|
|
)
|
2020-10-02 08:23:15 -04:00
|
|
|
try:
|
|
|
|
await hs.get_proxied_http_client().put_json(
|
2021-09-23 12:03:01 -04:00
|
|
|
hs.config.metrics.report_stats_endpoint, stats
|
2020-10-02 08:23:15 -04:00
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
logger.warning("Error reporting stats: %s", e)
|
|
|
|
|
|
|
|
|
2021-11-10 15:06:54 -05:00
|
|
|
def start_phone_stats_home(hs: "HomeServer") -> None:
|
2020-10-02 08:23:15 -04:00
|
|
|
"""
|
|
|
|
Start the background tasks which report phone home stats.
|
|
|
|
"""
|
|
|
|
clock = hs.get_clock()
|
|
|
|
|
2021-11-10 15:06:54 -05:00
|
|
|
stats: JsonDict = {}
|
2020-10-02 08:23:15 -04:00
|
|
|
|
2021-11-10 15:06:54 -05:00
|
|
|
def performance_stats_init() -> None:
|
2020-10-02 08:23:15 -04:00
|
|
|
_stats_process.clear()
|
|
|
|
_stats_process.append(
|
|
|
|
(int(hs.get_clock().time()), resource.getrusage(resource.RUSAGE_SELF))
|
|
|
|
)
|
|
|
|
|
|
|
|
# Rather than update on per session basis, batch up the requests.
|
|
|
|
# If you increase the loop period, the accuracy of user_daily_visits
|
|
|
|
# table will decrease
|
2022-02-23 06:04:02 -05:00
|
|
|
clock.looping_call(
|
|
|
|
hs.get_datastores().main.generate_user_daily_visits, 5 * 60 * 1000
|
|
|
|
)
|
2020-10-02 08:23:15 -04:00
|
|
|
|
|
|
|
# monthly active user limiting functionality
|
2022-02-23 06:04:02 -05:00
|
|
|
clock.looping_call(
|
|
|
|
hs.get_datastores().main.reap_monthly_active_users, 1000 * 60 * 60
|
|
|
|
)
|
|
|
|
hs.get_datastores().main.reap_monthly_active_users()
|
2020-10-02 08:23:15 -04:00
|
|
|
|
2020-10-07 11:27:56 -04:00
|
|
|
@wrap_as_background_process("generate_monthly_active_users")
|
2021-11-10 15:06:54 -05:00
|
|
|
async def generate_monthly_active_users() -> None:
|
2020-10-02 08:23:15 -04:00
|
|
|
current_mau_count = 0
|
2023-02-10 18:29:00 -05:00
|
|
|
current_mau_count_by_service: Mapping[str, int] = {}
|
2021-11-10 15:06:54 -05:00
|
|
|
reserved_users: Sized = ()
|
2022-02-23 06:04:02 -05:00
|
|
|
store = hs.get_datastores().main
|
2021-09-29 06:44:15 -04:00
|
|
|
if hs.config.server.limit_usage_by_mau or hs.config.server.mau_stats_only:
|
2020-10-02 08:23:15 -04:00
|
|
|
current_mau_count = await store.get_monthly_active_count()
|
|
|
|
current_mau_count_by_service = (
|
|
|
|
await store.get_monthly_active_count_by_service()
|
|
|
|
)
|
|
|
|
reserved_users = await store.get_registered_reserved_users()
|
|
|
|
current_mau_gauge.set(float(current_mau_count))
|
|
|
|
|
|
|
|
for app_service, count in current_mau_count_by_service.items():
|
|
|
|
current_mau_by_service_gauge.labels(app_service).set(float(count))
|
|
|
|
|
|
|
|
registered_reserved_users_mau_gauge.set(float(len(reserved_users)))
|
2021-09-29 06:44:15 -04:00
|
|
|
max_mau_gauge.set(float(hs.config.server.max_mau_value))
|
2020-10-02 08:23:15 -04:00
|
|
|
|
2021-09-29 06:44:15 -04:00
|
|
|
if hs.config.server.limit_usage_by_mau or hs.config.server.mau_stats_only:
|
2020-10-07 11:27:56 -04:00
|
|
|
generate_monthly_active_users()
|
|
|
|
clock.looping_call(generate_monthly_active_users, 5 * 60 * 1000)
|
2020-10-02 08:23:15 -04:00
|
|
|
# End of monthly active user settings
|
|
|
|
|
2021-09-23 12:03:01 -04:00
|
|
|
if hs.config.metrics.report_stats:
|
2020-10-02 08:23:15 -04:00
|
|
|
logger.info("Scheduling stats reporting for 3 hour intervals")
|
2020-10-07 11:27:56 -04:00
|
|
|
clock.looping_call(phone_stats_home, 3 * 60 * 60 * 1000, hs, stats)
|
2020-10-02 08:23:15 -04:00
|
|
|
|
|
|
|
# We need to defer this init for the cases that we daemonize
|
|
|
|
# otherwise the process ID we get is that of the non-daemon process
|
|
|
|
clock.call_later(0, performance_stats_init)
|
|
|
|
|
|
|
|
# We wait 5 minutes to send the first set of stats as the server can
|
|
|
|
# be quite busy the first few minutes
|
2020-10-07 11:27:56 -04:00
|
|
|
clock.call_later(5 * 60, phone_stats_home, hs, stats)
|