Skip to content

Commit 506ebea

Browse files
authored
feat(nodejs): add ability to be able to enable/disable active instrumentations by env var (#1653)
* feat(nodejs): add ability to be able to enable/disable active instrumentations by env var
1 parent b317ebd commit 506ebea

File tree

1 file changed

+118
-50
lines changed

1 file changed

+118
-50
lines changed

nodejs/packages/layer/src/wrapper.ts

Lines changed: 118 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -53,56 +53,21 @@ import {
5353
AwsLambdaInstrumentationConfig,
5454
} from '@opentelemetry/instrumentation-aws-lambda';
5555

56-
function defaultConfigureInstrumentations() {
57-
// Use require statements for instrumentation
58-
// to avoid having to have transitive dependencies on all the typescript definitions.
59-
const { DnsInstrumentation } = require('@opentelemetry/instrumentation-dns');
60-
const {
61-
ExpressInstrumentation,
62-
} = require('@opentelemetry/instrumentation-express');
63-
const {
64-
GraphQLInstrumentation,
65-
} = require('@opentelemetry/instrumentation-graphql');
66-
const {
67-
GrpcInstrumentation,
68-
} = require('@opentelemetry/instrumentation-grpc');
69-
const {
70-
HapiInstrumentation,
71-
} = require('@opentelemetry/instrumentation-hapi');
72-
const {
73-
HttpInstrumentation,
74-
} = require('@opentelemetry/instrumentation-http');
75-
const {
76-
IORedisInstrumentation,
77-
} = require('@opentelemetry/instrumentation-ioredis');
78-
const { KoaInstrumentation } = require('@opentelemetry/instrumentation-koa');
79-
const {
80-
MongoDBInstrumentation,
81-
} = require('@opentelemetry/instrumentation-mongodb');
82-
const {
83-
MySQLInstrumentation,
84-
} = require('@opentelemetry/instrumentation-mysql');
85-
const { NetInstrumentation } = require('@opentelemetry/instrumentation-net');
86-
const { PgInstrumentation } = require('@opentelemetry/instrumentation-pg');
87-
const {
88-
RedisInstrumentation,
89-
} = require('@opentelemetry/instrumentation-redis');
90-
return [
91-
new DnsInstrumentation(),
92-
new ExpressInstrumentation(),
93-
new GraphQLInstrumentation(),
94-
new GrpcInstrumentation(),
95-
new HapiInstrumentation(),
96-
new HttpInstrumentation(),
97-
new IORedisInstrumentation(),
98-
new KoaInstrumentation(),
99-
new MongoDBInstrumentation(),
100-
new MySQLInstrumentation(),
101-
new NetInstrumentation(),
102-
new PgInstrumentation(),
103-
new RedisInstrumentation(),
104-
];
105-
}
56+
const defaultInstrumentationList = [
57+
'dns',
58+
'express',
59+
'graphql',
60+
'grpc',
61+
'hapi',
62+
'http',
63+
'ioredis',
64+
'koa',
65+
'mongodb',
66+
'mysql',
67+
'net',
68+
'pg',
69+
'redis',
70+
];
10671

10772
declare global {
10873
// In case of downstream configuring span processors etc
@@ -126,6 +91,109 @@ declare global {
12691
function configureInstrumentations(): Instrumentation[];
12792
}
12893

94+
function getActiveInstumentations(): Set<string> {
95+
let enabledInstrumentations: string[] = defaultInstrumentationList;
96+
if (process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS) {
97+
enabledInstrumentations =
98+
process.env.OTEL_NODE_ENABLED_INSTRUMENTATIONS.split(',').map(i =>
99+
i.trim(),
100+
);
101+
}
102+
const instrumentationSet = new Set<string>(enabledInstrumentations);
103+
if (process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS) {
104+
const disableInstrumentations =
105+
process.env.OTEL_NODE_DISABLED_INSTRUMENTATIONS.split(',').map(i =>
106+
i.trim(),
107+
);
108+
disableInstrumentations.forEach(di => instrumentationSet.delete(di));
109+
}
110+
return instrumentationSet;
111+
}
112+
113+
function defaultConfigureInstrumentations() {
114+
const instrumentations = [];
115+
const activeInstrumentations = getActiveInstumentations();
116+
// Use require statements for instrumentation
117+
// to avoid having to have transitive dependencies on all the typescript definitions.
118+
if (activeInstrumentations.has('dns')) {
119+
const {
120+
DnsInstrumentation,
121+
} = require('@opentelemetry/instrumentation-dns');
122+
instrumentations.push(new DnsInstrumentation());
123+
}
124+
if (activeInstrumentations.has('express')) {
125+
const {
126+
ExpressInstrumentation,
127+
} = require('@opentelemetry/instrumentation-express');
128+
instrumentations.push(new ExpressInstrumentation());
129+
}
130+
if (activeInstrumentations.has('graphql')) {
131+
const {
132+
GraphQLInstrumentation,
133+
} = require('@opentelemetry/instrumentation-graphql');
134+
instrumentations.push(new GraphQLInstrumentation());
135+
}
136+
if (activeInstrumentations.has('grpc')) {
137+
const {
138+
GrpcInstrumentation,
139+
} = require('@opentelemetry/instrumentation-grpc');
140+
instrumentations.push(new GrpcInstrumentation());
141+
}
142+
if (activeInstrumentations.has('hapi')) {
143+
const {
144+
HapiInstrumentation,
145+
} = require('@opentelemetry/instrumentation-hapi');
146+
instrumentations.push(new HapiInstrumentation());
147+
}
148+
if (activeInstrumentations.has('http')) {
149+
const {
150+
HttpInstrumentation,
151+
} = require('@opentelemetry/instrumentation-http');
152+
instrumentations.push(new HttpInstrumentation());
153+
}
154+
if (activeInstrumentations.has('ioredis')) {
155+
const {
156+
IORedisInstrumentation,
157+
} = require('@opentelemetry/instrumentation-ioredis');
158+
instrumentations.push(new IORedisInstrumentation());
159+
}
160+
if (activeInstrumentations.has('koa')) {
161+
const {
162+
KoaInstrumentation,
163+
} = require('@opentelemetry/instrumentation-koa');
164+
instrumentations.push(new KoaInstrumentation());
165+
}
166+
if (activeInstrumentations.has('mongodb')) {
167+
const {
168+
MongoDBInstrumentation,
169+
} = require('@opentelemetry/instrumentation-mongodb');
170+
instrumentations.push(new MongoDBInstrumentation());
171+
}
172+
if (activeInstrumentations.has('mysql')) {
173+
const {
174+
MySQLInstrumentation,
175+
} = require('@opentelemetry/instrumentation-mysql');
176+
instrumentations.push(new MySQLInstrumentation());
177+
}
178+
if (activeInstrumentations.has('net')) {
179+
const {
180+
NetInstrumentation,
181+
} = require('@opentelemetry/instrumentation-net');
182+
instrumentations.push(new NetInstrumentation());
183+
}
184+
if (activeInstrumentations.has('pg')) {
185+
const { PgInstrumentation } = require('@opentelemetry/instrumentation-pg');
186+
instrumentations.push(new PgInstrumentation());
187+
}
188+
if (activeInstrumentations.has('redis')) {
189+
const {
190+
RedisInstrumentation,
191+
} = require('@opentelemetry/instrumentation-redis');
192+
instrumentations.push(new RedisInstrumentation());
193+
}
194+
return instrumentations;
195+
}
196+
129197
function createInstrumentations() {
130198
return [
131199
new AwsInstrumentation(

0 commit comments

Comments
 (0)