Repository navigation
fix(logging): read user_email from span attributes, not a ContextVar - #95
Merged
Merged
Conversation
이전 PR(#94)의 ContextVar 방식이 실제로는 동작하지 않는 버그였다. get_current_user()는 동기 함수(def)라 FastAPI가 스레드풀에서 실행하는데, anyio.to_thread.run_sync는 contextvars.copy_context()로 복사본을 만들어 그 안에서 실행하므로 ContextVar.set()이 원래 요청 컨텍스트로 전파되지 않는다. 즉 이미지를 새로 배포해도 로그에 user_email이 절대 찍히지 않았다. span.set_attribute()는 이미 값이 채워진 span 객체 자체를 참조로 공유하기 때문에 스레드 경계와 무관하게 작동한다 (user.id/user.email 트레이스 검색이 이미 되던 이유). 그래서 별도 ContextVar 없이 포매터에서 현재 span의 attributes를 직접 읽도록 변경.
wonzz
self-requested a review
July 3, 2026 03:32
wonzz
approved these changes
Jul 3, 2026
1 of 3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ContextVar기반user_email로그 주입이 실제로는 동작하지 않는 버그였음 (이미지 재배포해도 여전히 검색 안 되는 원인)get_current_user()는def(동기)라 FastAPI/anyio가 스레드풀에서 실행함 →anyio.to_thread.run_sync는contextvars.copy_context()로 컨텍스트 복사본을 만들어 그 안에서 실행 →ContextVar.set()이 원래 요청 컨텍스트로 절대 전파되지 않음 (실측 재현 완료)span.set_attribute()는 이미 만들어진 span 객체를 참조로 공유하므로 스레드 경계 영향을 안 받음 → 기존user.id/user.email트레이스 검색은 정상 동작했던 이유set_request_user_email제거하고, 포매터가trace.get_current_span().attributes에서 직접user.email을 읽도록 변경Test plan
anyio.to_thread.run_sync경계에서ContextVar.set()이 전파 안 되는 것 재현 확인opentelemetry.sdk.trace.Span.attributes가set_attribute이후 값을 정확히 반영하는 것 확인 (recording span), non-recording span에서는 안전하게 빈 값 처리되는 것 확인user_email필드가 찍히는지 확인