Skip to content

Commit 339188a

Browse files
committed
test(trace): add span log
1 parent 15cdf6f commit 339188a

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

src/interceptor/tracing-user.interceptor.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,4 @@ describe('TracingUserInterceptor', () => {
7676

7777
expect(mockCallHandler.handle).toHaveBeenCalled();
7878
});
79-
});
79+
});

src/interceptor/tracing-user.interceptor.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,41 @@ import {
33
NestInterceptor,
44
ExecutionContext,
55
CallHandler,
6+
Logger,
67
} from '@nestjs/common';
78
import { Observable } from 'rxjs';
89
import { trace } from '@opentelemetry/api';
910

1011
@Injectable()
1112
export class TracingUserInterceptor implements NestInterceptor {
13+
private readonly logger = new Logger(TracingUserInterceptor.name);
14+
1215
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
1316
const request = context.switchToHttp().getRequest();
1417
const userId = request.user?.id;
1518
const activeSpan = trace.getActiveSpan();
16-
if (activeSpan && userId) {
17-
activeSpan.setAttribute('user.id', userId);
19+
20+
this.logger.log(
21+
`Interceptor called - userId: ${userId}, hasSpan: ${!!activeSpan}, url: ${request.url}`,
22+
);
23+
24+
if (activeSpan) {
25+
if (userId) {
26+
activeSpan.setAttribute('user.id', userId);
27+
this.logger.log(`Added user.id attribute to span: ${userId}`);
28+
} else {
29+
this.logger.warn('No userId found in request.user');
30+
}
31+
32+
// Log span details
33+
const spanContext = activeSpan.spanContext();
34+
this.logger.log(
35+
`Span context - traceId: ${spanContext.traceId}, spanId: ${spanContext.spanId}`,
36+
);
37+
} else {
38+
this.logger.warn('No active span found');
1839
}
40+
1941
return next.handle();
2042
}
2143
}
22-

src/middlewares/tracing-user.middleware.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,4 @@ describe('TracingUserMiddleware', () => {
6262

6363
expect(next).toHaveBeenCalled();
6464
});
65-
});
65+
});

0 commit comments

Comments
 (0)