@@ -55,19 +55,6 @@ import {
55
55
AwsLambdaInstrumentation ,
56
56
AwsLambdaInstrumentationConfig ,
57
57
} from '@opentelemetry/instrumentation-aws-lambda' ;
58
- import { DnsInstrumentation } from '@opentelemetry/instrumentation-dns' ;
59
- import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express' ;
60
- import { GraphQLInstrumentation } from '@opentelemetry/instrumentation-graphql' ;
61
- import { GrpcInstrumentation } from '@opentelemetry/instrumentation-grpc' ;
62
- import { HapiInstrumentation } from '@opentelemetry/instrumentation-hapi' ;
63
- import { HttpInstrumentation } from '@opentelemetry/instrumentation-http' ;
64
- import { IORedisInstrumentation } from '@opentelemetry/instrumentation-ioredis' ;
65
- import { KoaInstrumentation } from '@opentelemetry/instrumentation-koa' ;
66
- import { MongoDBInstrumentation } from '@opentelemetry/instrumentation-mongodb' ;
67
- import { MySQLInstrumentation } from '@opentelemetry/instrumentation-mysql' ;
68
- import { NetInstrumentation } from '@opentelemetry/instrumentation-net' ;
69
- import { PgInstrumentation } from '@opentelemetry/instrumentation-pg' ;
70
- import { RedisInstrumentation } from '@opentelemetry/instrumentation-redis' ;
71
58
import { AWSXRayPropagator } from '@opentelemetry/propagator-aws-xray' ;
72
59
import { AWSXRayLambdaPropagator } from '@opentelemetry/propagator-aws-xray-lambda' ;
73
60
@@ -137,54 +124,91 @@ function getActiveInstumentations(): Set<string> {
137
124
return instrumentationSet ;
138
125
}
139
126
140
- function defaultConfigureInstrumentations ( ) {
127
+ async function defaultConfigureInstrumentations ( ) {
141
128
const instrumentations = [ ] ;
142
129
const activeInstrumentations = getActiveInstumentations ( ) ;
143
- // Use require statements for instrumentation
144
- // to avoid having to have transitive dependencies on all the typescript definitions.
145
130
if ( activeInstrumentations . has ( 'dns' ) ) {
131
+ const { DnsInstrumentation } = await import (
132
+ '@opentelemetry/instrumentation-dns'
133
+ ) ;
146
134
instrumentations . push ( new DnsInstrumentation ( ) ) ;
147
135
}
148
136
if ( activeInstrumentations . has ( 'express' ) ) {
137
+ const { ExpressInstrumentation } = await import (
138
+ '@opentelemetry/instrumentation-express'
139
+ ) ;
149
140
instrumentations . push ( new ExpressInstrumentation ( ) ) ;
150
141
}
151
142
if ( activeInstrumentations . has ( 'graphql' ) ) {
143
+ const { GraphQLInstrumentation } = await import (
144
+ '@opentelemetry/instrumentation-graphql'
145
+ ) ;
152
146
instrumentations . push ( new GraphQLInstrumentation ( ) ) ;
153
147
}
154
148
if ( activeInstrumentations . has ( 'grpc' ) ) {
149
+ const { GrpcInstrumentation } = await import (
150
+ '@opentelemetry/instrumentation-grpc'
151
+ ) ;
155
152
instrumentations . push ( new GrpcInstrumentation ( ) ) ;
156
153
}
157
154
if ( activeInstrumentations . has ( 'hapi' ) ) {
155
+ const { HapiInstrumentation } = await import (
156
+ '@opentelemetry/instrumentation-hapi'
157
+ ) ;
158
158
instrumentations . push ( new HapiInstrumentation ( ) ) ;
159
159
}
160
160
if ( activeInstrumentations . has ( 'http' ) ) {
161
+ const { HttpInstrumentation } = await import (
162
+ '@opentelemetry/instrumentation-http'
163
+ ) ;
161
164
instrumentations . push ( new HttpInstrumentation ( ) ) ;
162
165
}
163
166
if ( activeInstrumentations . has ( 'ioredis' ) ) {
167
+ const { IORedisInstrumentation } = await import (
168
+ '@opentelemetry/instrumentation-ioredis'
169
+ ) ;
164
170
instrumentations . push ( new IORedisInstrumentation ( ) ) ;
165
171
}
166
172
if ( activeInstrumentations . has ( 'koa' ) ) {
173
+ const { KoaInstrumentation } = await import (
174
+ '@opentelemetry/instrumentation-koa'
175
+ ) ;
167
176
instrumentations . push ( new KoaInstrumentation ( ) ) ;
168
177
}
169
178
if ( activeInstrumentations . has ( 'mongodb' ) ) {
179
+ const { MongoDBInstrumentation } = await import (
180
+ '@opentelemetry/instrumentation-mongodb'
181
+ ) ;
170
182
instrumentations . push ( new MongoDBInstrumentation ( ) ) ;
171
183
}
172
184
if ( activeInstrumentations . has ( 'mysql' ) ) {
185
+ const { MySQLInstrumentation } = await import (
186
+ '@opentelemetry/instrumentation-mysql'
187
+ ) ;
173
188
instrumentations . push ( new MySQLInstrumentation ( ) ) ;
174
189
}
175
190
if ( activeInstrumentations . has ( 'net' ) ) {
191
+ const { NetInstrumentation } = await import (
192
+ '@opentelemetry/instrumentation-net'
193
+ ) ;
176
194
instrumentations . push ( new NetInstrumentation ( ) ) ;
177
195
}
178
196
if ( activeInstrumentations . has ( 'pg' ) ) {
197
+ const { PgInstrumentation } = await import (
198
+ '@opentelemetry/instrumentation-pg'
199
+ ) ;
179
200
instrumentations . push ( new PgInstrumentation ( ) ) ;
180
201
}
181
202
if ( activeInstrumentations . has ( 'redis' ) ) {
203
+ const { RedisInstrumentation } = await import (
204
+ '@opentelemetry/instrumentation-redis'
205
+ ) ;
182
206
instrumentations . push ( new RedisInstrumentation ( ) ) ;
183
207
}
184
208
return instrumentations ;
185
209
}
186
210
187
- function createInstrumentations ( ) {
211
+ async function createInstrumentations ( ) {
188
212
return [
189
213
new AwsInstrumentation (
190
214
typeof configureAwsInstrumentation === 'function'
@@ -197,8 +221,8 @@ function createInstrumentations() {
197
221
: { } ,
198
222
) ,
199
223
...( typeof configureInstrumentations === 'function'
200
- ? configureInstrumentations
201
- : defaultConfigureInstrumentations ) ( ) ,
224
+ ? configureInstrumentations ( )
225
+ : await defaultConfigureInstrumentations ( ) ) ,
202
226
] ;
203
227
}
204
228
@@ -240,7 +264,7 @@ function getPropagator(): TextMapPropagator {
240
264
return new CompositePropagator ( { propagators } ) ;
241
265
}
242
266
243
- function initializeProvider ( ) {
267
+ async function initializeProvider ( ) {
244
268
const resource = detectResourcesSync ( {
245
269
detectors : [ awsLambdaDetector , envDetector , processDetector ] ,
246
270
} ) ;
@@ -324,7 +348,7 @@ function initializeProvider() {
324
348
// to prevent additional coldstart overhead
325
349
// caused by creations and initializations of instrumentations.
326
350
if ( ! instrumentations || ! instrumentations . length ) {
327
- instrumentations = createInstrumentations ( ) ;
351
+ instrumentations = await createInstrumentations ( ) ;
328
352
}
329
353
330
354
// Re-register instrumentation with initialized provider. Patched code will see the update.
@@ -336,35 +360,53 @@ function initializeProvider() {
336
360
} ) ;
337
361
}
338
362
339
- export function wrap ( ) {
340
- initializeProvider ( ) ;
363
+ export async function wrap ( ) {
364
+ if ( ! initialized ) {
365
+ throw new Error ( 'Not initialized yet' ) ;
366
+ }
367
+
368
+ await initializeProvider ( ) ;
341
369
}
342
370
343
- export function unwrap ( ) {
371
+ export async function unwrap ( ) {
372
+ if ( ! initialized ) {
373
+ throw new Error ( 'Not initialized yet' ) ;
374
+ }
375
+
344
376
if ( disableInstrumentations ) {
345
377
disableInstrumentations ( ) ;
346
378
disableInstrumentations = ( ) => { } ;
347
379
}
348
380
instrumentations = [ ] ;
381
+
349
382
context . disable ( ) ;
350
383
propagation . disable ( ) ;
351
384
trace . disable ( ) ;
352
385
metrics . disable ( ) ;
353
386
logs . disable ( ) ;
354
387
}
355
388
389
+ export async function init ( ) {
390
+ if ( initialized ) {
391
+ return ;
392
+ }
393
+
394
+ instrumentations = await createInstrumentations ( ) ;
395
+
396
+ // Register instrumentations synchronously to ensure code is patched even before provider is ready.
397
+ disableInstrumentations = registerInstrumentations ( {
398
+ instrumentations,
399
+ } ) ;
400
+
401
+ initialized = true ;
402
+ }
403
+
356
404
console . log ( 'Registering OpenTelemetry' ) ;
357
405
406
+ let initialized = false ;
407
+ let instrumentations : Instrumentation [ ] ;
408
+ let disableInstrumentations : ( ) => void ;
409
+
358
410
// Configure lambda logging
359
411
const logLevel = getEnv ( ) . OTEL_LOG_LEVEL ;
360
412
diag . setLogger ( new DiagConsoleLogger ( ) , logLevel ) ;
361
-
362
- let instrumentations = createInstrumentations ( ) ;
363
- let disableInstrumentations : ( ) => void ;
364
-
365
- // Register instrumentations synchronously to ensure code is patched even before provider is ready.
366
- disableInstrumentations = registerInstrumentations ( {
367
- instrumentations,
368
- } ) ;
369
-
370
- wrap ( ) ;
0 commit comments