Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(opentelemetry-instrumentation-fastify): Support Fastify V4 also #1164

Merged
merged 4 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ npm install --save @opentelemetry/instrumentation-http @opentelemetry/instrument

### Supported Versions

- `^3.0.0`
- fastify: `^3.0.0 || ^4.0.0`

## Usage

Expand Down
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",
markrzen marked this conversation as resolved.
Show resolved Hide resolved
"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', '^4.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');
markrzen marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -94,15 +94,15 @@ describe('fastify', () => {
let app: FastifyInstance;

async function startServer(): Promise<void> {
const address = await app.listen(0);
const address = await app.listen({ port: 0 });
markrzen marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -145,7 +145,7 @@ describe('fastify', () => {
const span = spans[3];
assert.deepStrictEqual(span.attributes, {
'fastify.type': 'request_handler',
'plugin.name': 'fastify-express',
'plugin.name': 'fastify -> @fastify/express',
markrzen marked this conversation as resolved.
Show resolved Hide resolved
[SemanticAttributes.HTTP_ROUTE]: '/test',
});
assert.strictEqual(span.name, `request handler - ${ANONYMOUS_NAME}`);
Expand All @@ -168,7 +168,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 @@ -232,7 +232,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 @@ -248,7 +248,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 @@ -338,12 +338,16 @@ 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'
Comment on lines +342 to +343
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain what changed so that now we need this extra check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely. Fastify reworked their namespacing of plugins. All plugins are now under the fastify top level namespace.

What used to be "plugin.name": "my-plugin" is now "plugin.name": "fastify -> my-plugin".

The extra check is there so that if you wanted to switch between fastify versions and run tests, you could and still have consistent results.

Fastify 3.x checking if the plugin name does not exist is enough, for Fastify 4.x the equivalent is to check if the plugin name is just fastify.

If you would like me to remove it from the tests as you don't think y'all will be switching versions, I am totally happy to.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blumamir if the explaination is fine for you feel free to go ahead and merge this

);
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 @@ -367,7 +371,9 @@ 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