fix(logging): stop noisy LocalProxy-not-mapped warning for guest users (#42306)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Amin Ghadersohi
2026-07-24 13:06:20 -04:00
committed by GitHub
parent 65528a5b76
commit 3bdf134aaa
2 changed files with 90 additions and 3 deletions

View File

@@ -28,6 +28,7 @@ from typing import Any, Callable, cast, Literal
from flask import g, has_request_context, request
from flask_appbuilder.const import API_URI_RIS_KEY
from sqlalchemy import inspect as sa_inspect
from sqlalchemy.exc import SQLAlchemyError
from superset.extensions import stats_logger_manager
@@ -193,11 +194,14 @@ class AbstractEventLogger(ABC):
if user_id is None and has_request_context():
try:
actual_user = g.get("user", None)
if actual_user is not None:
# Guest/anonymous users (e.g. embedded dashboards) are never
# DB-mapped, so adding them to the session always fails.
# This is expected and not worth logging.
if actual_user is not None and sa_inspect(actual_user, raiseerr=False):
db.session.add(actual_user)
user_id = get_user_id()
except Exception as ex:
logging.warning("Failed to add user to db session: %s", ex)
logger.debug("Failed to add user to db session: %s", ex)
user_id = None
payload = collect_request_payload()
if object_ref: