Add metric to breached-password check

This commit is contained in:
Deimos
2020-08-10 13:16:04 -06:00
parent d61b848816
commit a70cc61499
3 changed files with 31 additions and 3 deletions

View File

@@ -1,12 +1,12 @@
# Copyright (c) 2018 Tildes contributors <code@tildes.net>
# 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

View File

@@ -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)

View File

@@ -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()