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

Switch to the Python Prometheus library #3256

Merged
merged 23 commits into from
May 28, 2018
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.pyc
.*.swp
*~

.DS_Store
_trial_temp/
Expand All @@ -13,6 +14,7 @@ docs/build/
cmdclient_config.json
homeserver*.db
homeserver*.log
homeserver*.log.*
homeserver*.pid
homeserver*.yaml

Expand Down Expand Up @@ -40,6 +42,7 @@ media_store/
*.tac

build/
venv/

localhost-800*/
static/client/register/register_config.js
Expand Down
2 changes: 1 addition & 1 deletion synapse/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(self, hs):
self.TOKEN_NOT_FOUND_HTTP_STATUS = 401

self.token_cache = LruCache(CACHE_SIZE_FACTOR * 10000)
register_cache("token_cache", self.token_cache)
register_cache("cache", "token_cache", self.token_cache)

@defer.inlineCallbacks
def check_from_context(self, event, context, do_sig_check=True):
Expand Down
10 changes: 5 additions & 5 deletions synapse/app/homeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
from synapse.http.additional_resource import AdditionalResource
from synapse.http.server import RootRedirect
from synapse.http.site import SynapseSite
from synapse.metrics import register_memory_metrics
from synapse.metrics.resource import METRICS_PREFIX, MetricsResource
from synapse.metrics import RegistryProxy
from synapse.metrics.resource import METRICS_PREFIX
from synapse.python_dependencies import CONDITIONAL_REQUIREMENTS, \
check_requirements
from synapse.replication.http import ReplicationRestResource, REPLICATION_PREFIX
Expand All @@ -61,6 +61,8 @@
from twisted.web.server import GzipEncoderFactory
from twisted.web.static import File

from prometheus_client.twisted import MetricsResource

logger = logging.getLogger("synapse.app.homeserver")


Expand Down Expand Up @@ -230,7 +232,7 @@ def _configure_named_resource(self, name, compress=False):
resources[WEB_CLIENT_PREFIX] = build_resource_for_web_client(self)

if name == "metrics" and self.get_config().enable_metrics:
resources[METRICS_PREFIX] = MetricsResource(self)
resources[METRICS_PREFIX] = MetricsResource(RegistryProxy())

if name == "replication":
resources[REPLICATION_PREFIX] = ReplicationRestResource(self)
Expand Down Expand Up @@ -362,8 +364,6 @@ def start():
hs.get_datastore().start_doing_background_updates()
hs.get_federation_client().start_get_pdu_cache()

register_memory_metrics(hs)

reactor.callWhenRunning(start)

return hs
Expand Down
17 changes: 7 additions & 10 deletions synapse/federation/federation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,17 @@
FederationBase,
event_from_pdu_json,
)
import synapse.metrics
from synapse.util import logcontext, unwrapFirstError
from synapse.util.caches.expiringcache import ExpiringCache
from synapse.util.logcontext import make_deferred_yieldable, run_in_background
from synapse.util.logutils import log_function
from synapse.util.retryutils import NotRetryingDestination

logger = logging.getLogger(__name__)

from prometheus_client import Counter

# synapse.federation.federation_client is a silly name
metrics = synapse.metrics.get_metrics_for("synapse.federation.client")
logger = logging.getLogger(__name__)

sent_queries_counter = metrics.register_counter("sent_queries", labels=["type"])
sent_queries_counter = Counter("synapse_federation_client_sent_queries", "", ["type"])


