From a70cc614990ebf0faaab6542e3dd4ef6690de6a9 Mon Sep 17 00:00:00 2001 From: Deimos Date: Mon, 10 Aug 2020 13:16:04 -0600 Subject: [PATCH] Add metric to breached-password check --- tildes/tests/test_metrics.py | 4 ++-- tildes/tildes/lib/password.py | 3 +++ tildes/tildes/metrics.py | 27 ++++++++++++++++++++++++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/tildes/tests/test_metrics.py b/tildes/tests/test_metrics.py index db73ae9..ae6c862 100644 --- a/tildes/tests/test_metrics.py +++ b/tildes/tests/test_metrics.py @@ -1,12 +1,12 @@ # Copyright (c) 2018 Tildes contributors # SPDX-License-Identifier: AGPL-3.0-or-later -from tildes.metrics import _COUNTERS, _HISTOGRAMS +from tildes.metrics import _COUNTERS, _HISTOGRAMS, _SUMMARIES def test_all_metric_names_prefixed(): """Ensure all metric names have the 'tildes_' prefix.""" - for metric_dict in (_COUNTERS, _HISTOGRAMS): + for metric_dict in (_COUNTERS, _HISTOGRAMS, _SUMMARIES): metrics = metric_dict.values() for metric in metrics: # this is ugly, but seems to be the "generic" way to get the name diff --git a/tildes/tildes/lib/password.py b/tildes/tildes/lib/password.py index c52927f..967d23c 100644 --- a/tildes/tildes/lib/password.py +++ b/tildes/tildes/lib/password.py @@ -7,6 +7,8 @@ from hashlib import sha1 from redis import ConnectionError, Redis, ResponseError # noqa +from tildes.metrics import summary_timer + # unix socket path for redis server with the breached passwords bloom filter BREACHED_PASSWORDS_REDIS_SOCKET = "/run/redis_breached_passwords/socket" @@ -15,6 +17,7 @@ BREACHED_PASSWORDS_REDIS_SOCKET = "/run/redis_breached_passwords/socket" BREACHED_PASSWORDS_BF_KEY = "breached_passwords_bloom" +@summary_timer("breached_password_check") def is_breached_password(password: str) -> bool: """Return whether the password is in the breached-passwords list.""" redis = Redis(unix_socket_path=BREACHED_PASSWORDS_REDIS_SOCKET) diff --git a/tildes/tildes/metrics.py b/tildes/tildes/metrics.py index 666279a..2002db5 100644 --- a/tildes/tildes/metrics.py +++ b/tildes/tildes/metrics.py @@ -9,7 +9,7 @@ from typing import Callable -from prometheus_client import Counter, Histogram +from prometheus_client import Counter, Histogram, Summary _COUNTERS = { @@ -50,6 +50,13 @@ _HISTOGRAMS = { ), } +_SUMMARIES = { + "breached_password_check": Summary( + "tildes_breached_password_check_seconds", + "Time spent checking whether a password is in the breached list", + ), +} + def incr_counter(name: str, amount: int = 1, **labels: str) -> None: """Increment a Prometheus counter.""" @@ -80,3 +87,21 @@ def get_histogram(name: str, **labels: str) -> Histogram: def histogram_timer(name: str) -> Callable: """Return the .time() decorator for a Prometheus histogram.""" return get_histogram(name).time() + + +def get_summary(name: str, **labels: str) -> Summary: + """Return an (optionally labeled) Prometheus summary by name.""" + try: + hist = _SUMMARIES[name] + except KeyError: + raise ValueError("Invalid summary name") + + if labels: + hist = hist.labels(**labels) + + return hist + + +def summary_timer(name: str) -> Callable: + """Return the .time() decorator for a Prometheus summary.""" + return get_summary(name).time()