Skip to content

Commit ca4a561

Browse files
authored
fix: log plugin name in case the handler throws (#6947)
In case a plugin handler fails, it will deactivate the plugin. While doing so, the plugin that failed is logged. The id is placed on the constructor though, not on the instance. Due to that, it would log undefined so far. Fixing it by using the constructor is also not ideal though, due to having more than a single plugin with the same id. That id represents the instrumentation, not the plugin itself. An instrumentation can actually have multiple plugins and the failure will only deactivate a specific plugin. Therefore, the information about what plugin will be deactivated should log the name of the plugin instead of the id.
1 parent 26e7402 commit ca4a561

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

packages/dd-trace/src/plugins/plugin.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,13 @@ module.exports = class Plugin {
118118
* @returns {void}
119119
*/
120120
addSub (channelName, handler) {
121-
const plugin = this
122-
/**
123-
* @this {unknown}
124-
* @returns {unknown}
125-
*/
126-
const wrappedHandler = function () {
121+
const wrappedHandler = (...args) => {
127122
try {
128-
return handler.apply(this, arguments)
129-
} catch (e) {
130-
logger.error('Error in plugin handler:', e)
131-
logger.info('Disabling plugin: %s', plugin.id)
132-
plugin.configure(false)
123+
return handler.apply(this, args)
124+
} catch (error) {
125+
logger.error('Error in plugin handler:', error)
126+
logger.info('Disabling plugin: %s', this.constructor.name)
127+
this.configure(false)
133128
}
134129
}
135130
this._subscriptions.push(new Subscription(channelName, wrappedHandler))

packages/dd-trace/test/plugins/plugin.spec.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
'use strict'
22

3-
const { expect } = require('chai')
3+
const assert = require('node:assert')
4+
45
const { describe, it, after } = require('tap').mocha
56
const { channel } = require('dc-polyfill')
7+
const proxyquire = require('proxyquire')
8+
const sinon = require('sinon')
69

710
require('../setup/core')
811

9-
const Plugin = require('../../src/plugins/plugin')
12+
const log = {
13+
error: sinon.stub(),
14+
info: sinon.stub()
15+
}
16+
17+
const Plugin = proxyquire('../../src/plugins/plugin', {
18+
'../log': log
19+
})
1020
const { storage } = require('../../../datadog-core')
1121

1222
describe('Plugin', () => {
@@ -34,7 +44,7 @@ describe('Plugin', () => {
3444
}
3545

3646
start () {
37-
//
47+
assert.strictEqual(this, plugin)
3848
}
3949
}
4050

@@ -46,22 +56,25 @@ describe('Plugin', () => {
4656
plugin = new BadPlugin()
4757
plugin.configure({ enabled: true })
4858

49-
expect(plugin._enabled).to.be.true
59+
assert.strictEqual(plugin._enabled, true)
5060

5161
channel('apm:badPlugin:start').publish({ foo: 'bar' })
5262

53-
expect(plugin._enabled).to.be.false
63+
sinon.assert.calledWith(log.error, 'Error in plugin handler:', sinon.match.instanceOf(Error))
64+
sinon.assert.calledWith(log.info, 'Disabling plugin: %s', 'BadPlugin')
65+
66+
assert.strictEqual(plugin._enabled, false)
5467
})
5568

5669
it('should not disable with no error', () => {
5770
plugin = new GoodPlugin()
5871
plugin.configure({ enabled: true })
5972

60-
expect(plugin._enabled).to.be.true
73+
assert.strictEqual(plugin._enabled, true)
6174

6275
channel('apm:goodPlugin:start').publish({ foo: 'bar' })
6376

64-
expect(plugin._enabled).to.be.true
77+
assert.strictEqual(plugin._enabled, true)
6578
})
6679

6780
it('should run binding transforms with an undefined current store', () => {
@@ -79,7 +92,7 @@ describe('Plugin', () => {
7992

8093
storage('legacy').run({ noop: true }, () => {
8194
channel('apm:test:start').runStores({ currentStore: undefined }, () => {
82-
expect(storage('legacy').getStore()).to.equal(undefined)
95+
assert.strictEqual(storage('legacy').getStore(), undefined)
8396
})
8497
})
8598
})

0 commit comments

Comments
 (0)