Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add usage stats to prometheus monitoring #1037

Merged
merged 2 commits into from
Aug 22, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions synapse/app/homeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
from synapse.config.homeserver import HomeServerConfig
from synapse.crypto import context_factory
from synapse.util.logcontext import LoggingContext
from synapse.metrics import register_memory_metrics
from synapse.metrics import register_memory_metrics, get_metrics_for
from synapse.metrics.resource import MetricsResource, METRICS_PREFIX
from synapse.replication.resource import ReplicationResource, REPLICATION_PREFIX
from synapse.federation.transport.server import TransportLayerServer
Expand Down Expand Up @@ -385,6 +385,8 @@ def profiled(*args, **kargs):

start_time = hs.get_clock().time()

stats = {}

@defer.inlineCallbacks
def phone_stats_home():
logger.info("Gathering stats for reporting")
Expand All @@ -393,7 +395,10 @@ def phone_stats_home():
if uptime < 0:
uptime = 0

stats = {}
# If the stats directory is empty then this is the first time we've
# reported stats.
first_time = not stats

stats["homeserver"] = hs.config.server_name
stats["timestamp"] = now
stats["uptime_seconds"] = uptime
Expand All @@ -406,6 +411,25 @@ def phone_stats_home():
daily_messages = yield hs.get_datastore().count_daily_messages()
if daily_messages is not None:
stats["daily_messages"] = daily_messages
else:
stats.pop("daily_messages", None)

if first_time:
# Add callbacks to report the synapse stats as metrics whenever
# prometheus requests them, typically every 30s.
# As some of the stats are expensive to calculate we only update
# them when synapse phones home to matrix.org every 24 hours.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you comment that we only want to set the callbacks, as its very confusing since this function does only get called once every 24 hours.

metrics = get_metrics_for("synapse.usage")
metrics.add_callback("timestamp", lambda: stats["timestamp"])
metrics.add_callback("uptime_seconds", lambda: stats["uptime_seconds"])
metrics.add_callback("total_users", lambda: stats["total_users"])
metrics.add_callback("total_room_count", lambda: stats["total_room_count"])
metrics.add_callback(
"daily_active_users", lambda: stats["daily_active_users"]
)
metrics.add_callback(
"daily_messages", lambda: stats.get("daily_messages", 0)
)

logger.info("Reporting stats to matrix.org: %s" % (stats,))
try:
Expand Down