Use _ instead of . as a metric namespacing separator, for Prometheus

This commit is contained in:
Paul "LeoNerd" Evans 2015-03-06 16:18:21 +00:00
parent 0b96bb793e
commit b0cf867319
8 changed files with 34 additions and 18 deletions

View File

@ -69,11 +69,11 @@ class TransactionQueue(object):
# HACK to get unique tx id # HACK to get unique tx id
self._next_txn_id = int(self._clock.time_msec()) self._next_txn_id = int(self._clock.time_msec())
metrics.register_callback("pending_pdus", metrics.register_callback("pendingPdus",
lambda: {(dest,): len(pdus[dest]) for dest in pdus.keys()}, lambda: {(dest,): len(pdus[dest]) for dest in pdus.keys()},
labels=["dest"], labels=["dest"],
) )
metrics.register_callback("pending_edus", metrics.register_callback("pendingEdus",
lambda: {(dest,): len(edus[dest]) for dest in edus.keys()}, lambda: {(dest,): len(edus[dest]) for dest in edus.keys()},
labels=["dest"], labels=["dest"],
) )

View File

@ -136,7 +136,7 @@ class PresenceHandler(BaseHandler):
self._user_cachemap = {} self._user_cachemap = {}
self._user_cachemap_latest_serial = 0 self._user_cachemap_latest_serial = 0
metrics.register_callback("user_cachemap:size", metrics.register_callback("userCachemap:size",
lambda: len(self._user_cachemap) lambda: len(self._user_cachemap)
) )

View File

@ -34,10 +34,10 @@ logger = logging.getLogger(__name__)
metrics = synapse.metrics.get_metrics_for(__name__) metrics = synapse.metrics.get_metrics_for(__name__)
outgoing_requests_counter = metrics.register_counter("outgoing_requests", outgoing_requests_counter = metrics.register_counter("requests",
labels=["method"], labels=["method"],
) )
incoming_responses_counter = metrics.register_counter("incoming_responses", incoming_responses_counter = metrics.register_counter("responses",
labels=["method","code"], labels=["method","code"],
) )

View File

@ -43,10 +43,10 @@ logger = logging.getLogger(__name__)
metrics = synapse.metrics.get_metrics_for(__name__) metrics = synapse.metrics.get_metrics_for(__name__)
outgoing_requests_counter = metrics.register_counter("outgoing_requests", outgoing_requests_counter = metrics.register_counter("requests",
labels=["method"], labels=["method"],
) )
incoming_responses_counter = metrics.register_counter("incoming_responses", incoming_responses_counter = metrics.register_counter("responses",
labels=["method","code"], labels=["method","code"],
) )

View File

@ -37,10 +37,10 @@ logger = logging.getLogger(__name__)
metrics = synapse.metrics.get_metrics_for(__name__) metrics = synapse.metrics.get_metrics_for(__name__)
incoming_requests_counter = metrics.register_counter("incoming_requests", incoming_requests_counter = metrics.register_counter("requests",
labels=["method"], labels=["method"],
) )
outgoing_responses_counter = metrics.register_counter("outgoing_responses", outgoing_responses_counter = metrics.register_counter("responses",
labels=["method","code"], labels=["method","code"],
) )

View File

@ -41,7 +41,12 @@ class Metrics(object):
self.name_prefix = name self.name_prefix = name
def _register(self, metric_class, name, *args, **kwargs): def _register(self, metric_class, name, *args, **kwargs):
full_name = "%s.%s" % (self.name_prefix, name) if "_" in name:
raise ValueError("Metric names %s is invalid as it cannot contain an underscore"
% (name)
)
full_name = "%s_%s" % (self.name_prefix, name)
metric = metric_class(full_name, *args, **kwargs) metric = metric_class(full_name, *args, **kwargs)
@ -78,10 +83,13 @@ class Metrics(object):
return wrapped return wrapped
def get_metrics_for(name): def get_metrics_for(pkg_name):
""" Returns a Metrics instance for conveniently creating metrics """ Returns a Metrics instance for conveniently creating metrics
namespaced with the given name prefix. """ namespaced with the given name prefix. """
return Metrics(name)
# Convert a "package.name" to "package_name" because Prometheus doesn't
# let us use . in metric names
return Metrics(pkg_name.replace(".", "_"))
def render_all(): def render_all():

View File

@ -122,7 +122,7 @@ class Notifier(object):
all_listeners |= x all_listeners |= x
return len(all_listeners) return len(all_listeners)
metrics.register_callback("all_listeners", count_listeners) metrics.register_callback("listeners", count_listeners)
metrics.register_callback("rooms", metrics.register_callback("rooms",
lambda: count(bool, self.room_to_listeners.values()) lambda: count(bool, self.room_to_listeners.values())

View File

@ -40,7 +40,14 @@ metrics = synapse.metrics.get_metrics_for("synapse.storage")
sql_query_timer = metrics.register_timer("queries", labels=["verb"]) sql_query_timer = metrics.register_timer("queries", labels=["verb"])
sql_txn_timer = metrics.register_timer("transactions", labels=["desc"]) sql_txn_timer = metrics.register_timer("transactions", labels=["desc"])
sql_getevents_timer = metrics.register_timer("get_events", labels=["desc"]) sql_getevents_timer = metrics.register_timer("getEvents", labels=["desc"])
caches_by_name = {}
cache_counter = metrics.register_cache(
"cache",
lambda: {(name,): len(caches_by_name[name]) for name in caches_by_name.keys()},
labels=["name"],
)
# TODO(paul): # TODO(paul):
@ -62,8 +69,9 @@ def cached(max_entries=1000):
""" """
def wrap(orig): def wrap(orig):
cache = OrderedDict() cache = OrderedDict()
name = orig.__name__
counter = metrics.register_cache(orig.__name__, lambda: len(cache)) caches_by_name[name] = cache
def prefill(key, value): def prefill(key, value):
while len(cache) > max_entries: while len(cache) > max_entries:
@ -74,10 +82,10 @@ def cached(max_entries=1000):
@defer.inlineCallbacks @defer.inlineCallbacks
def wrapped(self, key): def wrapped(self, key):
if key in cache: if key in cache:
counter.inc_hits() cache_counter.inc_hits(name)
defer.returnValue(cache[key]) defer.returnValue(cache[key])
counter.inc_misses() cache_counter.inc_misses(name)
ret = yield orig(self, key) ret = yield orig(self, key)
prefill(key, ret) prefill(key, ret)
defer.returnValue(ret) defer.returnValue(ret)
@ -195,7 +203,7 @@ class SQLBaseStore(object):
self._get_event_cache = LruCache(hs.config.event_cache_size) self._get_event_cache = LruCache(hs.config.event_cache_size)
self._get_event_cache_counter = metrics.register_cache( self._get_event_cache_counter = metrics.register_cache(
"get_event_cache", "getEventCache",
size_callback=lambda: len(self._get_event_cache), size_callback=lambda: len(self._get_event_cache),
) )