Replies: 2 comments 1 reply
-
트래이스 로깅메서드 호출, fetch 요청, 쿼리 등을 각 요청에 대한 트래이스 로깅 1. TraceService로 Trace ID 관리// trace.service.ts
import { Injectable } from '@nestjs/common';
import { AsyncLocalStorage } from 'node:async_hooks';
import { v4 as uuidv4 } from 'uuid';
@Injectable()
export class TraceService {
private readonly asyncLocalStorage = new AsyncLocalStorage<Map<string, any>>();
run(callback: () => void) {
const store = new Map<string, any>();
store.set('traceId', uuidv4());
this.asyncLocalStorage.run(store, callback);
}
get traceId(): string | undefined {
const store = this.asyncLocalStorage.getStore();
return store ? store.get('traceId') : undefined;
}
} 2. NestJS의 글로벌 인터셉터 사용// trace.interceptor.ts
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';
import { TraceService } from './trace.service';
@Injectable()
export class TraceInterceptor implements NestInterceptor {
constructor(private readonly traceService: TraceService) {}
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return new Observable(observer => {
this.traceService.run(() => {
next.handle().subscribe({
next: (val) => observer.next(val),
error: (err) => observer.error(err),
complete: () => observer.complete(),
});
});
});
}
} 3.
|
Beta Was this translation helpful? Give feedback.
1 reply
-
메서드 호출 트레이싱 잘 안되서 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
AsyncLocalStorage
이용해 트래이스 ID 관리nestjs-pino
TypeORM 로깅에 적용
Beta Was this translation helpful? Give feedback.
All reactions