Skip to content

Commit

Permalink
Use keyword args to set Prometheus metric labels
Browse files Browse the repository at this point in the history
As suggested by @stchris
  • Loading branch information
tillprochaska committed Nov 22, 2023
1 parent bfad22f commit 846bff5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
14 changes: 12 additions & 2 deletions aleph/metrics/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,18 @@ def after_request(response):

duration = max(0, default_timer() - request.prometheus_start_time)

REQUEST.labels(method, status, api_endpoint, logged_in).inc()
REQUEST_DURATION.labels(method, status, api_endpoint).observe(duration)
REQUEST.labels(
method=method,
status=status,
api_endpoint=api_endpoint,
logged_in=logged_in,
).inc()

REQUEST_DURATION.labels(
method=method,
status=status,
api_endpoint=api_endpoint,
).observe(duration)

return response

Expand Down
14 changes: 7 additions & 7 deletions aleph/views/sessions_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ def password_login():
data = parse_request("Login")
role = Role.login(data.get("email"), data.get("password"))
if role is None:
AUTH_ATTEMPS.labels("password", "failed").inc()
AUTH_ATTEMPS.labels(method="password", result="failed").inc()
raise BadRequest(gettext("Invalid user or password."))

role.touch()
db.session.commit()
AUTH_ATTEMPS.labels("password", "success").inc()
AUTH_ATTEMPS.labels(method="password", result="success").inc()
update_role(role)
authz = Authz.from_role(role)
return jsonify({"status": "ok", "token": authz.to_token()})
Expand Down Expand Up @@ -104,30 +104,30 @@ def oauth_callback():
err = Unauthorized(gettext("Authentication has failed."))
state = cache.get_complex(_oauth_session(request.args.get("state")))
if state is None:
AUTH_ATTEMPS.labels("oauth", "failed").inc()
AUTH_ATTEMPS.labels(method="oauth", result="failed").inc()
raise err

try:
oauth.provider.framework.set_session_data(request, "state", state.get("state"))
uri = state.get("redirect_uri")
oauth_token = oauth.provider.authorize_access_token(redirect_uri=uri)
except AuthlibBaseError as err:
AUTH_ATTEMPS.labels("oauth", "failed").inc()
AUTH_ATTEMPS.labels(method="oauth", result="failed").inc()
log.warning("Failed OAuth: %r", err)
raise err
if oauth_token is None or isinstance(oauth_token, AuthlibBaseError):
AUTH_ATTEMPS.labels("oauth", "failed").inc()
AUTH_ATTEMPS.labels(method="oauth", result="failed").inc()
log.warning("Failed OAuth: %r", oauth_token)
raise err

role = handle_oauth(oauth.provider, oauth_token)
if role is None:
AUTH_ATTEMPS.labels("oauth", "failed").inc()
AUTH_ATTEMPS.labels(method="oauth", result="failed").inc()
raise err

db.session.commit()
update_role(role)
AUTH_ATTEMPS.labels("oauth", "success").inc()
AUTH_ATTEMPS.labels(method="oauth", result="success").inc()
log.debug("Logged in: %r", role)
request.authz = Authz.from_role(role)
token = request.authz.to_token()
Expand Down

0 comments on commit 846bff5

Please sign in to comment.