-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathwrapper.ts
146 lines (133 loc) · 5.79 KB
/
wrapper.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { NodeTracerConfig, NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import {
BatchSpanProcessor,
ConsoleSpanExporter,
SDKRegistrationConfig,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import { Instrumentation, registerInstrumentations } from '@opentelemetry/instrumentation';
import { awsLambdaDetector } from '@opentelemetry/resource-detector-aws';
import {
detectResourcesSync,
envDetector,
processDetector,
} from '@opentelemetry/resources';
import { AwsInstrumentation, AwsSdkInstrumentationConfig } from '@opentelemetry/instrumentation-aws-sdk';
import { AwsLambdaInstrumentation } from '@opentelemetry/instrumentation-aws-lambda';
import {
diag,
DiagConsoleLogger,
DiagLogLevel,
} from "@opentelemetry/api";
import { getEnv } from '@opentelemetry/core';
import { AwsLambdaInstrumentationConfig } from '@opentelemetry/instrumentation-aws-lambda';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
import { MeterProvider, MeterProviderOptions } from '@opentelemetry/sdk-metrics';
function defaultConfigureInstrumentations() {
// Use require statements for instrumentation to avoid having to have transitive dependencies on all the typescript
// definitions.
const { DnsInstrumentation } = require('@opentelemetry/instrumentation-dns');
const { ExpressInstrumentation } = require('@opentelemetry/instrumentation-express');
const { GraphQLInstrumentation } = require('@opentelemetry/instrumentation-graphql');
const { GrpcInstrumentation } = require('@opentelemetry/instrumentation-grpc');
const { HapiInstrumentation } = require('@opentelemetry/instrumentation-hapi');
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
const { IORedisInstrumentation } = require('@opentelemetry/instrumentation-ioredis');
const { KoaInstrumentation } = require('@opentelemetry/instrumentation-koa');
const { MongoDBInstrumentation } = require('@opentelemetry/instrumentation-mongodb');
const { MySQLInstrumentation } = require('@opentelemetry/instrumentation-mysql');
const { NetInstrumentation } = require('@opentelemetry/instrumentation-net');
const { PgInstrumentation } = require('@opentelemetry/instrumentation-pg');
const { RedisInstrumentation } = require('@opentelemetry/instrumentation-redis');
return [ new DnsInstrumentation(),
new ExpressInstrumentation(),
new GraphQLInstrumentation(),
new GrpcInstrumentation(),
new HapiInstrumentation(),
new HttpInstrumentation(),
new IORedisInstrumentation(),
new KoaInstrumentation(),
new MongoDBInstrumentation(),
new MySQLInstrumentation(),
new NetInstrumentation(),
new PgInstrumentation(),
new RedisInstrumentation(),
]
}
declare global {
// in case of downstream configuring span processors etc
function configureAwsInstrumentation(defaultConfig: AwsSdkInstrumentationConfig): AwsSdkInstrumentationConfig
function configureTracerProvider(tracerProvider: NodeTracerProvider): void
function configureTracer(defaultConfig: NodeTracerConfig): NodeTracerConfig;
function configureSdkRegistration(
defaultSdkRegistration: SDKRegistrationConfig
): SDKRegistrationConfig;
function configureMeter(defaultConfig: MeterProviderOptions): MeterProviderOptions;
function configureMeterProvider(meterProvider: MeterProvider): void
function configureLambdaInstrumentation(config: AwsLambdaInstrumentationConfig): AwsLambdaInstrumentationConfig
function configureInstrumentations(): Instrumentation[]
}
console.log('Registering OpenTelemetry');
const instrumentations = [
new AwsInstrumentation(
typeof configureAwsInstrumentation === 'function'
? configureAwsInstrumentation({suppressInternalInstrumentation: true})
: {suppressInternalInstrumentation: true,},
),
new AwsLambdaInstrumentation(typeof configureLambdaInstrumentation === 'function' ? configureLambdaInstrumentation({}) : {}),
...(typeof configureInstrumentations === 'function' ? configureInstrumentations: defaultConfigureInstrumentations)()
];
// configure lambda logging
const logLevel = getEnv().OTEL_LOG_LEVEL
diag.setLogger(new DiagConsoleLogger(), logLevel)
// Register instrumentations synchronously to ensure code is patched even before provider is ready.
registerInstrumentations({
instrumentations,
});
async function initializeProvider() {
const resource = detectResourcesSync({
detectors: [awsLambdaDetector, envDetector, processDetector],
});
let config: NodeTracerConfig = {
resource,
};
if (typeof configureTracer === 'function') {
config = configureTracer(config);
}
const tracerProvider = new NodeTracerProvider(config);
if (typeof configureTracerProvider === 'function') {
configureTracerProvider(tracerProvider)
} else {
// defaults
tracerProvider.addSpanProcessor(
new BatchSpanProcessor(new OTLPTraceExporter())
);
}
// logging for debug
if (logLevel === DiagLogLevel.DEBUG) {
tracerProvider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
}
let sdkRegistrationConfig: SDKRegistrationConfig = {};
if (typeof configureSdkRegistration === 'function') {
sdkRegistrationConfig = configureSdkRegistration(sdkRegistrationConfig);
}
tracerProvider.register(sdkRegistrationConfig);
// Configure default meter provider (doesn't export metrics)
let meterConfig: MeterProviderOptions = {
resource,
}
if (typeof configureMeter === 'function') {
meterConfig = configureMeter(meterConfig);
}
const meterProvider = new MeterProvider(meterConfig);
if (typeof configureMeterProvider === 'function') {
configureMeterProvider(meterProvider)
}
// Re-register instrumentation with initialized provider. Patched code will see the update.
registerInstrumentations({
instrumentations,
tracerProvider,
meterProvider
});
}
initializeProvider();