-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
132 lines (120 loc) · 3.59 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* eslint-disable @typescript-eslint/ban-ts-comment */
import {
DynamicModule,
MiddlewareConsumer,
Module,
NestModule,
} from '@nestjs/common';
import 'reflect-metadata';
import { context, Span, SpanStatusCode, trace } from '@opentelemetry/api';
import {
Constants,
ControllerInjector,
EventEmitterInjector,
GuardInjector,
LoggerInjector,
OpenTelemetryModule,
OpenTelemetryModuleConfig,
PipeInjector,
ScheduleInjector,
} from '@metinseylan/nestjs-opentelemetry';
export { Span, TraceService } from '@metinseylan/nestjs-opentelemetry';
import { ExporterConfig, JaegerExporter } from '@opentelemetry/exporter-jaeger';
import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
class ControllerInjectorWithError extends ControllerInjector {
protected wrap(prototype: Record<any, any>, traceName, attributes = {}) {
const method = {
[prototype.name]: function (...args: any[]) {
const tracer = trace.getTracer('default');
const currentSpan = tracer.startSpan(traceName);
return context.with(
trace.setSpan(context.active(), currentSpan),
() => {
currentSpan.setAttributes(attributes);
if (prototype.constructor.name === 'AsyncFunction') {
return prototype
.apply(this, args)
.catch((error) =>
ControllerInjectorWithError.recordException(
error,
currentSpan,
),
)
.finally(() => {
currentSpan.end();
});
} else {
try {
const result = prototype.apply(this, args);
currentSpan.end();
return result;
} catch (error) {
ControllerInjectorWithError.recordException(error, currentSpan);
} finally {
currentSpan.end();
}
}
},
);
},
}[prototype.name];
Reflect.defineMetadata(Constants.TRACE_METADATA, traceName, method);
this.affect(method);
this.reDecorate(prototype, method);
return method;
}
protected static recordException(error, span: Span) {
span.recordException(error);
span.setStatus({ code: SpanStatusCode.ERROR, message: error.message });
span.setAttributes({
track_error: true,
error_type: error.constructor?.name || error.name,
error_message: error.message,
});
throw error;
}
}
@Module({})
export class OpenTelemetrySetupModule
extends OpenTelemetryModule
implements NestModule
{
static async forRoot(
configuration: Partial<OpenTelemetryModuleConfig> = {},
): Promise<DynamicModule> {
const forRoot = await super.forRoot({
...configuration,
traceAutoInjectors: this.traceAutoInjectors(),
// @ts-ignore
spanProcessor: new SimpleSpanProcessor(
new JaegerExporter(this.jaegerExporterOptions()),
),
});
return {
...forRoot,
module: OpenTelemetrySetupModule,
};
}
configure(consumer: MiddlewareConsumer) {
consumer
.apply((req, res, next) => {
const spanContext = trace.getSpan(context.active())?.spanContext();
res.set('x-traceid', spanContext?.traceId || 'unknown');
next();
})
.forRoutes('*');
}
static traceAutoInjectors(): Array<any> {
return [
ControllerInjectorWithError,
GuardInjector,
EventEmitterInjector,
ScheduleInjector,
PipeInjector,
LoggerInjector,
];
}
static jaegerExporterOptions(): ExporterConfig {
return {};
}
}