PDU_RETRY_TIME_MS = 1 * 60 * 1000
Expand Down Expand Up @@ -108,7 +105,7 @@ def make_query(self, destination, query_type, args,
a Deferred which will eventually yield a JSON object from the
response
"""
sent_queries_counter.inc(query_type)
sent_queries_counter.labels(query_type).inc()

return self.transport_layer.make_query(
destination, query_type, args, retry_on_dns_fail=retry_on_dns_fail,
Expand All @@ -127,7 +124,7 @@ def query_client_keys(self, destination, content, timeout):
a Deferred which will eventually yield a JSON object from the
response
"""
sent_queries_counter.inc("client_device_keys")
sent_queries_counter.labels("client_device_keys").inc()
return self.transport_layer.query_client_keys(
destination, content, timeout
)
Expand All @@ -137,7 +134,7 @@ def query_user_devices(self, destination, user_id, timeout=30000):
"""Query the device keys for a list of user ids hosted on a remote
server.
"""
sent_queries_counter.inc("user_devices")
sent_queries_counter.labels("user_devices").inc()
return self.transport_layer.query_user_devices(
destination, user_id, timeout
)
Expand All @@ -154,7 +151,7 @@ def claim_client_keys(self, destination, content, timeout):
a Deferred which will eventually yield a JSON object from the
response
"""
sent_queries_counter.inc("client_one_time_keys")
sent_queries_counter.labels("client_one_time_keys").inc()
return self.transport_layer.claim_client_keys(
destination, content, timeout
)
Expand Down
19 changes: 10 additions & 9 deletions synapse/federation/federation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@

from synapse.federation.persistence import TransactionActions
from synapse.federation.units import Edu, Transaction
import synapse.metrics
from synapse.types import get_domain_from_id
from synapse.util import async
from synapse.util.caches.response_cache import ResponseCache
from synapse.util.logutils import log_function

from prometheus_client import Counter

from six import iteritems

# when processing incoming transactions, we try to handle multiple rooms in
Expand All @@ -41,17 +42,17 @@

logger = logging.getLogger(__name__)

# synapse.federation.federation_server is a silly name
metrics = synapse.metrics.get_metrics_for("synapse.federation.server")

received_pdus_counter = metrics.register_counter("received_pdus")
received_pdus_counter = Counter("synapse_federation_server_received_pdus", "")

received_edus_counter = metrics.register_counter("received_edus")
received_edus_counter = Counter("synapse_federation_server_received_edus", "")

received_queries_counter = metrics.register_counter("received_queries", labels=["type"])
received_queries_counter = Counter(
"synapse_federation_server_received_queries", "", ["type"]
)


class FederationServer(FederationBase):

def __init__(self, hs):
super(FederationServer, self).__init__(hs)

Expand Down Expand Up @@ -131,7 +132,7 @@ def _handle_incoming_transaction(self, transaction, request_time):

logger.debug("[%s] Transaction is new", transaction.transaction_id)

received_pdus_counter.inc_by(len(transaction.pdus))
received_pdus_counter.inc(len(transaction.pdus))

pdus_by_room = {}

Expand Down Expand Up @@ -292,7 +293,7 @@ def on_pull_request(self, origin, versions):

@defer.inlineCallbacks
def on_query_request(self, query_type, args):
received_queries_counter.inc(query_type)
received_queries_counter.labels(query_type).inc()
resp = yield self.registry.on_query(query_type, args)
defer.returnValue((200, resp))

Expand Down
11 changes: 3 additions & 8 deletions synapse/federation/send_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

from synapse.storage.presence import UserPresenceState
from synapse.util.metrics import Measure
import synapse.metrics
from synapse.metrics import LaterGauge

from blist import sorteddict
from collections import namedtuple
Expand All @@ -45,9 +45,6 @@
logger = logging.getLogger(__name__)


metrics = synapse.metrics.get_metrics_for(__name__)


class FederationRemoteSendQueue(object):
"""A drop in replacement for TransactionQueue"""

Expand Down Expand Up @@ -77,10 +74,8 @@ def __init__(self, hs):
# lambda binds to the queue rather than to the name of the queue which
# changes. ARGH.
def register(name, queue):
metrics.register_callback(
queue_name + "_size",
lambda: len(queue),
)
LaterGauge("synapse_federation_send_queue_%s_size" % (queue_name,),
"", lambda: len(queue))

for queue_name in [
"presence_map", "presence_changed", "keyed_edu", "keyed_edu_changed",
Expand Down
57 changes: 30 additions & 27 deletions synapse/federation/transaction_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@
from synapse.util.metrics import measure_func
from synapse.handlers.presence import format_user_presence_state, get_interested_remotes
import synapse.metrics
from synapse.metrics import LaterGauge
from synapse.metrics import (
sent_edus_counter,
sent_transactions_counter,
events_processed_counter,
)

from prometheus_client import Counter

import logging


logger = logging.getLogger(__name__)

metrics = synapse.metrics.get_metrics_for(__name__)

client_metrics = synapse.metrics.get_metrics_for("synapse.federation.client")
sent_pdus_destination_dist = client_metrics.register_distribution(
"sent_pdu_destinations"
sent_pdus_destination_dist = Counter(
"synapse_federation_transaction_queue_sent_pdu_destinations", ""
)
sent_edus_counter = client_metrics.register_counter("sent_edus")

sent_transactions_counter = client_metrics.register_counter("sent_transactions")

events_processed_counter = client_metrics.register_counter("events_processed")


class TransactionQueue(object):
Expand All @@ -69,8 +69,10 @@ def __init__(self, hs):
# done
self.pending_transactions = {}

metrics.register_callback(
"pending_destinations",
LaterGauge(
"synapse_federation_transaction_queue_pending_destinations",
"",
[],
lambda: len(self.pending_transactions),
)

Expand All @@ -94,12 +96,16 @@ def __init__(self, hs):
# Map of destination -> (edu_type, key) -> Edu
self.pending_edus_keyed_by_dest = edus_keyed = {}

metrics.register_callback(
"pending_pdus",
LaterGauge(
"synapse_federation_transaction_queue_pending_pdus",
"",
[],
lambda: sum(map(len, pdus.values())),
)
metrics.register_callback(
"pending_edus",
LaterGauge(
"synapse_federation_transaction_queue_pending_edus",
"",
[],
lambda: (
sum(map(len, edus.values()))
+ sum(map(len, presence.values()))
Expand Down Expand Up @@ -241,18 +247,15 @@ def handle_room_events(events):
now = self.clock.time_msec()
ts = yield self.store.get_received_ts(events[-1].event_id)

synapse.metrics.event_processing_lag.set(
now - ts, "federation_sender",
)
synapse.metrics.event_processing_last_ts.set(
ts, "federation_sender",
)
synapse.metrics.event_processing_lag.labels(
"federation_sender").set(now - ts)
synapse.metrics.event_processing_last_ts.labels(
"federation_sender").set(ts)

events_processed_counter.inc_by(len(events))
events_processed_counter.inc(len(events))

synapse.metrics.event_processing_positions.set(
next_token, "federation_sender",
)
synapse.metrics.event_processing_positions.labels(
"federation_sender").set(next_token)

finally:
self._is_processing = False
Expand All @@ -275,7 +278,7 @@ def _send_pdu(self, pdu, destinations):
if not destinations:
return

sent_pdus_destination_dist.inc_by(len(destinations))
sent_pdus_destination_dist.inc(len(destinations))

for destination in destinations:
self.pending_pdus_by_dest.setdefault(destination, []).append(
Expand Down
22 changes: 9 additions & 13 deletions synapse/handlers/appservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
from synapse.util.logcontext import (
make_deferred_yieldable, run_in_background,
)
from prometheus_client import Counter

import logging

logger = logging.getLogger(__name__)

metrics = synapse.metrics.get_metrics_for(__name__)

events_processed_counter = metrics.register_counter("events_processed")
events_processed_counter = Counter("synapse_handlers_appservice_events_processed", "")


def log_failure(failure):
Expand Down Expand Up @@ -128,18 +127,15 @@ def handle_room_events(events):
now = self.clock.time_msec()
ts = yield self.store.get_received_ts(events[-1].event_id)

synapse.metrics.event_processing_positions.set(
upper_bound, "appservice_sender",
)
synapse.metrics.event_processing_positions.labels(
"appservice_sender").set(upper_bound)

events_processed_counter.inc_by(len(events))
events_processed_counter.inc(len(events))

synapse.metrics.event_processing_lag.set(
now - ts, "appservice_sender",
)
synapse.metrics.event_processing_last_ts.set(
ts, "appservice_sender",
)
synapse.metrics.event_processing_lag.labels(
"appservice_sender").set(now - ts)
synapse.metrics.event_processing_last_ts.labels(
"appservice_sender").set(ts)
finally:
self.is_processing = False

Expand Down
Loading