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

fix: Fixed instrumentation of Fastify via ESM #2158

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions lib/instrumentation/fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
buildMiddlewareSpecForMiddlewareFunction
} = require('./fastify/spec-builders')
const { MiddlewareMounterSpec } = require('../shim/specs')
const symbols = require('../symbols')

/**
* These are the events that occur during a fastify
Expand Down Expand Up @@ -95,6 +96,9 @@ module.exports = function initialize(agent, fastify, moduleName, shim) {
*/
const wrappedExport = shim.wrapExport(fastify, function wrapFastifyModule(shim, fn) {
return function wrappedFastifyModule() {
if (fn[symbols.nrEsmProxy] === true) {
fn = fn.fastify
}
// call original function get get fastify object (which is singleton-ish)
const fastifyForWrapping = fn.apply(this, arguments)

Expand Down
63 changes: 63 additions & 0 deletions test/versioned/fastify-esm/issue-2155.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2021 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

// We cannot replicate the issue using a dynamic `await import('fastify')`.
// Thus, we cannot write a typical `tap` based test. Instead, we need to rely
// on this script exiting cleanly to represent a successful test.

import helper from '../../lib/agent_helper.js'
helper.instrumentMockedAgent()

import assert from 'assert'
import http from 'http'

import fastify from 'fastify'
const server = fastify({
forceCloseConnections: true,
logger: {
level: 'silent'
}
})

server.route({
method: 'GET',
path: '/',
handler(req, res) {
res.send('ok')
}
})

const timeout = setTimeout(() => {
// eslint-disable-next-line no-process-exit
process.exit(1)
}, 20_000)

const address = await server.listen({ host: '127.0.0.1', port: 0 })
const found = await new Promise((resolve, reject) => {
const req = http.request(address, (res) => {
let data = ''
res.on('data', (d) => {
data += d.toString()
})
res.on('end', () => {
resolve(data)
})
})

req.on('error', reject)
req.end()
})

assert.equal(found, 'ok')

await server.close()
clearTimeout(timeout)

// We really shouldn't need this `process.exit`, but on Node.js 18 with our
// versioned tests runner we'll see the runner consistently hang without it.
// To see it, use Node 18 and `npm run versioned:internal fastify-esm`. Note,
// that it must be "versioned:internal" and not "versioned:internal:major".
// eslint-disable-next-line no-process-exit
process.exit(0)
25 changes: 25 additions & 0 deletions test/versioned/fastify-esm/newrelic.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2022 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

exports.config = {
app_name: ['My Application'],
license_key: 'license key here',
logging: {
level: 'trace',
filepath: '../../../newrelic_agent.log'
},
utilization: {
detect_aws: false,
detect_pcf: false,
detect_azure: false,
detect_gcp: false,
detect_docker: false
},
transaction_tracer: {
enabled: true
}
}
24 changes: 24 additions & 0 deletions test/versioned/fastify-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "fastify-esm-tests",
"targets-comment": "omitted as it is covered by the standard fastify tests",
"version": "0.0.0",
"type": "module",
"private": true,
"tests": [
{
"engines": {
"node": ">=16"
},
"dependencies": {
"fastify": ">=3.0.0",
"middie": "5.3.0"
},
"files": [
"issue-2155.test.mjs"
]
}
],
"engines": {
"node": ">=16"
}
}
Loading