Skip to content

[feat] PB-121 TRACE-3 (1/2) 비동기 이메일 Trace 컨텍스트 - #243

Merged
geonusp merged 1 commit into
mainfrom
feature/trace-async-context
Aug 25, 2026
Merged

geonusp merged 1 commit into
mainfrom
feature/trace-async-context

Conversation

@PHJ2000

@PHJ2000 PHJ2000 commented Aug 22, 2026 •

Copy link
Copy Markdown
Collaborator

✨ Feature PR

🔗 이슈 링크 (필수)


🧩 이 PR의 변경사항 (필수)

전체 중 위치 (Stacked PR 시)

문제와 원인

  • 기존 MdcTaskDecorator는 requestId/userId MDC만 worker 스레드로 전달해 실제 Trace 컨텍스트와 부모·자식 관계가 @Async 경계에서 끊겼다.
  • 이메일 작업 span과 SMTP 재시도 프록시가 같은 메서드에 있으면 재시도마다 작업 span이 중복되거나 @Retryable 경계를 잘못 감쌀 수 있었다.
  • worker 스레드 재사용 시 정상·예외 종료 후 Trace/MDC가 복원되는지를 실제 executor와 exporter로 수집한 span으로 증명하는 테스트가 없었다.

이 PR에서 변경한 것

  • MdcTaskDecorator가 작업 제출 시점의 Micrometer Trace 컨텍스트를 캡처하고 worker에서 복원하도록 변경
  • 기존 requestId/userId MDC 전파를 유지하면서 traceId/spanId 문자열 복사는 계속 차단하고, 실행 후 이전 worker 컨텍스트를 finally에서 복원
  • 이메일 작업 전체를 나타내는 email.verification.send span을 AsyncEmailService에 추가
  • 기존 @Retryable/@Recover 로직을 RetryingEmailDeliveryService로 분리해 비동기 작업 span 하나가 재시도 전체를 감싸도록 구성
  • 부모 Trace가 없는 직접 호출에서는 이메일 작업 span이 새 루트로 시작되는 계약 추가
  • 실제 회원가입 HTTP 요청 → transaction commit → AFTER_COMMIT listener → emailTaskExecutor 경로를 in-memory exporter로 검증
  • 정상·예외 작업 뒤 동일 worker를 재사용해 Trace와 requestId/userId가 누수되지 않는지 검증
  • 테스트용 span exporter와 대기 도구를 공통 지원 코드로 분리

✅ 이 PR이 커버하는 AC (필수)

Covers (이 PR에서 완료)

  • AC1: HTTP parent가 있는 AFTER_COMMIT 경로에서 동일 trace를 유지하고, 제출 시점 current span의 자식으로 email.verification.send 생성
    • Spring 내부 request observation이 존재하는 실제 환경에서는 HTTP SERVER span의 후손 관계까지 exporter parent chain으로 검증
  • AC2: 부모 Trace가 없는 직접 호출에서 email.verification.send를 새 루트 span으로 생성
  • AC3: 정상·예외 종료 후 Trace/MDC 복원 및 동일 worker 재사용 시 이전 작업의 값이 남지 않음
  • AC6(PR1 범위): 기존 비동기 실행과 재시도 횟수·backoff·@Recover 동작 유지
  • AC7(PR1 로컬 범위): 실제 emailTaskExecutor·AFTER_COMMIT·HTTP 경로와 in-memory exporter 통합 테스트 및 전체 회귀 테스트 통과

Not covered (후속 작업)

  • AC4: 시도별 email.smtp.send와 성공·재시도·한도 소진 결과 → TRACE-3 PR 2/2
  • AC5: span에 민감정보를 수집하지 않고 기존 수신 이메일 원문 로그 제거 → TRACE-3 PR 2/2
  • AC8: dev Tempo에서 대표 Trace와 애플리케이션 로그 traceId 대조 → 전체 코드 병합 후 런타임 검증 단계

🧪 테스트 결과 (필수)

로컬

