-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create telemetry interceptor for http requests
- Loading branch information
1 parent
4a50be5
commit 5010f91
Showing
7 changed files
with
58 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
CallHandler, | ||
ExecutionContext, | ||
Injectable, | ||
NestInterceptor, | ||
} from '@nestjs/common'; | ||
import { context, trace } from '@opentelemetry/api'; | ||
import { Observable } from 'rxjs'; | ||
import { tap } from 'rxjs/operators'; | ||
|
||
@Injectable() | ||
export class TelemetryInterceptor implements NestInterceptor { | ||
intercept(_context: ExecutionContext, next: CallHandler): Observable<any> { | ||
const request = _context.switchToHttp().getRequest(); | ||
const span = trace.getSpan(context.active()); | ||
|
||
if (span) { | ||
span.setAttribute('http.request_body', JSON.stringify(request.body)); | ||
span.setAttribute( | ||
'http.request_headers', | ||
JSON.stringify(request.headers), | ||
); | ||
} | ||
|
||
return next.handle().pipe( | ||
tap({ | ||
next: (data) => { | ||
if (span) { | ||
span.setAttribute('http.response_body', JSON.stringify(data)); | ||
} | ||
}, | ||
error: (error) => { | ||
if (span) { | ||
span.setAttribute('http.error', JSON.stringify(error)); | ||
} | ||
}, | ||
}), | ||
); | ||
} | ||
} |