Skip to content

Commit

Permalink
fix(instrumentation-fastify): Support both CommonJS and ESM (open-tel…
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwood091 committed Jul 14, 2023
1 parent 1195872 commit f6200ef
Show file tree
Hide file tree
Showing 5 changed files with 925 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"lint:fix": "eslint . --ext .ts --fix",
"precompile": "tsc --version && lerna run version:update --scope @opentelemetry/instrumentation-fastify --include-dependencies",
"prepare": "npm run compile",
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'",
"test:cjs": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'",
"test:esm": "nyc node --experimental-loader=@opentelemetry/instrumentation/hook.mjs ./node_modules/mocha/bin/mocha 'test/**/*.test.mjs'",
"test": "npm run test:cjs && npm run test:esm",
"version:update": "node ../../../scripts/version-update.js",
"prewatch": "npm run precompile",
"watch": "tsc -w"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,17 @@ import {
FastifyNames,
FastifyTypes,
} from './enums/AttributeNames';
import type { HandlerOriginal, PluginFastifyReply } from './internal-types';
import type {
CjsFastifyModuleExports,
EsmFastifyModuleExports,
FastifyModuleExports,
HandlerOriginal,
PluginFastifyReply,
} from './internal-types';
import type { FastifyInstrumentationConfig } from './types';
import {
endSpan,
isEsmFastifyModuleExports,
safeExecuteInTheMiddleMaybePromise,
startSpan,
} from './utils';
Expand Down Expand Up @@ -197,24 +204,48 @@ export class FastifyInstrumentation extends InstrumentationBase {
};
}

private _patchConstructor(
original: () => FastifyInstance
): () => FastifyInstance {
private _getPatchedFastifyInstance(module: () => FastifyInstance) {
const instrumentation = this;
this._diag.debug('Patching fastify constructor function');

function fastify(this: FastifyInstance, ...args: any) {
const app: FastifyInstance = original.apply(this, args);
return function fastify(this: FastifyInstance, ...args: any) {
const app: FastifyInstance = module.apply(this, args);
app.addHook('onRequest', instrumentation._hookOnRequest());
app.addHook('preHandler', instrumentation._hookPreHandler());

instrumentation._wrap(app, 'addHook', instrumentation._wrapAddHook());

return app;
}
};
}

private _patchConstructor(
original: FastifyModuleExports
): () => FastifyInstance {
this._diag.debug('Patching fastify constructor function');

return isEsmFastifyModuleExports(original)
? this._patchConstructorInEsm(original)
: this._patchConstructorInCjs(original);
}

private _patchConstructorInEsm(
original: EsmFastifyModuleExports
): () => FastifyInstance {
const fastify = this._getPatchedFastifyInstance(original.default);

this._wrap(original, 'default', () => fastify);
this._wrap(original, 'fastify', () => fastify);

return fastify;
}

private _patchConstructorInCjs(
original: CjsFastifyModuleExports
): () => FastifyInstance {
const fastify = this._getPatchedFastifyInstance(original);

Object.assign(fastify, { default: fastify, fastify });

fastify.fastify = fastify;
fastify.default = fastify;
return fastify;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,22 @@
*/

import { Span } from '@opentelemetry/api';
import type { FastifyReply } from 'fastify';
import type { FastifyInstance, FastifyReply } from 'fastify';
import { spanRequestSymbol } from './constants';

export type HandlerOriginal = (() => Promise<unknown>) & (() => void);

export type PluginFastifyReply = FastifyReply & {
[spanRequestSymbol]?: Span[];
};

export type CjsFastifyModuleExports = () => FastifyInstance;

export type EsmFastifyModuleExports = {
default: () => FastifyInstance;
fastify: () => FastifyInstance;
};

export type FastifyModuleExports =
| CjsFastifyModuleExports
| EsmFastifyModuleExports;
16 changes: 15 additions & 1 deletion plugins/node/opentelemetry-instrumentation-fastify/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import {
} from '@opentelemetry/api';
import { spanRequestSymbol } from './constants';

import type { PluginFastifyReply } from './internal-types';
import type {
EsmFastifyModuleExports,
FastifyModuleExports,
PluginFastifyReply,
} from './internal-types';

/**
* Starts Span
Expand Down Expand Up @@ -137,3 +141,13 @@ function isPromise<T>(val: T | Promise<T>): val is Promise<T> {
false
);
}

/**
* Type guard for EsmFastifyModuleExports.
* @param module - Fastify's module exports.
*/
export function isEsmFastifyModuleExports(
module: FastifyModuleExports
): module is EsmFastifyModuleExports {
return 'default' in module;
}
Loading

0 comments on commit f6200ef

Please sign in to comment.