Skip to content

Commit b51d88b

Browse files
authored
Merge pull request #172 from import-ai/refactor/trace
refactor(trace): add middleware
2 parents b170102 + a6c6d91 commit b51d88b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/app/app.module.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +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 { UserInterceptor } from 'omniboxd/interceptor/user.interceptor';
5152

5253
@Module({})
5354
export class AppModule implements NestModule {
@@ -72,6 +73,10 @@ export class AppModule implements NestModule {
7273
provide: APP_INTERCEPTOR,
7374
useClass: ClassSerializerInterceptor,
7475
},
76+
{
77+
provide: APP_INTERCEPTOR,
78+
useClass: UserInterceptor,
79+
},
7580
],
7681
imports: [
7782
ConfigModule.forRoot({
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {
2+
Injectable,
3+
NestInterceptor,
4+
ExecutionContext,
5+
CallHandler,
6+
} from '@nestjs/common';
7+
import { Observable } from 'rxjs';
8+
import { trace, context } from '@opentelemetry/api';
9+
10+
@Injectable()
11+
export class UserInterceptor implements NestInterceptor {
12+
intercept(
13+
executionContext: ExecutionContext,
14+
next: CallHandler,
15+
): Observable<any> {
16+
const req = executionContext.switchToHttp().getRequest();
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+
});
24+
return next.handle();
25+
}
26+
}

0 commit comments

Comments
 (0)