Skip to content

feat: 요청별 로그에 userId MDC 추가 - #211

Merged
unam98 merged 2 commits into
devfrom
feat/mdc-user-id-logging
Aug 5, 2026
Merged

feat: 요청별 로그에 userId MDC 추가#211
unam98 merged 2 commits into
devfrom
feat/mdc-user-id-logging

Conversation

@unam98

@unam98 unam98 commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator

배경

유저별 버그 제보 대응 시 해당 유저의 요청/에러 로그를 바로 추적할 수 있도록 userId를 로그에 남김.

변경 사항

  • UserIdResolver: JWT에서 userId 파싱하는 시점(방문자 모드 포함)에 MDC.put("userId", ...) 추가
  • MdcLoggingFilter: 요청 종료 시 MDC.remove("userId") 추가 (스레드 재사용 시 값 누출 방지)
  • logback-spring.xml: 로그 패턴에 [userId=%X{userId}] 추가

활용

배포 후 Grafana Loki에서 {service_name="runnect-server-prod"} | userId="123"로 특정 유저 로그 검색 가능.

테스트

  • 로컬에서 인증 API 호출 후 로그에 userId 찍히는지 확인 (미완료)
  • 컴파일 확인 (JDK 19)

Summary by CodeRabbit

  • New Features

    • Request logs now include a user identifier alongside the existing trace ID.
    • Logged user information is automatically set for both visitor and signed-in requests.
  • Bug Fixes

    • Prevented user ID data from carrying over between requests by clearing it after request processing.

유저 문의/버그 제보 시 특정 유저의 요청·에러 로그를 추적할 수 있도록,
JWT에서 userId를 파싱하는 시점에 MDC에 담아 로그 전체에 남긴다.
@unam98 unam98 self-assigned this Aug 5, 2026
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@unam98, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 49 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: bec7cd9a-9d00-4460-ac26-9a0fe6874115

📥 Commits

Reviewing files that changed from the base of the PR and between f1a7044 and 2914ae5.

📒 Files selected for processing (1)
  • src/main/java/org/runnect/server/config/logging/MdcLoggingFilter.java
📝 Walkthrough

Walkthrough

The request user ID is stored in SLF4J MDC for visitor and authenticated requests. The filter removes it after request processing. The Logback pattern outputs the MDC user ID with the trace ID.

Changes

User ID logging

Layer / File(s) Summary
Populate request user ID context
src/main/java/org/runnect/server/common/resolver/userId/UserIdResolver.java
UserIdResolver stores the configured visitor ID or parsed authenticated user ID in MDC before returning it.
Clear and render user ID context
src/main/java/org/runnect/server/config/logging/MdcLoggingFilter.java, src/main/resources/logback-spring.xml
MdcLoggingFilter removes userId during cleanup. The Logback pattern outputs userId with traceId.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Possibly related PRs

Suggested reviewers: alh0409

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes adding userId to MDC for request-specific logs, which is the main change in the pull request.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/mdc-user-id-logging

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/main/java/org/runnect/server/common/resolver/userId/UserIdResolver.java`:
- Line 80: Move the MDC.put("userId", ...) initialization from
UserIdResolver.resolveArgument into the request-level MdcLoggingFilter or
equivalent interceptor so every request, including handlers without `@UserId`
parameters, has a userId value. Preserve the existing visitor-ID fallback and
ensure the filter sets it before request logging occurs.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 41678704-9c9d-4409-ad49-3efa882c7839

📥 Commits

Reviewing files that changed from the base of the PR and between e7a8c17 and f1a7044.

📒 Files selected for processing (3)
  • src/main/java/org/runnect/server/common/resolver/userId/UserIdResolver.java
  • src/main/java/org/runnect/server/config/logging/MdcLoggingFilter.java
  • src/main/resources/logback-spring.xml

@userid 파라미터가 없는 요청(토큰 재발급, 배너 등)이나 인자 리졸브 이전 단계에서
예외가 나는 경우 userId가 로그에 안 남는 문제를 MdcLoggingFilter에서
accessToken을 미리 파싱해두는 방식으로 보완. 실제 인증 검증/방문자 모드 처리는
UserIdResolver가 그대로 담당하고, 그 결과가 이 값을 덮어쓴다.

로컬 DB/Redis 띄우고 실제 JWT로 검증:
- @userid 있는 요청: 요청 진입 로그부터 userId 표시
- @userid 없는 공개 엔드포인트: 토큰만 있으면 userId 표시
@unam98
unam98 merged commit c8055b0 into dev Aug 5, 2026
2 checks passed
@unam98
unam98 deleted the feat/mdc-user-id-logging branch August 5, 2026 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants