Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-47716: Revert "Move auth metrics reporting to a background task" #1167

Merged
merged 1 commit into from
Nov 22, 2024
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: 0 additions & 3 deletions changelog.d/20241122_105538_rra_DM_47716.md

This file was deleted.

53 changes: 15 additions & 38 deletions src/gafaelfawr/handlers/ingress.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@
from datetime import timedelta
from typing import Annotated

from fastapi import (
APIRouter,
BackgroundTasks,
Depends,
Header,
HTTPException,
Query,
Response,
)
import sentry_sdk
from fastapi import APIRouter, Depends, Header, HTTPException, Query, Response
from safir.datetime import current_datetime
from safir.models import ErrorModel
from safir.slack.webhook import SlackRouteErrorHandler
Expand All @@ -34,7 +27,7 @@
from ..constants import MINIMUM_LIFETIME
from ..dependencies.auth import AuthenticateRead
from ..dependencies.context import RequestContext, context_dependency
from ..events import AuthBotEvent, AuthUserEvent, FrontendEvents
from ..events import AuthBotEvent, AuthUserEvent
from ..exceptions import (
ExternalUserInfoError,
InsufficientScopeError,
Expand Down Expand Up @@ -312,30 +305,6 @@ async def authenticate_with_type(
return await authenticate(context=context)


async def publish_auth_event(
events: FrontendEvents, auth_config: AuthConfig, token_data: TokenData
) -> None:
"""Publish metrics events for a successful authentication.

Parameters
----------
events
Event manager.
auth_config
Requested authentication parameters.
token_data
Successful authentication data.
"""
username = token_data.username
service = auth_config.service
if is_bot_user(token_data.username):
bot_event = AuthBotEvent(username=username, service=service)
await events.auth_bot.publish(bot_event)
else:
user_event = AuthUserEvent(username=username, service=service)
await events.auth_user.publish(user_event)


@router.get(
"/ingress/auth",
description="Meant to be used as an NGINX auth_request handler",
Expand All @@ -353,7 +322,6 @@ async def get_auth(
token_data: Annotated[TokenData, Depends(authenticate_with_type)],
context: Annotated[RequestContext, Depends(context_dependency)],
response: Response,
background_tasks: BackgroundTasks,
) -> dict[str, str]:
check_lifetime(context, auth_config, token_data)

Expand Down Expand Up @@ -392,9 +360,18 @@ async def get_auth(
headers = await build_success_headers(context, auth_config, token_data)
for key, value in headers:
response.headers.append(key, value)
background_tasks.add_task(
publish_auth_event, context.events, auth_config, token_data
)

# Send a metric event for the authentication.
with sentry_sdk.start_span(name="events.publish"):
username = token_data.username
service = auth_config.service
if is_bot_user(username):
bot_event = AuthBotEvent(username=username, service=service)
await context.events.auth_bot.publish(bot_event)
else:
user_event = AuthUserEvent(username=username, service=service)
await context.events.auth_user.publish(user_event)

return {"status": "ok"}


Expand Down