opentelemetry-esbuild-plugin-node 3.0.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -106
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/{build/src → dist}/index.js +0 -1
- package/dist/plugin.d.ts +4 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +122 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -0
- package/dist/types.d.ts +12 -0
- package/dist/types.d.ts.map +1 -0
- package/{build/src/config → dist}/types.js +0 -1
- package/eslint.config.mjs +23 -0
- package/package.json +29 -63
- package/{build/src/types.js → src/index.ts} +3 -3
- package/src/plugin.ts +167 -0
- package/{build/src/config/index.js → src/types.ts} +15 -6
- package/tsconfig.json +13 -0
- package/tsconfig.lib.json +17 -0
- package/LICENSE +0 -201
- package/build/src/common.d.ts +0 -3
- package/build/src/common.js +0 -64
- package/build/src/common.js.map +0 -1
- package/build/src/config/index.d.ts +0 -2
- package/build/src/config/index.js.map +0 -1
- package/build/src/config/main.d.ts +0 -13
- package/build/src/config/main.js +0 -55
- package/build/src/config/main.js.map +0 -1
- package/build/src/config/types.d.ts +0 -7
- package/build/src/config/types.js.map +0 -1
- package/build/src/index.d.ts +0 -3
- package/build/src/index.js.map +0 -1
- package/build/src/plugin.d.ts +0 -4
- package/build/src/plugin.js +0 -228
- package/build/src/plugin.js.map +0 -1
- package/build/src/types.d.ts +0 -90
- package/build/src/types.js.map +0 -1
package/build/src/config/main.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* Copyright The Authors
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
17
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.getOtelPackageToInstrumentationConfig = getOtelPackageToInstrumentationConfig;
|
|
19
|
-
const instrumentation_1 = require("@opentelemetry/instrumentation");
|
|
20
|
-
function getModuleDefinitions(instrumentation) {
|
|
21
|
-
var _a;
|
|
22
|
-
if (instrumentation instanceof instrumentation_1.InstrumentationBase) {
|
|
23
|
-
return (_a = instrumentation.getModuleDefinitions()) !== null && _a !== void 0 ? _a : [];
|
|
24
|
-
}
|
|
25
|
-
return [];
|
|
26
|
-
}
|
|
27
|
-
function configGenerator(config) {
|
|
28
|
-
if (!config)
|
|
29
|
-
return;
|
|
30
|
-
return JSON.stringify(Object.fromEntries(Object.entries(config).filter(([, v]) => typeof v !== 'function')));
|
|
31
|
-
}
|
|
32
|
-
function getOtelPackageToInstrumentationConfig(instrumentations) {
|
|
33
|
-
const instrumentationModuleDefinitionsByInstrumentationName = Object.fromEntries(instrumentations.map(i => [
|
|
34
|
-
i.instrumentationName,
|
|
35
|
-
getModuleDefinitions(i),
|
|
36
|
-
]));
|
|
37
|
-
const instrumentationModuleDefinitions = Object.values(instrumentationModuleDefinitionsByInstrumentationName).flat();
|
|
38
|
-
const otelPackageToInstrumentationConfig = {};
|
|
39
|
-
for (const instrumentation of instrumentations) {
|
|
40
|
-
const moduleDefinitions = instrumentationModuleDefinitionsByInstrumentationName[instrumentation.instrumentationName];
|
|
41
|
-
for (const instrumentationModuleDefinition of moduleDefinitions) {
|
|
42
|
-
otelPackageToInstrumentationConfig[instrumentationModuleDefinition.name] =
|
|
43
|
-
{
|
|
44
|
-
oTelInstrumentationPackage: instrumentation.instrumentationName,
|
|
45
|
-
oTelInstrumentationClass: instrumentation.constructor.name,
|
|
46
|
-
configGenerator,
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return {
|
|
51
|
-
otelPackageToInstrumentationConfig,
|
|
52
|
-
instrumentationModuleDefinitions,
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
//# sourceMappingURL=main.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"main.js","sourceRoot":"","sources":["../../../src/config/main.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;AA+BH,sFA6CC;AA1ED,oEAIwC;AAIxC,SAAS,oBAAoB,CAC3B,eAAgC;;IAEhC,IAAI,eAAe,YAAY,qCAAmB,EAAE,CAAC;QACnD,OAAO,MAAA,eAAe,CAAC,oBAAoB,EAAE,mCAAI,EAAE,CAAC;IACtD,CAAC;IAED,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,SAAS,eAAe,CACtB,MAAU;IAEV,IAAI,CAAC,MAAM;QAAE,OAAO;IACpB,OAAO,IAAI,CAAC,SAAS,CACnB,MAAM,CAAC,WAAW,CAChB,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,UAAU,CAAC,CAClE,CACF,CAAC;AACJ,CAAC;AAED,SAAgB,qCAAqC,CACnD,gBAAmC;IAEnC,MAAM,qDAAqD,GACzD,MAAM,CAAC,WAAW,CAChB,gBAAgB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;QACxB,CAAC,CAAC,mBAAmB;QACrB,oBAAoB,CAAC,CAAC,CAAC;KACxB,CAAC,CACH,CAAC;IAEJ,MAAM,gCAAgC,GAAG,MAAM,CAAC,MAAM,CACpD,qDAAqD,CACtD,CAAC,IAAI,EAAE,CAAC;IAET,MAAM,kCAAkC,GASpC,EAAE,CAAC;IACP,KAAK,MAAM,eAAe,IAAI,gBAAgB,EAAE,CAAC;QAC/C,MAAM,iBAAiB,GACrB,qDAAqD,CACnD,eAAe,CAAC,mBAAmB,CACpC,CAAC;QAEJ,KAAK,MAAM,+BAA+B,IAAI,iBAAiB,EAAE,CAAC;YAChE,kCAAkC,CAAC,+BAA+B,CAAC,IAAI,CAAC;gBACtE;oBACE,0BAA0B,EACxB,eAAe,CAAC,mBAA4D;oBAC9E,wBAAwB,EAAE,eAAe,CAAC,WAAW,CAAC,IAAI;oBAC1D,eAAe;iBAChB,CAAC;QACN,CAAC;IACH,CAAC;IACD,OAAO;QACL,kCAAkC;QAClC,gCAAgC;KACjC,CAAC;AACJ,CAAC","sourcesContent":["/*\n * Copyright The Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n Instrumentation,\n InstrumentationBase,\n InstrumentationModuleDefinition,\n} from '@opentelemetry/instrumentation';\n\nimport { EsbuildInstrumentationConfigMap } from '../types';\n\nfunction getModuleDefinitions(\n instrumentation: Instrumentation\n): InstrumentationModuleDefinition[] {\n if (instrumentation instanceof InstrumentationBase) {\n return instrumentation.getModuleDefinitions() ?? [];\n }\n\n return [];\n}\n\nfunction configGenerator<T extends { enabled?: boolean }>(\n config?: T\n): string | undefined {\n if (!config) return;\n return JSON.stringify(\n Object.fromEntries(\n Object.entries(config).filter(([, v]) => typeof v !== 'function')\n )\n );\n}\n\nexport function getOtelPackageToInstrumentationConfig(\n instrumentations: Instrumentation[]\n) {\n const instrumentationModuleDefinitionsByInstrumentationName =\n Object.fromEntries(\n instrumentations.map(i => [\n i.instrumentationName,\n getModuleDefinitions(i),\n ])\n );\n\n const instrumentationModuleDefinitions = Object.values(\n instrumentationModuleDefinitionsByInstrumentationName\n ).flat();\n\n const otelPackageToInstrumentationConfig: Record<\n string,\n {\n oTelInstrumentationPackage: keyof EsbuildInstrumentationConfigMap;\n oTelInstrumentationClass: string;\n configGenerator: <T extends { enabled?: boolean }>(\n config?: T\n ) => string | undefined;\n }\n > = {};\n for (const instrumentation of instrumentations) {\n const moduleDefinitions =\n instrumentationModuleDefinitionsByInstrumentationName[\n instrumentation.instrumentationName\n ];\n\n for (const instrumentationModuleDefinition of moduleDefinitions) {\n otelPackageToInstrumentationConfig[instrumentationModuleDefinition.name] =\n {\n oTelInstrumentationPackage:\n instrumentation.instrumentationName as keyof EsbuildInstrumentationConfigMap,\n oTelInstrumentationClass: instrumentation.constructor.name,\n configGenerator,\n };\n }\n }\n return {\n otelPackageToInstrumentationConfig,\n instrumentationModuleDefinitions,\n };\n}\n"]}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { EsbuildInstrumentationConfigMap } from '../types';
|
|
2
|
-
export interface InstrumentationConfig<T extends keyof EsbuildInstrumentationConfigMap> {
|
|
3
|
-
oTelInstrumentationPackage: T;
|
|
4
|
-
oTelInstrumentationClass: string;
|
|
5
|
-
configGenerator: (config?: EsbuildInstrumentationConfigMap[T]) => string | undefined;
|
|
6
|
-
}
|
|
7
|
-
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/config/types.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG","sourcesContent":["/*\n * Copyright The Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { EsbuildInstrumentationConfigMap } from '../types';\n\nexport interface InstrumentationConfig<\n T extends keyof EsbuildInstrumentationConfigMap,\n> {\n oTelInstrumentationPackage: T;\n oTelInstrumentationClass: string;\n configGenerator: (\n config?: EsbuildInstrumentationConfigMap[T]\n ) => string | undefined;\n}\n"]}
|
package/build/src/index.d.ts
DELETED
package/build/src/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,mCAA+C;AAAtC,6GAAA,mBAAmB,OAAA","sourcesContent":["/*\n * Copyright The Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nexport { openTelemetryPlugin } from './plugin';\nexport { OpenTelemetryPluginParams } from './types';\n"]}
|
package/build/src/plugin.d.ts
DELETED
package/build/src/plugin.js
DELETED
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* Copyright The Authors
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
17
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.openTelemetryPlugin = openTelemetryPlugin;
|
|
19
|
-
const path_1 = require("path");
|
|
20
|
-
const module_1 = require("module");
|
|
21
|
-
const promises_1 = require("fs/promises");
|
|
22
|
-
const semver_1 = require("semver");
|
|
23
|
-
const common_1 = require("./common");
|
|
24
|
-
const auto_instrumentations_node_1 = require("@opentelemetry/auto-instrumentations-node");
|
|
25
|
-
const config_1 = require("./config");
|
|
26
|
-
const NODE_MODULES = 'node_modules/';
|
|
27
|
-
const BUILT_INS = new Set(module_1.builtinModules.flatMap(b => [b, `node:${b}`]));
|
|
28
|
-
function validateConfig(pluginConfig) {
|
|
29
|
-
if (!pluginConfig)
|
|
30
|
-
return;
|
|
31
|
-
if (pluginConfig.instrumentationConfig && pluginConfig.instrumentations) {
|
|
32
|
-
throw new Error('OpenTelemetryPluginParams and instrumentations must not be used together');
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
function openTelemetryPlugin(pluginConfig) {
|
|
36
|
-
var _a;
|
|
37
|
-
validateConfig(pluginConfig);
|
|
38
|
-
const { otelPackageToInstrumentationConfig, instrumentationModuleDefinitions, } = (0, config_1.getOtelPackageToInstrumentationConfig)((_a = pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.instrumentations) !== null && _a !== void 0 ? _a : (0, auto_instrumentations_node_1.getNodeAutoInstrumentations)());
|
|
39
|
-
return {
|
|
40
|
-
name: 'open-telemetry',
|
|
41
|
-
setup(build) {
|
|
42
|
-
build.onResolve({ filter: /.*/ }, async (args) => {
|
|
43
|
-
if (shouldIgnoreModule({
|
|
44
|
-
namespace: args.namespace,
|
|
45
|
-
path: args.path,
|
|
46
|
-
importer: args.importer,
|
|
47
|
-
externalModules: pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.externalModules,
|
|
48
|
-
pathPrefixesToIgnore: pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.pathPrefixesToIgnore,
|
|
49
|
-
})) {
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
let path;
|
|
53
|
-
let extractedModule;
|
|
54
|
-
try {
|
|
55
|
-
const result = extractPackageAndModulePath(args.path, args.resolveDir);
|
|
56
|
-
path = result.path;
|
|
57
|
-
extractedModule = result.extractedModule;
|
|
58
|
-
}
|
|
59
|
-
catch (_a) {
|
|
60
|
-
// Some libraries like `mongodb` require optional dependencies, which may not be present and their absence doesn't break the code
|
|
61
|
-
// Currently esbuild doesn't provide any better way to handle this in plugins: https://github.com/evanw/esbuild/issues/1127
|
|
62
|
-
}
|
|
63
|
-
// If it's a local import, don't patch it
|
|
64
|
-
if (!extractedModule)
|
|
65
|
-
return;
|
|
66
|
-
// We'll rely on the OTel auto-instrumentation at runtime to patch builtin modules
|
|
67
|
-
if (isBuiltIn(args.path, extractedModule))
|
|
68
|
-
return;
|
|
69
|
-
const moduleVersion = await getModuleVersion({
|
|
70
|
-
extractedModule,
|
|
71
|
-
resolveDir: args.resolveDir,
|
|
72
|
-
build,
|
|
73
|
-
});
|
|
74
|
-
if (!moduleVersion)
|
|
75
|
-
return;
|
|
76
|
-
// See if we have an instrumentation registered for this package
|
|
77
|
-
const matchingInstrumentation = getInstrumentation({
|
|
78
|
-
instrumentationModuleDefinitions,
|
|
79
|
-
extractedModule,
|
|
80
|
-
moduleVersion,
|
|
81
|
-
path: args.path,
|
|
82
|
-
});
|
|
83
|
-
if (!matchingInstrumentation)
|
|
84
|
-
return;
|
|
85
|
-
const pluginData = {
|
|
86
|
-
extractedModule,
|
|
87
|
-
moduleVersion,
|
|
88
|
-
shouldPatchPackage: true,
|
|
89
|
-
instrumentationName: matchingInstrumentation.name,
|
|
90
|
-
};
|
|
91
|
-
return { path, pluginData };
|
|
92
|
-
});
|
|
93
|
-
build.onLoad({ filter: /.*/ }, async ({ path, pluginData }) => {
|
|
94
|
-
// Ignore any packages that don't have an instrumentation registered for them
|
|
95
|
-
if (!(pluginData === null || pluginData === void 0 ? void 0 : pluginData.shouldPatchPackage))
|
|
96
|
-
return;
|
|
97
|
-
const contents = await (0, promises_1.readFile)(path);
|
|
98
|
-
const config = otelPackageToInstrumentationConfig[pluginData.instrumentationName];
|
|
99
|
-
if (!config)
|
|
100
|
-
return;
|
|
101
|
-
const packageConfig = getPackageConfig({
|
|
102
|
-
pluginConfig,
|
|
103
|
-
oTelInstrumentationPackage: config.oTelInstrumentationPackage,
|
|
104
|
-
});
|
|
105
|
-
const extractedModule = pluginData.extractedModule;
|
|
106
|
-
return {
|
|
107
|
-
contents: (0, common_1.wrapModule)(contents.toString(), {
|
|
108
|
-
path: (0, path_1.join)(extractedModule.package || '', extractedModule.path || ''),
|
|
109
|
-
moduleVersion: pluginData.moduleVersion,
|
|
110
|
-
instrumentationName: pluginData.instrumentationName,
|
|
111
|
-
oTelInstrumentationClass: config.oTelInstrumentationClass,
|
|
112
|
-
oTelInstrumentationPackage: config.oTelInstrumentationPackage,
|
|
113
|
-
oTelInstrumentationConstructorArgs: config.configGenerator(packageConfig),
|
|
114
|
-
}),
|
|
115
|
-
resolveDir: (0, path_1.dirname)(path),
|
|
116
|
-
};
|
|
117
|
-
});
|
|
118
|
-
},
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
function getPackageConfig({ pluginConfig, oTelInstrumentationPackage, }) {
|
|
122
|
-
var _a;
|
|
123
|
-
if (!pluginConfig)
|
|
124
|
-
return;
|
|
125
|
-
if (pluginConfig.instrumentations) {
|
|
126
|
-
const matchingPlugin = pluginConfig.instrumentations.find(i => i.instrumentationName === oTelInstrumentationPackage);
|
|
127
|
-
if (!matchingPlugin) {
|
|
128
|
-
throw new Error(`Instrumentation ${oTelInstrumentationPackage} was found but does not exist in list of instrumentations`);
|
|
129
|
-
}
|
|
130
|
-
return matchingPlugin.getConfig();
|
|
131
|
-
}
|
|
132
|
-
return (_a = pluginConfig.instrumentationConfig) === null || _a === void 0 ? void 0 : _a[oTelInstrumentationPackage];
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* For a given full path to a module,
|
|
136
|
-
* return the package name it belongs to and the local path to the module
|
|
137
|
-
* input: '/foo/node_modules/@co/stuff/foo/bar/baz.js'
|
|
138
|
-
* output: { package: '@co/stuff', path: 'foo/bar/baz.js' }
|
|
139
|
-
*/
|
|
140
|
-
function extractPackageAndModulePath(originalPath, resolveDir) {
|
|
141
|
-
// @see https://github.com/nodejs/node/issues/47000
|
|
142
|
-
const path = require.resolve(originalPath === '.' ? './' : originalPath === '..' ? '../' : originalPath, { paths: [resolveDir] });
|
|
143
|
-
const nodeModulesIndex = path.lastIndexOf(NODE_MODULES);
|
|
144
|
-
if (nodeModulesIndex < 0)
|
|
145
|
-
return { path, extractedModule: null };
|
|
146
|
-
const subPath = path.substring(nodeModulesIndex + NODE_MODULES.length);
|
|
147
|
-
const firstSlashIndex = subPath.indexOf('/');
|
|
148
|
-
if (!subPath.startsWith('@')) {
|
|
149
|
-
return {
|
|
150
|
-
path,
|
|
151
|
-
extractedModule: {
|
|
152
|
-
package: subPath.substring(0, firstSlashIndex),
|
|
153
|
-
path: subPath.substring(firstSlashIndex + 1),
|
|
154
|
-
},
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
const secondSlash = subPath.substring(firstSlashIndex + 1).indexOf('/');
|
|
158
|
-
return {
|
|
159
|
-
path,
|
|
160
|
-
extractedModule: {
|
|
161
|
-
package: subPath.substring(0, firstSlashIndex + secondSlash + 1),
|
|
162
|
-
path: subPath.substring(firstSlashIndex + secondSlash + 2),
|
|
163
|
-
},
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
function shouldIgnoreModule({ namespace, path, importer, externalModules, pathPrefixesToIgnore, }) {
|
|
167
|
-
// If onLoad is being triggered from another plugin, ignore it
|
|
168
|
-
if (namespace !== 'file')
|
|
169
|
-
return true;
|
|
170
|
-
// If it's a local import from our code, ignore it
|
|
171
|
-
if (!importer.includes(NODE_MODULES) && path.startsWith('.'))
|
|
172
|
-
return true;
|
|
173
|
-
// If it starts with a prefix to ignore, ignore it
|
|
174
|
-
if (pathPrefixesToIgnore === null || pathPrefixesToIgnore === void 0 ? void 0 : pathPrefixesToIgnore.some(prefix => path.startsWith(prefix))) {
|
|
175
|
-
return true;
|
|
176
|
-
}
|
|
177
|
-
// If it's marked as external, ignore it
|
|
178
|
-
if (externalModules === null || externalModules === void 0 ? void 0 : externalModules.includes(path))
|
|
179
|
-
return true;
|
|
180
|
-
return false;
|
|
181
|
-
}
|
|
182
|
-
function isBuiltIn(path, extractedModule) {
|
|
183
|
-
return (BUILT_INS.has(path) ||
|
|
184
|
-
BUILT_INS.has(`${extractedModule.package}/${extractedModule.path}`));
|
|
185
|
-
}
|
|
186
|
-
const moduleVersionByPackageJsonPath = new Map();
|
|
187
|
-
async function getModuleVersion({ extractedModule, resolveDir, build, }) {
|
|
188
|
-
const path = `${extractedModule.package}/package.json`;
|
|
189
|
-
const contents = moduleVersionByPackageJsonPath.get(path);
|
|
190
|
-
if (contents)
|
|
191
|
-
return contents;
|
|
192
|
-
const { path: packageJsonPath } = await build.resolve(path, {
|
|
193
|
-
resolveDir,
|
|
194
|
-
kind: 'require-resolve',
|
|
195
|
-
});
|
|
196
|
-
if (!packageJsonPath)
|
|
197
|
-
return;
|
|
198
|
-
const packageJsonContents = await (0, promises_1.readFile)(packageJsonPath);
|
|
199
|
-
const moduleVersion = JSON.parse(packageJsonContents.toString()).version;
|
|
200
|
-
moduleVersionByPackageJsonPath.set(path, moduleVersion);
|
|
201
|
-
return moduleVersion;
|
|
202
|
-
}
|
|
203
|
-
function getInstrumentation({ instrumentationModuleDefinitions, extractedModule, path, moduleVersion, }) {
|
|
204
|
-
for (const instrumentationModuleDefinition of instrumentationModuleDefinitions) {
|
|
205
|
-
const fullModulePath = `${extractedModule.package}/${extractedModule.path}`;
|
|
206
|
-
const nameMatches = instrumentationModuleDefinition.name === path ||
|
|
207
|
-
instrumentationModuleDefinition.name === fullModulePath;
|
|
208
|
-
if (!nameMatches) {
|
|
209
|
-
const fileMatch = instrumentationModuleDefinition.files.find(file => {
|
|
210
|
-
return file.name === path || file.name === fullModulePath;
|
|
211
|
-
});
|
|
212
|
-
if (!fileMatch)
|
|
213
|
-
continue;
|
|
214
|
-
}
|
|
215
|
-
if (instrumentationModuleDefinition.supportedVersions.some(supportedVersion => (0, semver_1.satisfies)(moduleVersion, supportedVersion))) {
|
|
216
|
-
return instrumentationModuleDefinition;
|
|
217
|
-
}
|
|
218
|
-
if (instrumentationModuleDefinition.files.some(file => {
|
|
219
|
-
if (file.name !== path && file.name !== fullModulePath)
|
|
220
|
-
return false;
|
|
221
|
-
return file.supportedVersions.some(supportedVersion => (0, semver_1.satisfies)(moduleVersion, supportedVersion));
|
|
222
|
-
})) {
|
|
223
|
-
return instrumentationModuleDefinition;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
return null;
|
|
227
|
-
}
|
|
228
|
-
//# sourceMappingURL=plugin.js.map
|
package/build/src/plugin.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;AAiCH,kDAgHC;AAvID,+BAAqC;AAGrC,mCAAwC;AACxC,0CAAuC;AACvC,mCAAmC;AACnC,qCAAsC;AACtC,0FAAwF;AACxF,qCAAiE;AAEjE,MAAM,YAAY,GAAG,eAAe,CAAC;AAErC,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,uBAAc,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAEzE,SAAS,cAAc,CAAC,YAAwC;IAC9D,IAAI,CA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IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,gBAAgB,CAAC,EAAE,CACpD,IAAA,kBAAS,EAAC,aAAa,EAAE,gBAAgB,CAAC,CAC3C,CAAC;QACJ,CAAC,CAAC,EACF,CAAC;YACD,OAAO,+BAA+B,CAAC;QACzC,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["/*\n * Copyright The Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n EsbuildInstrumentationConfigMap,\n ExtractedModule,\n OnLoadArgs,\n OpenTelemetryPluginParams,\n PluginData,\n} from './types';\nimport { Plugin, PluginBuild } from 'esbuild';\nimport { dirname, join } from 'path';\n\nimport { InstrumentationModuleDefinition } from '@opentelemetry/instrumentation';\nimport { builtinModules } from 'module';\nimport { readFile } from 'fs/promises';\nimport { satisfies } from 'semver';\nimport { wrapModule } from './common';\nimport { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';\nimport { getOtelPackageToInstrumentationConfig } from './config';\n\nconst NODE_MODULES = 'node_modules/';\n\nconst BUILT_INS = new Set(builtinModules.flatMap(b => [b, `node:${b}`]));\n\nfunction validateConfig(pluginConfig?: OpenTelemetryPluginParams) {\n if (!pluginConfig) return;\n if (pluginConfig.instrumentationConfig && pluginConfig.instrumentations) {\n throw new Error(\n 'OpenTelemetryPluginParams and instrumentations must not be used together'\n );\n }\n}\n\nexport function openTelemetryPlugin(\n pluginConfig?: OpenTelemetryPluginParams\n): Plugin {\n validateConfig(pluginConfig);\n\n const {\n otelPackageToInstrumentationConfig,\n instrumentationModuleDefinitions,\n } = getOtelPackageToInstrumentationConfig(\n pluginConfig?.instrumentations ?? getNodeAutoInstrumentations()\n );\n\n return {\n name: 'open-telemetry',\n setup(build) {\n build.onResolve({ filter: /.*/ }, async args => {\n if (\n shouldIgnoreModule({\n namespace: args.namespace,\n path: args.path,\n importer: args.importer,\n externalModules: pluginConfig?.externalModules,\n pathPrefixesToIgnore: pluginConfig?.pathPrefixesToIgnore,\n })\n ) {\n return;\n }\n\n let path;\n let extractedModule;\n\n try {\n const result = extractPackageAndModulePath(\n args.path,\n args.resolveDir\n );\n path = result.path;\n extractedModule = result.extractedModule;\n } catch {\n // Some libraries like `mongodb` require optional dependencies, which may not be present and their absence doesn't break the code\n // Currently esbuild doesn't provide any better way to handle this in plugins: https://github.com/evanw/esbuild/issues/1127\n }\n\n // If it's a local import, don't patch it\n if (!extractedModule) return;\n\n // We'll rely on the OTel auto-instrumentation at runtime to patch builtin modules\n if (isBuiltIn(args.path, extractedModule)) return;\n\n const moduleVersion = await getModuleVersion({\n extractedModule,\n resolveDir: args.resolveDir,\n build,\n });\n if (!moduleVersion) return;\n\n // See if we have an instrumentation registered for this package\n const matchingInstrumentation = getInstrumentation({\n instrumentationModuleDefinitions,\n extractedModule,\n moduleVersion,\n path: args.path,\n });\n if (!matchingInstrumentation) return;\n\n const pluginData: PluginData = {\n extractedModule,\n moduleVersion,\n shouldPatchPackage: true,\n instrumentationName: matchingInstrumentation.name,\n };\n\n return { path, pluginData };\n });\n\n build.onLoad(\n { filter: /.*/ },\n async ({ path, pluginData }: OnLoadArgs) => {\n // Ignore any packages that don't have an instrumentation registered for them\n if (!pluginData?.shouldPatchPackage) return;\n\n const contents = await readFile(path);\n\n const config =\n otelPackageToInstrumentationConfig[pluginData.instrumentationName];\n if (!config) return;\n\n const packageConfig = getPackageConfig({\n pluginConfig,\n oTelInstrumentationPackage: config.oTelInstrumentationPackage,\n });\n const extractedModule = pluginData.extractedModule;\n\n return {\n contents: wrapModule(contents.toString(), {\n path: join(\n extractedModule.package || '',\n extractedModule.path || ''\n ),\n moduleVersion: pluginData.moduleVersion,\n instrumentationName: pluginData.instrumentationName,\n oTelInstrumentationClass: config.oTelInstrumentationClass,\n oTelInstrumentationPackage: config.oTelInstrumentationPackage,\n oTelInstrumentationConstructorArgs:\n config.configGenerator(packageConfig),\n }),\n resolveDir: dirname(path),\n };\n }\n );\n },\n };\n}\n\nfunction getPackageConfig({\n pluginConfig,\n oTelInstrumentationPackage,\n}: {\n pluginConfig?: OpenTelemetryPluginParams;\n oTelInstrumentationPackage: keyof EsbuildInstrumentationConfigMap;\n}) {\n if (!pluginConfig) return;\n if (pluginConfig.instrumentations) {\n const matchingPlugin = pluginConfig.instrumentations.find(\n i => i.instrumentationName === oTelInstrumentationPackage\n );\n if (!matchingPlugin) {\n throw new Error(\n `Instrumentation ${oTelInstrumentationPackage} was found but does not exist in list of instrumentations`\n );\n }\n return matchingPlugin.getConfig();\n }\n return pluginConfig.instrumentationConfig?.[oTelInstrumentationPackage];\n}\n\n/**\n * For a given full path to a module,\n * return the package name it belongs to and the local path to the module\n * input: '/foo/node_modules/@co/stuff/foo/bar/baz.js'\n * output: { package: '@co/stuff', path: 'foo/bar/baz.js' }\n */\nfunction extractPackageAndModulePath(\n originalPath: string,\n resolveDir: string\n): { path: string; extractedModule: ExtractedModule | null } {\n // @see https://github.com/nodejs/node/issues/47000\n const path = require.resolve(\n originalPath === '.' ? './' : originalPath === '..' ? '../' : originalPath,\n { paths: [resolveDir] }\n );\n\n const nodeModulesIndex = path.lastIndexOf(NODE_MODULES);\n if (nodeModulesIndex < 0) return { path, extractedModule: null };\n\n const subPath = path.substring(nodeModulesIndex + NODE_MODULES.length);\n const firstSlashIndex = subPath.indexOf('/');\n\n if (!subPath.startsWith('@')) {\n return {\n path,\n extractedModule: {\n package: subPath.substring(0, firstSlashIndex),\n path: subPath.substring(firstSlashIndex + 1),\n },\n };\n }\n\n const secondSlash = subPath.substring(firstSlashIndex + 1).indexOf('/');\n return {\n path,\n extractedModule: {\n package: subPath.substring(0, firstSlashIndex + secondSlash + 1),\n path: subPath.substring(firstSlashIndex + secondSlash + 2),\n },\n };\n}\n\nfunction shouldIgnoreModule({\n namespace,\n path,\n importer,\n externalModules,\n pathPrefixesToIgnore,\n}: {\n namespace: string;\n path: string;\n importer: string;\n externalModules?: string[];\n pathPrefixesToIgnore?: string[];\n}): boolean {\n // If onLoad is being triggered from another plugin, ignore it\n if (namespace !== 'file') return true;\n // If it's a local import from our code, ignore it\n if (!importer.includes(NODE_MODULES) && path.startsWith('.')) return true;\n // If it starts with a prefix to ignore, ignore it\n if (pathPrefixesToIgnore?.some(prefix => path.startsWith(prefix))) {\n return true;\n }\n // If it's marked as external, ignore it\n if (externalModules?.includes(path)) return true;\n\n return false;\n}\n\nfunction isBuiltIn(path: string, extractedModule: ExtractedModule): boolean {\n return (\n BUILT_INS.has(path) ||\n BUILT_INS.has(`${extractedModule.package}/${extractedModule.path}`)\n );\n}\n\nconst moduleVersionByPackageJsonPath = new Map<string, string>();\n\nasync function getModuleVersion({\n extractedModule,\n resolveDir,\n build,\n}: {\n extractedModule: ExtractedModule;\n resolveDir: string;\n build: PluginBuild;\n}) {\n const path = `${extractedModule.package}/package.json`;\n const contents = moduleVersionByPackageJsonPath.get(path);\n if (contents) return contents;\n\n const { path: packageJsonPath } = await build.resolve(path, {\n resolveDir,\n kind: 'require-resolve',\n });\n if (!packageJsonPath) return;\n\n const packageJsonContents = await readFile(packageJsonPath);\n const moduleVersion = JSON.parse(packageJsonContents.toString()).version;\n moduleVersionByPackageJsonPath.set(path, moduleVersion);\n return moduleVersion;\n}\n\nfunction getInstrumentation({\n instrumentationModuleDefinitions,\n extractedModule,\n path,\n moduleVersion,\n}: {\n instrumentationModuleDefinitions: InstrumentationModuleDefinition[];\n extractedModule: ExtractedModule;\n path: string;\n moduleVersion: string;\n}): InstrumentationModuleDefinition | null {\n for (const instrumentationModuleDefinition of instrumentationModuleDefinitions) {\n const fullModulePath = `${extractedModule.package}/${extractedModule.path}`;\n const nameMatches =\n instrumentationModuleDefinition.name === path ||\n instrumentationModuleDefinition.name === fullModulePath;\n\n if (!nameMatches) {\n const fileMatch = instrumentationModuleDefinition.files.find(file => {\n return file.name === path || file.name === fullModulePath;\n });\n if (!fileMatch) continue;\n }\n\n if (\n instrumentationModuleDefinition.supportedVersions.some(supportedVersion =>\n satisfies(moduleVersion, supportedVersion)\n )\n ) {\n return instrumentationModuleDefinition;\n }\n\n if (\n instrumentationModuleDefinition.files.some(file => {\n if (file.name !== path && file.name !== fullModulePath) return false;\n return file.supportedVersions.some(supportedVersion =>\n satisfies(moduleVersion, supportedVersion)\n );\n })\n ) {\n return instrumentationModuleDefinition;\n }\n }\n return null;\n}\n"]}
|
package/build/src/types.d.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import type { OnLoadArgs as EsbuildOnLoadArgs } from 'esbuild';
|
|
2
|
-
import type { InstrumentationConfigMap } from '@opentelemetry/auto-instrumentations-node';
|
|
3
|
-
import { Instrumentation } from '@opentelemetry/instrumentation';
|
|
4
|
-
export interface ExtractedModule {
|
|
5
|
-
package: string;
|
|
6
|
-
path: string;
|
|
7
|
-
}
|
|
8
|
-
export type PluginData = {
|
|
9
|
-
extractedModule: ExtractedModule;
|
|
10
|
-
shouldPatchPackage: boolean;
|
|
11
|
-
moduleVersion: string;
|
|
12
|
-
instrumentationName: string;
|
|
13
|
-
};
|
|
14
|
-
export type OnLoadArgs = Omit<EsbuildOnLoadArgs, 'pluginData'> & {
|
|
15
|
-
pluginData?: PluginData;
|
|
16
|
-
};
|
|
17
|
-
export interface ModuleParams {
|
|
18
|
-
path?: string;
|
|
19
|
-
oTelInstrumentationPackage: string;
|
|
20
|
-
oTelInstrumentationClass: string;
|
|
21
|
-
oTelInstrumentationConstructorArgs?: string;
|
|
22
|
-
instrumentationName?: string;
|
|
23
|
-
moduleVersion: string;
|
|
24
|
-
}
|
|
25
|
-
type _RemoveFunctions<T> = {
|
|
26
|
-
[P in keyof T as T[P] extends (...args: unknown[]) => unknown ? never : P]: T[P];
|
|
27
|
-
};
|
|
28
|
-
export type RemoveFunctions<T> = Partial<_RemoveFunctions<Required<T>>>;
|
|
29
|
-
type BuiltinPackages = '@opentelemetry/instrumentation-dns' | '@opentelemetry/instrumentation-fs' | '@opentelemetry/instrumentation-http';
|
|
30
|
-
type NonBuiltinInstrumentationConfigMap = Omit<InstrumentationConfigMap, BuiltinPackages>;
|
|
31
|
-
export type EsbuildInstrumentationConfigMap = {
|
|
32
|
-
[K in keyof NonBuiltinInstrumentationConfigMap]: RemoveFunctions<NonBuiltinInstrumentationConfigMap[K]>;
|
|
33
|
-
};
|
|
34
|
-
export interface OpenTelemetryPluginParams {
|
|
35
|
-
/**
|
|
36
|
-
* Allow configuring instrumentations loaded from getNodeAutoInstrumentations (from @opentelemetry/auto-instrumentations-node).
|
|
37
|
-
*
|
|
38
|
-
* @deprecated Use `instrumentations` instead and pass in already configured instrumentations
|
|
39
|
-
*/
|
|
40
|
-
instrumentationConfig?: EsbuildInstrumentationConfigMap;
|
|
41
|
-
/** Modules to consider external and ignore from the plugin */
|
|
42
|
-
externalModules?: string[];
|
|
43
|
-
/**
|
|
44
|
-
* Path prefixes to ignore.
|
|
45
|
-
*
|
|
46
|
-
* ie if you configure compilerOptions.paths in your tsconfig.json to use something like `~/` for the
|
|
47
|
-
* root of your project then you could set that here to ignore modules
|
|
48
|
-
*/
|
|
49
|
-
pathPrefixesToIgnore?: string[];
|
|
50
|
-
/**
|
|
51
|
-
* Instrumentations to apply
|
|
52
|
-
* Defaults to `getNodeAutoInstrumentations()` from `@opentelemetry/auto-instrumentations-node`.
|
|
53
|
-
* NB: getNodeAutoInstrumentations() can change what it returns (and what is enabled by default) version to version
|
|
54
|
-
* so as this plugin updates dependencies that may change, if you are not manually configuring instrumentations.
|
|
55
|
-
*
|
|
56
|
-
* NB: Not all config options for each instrumentation will be respected. Notably, functions will be ignored
|
|
57
|
-
* as this plugin requires serializing the configs as JSON during bundling which are then read at runtime.
|
|
58
|
-
*
|
|
59
|
-
* This works:
|
|
60
|
-
* ```typescript
|
|
61
|
-
* openTelemetryPlugin({
|
|
62
|
-
* instrumentations: [
|
|
63
|
-
* new PinoInstrumentation({
|
|
64
|
-
* logKeys: {
|
|
65
|
-
* traceId: 'traceId',
|
|
66
|
-
* spanId: 'spanId',
|
|
67
|
-
* traceFlags: 'traceFlags',
|
|
68
|
-
* }
|
|
69
|
-
* })
|
|
70
|
-
* ]
|
|
71
|
-
* })
|
|
72
|
-
* ```
|
|
73
|
-
*
|
|
74
|
-
* This would not (logHook would be ignored)
|
|
75
|
-
* ```typescript
|
|
76
|
-
* openTelemetryPlugin({
|
|
77
|
-
* instrumentations: [
|
|
78
|
-
* new PinoInstrumentation({
|
|
79
|
-
* logHook: (span, record) => {
|
|
80
|
-
* record['resource.service.name'] = provider.resource.attributes['service.name'];
|
|
81
|
-
* }
|
|
82
|
-
* })
|
|
83
|
-
* ]
|
|
84
|
-
* })
|
|
85
|
-
* ```
|
|
86
|
-
*/
|
|
87
|
-
instrumentations?: Instrumentation[];
|
|
88
|
-
}
|
|
89
|
-
export {};
|
|
90
|
-
//# sourceMappingURL=types.d.ts.map
|
package/build/src/types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG","sourcesContent":["/*\n * Copyright The Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport type { OnLoadArgs as EsbuildOnLoadArgs } from 'esbuild';\nimport type { InstrumentationConfigMap } from '@opentelemetry/auto-instrumentations-node';\nimport { Instrumentation } from '@opentelemetry/instrumentation';\n\nexport interface ExtractedModule {\n package: string;\n path: string;\n}\n\nexport type PluginData = {\n extractedModule: ExtractedModule;\n shouldPatchPackage: boolean;\n moduleVersion: string;\n instrumentationName: string;\n};\n\nexport type OnLoadArgs = Omit<EsbuildOnLoadArgs, 'pluginData'> & {\n pluginData?: PluginData;\n};\n\nexport interface ModuleParams {\n path?: string;\n oTelInstrumentationPackage: string;\n oTelInstrumentationClass: string;\n oTelInstrumentationConstructorArgs?: string;\n instrumentationName?: string;\n moduleVersion: string;\n}\n\ntype _RemoveFunctions<T> = {\n [P in keyof T as T[P] extends (...args: unknown[]) => unknown\n ? never\n : P]: T[P];\n};\n\n// _RemoveFunctions does not work on optional fields, so first make the type required then apply Partial to the result\nexport type RemoveFunctions<T> = Partial<_RemoveFunctions<Required<T>>>;\n\ntype BuiltinPackages =\n | '@opentelemetry/instrumentation-dns'\n | '@opentelemetry/instrumentation-fs'\n | '@opentelemetry/instrumentation-http';\n\ntype NonBuiltinInstrumentationConfigMap = Omit<\n InstrumentationConfigMap,\n BuiltinPackages\n>;\n\nexport type EsbuildInstrumentationConfigMap = {\n [K in keyof NonBuiltinInstrumentationConfigMap]: RemoveFunctions<\n NonBuiltinInstrumentationConfigMap[K]\n >;\n};\n\nexport interface OpenTelemetryPluginParams {\n /**\n * Allow configuring instrumentations loaded from getNodeAutoInstrumentations (from @opentelemetry/auto-instrumentations-node).\n *\n * @deprecated Use `instrumentations` instead and pass in already configured instrumentations\n */\n instrumentationConfig?: EsbuildInstrumentationConfigMap;\n\n /** Modules to consider external and ignore from the plugin */\n externalModules?: string[];\n\n /**\n * Path prefixes to ignore.\n *\n * ie if you configure compilerOptions.paths in your tsconfig.json to use something like `~/` for the\n * root of your project then you could set that here to ignore modules\n */\n pathPrefixesToIgnore?: string[];\n\n /**\n * Instrumentations to apply\n * Defaults to `getNodeAutoInstrumentations()` from `@opentelemetry/auto-instrumentations-node`.\n * NB: getNodeAutoInstrumentations() can change what it returns (and what is enabled by default) version to version\n * so as this plugin updates dependencies that may change, if you are not manually configuring instrumentations.\n *\n * NB: Not all config options for each instrumentation will be respected. Notably, functions will be ignored\n * as this plugin requires serializing the configs as JSON during bundling which are then read at runtime.\n *\n * This works:\n * ```typescript\n * openTelemetryPlugin({\n * instrumentations: [\n * new PinoInstrumentation({\n * logKeys: {\n * traceId: 'traceId',\n * spanId: 'spanId',\n * traceFlags: 'traceFlags',\n * }\n * })\n * ]\n * })\n * ```\n *\n * This would not (logHook would be ignored)\n * ```typescript\n * openTelemetryPlugin({\n * instrumentations: [\n * new PinoInstrumentation({\n * logHook: (span, record) => {\n * record['resource.service.name'] = provider.resource.attributes['service.name'];\n * }\n * })\n * ]\n * })\n * ```\n */\n instrumentations?: Instrumentation[];\n}\n"]}
|