Skip to content

Commit 9fdb0b8

Browse files
authored
refactor: Refactored q instrumentation to subscribe to events emitted (#3853)
1 parent ca3010a commit 9fdb0b8

File tree

7 files changed

+76
-37
lines changed

7 files changed

+76
-37
lines changed

lib/instrumentation/q.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

lib/instrumentations.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ module.exports = function instrumentations() {
2424
memcached: { type: InstrumentationDescriptor.TYPE_DATASTORE },
2525
mongodb: { type: InstrumentationDescriptor.TYPE_DATASTORE },
2626
next: { module: './instrumentation/nextjs' },
27-
q: { type: null },
2827
restify: { type: InstrumentationDescriptor.TYPE_WEB_FRAMEWORK },
2928
when: { module: './instrumentation/when' }
3029
}

lib/subscriber-configs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const subscribers = {
2828
...require('./subscribers/openai/config'),
2929
...require('./subscribers/pg/config'),
3030
...require('./subscribers/pino/config'),
31+
...require('./subscribers/q/config'),
3132
...require('./subscribers/redis/config'),
3233
...require('./subscribers/redis-client/config'),
3334
...require('./subscribers/undici/config'),

lib/subscribers/q/config.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2026 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
'use strict'
6+
7+
module.exports = {
8+
q: [
9+
{
10+
path: './q/next-tick',
11+
instrumentations: [{
12+
channelName: 'nr_nextTick',
13+
module: { name: 'q', versionRange: '>=1.3.0', filePath: 'q.js' },
14+
functionQuery: {
15+
expressionName: 'nextTick',
16+
kind: 'Sync'
17+
}
18+
}]
19+
},
20+
{
21+
path: './q/run-after',
22+
instrumentations: [{
23+
channelName: 'nr_runAfter',
24+
module: { name: 'q', versionRange: '>=1.3.0', filePath: 'q.js' },
25+
functionQuery: {
26+
expressionName: 'runAfter',
27+
kind: 'Sync'
28+
}
29+
}]
30+
}
31+
]
32+
}

lib/subscribers/q/next-tick.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2026 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
'use strict'
7+
8+
const BaseSubscriber = require('../base')
9+
10+
module.exports = class QNextTick extends BaseSubscriber {
11+
constructor({ agent, logger, channelName = 'nr_nextTick' }) {
12+
super({ agent, logger, channelName, packageName: 'q' })
13+
this.events = ['end']
14+
}
15+
16+
handler(data, ctx) {
17+
const { arguments: args } = data
18+
data.arguments[0] = this.agent.tracer.bindFunction(args[0], ctx)
19+
return ctx
20+
}
21+
}

lib/subscribers/q/run-after.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright 2026 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
'use strict'
7+
8+
const QNextTick = require('./next-tick')
9+
10+
module.exports = class QRunAfter extends QNextTick {
11+
constructor({ agent, logger }) {
12+
super({ agent, logger, channelName: 'nr_runAfter' })
13+
}
14+
}

test/versioned/q/q.test.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ test.afterEach((ctx) => {
3131
removeModules(['q'])
3232
})
3333

34-
test('should load tracking metrics', (t) => {
35-
const { agent } = t.nr
34+
test('should load tracking metrics', async (t) => {
35+
const { agent, q } = t.nr
3636
const { version } = require('q/package.json')
37-
assertPackageMetrics({ agent, pkg: 'q', version })
37+
await q()
38+
assertPackageMetrics({ agent, pkg: 'q', version, subscriberType: true })
3839
})
3940

40-
test('q.invoke', (t, end) => {
41+
test('q.invoke', async (t) => {
4142
const { agent, q } = t.nr
4243
const firstTest = q.defer()
4344
const secondTest = q.defer()
@@ -56,10 +57,10 @@ test('q.invoke', (t, end) => {
5657
})
5758
})
5859

59-
q.all([firstTest, secondTest]).then(() => end())
60+
await q.all([firstTest.promise, secondTest.promise])
6061
})
6162

62-
test('q.then', (t, end) => {
63+
test('q.then', async (t) => {
6364
const { agent, q } = t.nr
6465
const firstTest = q.defer()
6566
const secondTest = q.defer()
@@ -78,7 +79,7 @@ test('q.then', (t, end) => {
7879
})
7980
})
8081

81-
q.all([firstTest, secondTest]).then(() => end())
82+
await q.all([firstTest.promise, secondTest.promise])
8283
})
8384

8485
test('q.then rejections', async (t) => {

0 commit comments

Comments
 (0)