Skip to content

Commit

Permalink
feat(opentelemetry-instrumentation-fastify): Support Fastify V4 also
Browse files Browse the repository at this point in the history
  • Loading branch information
markrzen committed Sep 9, 2022
1 parent d3cb60d commit d46f326
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@
"@opentelemetry/api": "^1.0.0"
},
"devDependencies": {
"@fastify/express": "^2.0.2",
"@opentelemetry/api": "^1.0.0",
"@opentelemetry/context-async-hooks": "^1.3.1",
"@opentelemetry/instrumentation-http": "0.30.0",
"@opentelemetry/sdk-trace-node": "^1.3.1",
"@opentelemetry/sdk-trace-base": "^1.3.1",
"@opentelemetry/sdk-trace-node": "^1.3.1",
"@types/express": "4.17.13",
"@types/mocha": "7.0.2",
"@types/node": "16.11.21",
"fastify-express": "0.3.3",
"gts": "3.1.0",
"mocha": "7.2.0",
"nyc": "15.1.0",
Expand All @@ -65,7 +65,7 @@
"@opentelemetry/core": "^1.0.0",
"@opentelemetry/instrumentation": "^0.32.0",
"@opentelemetry/semantic-conventions": "^1.0.0",
"fastify": "^3.19.2"
"fastify": "^4.5.3"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-fastify#readme"
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class FastifyInstrumentation extends InstrumentationBase {
return [
new InstrumentationNodeModuleDefinition<any>(
'fastify',
['^3.0.0'],
['>=3.0.0'],
(moduleExports, moduleVersion) => {
this._diag.debug(`Applying patch for fastify@${moduleVersion}`);
return this._patchConstructor(moduleExports);
Expand Down Expand Up @@ -100,6 +100,8 @@ export class FastifyInstrumentation extends InstrumentationBase {
syncFunctionWithDone: boolean
): () => Promise<unknown> {
const instrumentation = this;
this._diag.debug('Patching fastify route.handler function');

return function (this: any, ...args: unknown[]): Promise<unknown> {
if (!instrumentation.isEnabled()) {
return original.apply(this, args);
Expand Down Expand Up @@ -156,6 +158,8 @@ export class FastifyInstrumentation extends InstrumentationBase {
original: FastifyInstance['addHook']
) => () => FastifyInstance {
const instrumentation = this;
this._diag.debug('Patching fastify server.addHook function');

return function (
original: FastifyInstance['addHook']
): () => FastifyInstance {
Expand Down Expand Up @@ -188,6 +192,7 @@ export class FastifyInstrumentation extends InstrumentationBase {
original: () => FastifyInstance
): () => FastifyInstance {
const instrumentation = this;
this._diag.debug('Patching fastify constructor function');

function fastify(this: FastifyInstance, ...args: any) {
const app: FastifyInstance = original.apply(this, args);
Expand All @@ -206,6 +211,8 @@ export class FastifyInstrumentation extends InstrumentationBase {

public _patchSend() {
const instrumentation = this;
this._diag.debug('Patching fastify reply.send function');

return function patchSend(
original: () => FastifyReply
): () => FastifyReply {
Expand Down Expand Up @@ -233,6 +240,8 @@ export class FastifyInstrumentation extends InstrumentationBase {

public _hookPreHandler() {
const instrumentation = this;
this._diag.debug('Patching fastify preHandler function');

return function preHandler(
this: any,
request: FastifyRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ provider.addSpanProcessor(spanProcessor);
instrumentation.enable();
httpInstrumentation.enable();

import 'fastify-express';
import '@fastify/express';
import { FastifyInstance } from 'fastify/types/instance';

const Fastify = require('fastify');
Expand All @@ -86,15 +86,15 @@ describe('fastify', () => {
let app: FastifyInstance;

async function startServer(): Promise<void> {
const address = await app.listen(0);
const address = await app.listen({ port: 0 });
const url = new URL(address);
PORT = parseInt(url.port, 10);
}

beforeEach(async () => {
instrumentation.enable();
app = Fastify();
app.register(require('fastify-express'));
app.register(require('@fastify/express'));
});

afterEach(async () => {
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('fastify', () => {
const span = spans[3];
assert.deepStrictEqual(span.attributes, {
'fastify.type': 'request_handler',
'plugin.name': 'fastify-express',
'plugin.name': 'fastify -> @fastify/express',
[SemanticAttributes.HTTP_ROUTE]: '/test',
});
assert.strictEqual(span.name, `request handler - ${ANONYMOUS_NAME}`);
Expand All @@ -160,7 +160,7 @@ describe('fastify', () => {
assert.deepStrictEqual(span.attributes, {
'fastify.type': 'request_handler',
'fastify.name': 'namedHandler',
'plugin.name': 'fastify-express',
'plugin.name': 'fastify -> @fastify/express',
[SemanticAttributes.HTTP_ROUTE]: '/test',
});
assert.strictEqual(span.name, 'request handler - namedHandler');
Expand Down Expand Up @@ -224,7 +224,7 @@ describe('fastify', () => {
assert.strictEqual(span.name, 'middleware - runConnect');
assert.deepStrictEqual(span.attributes, {
'fastify.type': 'middleware',
'plugin.name': 'fastify-express',
'plugin.name': 'fastify -> @fastify/express',
'hook.name': 'onRequest',
});

Expand All @@ -240,7 +240,7 @@ describe('fastify', () => {
assert.strictEqual(span.name, 'middleware - enhanceRequest');
assert.deepStrictEqual(span.attributes, {
'fastify.type': 'middleware',
'plugin.name': 'fastify-express',
'plugin.name': 'fastify -> @fastify/express',
'hook.name': 'onRequest',
});

Expand Down Expand Up @@ -330,12 +330,12 @@ describe('fastify', () => {

// done was not yet called from the hook, so it should not end the span
const preDoneSpans = getSpans().filter(
s => !s.attributes[AttributeNames.PLUGIN_NAME]
s => !s.attributes[AttributeNames.PLUGIN_NAME] || s.attributes[AttributeNames.PLUGIN_NAME] === 'fastify'
);
assert.strictEqual(preDoneSpans.length, 0);
hookDone!();
const postDoneSpans = getSpans().filter(
s => !s.attributes[AttributeNames.PLUGIN_NAME]
s => !s.attributes[AttributeNames.PLUGIN_NAME] || s.attributes[AttributeNames.PLUGIN_NAME] === 'fastify'
);
assert.strictEqual(postDoneSpans.length, 1);
});
Expand All @@ -359,7 +359,7 @@ describe('fastify', () => {
await startServer();
await httpRequest.get(`http://localhost:${PORT}/test`);
const spans = getSpans().filter(
s => !s.attributes[AttributeNames.PLUGIN_NAME]
s => !s.attributes[AttributeNames.PLUGIN_NAME] || s.attributes[AttributeNames.PLUGIN_NAME] === 'fastify'
);
assert.strictEqual(spans.length, 1);
});
Expand Down

0 comments on commit d46f326

Please sign in to comment.