@opentelemetry/instrumentation-fastify 0.32.3 → 0.32.5
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/build/src/constants.d.ts +1 -1
- package/build/src/constants.js +16 -7
- package/build/src/constants.js.map +1 -1
- package/build/src/instrumentation.js +13 -8
- package/build/src/instrumentation.js.map +1 -1
- package/build/src/version.d.ts +1 -1
- package/build/src/version.js +1 -1
- package/build/src/version.js.map +1 -1
- package/package.json +9 -9
package/build/src/constants.d.ts
CHANGED
package/build/src/constants.js
CHANGED
|
@@ -15,12 +15,21 @@
|
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.
|
|
18
|
+
exports.hooksNamesToWrap = exports.spanRequestSymbol = void 0;
|
|
19
19
|
exports.spanRequestSymbol = Symbol('opentelemetry.instrumentation.fastify.request_active_span');
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
// The instrumentation creates a span for invocations of lifecycle hook handlers
|
|
21
|
+
// that take `(request, reply, ...[, done])` arguments. Currently this is all
|
|
22
|
+
// lifecycle hooks except `onRequestAbort`.
|
|
23
|
+
// https://fastify.dev/docs/latest/Reference/Hooks
|
|
24
|
+
exports.hooksNamesToWrap = new Set([
|
|
25
|
+
'onTimeout',
|
|
26
|
+
'onRequest',
|
|
27
|
+
'preParsing',
|
|
28
|
+
'preValidation',
|
|
29
|
+
'preSerialization',
|
|
30
|
+
'preHandler',
|
|
31
|
+
'onSend',
|
|
32
|
+
'onResponse',
|
|
33
|
+
'onError',
|
|
34
|
+
]);
|
|
26
35
|
//# sourceMappingURL=constants.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEU,QAAA,iBAAiB,GAAG,MAAM,CACrC,2DAA2D,CAC5D,CAAC;
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEU,QAAA,iBAAiB,GAAG,MAAM,CACrC,2DAA2D,CAC5D,CAAC;AAEF,gFAAgF;AAChF,6EAA6E;AAC7E,2CAA2C;AAC3C,kDAAkD;AACrC,QAAA,gBAAgB,GAAG,IAAI,GAAG,CAAC;IACtC,WAAW;IACX,WAAW;IACX,YAAY;IACZ,eAAe;IACf,kBAAkB;IAClB,YAAY;IACZ,QAAQ;IACR,YAAY;IACZ,SAAS;CACV,CAAC,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\nexport const spanRequestSymbol = Symbol(\n 'opentelemetry.instrumentation.fastify.request_active_span'\n);\n\n// The instrumentation creates a span for invocations of lifecycle hook handlers\n// that take `(request, reply, ...[, done])` arguments. Currently this is all\n// lifecycle hooks except `onRequestAbort`.\n// https://fastify.dev/docs/latest/Reference/Hooks\nexport const hooksNamesToWrap = new Set([\n 'onTimeout',\n 'onRequest',\n 'preParsing',\n 'preValidation',\n 'preSerialization',\n 'preHandler',\n 'onSend',\n 'onResponse',\n 'onError',\n]);\n"]}
|
|
@@ -47,14 +47,15 @@ class FastifyInstrumentation extends instrumentation_1.InstrumentationBase {
|
|
|
47
47
|
_hookOnRequest() {
|
|
48
48
|
const instrumentation = this;
|
|
49
49
|
return function onRequest(request, reply, done) {
|
|
50
|
-
var _a, _b;
|
|
51
50
|
if (!instrumentation.isEnabled()) {
|
|
52
51
|
return done();
|
|
53
52
|
}
|
|
54
53
|
instrumentation._wrap(reply, 'send', instrumentation._patchSend());
|
|
55
54
|
const anyRequest = request;
|
|
56
55
|
const rpcMetadata = (0, core_1.getRPCMetadata)(api_1.context.active());
|
|
57
|
-
const routeName =
|
|
56
|
+
const routeName = anyRequest.routeOptions
|
|
57
|
+
? anyRequest.routeOptions.url // since fastify@4.10.0
|
|
58
|
+
: request.routerPath;
|
|
58
59
|
if (routeName && (rpcMetadata === null || rpcMetadata === void 0 ? void 0 : rpcMetadata.type) === core_1.RPCType.HTTP) {
|
|
59
60
|
rpcMetadata.route = routeName;
|
|
60
61
|
}
|
|
@@ -111,8 +112,8 @@ class FastifyInstrumentation extends instrumentation_1.InstrumentationBase {
|
|
|
111
112
|
const name = args[0];
|
|
112
113
|
const handler = args[1];
|
|
113
114
|
const pluginName = this.pluginName;
|
|
114
|
-
if (constants_1.
|
|
115
|
-
return original.apply(this,
|
|
115
|
+
if (!constants_1.hooksNamesToWrap.has(name)) {
|
|
116
|
+
return original.apply(this, args);
|
|
116
117
|
}
|
|
117
118
|
const syncFunctionWithDone = typeof args[args.length - 1] === 'function' &&
|
|
118
119
|
handler.constructor.name !== 'AsyncFunction';
|
|
@@ -161,18 +162,22 @@ class FastifyInstrumentation extends instrumentation_1.InstrumentationBase {
|
|
|
161
162
|
const instrumentation = this;
|
|
162
163
|
this._diag.debug('Patching fastify preHandler function');
|
|
163
164
|
return function preHandler(request, reply, done) {
|
|
164
|
-
var _a, _b
|
|
165
|
+
var _a, _b;
|
|
165
166
|
if (!instrumentation.isEnabled()) {
|
|
166
167
|
return done();
|
|
167
168
|
}
|
|
168
169
|
const anyRequest = request;
|
|
169
|
-
const handler = ((_a = anyRequest.routeOptions) === null || _a === void 0 ? void 0 : _a.handler) || ((_b = anyRequest.context) === null || _b === void 0 ? void 0 : _b.handler)
|
|
170
|
-
const handlerName = handler === null || handler === void 0 ? void 0 : handler.name.
|
|
170
|
+
const handler = ((_a = anyRequest.routeOptions) === null || _a === void 0 ? void 0 : _a.handler) || ((_b = anyRequest.context) === null || _b === void 0 ? void 0 : _b.handler);
|
|
171
|
+
const handlerName = (handler === null || handler === void 0 ? void 0 : handler.name.startsWith('bound '))
|
|
172
|
+
? handler.name.substr(6)
|
|
173
|
+
: handler === null || handler === void 0 ? void 0 : handler.name;
|
|
171
174
|
const spanName = `${AttributeNames_1.FastifyNames.REQUEST_HANDLER} - ${handlerName || this.pluginName || exports.ANONYMOUS_NAME}`;
|
|
172
175
|
const spanAttributes = {
|
|
173
176
|
[AttributeNames_1.AttributeNames.PLUGIN_NAME]: this.pluginName,
|
|
174
177
|
[AttributeNames_1.AttributeNames.FASTIFY_TYPE]: AttributeNames_1.FastifyTypes.REQUEST_HANDLER,
|
|
175
|
-
[semantic_conventions_1.SemanticAttributes.HTTP_ROUTE]:
|
|
178
|
+
[semantic_conventions_1.SemanticAttributes.HTTP_ROUTE]: anyRequest.routeOptions
|
|
179
|
+
? anyRequest.routeOptions.url // since fastify@4.10.0
|
|
180
|
+
: request.routerPath,
|
|
176
181
|
};
|
|
177
182
|
if (handlerName) {
|
|
178
183
|
spanAttributes[AttributeNames_1.AttributeNames.FASTIFY_NAME] = handlerName;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"instrumentation.js","sourceRoot":"","sources":["../../src/instrumentation.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,4CAK4B;AAC5B,8CAA8D;AAC9D,oEAIwC;AACxC,8EAAyE;AAOzE,2CAAmD;AACnD,2DAIgC;AAGhC,mCAIiB;AACjB,uCAAoC;AAEvB,QAAA,cAAc,GAAG,WAAW,CAAC;AAE1C,gDAAgD;AAChD,MAAa,sBAAuB,SAAQ,qCAAmB;IAC7D,YAAY,SAAuC,EAAE;QACnD,KAAK,CACH,wCAAwC,EACxC,iBAAO,EACP,MAAM,CAAC,MAAM,CAAC,EAAE,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3D,CAAC,yCAAkB,CAAC,UAAU,CAAC,EAC7B,CAAA,MAAA,MAAA,UAAU,CAAC,YAAY,0CAAE,MAAM,0CAAE,GAAG,KAAI,OAAO,CAAC,UAAU;aAC7D,CAAC;YACF,IAAI,WAAW,EAAE;gBACf,cAAc,CAAC,+BAAc,CAAC,YAAY,CAAC,GAAG,WAAW,CAAC;aAC3D;YACD,MAAM,IAAI,GAAG,IAAA,iBAAS,EACpB,KAAK,EACL,eAAe,CAAC,MAAM,EACtB,QAAQ,EACR,cAAc,CACf,CAAC;YAEF,IAAI,eAAe,CAAC,SAAS,EAAE,CAAC,WAAW,EAAE;gBAC3C,IAAA,wCAAsB,EACpB,GAAG,EAAE,CAAC,eAAe,CAAC,SAAS,EAAE,CAAC,WAAY,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,CAAC,EACjE,CAAC,CAAC,EAAE;oBACF,IAAI,CAAC,EAAE;wBACL,eAAe,CAAC,KAAK,CAAC,KAAK,CAAC,qBAAqB,EAAE,CAAC,CAAC,CAAC;qBACvD;gBACH,CAAC,EACD,IAAI,CACL,CAAC;aACH;YAED,OAAO,aAAO,CAAC,IAAI,CAAC,WAAK,CAAC,OAAO,CAAC,aAAO,CAAC,MAAM,EAAE,EAAE,IAAI,CAAC,EAAE,GAAG,EAAE;gBAC9D,IAAI,EAAE,CAAC;YACT,CAAC,CAAC,CAAC;QACL,CAAC,CAAC;IACJ,CAAC;CACF;AA9PD,wDA8PC","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 context,\n SpanAttributes,\n SpanStatusCode,\n trace,\n} from '@opentelemetry/api';\nimport { getRPCMetadata, RPCType } from '@opentelemetry/core';\nimport {\n InstrumentationBase,\n InstrumentationNodeModuleDefinition,\n safeExecuteInTheMiddle,\n} from '@opentelemetry/instrumentation';\nimport { SemanticAttributes } from '@opentelemetry/semantic-conventions';\nimport type {\n HookHandlerDoneFunction,\n FastifyInstance,\n FastifyRequest,\n FastifyReply,\n} from 'fastify';\nimport { applicationHookNames } from './constants';\nimport {\n AttributeNames,\n FastifyNames,\n FastifyTypes,\n} from './enums/AttributeNames';\nimport type { HandlerOriginal, PluginFastifyReply } from './internal-types';\nimport type { FastifyInstrumentationConfig } from './types';\nimport {\n endSpan,\n safeExecuteInTheMiddleMaybePromise,\n startSpan,\n} from './utils';\nimport { VERSION } from './version';\n\nexport const ANONYMOUS_NAME = 'anonymous';\n\n/** Fastify instrumentation for OpenTelemetry */\nexport class FastifyInstrumentation extends InstrumentationBase {\n constructor(config: FastifyInstrumentationConfig = {}) {\n super(\n '@opentelemetry/instrumentation-fastify',\n VERSION,\n Object.assign({}, config)\n );\n }\n\n override setConfig(config: FastifyInstrumentationConfig = {}) {\n this._config = Object.assign({}, config);\n }\n\n override getConfig(): FastifyInstrumentationConfig {\n return this._config as FastifyInstrumentationConfig;\n }\n\n init() {\n return [\n new InstrumentationNodeModuleDefinition<any>(\n 'fastify',\n ['^3.0.0', '^4.0.0'],\n (moduleExports, moduleVersion) => {\n this._diag.debug(`Applying patch for fastify@${moduleVersion}`);\n return this._patchConstructor(moduleExports);\n }\n ),\n ];\n }\n\n private _hookOnRequest() {\n const instrumentation = this;\n return function onRequest(\n request: FastifyRequest,\n reply: FastifyReply,\n done: HookHandlerDoneFunction\n ) {\n if (!instrumentation.isEnabled()) {\n return done();\n }\n instrumentation._wrap(reply, 'send', instrumentation._patchSend());\n\n const anyRequest = request as any;\n\n const rpcMetadata = getRPCMetadata(context.active());\n const routeName =\n anyRequest.routeOptions?.config?.url || request.routerPath;\n if (routeName && rpcMetadata?.type === RPCType.HTTP) {\n rpcMetadata.route = routeName;\n }\n done();\n };\n }\n\n private _wrapHandler(\n pluginName: string,\n hookName: string,\n original: (...args: unknown[]) => Promise<unknown>,\n syncFunctionWithDone: boolean\n ): () => Promise<unknown> {\n const instrumentation = this;\n this._diag.debug('Patching fastify route.handler function');\n\n return function (this: any, ...args: unknown[]): Promise<unknown> {\n if (!instrumentation.isEnabled()) {\n return original.apply(this, args);\n }\n\n const name = original.name || pluginName || ANONYMOUS_NAME;\n const spanName = `${FastifyNames.MIDDLEWARE} - ${name}`;\n\n const reply = args[1] as PluginFastifyReply;\n\n const span = startSpan(reply, instrumentation.tracer, spanName, {\n [AttributeNames.FASTIFY_TYPE]: FastifyTypes.MIDDLEWARE,\n [AttributeNames.PLUGIN_NAME]: pluginName,\n [AttributeNames.HOOK_NAME]: hookName,\n });\n\n const origDone =\n syncFunctionWithDone &&\n (args[args.length - 1] as HookHandlerDoneFunction);\n if (origDone) {\n args[args.length - 1] = function (\n ...doneArgs: Parameters<HookHandlerDoneFunction>\n ) {\n endSpan(reply);\n origDone.apply(this, doneArgs);\n };\n }\n\n return context.with(trace.setSpan(context.active(), span), () => {\n return safeExecuteInTheMiddleMaybePromise(\n () => {\n return original.apply(this, args);\n },\n err => {\n if (err instanceof Error) {\n span.setStatus({\n code: SpanStatusCode.ERROR,\n message: err.message,\n });\n span.recordException(err);\n }\n // async hooks should end the span as soon as the promise is resolved\n if (!syncFunctionWithDone) {\n endSpan(reply);\n }\n }\n );\n });\n };\n }\n\n private _wrapAddHook(): (\n original: FastifyInstance['addHook']\n ) => () => FastifyInstance {\n const instrumentation = this;\n this._diag.debug('Patching fastify server.addHook function');\n\n return function (\n original: FastifyInstance['addHook']\n ): () => FastifyInstance {\n return function wrappedAddHook(this: any, ...args: any) {\n const name = args[0] as string;\n const handler = args[1] as HandlerOriginal;\n const pluginName = this.pluginName;\n if (applicationHookNames.includes(name)) {\n return original.apply(this, [name, handler] as never);\n }\n\n const syncFunctionWithDone =\n typeof args[args.length - 1] === 'function' &&\n handler.constructor.name !== 'AsyncFunction';\n\n return original.apply(this, [\n name,\n instrumentation._wrapHandler(\n pluginName,\n name,\n handler,\n syncFunctionWithDone\n ),\n ] as never);\n };\n };\n }\n\n private _patchConstructor(moduleExports: {\n fastify: () => FastifyInstance;\n }): () => FastifyInstance {\n const instrumentation = this;\n this._diag.debug('Patching fastify constructor function');\n\n function fastify(this: FastifyInstance, ...args: any) {\n const app: FastifyInstance = moduleExports.fastify.apply(this, args);\n app.addHook('onRequest', instrumentation._hookOnRequest());\n app.addHook('preHandler', instrumentation._hookPreHandler());\n\n instrumentation._wrap(app, 'addHook', instrumentation._wrapAddHook());\n\n return app;\n }\n\n fastify.fastify = fastify;\n fastify.default = fastify;\n return fastify;\n }\n\n private _patchSend() {\n const instrumentation = this;\n this._diag.debug('Patching fastify reply.send function');\n\n return function patchSend(\n original: () => FastifyReply\n ): () => FastifyReply {\n return function send(this: FastifyReply, ...args: any) {\n const maybeError: any = args[0];\n\n if (!instrumentation.isEnabled()) {\n return original.apply(this, args);\n }\n\n return safeExecuteInTheMiddle<FastifyReply>(\n () => {\n return original.apply(this, args);\n },\n err => {\n if (!err && maybeError instanceof Error) {\n err = maybeError;\n }\n endSpan(this, err);\n }\n );\n };\n };\n }\n\n private _hookPreHandler() {\n const instrumentation = this;\n this._diag.debug('Patching fastify preHandler function');\n\n return function preHandler(\n this: any,\n request: FastifyRequest,\n reply: FastifyReply,\n done: HookHandlerDoneFunction\n ) {\n if (!instrumentation.isEnabled()) {\n return done();\n }\n const anyRequest = request as any;\n\n const handler =\n anyRequest.routeOptions?.handler || anyRequest.context?.handler || {};\n\n const handlerName = handler?.name.substr(6);\n const spanName = `${FastifyNames.REQUEST_HANDLER} - ${\n handlerName || this.pluginName || ANONYMOUS_NAME\n }`;\n\n const spanAttributes: SpanAttributes = {\n [AttributeNames.PLUGIN_NAME]: this.pluginName,\n [AttributeNames.FASTIFY_TYPE]: FastifyTypes.REQUEST_HANDLER,\n [SemanticAttributes.HTTP_ROUTE]:\n anyRequest.routeOptions?.config?.url || request.routerPath,\n };\n if (handlerName) {\n spanAttributes[AttributeNames.FASTIFY_NAME] = handlerName;\n }\n const span = startSpan(\n reply,\n instrumentation.tracer,\n spanName,\n spanAttributes\n );\n\n if (instrumentation.getConfig().requestHook) {\n safeExecuteInTheMiddle(\n () => instrumentation.getConfig().requestHook!(span, { request }),\n e => {\n if (e) {\n instrumentation._diag.error('request hook failed', e);\n }\n },\n true\n );\n }\n\n return context.with(trace.setSpan(context.active(), span), () => {\n done();\n });\n };\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"instrumentation.js","sourceRoot":"","sources":["../../src/instrumentation.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,4CAK4B;AAC5B,8CAA8D;AAC9D,oEAIwC;AACxC,8EAAyE;AAOzE,2CAA+C;AAC/C,2DAIgC;AAGhC,mCAIiB;AACjB,uCAAoC;AAEvB,QAAA,cAAc,GAAG,WAAW,CAAC;AAE1C,gDAAgD;AAChD,MAAa,sBAAuB,SAAQ,qCAAmB;IAC7D,YAAY,SAAuC,EAAE;QACnD,KAAK,CACH,wCAAwC,EACxC,iBAAO,EACP,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,CAC1B,CAAC;IACJ,CAAC;IAEQ,SAAS,CAAC,SAAuC,EAAE;QAC1D,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;IAC3C,CAAC;IAEQ,SAAS;QAChB,OAAO,IAAI,CAAC,OAAuC,CAAC;IACtD,CAAC;IAED,IAAI;QACF,OAAO;YACL,IAAI,qDAAmC,CACrC,SAAS,EACT,CAAC,QAAQ,EAAE,QAAQ,CAAC,EACpB,CAAC,aAAa,EAAE,aAAa,EAAE,EAAE;gBAC/B,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,8BAA8B,aAAa,EAAE,C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S,EACpB,KAAK,EACL,eAAe,CAAC,MAAM,EACtB,QAAQ,EACR,cAAc,CACf,CAAC;YAEF,IAAI,eAAe,CAAC,SAAS,EAAE,CAAC,WAAW,EAAE;gBAC3C,IAAA,wCAAsB,EACpB,GAAG,EAAE,CAAC,eAAe,CAAC,SAAS,EAAE,CAAC,WAAY,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,CAAC,EACjE,CAAC,CAAC,EAAE;oBACF,IAAI,CAAC,EAAE;wBACL,eAAe,CAAC,KAAK,CAAC,KAAK,CAAC,qBAAqB,EAAE,CAAC,CAAC,CAAC;qBACvD;gBACH,CAAC,EACD,IAAI,CACL,CAAC;aACH;YAED,OAAO,aAAO,CAAC,IAAI,CAAC,WAAK,CAAC,OAAO,CAAC,aAAO,CAAC,MAAM,EAAE,EAAE,IAAI,CAAC,EAAE,GAAG,EAAE;gBAC9D,IAAI,EAAE,CAAC;YACT,CAAC,CAAC,CAAC;QACL,CAAC,CAAC;IACJ,CAAC;CACF;AAlQD,wDAkQC","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 context,\n SpanAttributes,\n SpanStatusCode,\n trace,\n} from '@opentelemetry/api';\nimport { getRPCMetadata, RPCType } from '@opentelemetry/core';\nimport {\n InstrumentationBase,\n InstrumentationNodeModuleDefinition,\n safeExecuteInTheMiddle,\n} from '@opentelemetry/instrumentation';\nimport { SemanticAttributes } from '@opentelemetry/semantic-conventions';\nimport type {\n HookHandlerDoneFunction,\n FastifyInstance,\n FastifyRequest,\n FastifyReply,\n} from 'fastify';\nimport { hooksNamesToWrap } from './constants';\nimport {\n AttributeNames,\n FastifyNames,\n FastifyTypes,\n} from './enums/AttributeNames';\nimport type { HandlerOriginal, PluginFastifyReply } from './internal-types';\nimport type { FastifyInstrumentationConfig } from './types';\nimport {\n endSpan,\n safeExecuteInTheMiddleMaybePromise,\n startSpan,\n} from './utils';\nimport { VERSION } from './version';\n\nexport const ANONYMOUS_NAME = 'anonymous';\n\n/** Fastify instrumentation for OpenTelemetry */\nexport class FastifyInstrumentation extends InstrumentationBase {\n constructor(config: FastifyInstrumentationConfig = {}) {\n super(\n '@opentelemetry/instrumentation-fastify',\n VERSION,\n Object.assign({}, config)\n );\n }\n\n override setConfig(config: FastifyInstrumentationConfig = {}) {\n this._config = Object.assign({}, config);\n }\n\n override getConfig(): FastifyInstrumentationConfig {\n return this._config as FastifyInstrumentationConfig;\n }\n\n init() {\n return [\n new InstrumentationNodeModuleDefinition<any>(\n 'fastify',\n ['^3.0.0', '^4.0.0'],\n (moduleExports, moduleVersion) => {\n this._diag.debug(`Applying patch for fastify@${moduleVersion}`);\n return this._patchConstructor(moduleExports);\n }\n ),\n ];\n }\n\n private _hookOnRequest() {\n const instrumentation = this;\n return function onRequest(\n request: FastifyRequest,\n reply: FastifyReply,\n done: HookHandlerDoneFunction\n ) {\n if (!instrumentation.isEnabled()) {\n return done();\n }\n instrumentation._wrap(reply, 'send', instrumentation._patchSend());\n\n const anyRequest = request as any;\n\n const rpcMetadata = getRPCMetadata(context.active());\n const routeName = anyRequest.routeOptions\n ? anyRequest.routeOptions.url // since fastify@4.10.0\n : request.routerPath;\n if (routeName && rpcMetadata?.type === RPCType.HTTP) {\n rpcMetadata.route = routeName;\n }\n done();\n };\n }\n\n private _wrapHandler(\n pluginName: string,\n hookName: string,\n original: (...args: unknown[]) => Promise<unknown>,\n syncFunctionWithDone: boolean\n ): () => Promise<unknown> {\n const instrumentation = this;\n this._diag.debug('Patching fastify route.handler function');\n\n return function (this: any, ...args: unknown[]): Promise<unknown> {\n if (!instrumentation.isEnabled()) {\n return original.apply(this, args);\n }\n\n const name = original.name || pluginName || ANONYMOUS_NAME;\n const spanName = `${FastifyNames.MIDDLEWARE} - ${name}`;\n\n const reply = args[1] as PluginFastifyReply;\n\n const span = startSpan(reply, instrumentation.tracer, spanName, {\n [AttributeNames.FASTIFY_TYPE]: FastifyTypes.MIDDLEWARE,\n [AttributeNames.PLUGIN_NAME]: pluginName,\n [AttributeNames.HOOK_NAME]: hookName,\n });\n\n const origDone =\n syncFunctionWithDone &&\n (args[args.length - 1] as HookHandlerDoneFunction);\n if (origDone) {\n args[args.length - 1] = function (\n ...doneArgs: Parameters<HookHandlerDoneFunction>\n ) {\n endSpan(reply);\n origDone.apply(this, doneArgs);\n };\n }\n\n return context.with(trace.setSpan(context.active(), span), () => {\n return safeExecuteInTheMiddleMaybePromise(\n () => {\n return original.apply(this, args);\n },\n err => {\n if (err instanceof Error) {\n span.setStatus({\n code: SpanStatusCode.ERROR,\n message: err.message,\n });\n span.recordException(err);\n }\n // async hooks should end the span as soon as the promise is resolved\n if (!syncFunctionWithDone) {\n endSpan(reply);\n }\n }\n );\n });\n };\n }\n\n private _wrapAddHook(): (\n original: FastifyInstance['addHook']\n ) => () => FastifyInstance {\n const instrumentation = this;\n this._diag.debug('Patching fastify server.addHook function');\n\n return function (\n original: FastifyInstance['addHook']\n ): () => FastifyInstance {\n return function wrappedAddHook(this: any, ...args: any) {\n const name = args[0] as string;\n const handler = args[1] as HandlerOriginal;\n const pluginName = this.pluginName;\n if (!hooksNamesToWrap.has(name)) {\n return original.apply(this, args);\n }\n\n const syncFunctionWithDone =\n typeof args[args.length - 1] === 'function' &&\n handler.constructor.name !== 'AsyncFunction';\n\n return original.apply(this, [\n name,\n instrumentation._wrapHandler(\n pluginName,\n name,\n handler,\n syncFunctionWithDone\n ),\n ] as never);\n };\n };\n }\n\n private _patchConstructor(moduleExports: {\n fastify: () => FastifyInstance;\n }): () => FastifyInstance {\n const instrumentation = this;\n this._diag.debug('Patching fastify constructor function');\n\n function fastify(this: FastifyInstance, ...args: any) {\n const app: FastifyInstance = moduleExports.fastify.apply(this, args);\n app.addHook('onRequest', instrumentation._hookOnRequest());\n app.addHook('preHandler', instrumentation._hookPreHandler());\n\n instrumentation._wrap(app, 'addHook', instrumentation._wrapAddHook());\n\n return app;\n }\n\n fastify.fastify = fastify;\n fastify.default = fastify;\n return fastify;\n }\n\n private _patchSend() {\n const instrumentation = this;\n this._diag.debug('Patching fastify reply.send function');\n\n return function patchSend(\n original: () => FastifyReply\n ): () => FastifyReply {\n return function send(this: FastifyReply, ...args: any) {\n const maybeError: any = args[0];\n\n if (!instrumentation.isEnabled()) {\n return original.apply(this, args);\n }\n\n return safeExecuteInTheMiddle<FastifyReply>(\n () => {\n return original.apply(this, args);\n },\n err => {\n if (!err && maybeError instanceof Error) {\n err = maybeError;\n }\n endSpan(this, err);\n }\n );\n };\n };\n }\n\n private _hookPreHandler() {\n const instrumentation = this;\n this._diag.debug('Patching fastify preHandler function');\n\n return function preHandler(\n this: any,\n request: FastifyRequest,\n reply: FastifyReply,\n done: HookHandlerDoneFunction\n ) {\n if (!instrumentation.isEnabled()) {\n return done();\n }\n const anyRequest = request as any;\n\n const handler =\n anyRequest.routeOptions?.handler || anyRequest.context?.handler;\n\n const handlerName = handler?.name.startsWith('bound ')\n ? handler.name.substr(6)\n : handler?.name;\n const spanName = `${FastifyNames.REQUEST_HANDLER} - ${\n handlerName || this.pluginName || ANONYMOUS_NAME\n }`;\n\n const spanAttributes: SpanAttributes = {\n [AttributeNames.PLUGIN_NAME]: this.pluginName,\n [AttributeNames.FASTIFY_TYPE]: FastifyTypes.REQUEST_HANDLER,\n [SemanticAttributes.HTTP_ROUTE]: anyRequest.routeOptions\n ? anyRequest.routeOptions.url // since fastify@4.10.0\n : request.routerPath,\n };\n if (handlerName) {\n spanAttributes[AttributeNames.FASTIFY_NAME] = handlerName;\n }\n const span = startSpan(\n reply,\n instrumentation.tracer,\n spanName,\n spanAttributes\n );\n\n if (instrumentation.getConfig().requestHook) {\n safeExecuteInTheMiddle(\n () => instrumentation.getConfig().requestHook!(span, { request }),\n e => {\n if (e) {\n instrumentation._diag.error('request hook failed', e);\n }\n },\n true\n );\n }\n\n return context.with(trace.setSpan(context.active(), span), () => {\n done();\n });\n };\n }\n}\n"]}
|
package/build/src/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const VERSION = "0.32.
|
|
1
|
+
export declare const VERSION = "0.32.5";
|
|
2
2
|
//# sourceMappingURL=version.d.ts.map
|
package/build/src/version.js
CHANGED
package/build/src/version.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,4DAA4D;AAC/C,QAAA,OAAO,GAAG,QAAQ,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\n// this is autogenerated file, see scripts/version-update.js\nexport const VERSION = '0.32.
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,4DAA4D;AAC/C,QAAA,OAAO,GAAG,QAAQ,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\n// this is autogenerated file, see scripts/version-update.js\nexport const VERSION = '0.32.5';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opentelemetry/instrumentation-fastify",
|
|
3
|
-
"version": "0.32.
|
|
3
|
+
"version": "0.32.5",
|
|
4
4
|
"description": "OpenTelemetry fastify automatic instrumentation package.",
|
|
5
5
|
"main": "build/src/index.js",
|
|
6
6
|
"types": "build/src/index.d.ts",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"lint": "eslint . --ext .ts",
|
|
12
12
|
"lint:fix": "eslint . --ext .ts --fix",
|
|
13
13
|
"precompile": "tsc --version && lerna run version:update --scope @opentelemetry/instrumentation-fastify --include-dependencies",
|
|
14
|
-
"
|
|
14
|
+
"prepublishOnly": "npm run compile",
|
|
15
15
|
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'",
|
|
16
16
|
"test-all-versions": "tav",
|
|
17
17
|
"version:update": "node ../../../scripts/version-update.js",
|
|
@@ -34,10 +34,7 @@
|
|
|
34
34
|
"files": [
|
|
35
35
|
"build/src/**/*.js",
|
|
36
36
|
"build/src/**/*.js.map",
|
|
37
|
-
"build/src/**/*.d.ts"
|
|
38
|
-
"doc",
|
|
39
|
-
"LICENSE",
|
|
40
|
-
"README.md"
|
|
37
|
+
"build/src/**/*.d.ts"
|
|
41
38
|
],
|
|
42
39
|
"publishConfig": {
|
|
43
40
|
"access": "public"
|
|
@@ -49,25 +46,28 @@
|
|
|
49
46
|
"@fastify/express": "^2.0.2",
|
|
50
47
|
"@opentelemetry/api": "^1.3.0",
|
|
51
48
|
"@opentelemetry/context-async-hooks": "^1.8.0",
|
|
52
|
-
"@opentelemetry/
|
|
49
|
+
"@opentelemetry/contrib-test-utils": "^0.35.0",
|
|
50
|
+
"@opentelemetry/instrumentation-http": "^0.45.1",
|
|
53
51
|
"@opentelemetry/sdk-trace-base": "^1.8.0",
|
|
54
52
|
"@opentelemetry/sdk-trace-node": "^1.8.0",
|
|
55
53
|
"@types/express": "4.17.18",
|
|
56
54
|
"@types/mocha": "7.0.2",
|
|
57
55
|
"@types/node": "18.15.3",
|
|
56
|
+
"@types/semver": "7.5.5",
|
|
58
57
|
"fastify": "4.18.0",
|
|
59
58
|
"mocha": "7.2.0",
|
|
60
59
|
"nyc": "15.1.0",
|
|
61
60
|
"rimraf": "5.0.5",
|
|
61
|
+
"semver": "^7.5.4",
|
|
62
62
|
"test-all-versions": "5.0.1",
|
|
63
63
|
"ts-mocha": "10.0.0",
|
|
64
64
|
"typescript": "4.4.4"
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
67
|
"@opentelemetry/core": "^1.8.0",
|
|
68
|
-
"@opentelemetry/instrumentation": "^0.
|
|
68
|
+
"@opentelemetry/instrumentation": "^0.45.1",
|
|
69
69
|
"@opentelemetry/semantic-conventions": "^1.0.0"
|
|
70
70
|
},
|
|
71
71
|
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-fastify#readme",
|
|
72
|
-
"gitHead": "
|
|
72
|
+
"gitHead": "c7e7000b7bf79b0b107c448b403c2613a9b9e2c1"
|
|
73
73
|
}
|