./gradlew test --tests 'com.beachcheck.auth.integration.AsyncEmailRetryIntegrationTest' \
  --tests 'com.beachcheck.auth.integration.AsyncEmailTracingIntegrationTest' \
  --tests 'com.beachcheck.auth.service.AsyncEmailServiceTest' \
  --tests 'com.beachcheck.auth.service.RetryingEmailDeliveryServiceTest'
./gradlew check
git diff --check
  • ✅ 최종 커밋 5b85c7a0 기준 전체 293 tests
  • ✅ failures 0, errors 0, skipped 0
  • ✅ Checkstyle, Spotless, JaCoCo 포함 BUILD SUCCESSFUL
  • ✅ git diff --check 통과

CI

  • ✅ Observability IaC Validation 통과
  • ✅ Backend Build & Test (Unit + Integration) 통과
  • ✅ PR HEAD 5b85c7a 기준 전체 CI 통과

Smoke 테스트

  • 부모가 있는 직접 제출 → email.verification.send가 제출 시점 span의 직접 자식
  • 실제 회원가입 HTTP → AFTER_COMMIT → 비동기 worker까지 동일 Trace parent chain 유지
  • 부모가 없는 직접 호출 → 유효한 루트 이메일 작업 span 생성
  • 정상·예외 작업 뒤 동일 worker 재사용 → Trace/MDC 값이 남지 않음
  • 최대 재시도 소진 → recover 1회, 성공 로그가 잘못 기록되지 않음
  • 결과: ✅ 로컬 통합 테스트 통과

🧭 영향 범위 체크 (필수)

  • API 변경
  • DB 변경
  • 설정 변경 (emailTaskExecutor에 실제 Trace 컨텍스트 전파 적용, 외부 환경변수 추가 없음)
  • Breaking change

💬 리뷰 포인트 (필수)

  • 집중해서 볼 파일/라인:
    • MdcTaskDecorator: 제출 시점 Trace 캡처와 정상·예외 종료 후 worker 컨텍스트 복원
    • AsyncEmailService: 재시도 전체를 한 번 감싸는 email.verification.send span 경계
    • RetryingEmailDeliveryService: 기존 @Retryable/@Recover 프록시 동작 보존
    • AsyncEmailRetryIntegrationTest: 실제 HTTP SERVER → Spring request observation → 비동기 이메일 span parent chain
    • AsyncEmailTracingIntegrationTest: 부모 없음, 정상·예외 worker 재사용 누수 방지
  • 트레이드오프/대안:
    • 실제 HTTP 요청에서는 Spring Security/request observation이 HTTP SERVER span과 이메일 span 사이에 존재할 수 있다. 현재 활성화된 span을 건너뛰어 계층을 평탄화하지 않고 실제 실행 인과관계를 보존한다.
    • SMTP 시도별 span과 민감 로그 정리는 PR2로 분리해 이 PR에서는 이메일 작업 단위 Trace와 컨텍스트 경계만 다룬다.

@coderabbitai

coderabbitai Bot commented Aug 22, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: cb6c4a67-ec73-4f1b-be0e-1a2ce2154c1d

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

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.

@PHJ2000 PHJ2000 self-assigned this Aug 24, 2026
@PHJ2000
PHJ2000 marked this pull request as ready for review August 24, 2026 14:06

@geonusp geonusp left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

기능적으로 문제되는 부분은 없어 보이므로 승인합니다. 수고하셨습니다!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

suggestion: 현재 테스트들은 애플리케이션 내부 Trace만 검증하고 실제 Tempo와 연결되는 부분은 검증하지 않습니다. 실제 Tempo와 연결되는 테스트를 만드는 것이 어떨까 제안드립니다.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

좋은 제안 감사합니다. 이번 PR에서는 in-memory exporter를 활용해 HTTP 요청부터 AFTER_COMMIT 이후 비동기 이메일 작업까지 Trace 컨텍스트가 올바르게 전파되는지 검증하는 데 집중했습니다. 실제 Tempo 연동 및 조회 검증은 후속 PR2에서 SMTP 재시도 Trace까지 반영한 뒤, 이메일 전체 흐름을 대상으로 진행하겠습니다.

@geonusp
geonusp merged commit f155454 into main Aug 25, 2026
3 checks passed
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