feat: RequestContext 기반 Trace ID / Correlation ID 도입 및 요청 로깅 개선 - #126
feat: RequestContext 기반 Trace ID / Correlation ID 도입 및 요청 로깅 개선#126labyrinth30 wants to merge 1 commit into
Conversation
- AsyncLocalStorage 기반 RequestContext 추가 (requestId, traceId, userId, authUid 보관) - HTTP 요청 헤더(x-request-id, x-correlation-id, x-cloud-trace-context, traceparent) 파싱 및 UUID fallback 지원 - LoggingMiddleware에서 RequestContext.run 실행 및 응답 헤더(x-request-id) 자동 주입 - 상태 코드별 로그 레벨 분기 (2xx/3xx log, 4xx warn, 5xx error) - AppModule Pino mixin에 RequestContext를 연결해 모든 로거에서 requestId/userId 자동 기록 - CurrentUserGuard, AuthUidGuard에서 인증 정보 RequestContext에 연동 - SentryErrorReporter에 request_id 및 user 태깅 지원으로 Cloud Logging-Sentry 간 추적성 확보
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (9)
Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review. 📝 WalkthroughWalkthrough
Changes요청 컨텍스트 기반 관측성
Estimated code review effort: 3 (Moderate) | ~25 minutes Merge Risk: 🔵 Low · up to Client-supplied request and trace identifiers are now echoed and propagated into logs and error reports without strict validation, which can allow misleading request correlation or excess telemetry cardinality. The PR is mergeable with explicit owner awareness and follow-up to validate identifiers and distinguish trusted upstream values from server-generated IDs. Sequence Diagram(s)sequenceDiagram
participant Client
participant LoggingMiddleware
participant RequestContext
participant AuthGuards
participant LoggerAndSentry
Client->>LoggingMiddleware: HTTP request with identity headers
LoggingMiddleware->>RequestContext: extractOrCreate(req)
LoggingMiddleware->>RequestContext: run({ requestId, traceId }, next)
LoggingMiddleware->>AuthGuards: execute authentication
AuthGuards->>RequestContext: setUserId(), setAuthUid()
LoggingMiddleware->>LoggerAndSentry: record request outcome
LoggerAndSentry->>RequestContext: read request and user identifiers
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
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. Comment |
📌 Description
HTTP 요청별 고유한 식별자(Trace ID / Correlation ID)를 비동기 컨텍스트로 전파하고, Pino 및 Sentry와 연동하여 로깅 및 장애 추적성을 개선합니다.
🚀 주요 변경 사항
RequestContext(AsyncLocalStorage 기반 컨텍스트 관리)src/common/context/request-context.ts: Node.js/Bun의AsyncLocalStorage를 활용하여 요청 수명주기 동안requestId,traceId,userId,authUid를 보관 및 비동기 전파.x-request-id,x-correlation-id,x-cloud-trace-context,traceparent)를 우선순위에 따라 파싱하고, 없으면crypto.randomUUID()로 자동 생성.LoggingMiddleware고도화RequestContext.run()안에서next()를 실행하여 하위 모든 비동기 흐름에 컨텍스트 전파.x-request-id를 자동 주입하여 클라이언트-서버 간 트레이싱 지원.2xx/3xx->log,4xx->warn,5xx->error).Pino 자동 Mixin 연결 (
AppModule)LoggerModule.forRootAsync의mixin에RequestContext.get()을 연결하여, 컨트롤러·서비스·필터 등 코드베이스 어디서든 로거를 호출해도 JSON 로그 최상위에requestId,traceId,userId가 자동 기록됨.인증 가드 연동 (
CurrentUserGuard,AuthUidGuard)RequestContext.setUserId()/setAuthUid()를 호출하여 인증 이후의 모든 로그에 유저 정보 바인딩.Sentry 연동 보강 (
SentryErrorReporter)request_id태그와user정보를 자동으로 주입하여, Sentry 이슈에서 확인한request_id로 Cloud Logging 전체 로그를 1:1로 검색 가능.✅ Done
RequestContext클래스 구현 및 단위 테스트 (7개 케이스)LoggingMiddleware컨텍스트 연동 / 응답 헤더 주입 / 로그 레벨 분기 및 테스트AppModulePino mixin 설정CurrentUserGuard,AuthUidGuard컨텍스트 연동SentryErrorReporterrequest_id 및 user 태깅 및 테스트Summary by CodeRabbit
신규 기능
개선