opentelemetry-esbuild-plugin-node 0.2.6-alpha.19 → 0.3.1-10
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 +41 -100
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/{build/src → dist}/index.js +1 -2
- 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 +1 -2
- package/eslint.config.mjs +23 -0
- package/package.json +32 -64
- package/{build/src/types.js → src/index.ts} +4 -4
- package/src/plugin.ts +167 -0
- package/{build/src/config/index.js → src/types.ts} +16 -7
- 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 -65
- 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 -18
- package/build/src/config/main.js +0 -57
- 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 -202
- package/build/src/plugin.js.map +0 -1
- package/build/src/types.d.ts +0 -46
- package/build/src/types.js.map +0 -1
package/build/src/plugin.js
DELETED
|
@@ -1,202 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* Copyright The OpenTelemetry 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 = void 0;
|
|
19
|
-
const path_1 = require("path");
|
|
20
|
-
const main_1 = require("./config/main");
|
|
21
|
-
const module_1 = require("module");
|
|
22
|
-
const promises_1 = require("fs/promises");
|
|
23
|
-
const semver_1 = require("semver");
|
|
24
|
-
const common_1 = require("./common");
|
|
25
|
-
const NODE_MODULES = 'node_modules/';
|
|
26
|
-
const BUILT_INS = new Set(module_1.builtinModules.flatMap(b => [b, `node:${b}`]));
|
|
27
|
-
function openTelemetryPlugin(pluginConfig) {
|
|
28
|
-
return {
|
|
29
|
-
name: 'open-telemetry',
|
|
30
|
-
setup(build) {
|
|
31
|
-
build.onResolve({ filter: /.*/ }, async (args) => {
|
|
32
|
-
if (shouldIgnoreModule({
|
|
33
|
-
namespace: args.namespace,
|
|
34
|
-
path: args.path,
|
|
35
|
-
importer: args.importer,
|
|
36
|
-
externalModules: pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.externalModules,
|
|
37
|
-
pathPrefixesToIgnore: pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.pathPrefixesToIgnore,
|
|
38
|
-
})) {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
let path;
|
|
42
|
-
let extractedModule;
|
|
43
|
-
try {
|
|
44
|
-
const result = extractPackageAndModulePath(args.path, args.resolveDir);
|
|
45
|
-
path = result.path;
|
|
46
|
-
extractedModule = result.extractedModule;
|
|
47
|
-
}
|
|
48
|
-
catch (e) {
|
|
49
|
-
// Some libraries like `mongodb` require optional dependencies, which may not be present and their absence doesn't break the code
|
|
50
|
-
// Currently esbuild doesn't provide any better way to handle this in plugins: https://github.com/evanw/esbuild/issues/1127
|
|
51
|
-
}
|
|
52
|
-
// If it's a local import, don't patch it
|
|
53
|
-
if (!extractedModule)
|
|
54
|
-
return;
|
|
55
|
-
// We'll rely on the OTel auto-instrumentation at runtime to patch builtin modules
|
|
56
|
-
if (isBuiltIn(args.path, extractedModule))
|
|
57
|
-
return;
|
|
58
|
-
const moduleVersion = await getModuleVersion({
|
|
59
|
-
extractedModule,
|
|
60
|
-
resolveDir: args.resolveDir,
|
|
61
|
-
build,
|
|
62
|
-
});
|
|
63
|
-
if (!moduleVersion)
|
|
64
|
-
return;
|
|
65
|
-
// See if we have an instrumentation registered for this package
|
|
66
|
-
const matchingInstrumentation = getInstrumentation({
|
|
67
|
-
extractedModule,
|
|
68
|
-
moduleVersion,
|
|
69
|
-
path: args.path,
|
|
70
|
-
});
|
|
71
|
-
if (!matchingInstrumentation)
|
|
72
|
-
return;
|
|
73
|
-
const pluginData = {
|
|
74
|
-
extractedModule,
|
|
75
|
-
moduleVersion,
|
|
76
|
-
shouldPatchPackage: true,
|
|
77
|
-
instrumentationName: matchingInstrumentation.name,
|
|
78
|
-
};
|
|
79
|
-
return { path, pluginData };
|
|
80
|
-
});
|
|
81
|
-
build.onLoad({ filter: /.*/ }, async ({ path, pluginData }) => {
|
|
82
|
-
var _a;
|
|
83
|
-
// Ignore any packages that don't have an instrumentation registered for them
|
|
84
|
-
if (!(pluginData === null || pluginData === void 0 ? void 0 : pluginData.shouldPatchPackage))
|
|
85
|
-
return;
|
|
86
|
-
const contents = await (0, promises_1.readFile)(path);
|
|
87
|
-
const config = main_1.otelPackageToInstrumentationConfig[pluginData.instrumentationName];
|
|
88
|
-
if (!config)
|
|
89
|
-
return;
|
|
90
|
-
const packageConfig = (_a = pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.instrumentationConfig) === null || _a === void 0 ? void 0 : _a[config.oTelInstrumentationPackage];
|
|
91
|
-
const extractedModule = pluginData.extractedModule;
|
|
92
|
-
return {
|
|
93
|
-
contents: (0, common_1.wrapModule)(contents.toString(), {
|
|
94
|
-
path: (0, path_1.join)(extractedModule.package || '', extractedModule.path || ''),
|
|
95
|
-
moduleVersion: pluginData.moduleVersion,
|
|
96
|
-
instrumentationName: pluginData.instrumentationName,
|
|
97
|
-
oTelInstrumentationClass: config.oTelInstrumentationClass,
|
|
98
|
-
oTelInstrumentationPackage: config.oTelInstrumentationPackage,
|
|
99
|
-
oTelInstrumentationConstructorArgs: config.configGenerator(packageConfig),
|
|
100
|
-
}),
|
|
101
|
-
resolveDir: (0, path_1.dirname)(path),
|
|
102
|
-
};
|
|
103
|
-
});
|
|
104
|
-
},
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
exports.openTelemetryPlugin = openTelemetryPlugin;
|
|
108
|
-
/**
|
|
109
|
-
* For a given full path to a module,
|
|
110
|
-
* return the package name it belongs to and the local path to the module
|
|
111
|
-
* input: '/foo/node_modules/@co/stuff/foo/bar/baz.js'
|
|
112
|
-
* output: { package: '@co/stuff', path: 'foo/bar/baz.js' }
|
|
113
|
-
*/
|
|
114
|
-
function extractPackageAndModulePath(originalPath, resolveDir) {
|
|
115
|
-
// @see https://github.com/nodejs/node/issues/47000
|
|
116
|
-
const path = require.resolve(originalPath === '.' ? './' : originalPath === '..' ? '../' : originalPath, { paths: [resolveDir] });
|
|
117
|
-
const nodeModulesIndex = path.lastIndexOf(NODE_MODULES);
|
|
118
|
-
if (nodeModulesIndex < 0)
|
|
119
|
-
return { path, extractedModule: null };
|
|
120
|
-
const subPath = path.substring(nodeModulesIndex + NODE_MODULES.length);
|
|
121
|
-
const firstSlashIndex = subPath.indexOf('/');
|
|
122
|
-
if (!subPath.startsWith('@')) {
|
|
123
|
-
return {
|
|
124
|
-
path,
|
|
125
|
-
extractedModule: {
|
|
126
|
-
package: subPath.substring(0, firstSlashIndex),
|
|
127
|
-
path: subPath.substring(firstSlashIndex + 1),
|
|
128
|
-
},
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
const secondSlash = subPath.substring(firstSlashIndex + 1).indexOf('/');
|
|
132
|
-
return {
|
|
133
|
-
path,
|
|
134
|
-
extractedModule: {
|
|
135
|
-
package: subPath.substring(0, firstSlashIndex + secondSlash + 1),
|
|
136
|
-
path: subPath.substring(firstSlashIndex + secondSlash + 2),
|
|
137
|
-
},
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
function shouldIgnoreModule({ namespace, path, importer, externalModules, pathPrefixesToIgnore, }) {
|
|
141
|
-
// If onLoad is being triggered from another plugin, ignore it
|
|
142
|
-
if (namespace !== 'file')
|
|
143
|
-
return true;
|
|
144
|
-
// If it's a local import from our code, ignore it
|
|
145
|
-
if (!importer.includes(NODE_MODULES) && path.startsWith('.'))
|
|
146
|
-
return true;
|
|
147
|
-
// If it starts with a prefix to ignore, ignore it
|
|
148
|
-
if (pathPrefixesToIgnore === null || pathPrefixesToIgnore === void 0 ? void 0 : pathPrefixesToIgnore.some(prefix => path.startsWith(prefix))) {
|
|
149
|
-
return true;
|
|
150
|
-
}
|
|
151
|
-
// If it's marked as external, ignore it
|
|
152
|
-
if (externalModules === null || externalModules === void 0 ? void 0 : externalModules.includes(path))
|
|
153
|
-
return true;
|
|
154
|
-
return false;
|
|
155
|
-
}
|
|
156
|
-
function isBuiltIn(path, extractedModule) {
|
|
157
|
-
return (BUILT_INS.has(path) ||
|
|
158
|
-
BUILT_INS.has(`${extractedModule.package}/${extractedModule.path}`));
|
|
159
|
-
}
|
|
160
|
-
const moduleVersionByPackageJsonPath = new Map();
|
|
161
|
-
async function getModuleVersion({ extractedModule, resolveDir, build, }) {
|
|
162
|
-
const path = `${extractedModule.package}/package.json`;
|
|
163
|
-
const contents = moduleVersionByPackageJsonPath.get(path);
|
|
164
|
-
if (contents)
|
|
165
|
-
return contents;
|
|
166
|
-
const { path: packageJsonPath } = await build.resolve(path, {
|
|
167
|
-
resolveDir,
|
|
168
|
-
kind: 'require-resolve',
|
|
169
|
-
});
|
|
170
|
-
if (!packageJsonPath)
|
|
171
|
-
return;
|
|
172
|
-
const packageJsonContents = await (0, promises_1.readFile)(packageJsonPath);
|
|
173
|
-
const moduleVersion = JSON.parse(packageJsonContents.toString()).version;
|
|
174
|
-
moduleVersionByPackageJsonPath.set(path, moduleVersion);
|
|
175
|
-
return moduleVersion;
|
|
176
|
-
}
|
|
177
|
-
function getInstrumentation({ extractedModule, path, moduleVersion, }) {
|
|
178
|
-
for (const instrumentationModuleDefinition of main_1.instrumentationModuleDefinitions) {
|
|
179
|
-
const fullModulePath = `${extractedModule.package}/${extractedModule.path}`;
|
|
180
|
-
const nameMatches = instrumentationModuleDefinition.name === path ||
|
|
181
|
-
instrumentationModuleDefinition.name === fullModulePath;
|
|
182
|
-
if (!nameMatches) {
|
|
183
|
-
const fileMatch = instrumentationModuleDefinition.files.find(file => {
|
|
184
|
-
return file.name === path || file.name === fullModulePath;
|
|
185
|
-
});
|
|
186
|
-
if (!fileMatch)
|
|
187
|
-
continue;
|
|
188
|
-
}
|
|
189
|
-
if (instrumentationModuleDefinition.supportedVersions.some(supportedVersion => (0, semver_1.satisfies)(moduleVersion, supportedVersion))) {
|
|
190
|
-
return instrumentationModuleDefinition;
|
|
191
|
-
}
|
|
192
|
-
if (instrumentationModuleDefinition.files.some(file => {
|
|
193
|
-
if (file.name !== path && file.name !== fullModulePath)
|
|
194
|
-
return false;
|
|
195
|
-
return file.supportedVersions.some(supportedVersion => (0, semver_1.satisfies)(moduleVersion, supportedVersion));
|
|
196
|
-
})) {
|
|
197
|
-
return instrumentationModuleDefinition;
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
return null;
|
|
201
|
-
}
|
|
202
|
-
//# sourceMappingURL=plugin.js.map
|
package/build/src/plugin.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/plugin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ChB,MAAM,SAAS,GAAG,+BAA+B,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;gBAClE,OAAO,IAAI,CAAC,IAAI,KAAK,IAAI,IAAI,IAAI,CAAC,IAAI,KAAK,cAAc,CAAC;YAC5D,CAAC,CAAC,CAAC;YACH,IAAI,CAAC,SAAS;gBAAE,SAAS;SAC1B;QAED,IACE,+BAA+B,CAAC,iBAAiB,CAAC,IAAI,CAAC,gBAAgB,CAAC,EAAE,CACxE,IAAA,kBAAS,EAAC,aAAa,EAAE,gBAAgB,CAAC,CAC3C,EACD;YACA,OAAO,+BAA+B,CAAC;SACxC;QAED,IACE,+BAA+B,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;YAChD,IAAI,IAAI,CAAC,IAAI,KAAK,IAAI,IAAI,IAAI,CAAC,IAAI,KAAK,cAAc;gBAAE,OAAO,KAAK,CAAC;YACrE,OAAO,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,gBAAgB,CAAC,EAAE,CACpD,IAAA,kBAAS,EAAC,aAAa,EAAE,gBAAgB,CAAC,CAC3C,CAAC;QACJ,CAAC,CAAC,EACF;YACA,OAAO,+BAA+B,CAAC;SACxC;KACF;IACD,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["/*\n * Copyright The OpenTelemetry 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 ExtractedModule,\n OnLoadArgs,\n OpenTelemetryPluginParams,\n PluginData,\n} from './types';\nimport { Plugin, PluginBuild } from 'esbuild';\nimport { dirname, join } from 'path';\nimport {\n instrumentationModuleDefinitions,\n otelPackageToInstrumentationConfig,\n} from './config/main';\n\nimport { InstrumentationModuleDefinition } from '@opentelemetry/instrumentation';\nimport { builtinModules } from 'module';\nimport { readFile } from 'fs/promises';\nimport { satisfies } from 'semver';\nimport { wrapModule } from './common';\n\nconst NODE_MODULES = 'node_modules/';\n\nconst BUILT_INS = new Set(builtinModules.flatMap(b => [b, `node:${b}`]));\n\nexport function openTelemetryPlugin(\n pluginConfig?: OpenTelemetryPluginParams\n): Plugin {\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 (e) {\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 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 =\n pluginConfig?.instrumentationConfig?.[\n 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\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 extractedModule,\n path,\n moduleVersion,\n}: {\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,46 +0,0 @@
|
|
|
1
|
-
import type { OnLoadArgs as EsbuildOnLoadArgs } from 'esbuild';
|
|
2
|
-
import type { InstrumentationConfigMap } from '@opentelemetry/auto-instrumentations-node';
|
|
3
|
-
export interface ExtractedModule {
|
|
4
|
-
package: string;
|
|
5
|
-
path: string;
|
|
6
|
-
}
|
|
7
|
-
export declare type PluginData = {
|
|
8
|
-
extractedModule: ExtractedModule;
|
|
9
|
-
shouldPatchPackage: boolean;
|
|
10
|
-
moduleVersion: string;
|
|
11
|
-
instrumentationName: string;
|
|
12
|
-
};
|
|
13
|
-
export declare type OnLoadArgs = Omit<EsbuildOnLoadArgs, 'pluginData'> & {
|
|
14
|
-
pluginData?: PluginData;
|
|
15
|
-
};
|
|
16
|
-
export interface ModuleParams {
|
|
17
|
-
path?: string;
|
|
18
|
-
oTelInstrumentationPackage: string;
|
|
19
|
-
oTelInstrumentationClass: string;
|
|
20
|
-
oTelInstrumentationConstructorArgs?: string;
|
|
21
|
-
instrumentationName?: string;
|
|
22
|
-
moduleVersion: string;
|
|
23
|
-
}
|
|
24
|
-
declare type _RemoveFunctions<T> = {
|
|
25
|
-
[P in keyof T as T[P] extends (...args: unknown[]) => unknown ? never : P]: T[P];
|
|
26
|
-
};
|
|
27
|
-
export declare type RemoveFunctions<T> = Partial<_RemoveFunctions<Required<T>>>;
|
|
28
|
-
declare type BuiltinPackages = '@opentelemetry/instrumentation-dns' | '@opentelemetry/instrumentation-fs' | '@opentelemetry/instrumentation-http';
|
|
29
|
-
declare type NonBuiltinInstrumentationConfigMap = Omit<InstrumentationConfigMap, BuiltinPackages>;
|
|
30
|
-
export declare type EsbuildInstrumentationConfigMap = {
|
|
31
|
-
[K in keyof NonBuiltinInstrumentationConfigMap]: RemoveFunctions<NonBuiltinInstrumentationConfigMap[K]>;
|
|
32
|
-
};
|
|
33
|
-
export interface OpenTelemetryPluginParams {
|
|
34
|
-
instrumentationConfig?: EsbuildInstrumentationConfigMap;
|
|
35
|
-
/** Modules to consider external and ignore from the plugin */
|
|
36
|
-
externalModules?: string[];
|
|
37
|
-
/**
|
|
38
|
-
* Path prefixes to ignore.
|
|
39
|
-
*
|
|
40
|
-
* ie if you configure compilerOptions.paths in your tsconfig.json to use something like `~/` for the
|
|
41
|
-
* root of your project then you could set that here to ignore modules
|
|
42
|
-
*/
|
|
43
|
-
pathPrefixesToIgnore?: string[];
|
|
44
|
-
}
|
|
45
|
-
export {};
|
|
46
|
-
//# 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 OpenTelemetry 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';\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 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"]}
|