testing-amplication-opentelemetry-nestjs
Version:
Testing OpenTelemetry module for Nestjs with auto instrumentation and resource detection. Initially forked from https://github.com/overbit/opentelemetry-nestjs.git
52 lines (45 loc) • 1.71 kB
text/typescript
import { Injectable, Logger } from '@nestjs/common';
import { ModulesContainer } from '@nestjs/core';
import { BaseTraceInjector } from './BaseTraceInjector';
import { Injector } from './Injector';
import { SpanKind } from '@opentelemetry/api';
()
export class ControllerInjector extends BaseTraceInjector implements Injector {
private readonly loggerService = new Logger();
constructor(protected readonly modulesContainer: ModulesContainer) {
super(modulesContainer);
}
public inject() {
const controllers = this.getControllers();
for (const controller of controllers) {
const keys = this.metadataScanner.getAllMethodNames(
controller.metatype.prototype,
);
for (const key of keys) {
if (
!this.isDecorated(controller.metatype.prototype[key]) &&
!this.isAffected(controller.metatype.prototype[key]) &&
(this.isPath(controller.metatype.prototype[key]) ||
this.isMicroservice(controller.metatype.prototype[key]))
) {
const traceName = `Controller->${controller.name}.${controller.metatype.prototype[key].name}`;
const method = this.wrap(
controller.metatype.prototype[key],
traceName,
{
controller: controller.name,
method: controller.metatype.prototype[key].name,
},
SpanKind.SERVER,
);
this.reDecorate(controller.metatype.prototype[key], method);
controller.metatype.prototype[key] = method;
this.loggerService.log(
`Mapped ${controller.name}.${key}`,
this.constructor.name,
);
}
}
}
}
}