Skip to content

Commit a6c6d91

Browse files
committed
refactor(user): update interceptor
1 parent 0df3cc7 commit a6c6d91

File tree

3 files changed

+10
-22
lines changed

3 files changed

+10
-22
lines changed

src/app/app.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import { ScanResourceAttachments1755504936756 } from 'omniboxd/migrations/175550
4848
import { SharesAllResources1754471311959 } from 'omniboxd/migrations/1754471311959-shares-all-resources';
4949
import { ResourcesModule } from 'omniboxd/resources/resources.module';
5050
import { TraceModule } from 'omniboxd/trace/trace.module';
51-
import { TracingUserInterceptor } from 'omniboxd/interceptor/tracing-user.interceptor';
51+
import { UserInterceptor } from 'omniboxd/interceptor/user.interceptor';
5252

5353
@Module({})
5454
export class AppModule implements NestModule {
@@ -75,7 +75,7 @@ export class AppModule implements NestModule {
7575
},
7676
{
7777
provide: APP_INTERCEPTOR,
78-
useClass: TracingUserInterceptor,
78+
useClass: UserInterceptor,
7979
},
8080
],
8181
imports: [

src/interceptor/tracing-user.interceptor.ts renamed to src/interceptor/user.interceptor.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,24 @@ import {
33
NestInterceptor,
44
ExecutionContext,
55
CallHandler,
6-
Logger,
76
} from '@nestjs/common';
87
import { Observable } from 'rxjs';
98
import { trace, context } from '@opentelemetry/api';
109

1110
@Injectable()
12-
export class TracingUserInterceptor implements NestInterceptor {
13-
private readonly logger = new Logger(TracingUserInterceptor.name);
14-
11+
export class UserInterceptor implements NestInterceptor {
1512
intercept(
1613
executionContext: ExecutionContext,
1714
next: CallHandler,
1815
): Observable<any> {
1916
const req = executionContext.switchToHttp().getRequest();
20-
const span = trace.getSpan(context.active());
21-
if (req.user?.id && span) {
22-
span.setAttribute('user.id', req.user.id);
23-
}
24-
this.logger.log(`span ${span?.spanContext().spanId} user ${req.user?.id}`);
17+
const tracer = trace.getTracer('user-interceptor');
18+
tracer.startActiveSpan('UserInterceptor', {}, context.active(), (span) => {
19+
if (req.user?.id) {
20+
span.setAttribute('user.id', req.user.id);
21+
}
22+
span.end();
23+
});
2524
return next.handle();
2625
}
2726
}

src/tracing.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
88
import { NestInstrumentation } from '@opentelemetry/instrumentation-nestjs-core';
99
import { TypeormInstrumentation } from '@opentelemetry/instrumentation-typeorm';
1010
import { isEmpty } from 'omniboxd/utils/is-empty';
11-
import { Span } from '@opentelemetry/api';
12-
import { ServerResponse } from 'http';
1311

1412
const env = process.env.ENV || 'unknown';
1513
const serviceName = `omnibox-backend`;
@@ -45,15 +43,6 @@ if (isTracingEnabled()) {
4543
ignoreIncomingRequestHook: (req) => {
4644
return excludedUrls.some((url) => req.url?.includes(url)) || false;
4745
},
48-
responseHook: (span: Span, response: ServerResponse) => {
49-
const req = response.req as any;
50-
if (req.user?.id) {
51-
span.setAttribute('user.id', req.user.id);
52-
}
53-
console.log(
54-
`resp span ${span.spanContext().spanId} user ${req.user?.id}`,
55-
);
56-
},
5746
}),
5847
new ExpressInstrumentation(),
5948
new NestInstrumentation(),

0 commit comments

Comments
 (0)