🐛 Bug Report: @amplication/opentelemetry-nestjs irrelevant spans appear in trace info when the same guard decorates multiple handlers
ssilve1989 opened this issue · comments
What happened?
Traces for an http endpoint inside a controller, that has a Guard
shows irrelevant spans for all other methods decorated with the same guard.
When hitting the following controller as curl http://localhost:8080/
@Controller()
export class AppController {
constructor(
private readonly appService: AppService,
private readonly logger: PinoLoggerService,
) {}
@Get()
@UseGuards(SampleGuard)
getHello(): string {
this.logger.log('Hello world');
return this.appService.getHello();
}
@Get('/metrics')
@UseGuards(SampleGuard)
getMetrics() {
return client.register.metrics();
}
@Get('foo')
@UseGuards(SampleGuard)
foo() {}
@Get('bar')
@UseGuards(SampleGuard)
bar() {}
}
there are spans for every method in the controller that is decorated with the same guards as the one we've hit.
What you expected to happen
I expect to only see span information related to the handler actually being invoked.
How to reproduce
- setup telemetry
- create a controller with multiple handlers that have the same guard
- hit a single one of those endpoints
- view trace information in relevant exporter platform
my entire setup can be found here: https://github.com/ssilve1989/opentelemetry-poc/tree/bug/redundant-guard-traces
Amplication version
No response
Environment
Node: v20.11.1
Docker: 25.0.5, build 5dc9bcc
Zipkin: v3.3.0 commit dfd8ee2
@amplication/opentelemetry-nestjs: 5.0.3
Are you willing to submit PR?
Yes I am willing to submit a PR!
After looking at the code, this seems to be because the properties of a controller are enumerated looking for guards to wrap, so it can end up wrapping the same guard multiple times.
This is not limited to a single controller either since all controllers are enumerated as well and when a guard is re-used across controllers the same behavior happens and now its incorrectly wrapped everywhere the guard is used.