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 instrumented functions missing symbols from the original #1128

Merged
merged 4 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 10 additions & 0 deletions packages/datadog-plugin-dns/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const semver = require('semver')
const agent = require('../../dd-trace/test/plugins/agent')
const { promisify } = require('util')

wrapIt()

Expand Down Expand Up @@ -137,6 +138,15 @@ describe('Plugin', () => {
})
})

it('should work with promisify', () => {
const lookup = promisify(dns.lookup)

return lookup('localhost', 4).then(({ address, family }) => {
expect(address).to.equal('127.0.0.1')
expect(family).to.equal(4)
})
})

if (semver.gte(process.version, '8.3.0')) {
it('should instrument Resolver', done => {
const resolver = new dns.Resolver()
Expand Down
14 changes: 13 additions & 1 deletion packages/dd-trace/src/instrumenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,19 @@ class Instrumenter {
})
})

shimmer.massWrap.call(this, nodules, names, wrapper)
shimmer.massWrap.call(this, nodules, names, function (original, name) {
rochdev marked this conversation as resolved.
Show resolved Hide resolved
const wrapped = wrapper(original, name)
const props = Object.getOwnPropertyDescriptors(original)
const keys = Reflect.ownKeys(props)

for (const key of keys) {
if (typeof key !== 'symbol' || wrapped.hasOwnProperty(key)) continue

Object.defineProperty(wrapped, key, props[key])
}

return wrapped
})
}

unwrap (nodules, names, wrapper) {
Expand Down
46 changes: 36 additions & 10 deletions packages/dd-trace/test/instrumenter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

const proxyquire = require('proxyquire')
const path = require('path')
const shimmer = require('shimmer')
const { expect } = require('chai')

describe('Instrumenter', () => {
let Instrumenter
let instrumenter
let integrations
let tracer
let shimmer

beforeEach(() => {
tracer = {
Expand Down Expand Up @@ -50,12 +51,7 @@ describe('Instrumenter', () => {
}
}

shimmer = sinon.spy()
shimmer.massWrap = sinon.spy()
shimmer.massUnwrap = sinon.spy()

Instrumenter = proxyquire('../src/instrumenter', {
'shimmer': shimmer,
'./platform': {
plugins: {
'http': integrations.http,
Expand Down Expand Up @@ -299,12 +295,40 @@ describe('Instrumenter', () => {

describe('wrap', () => {
it('should wrap the method on the object', () => {
const method = () => {}
const obj = { method }
const wrapper = sinon.stub().returns(() => 'test')

instrumenter.wrap(obj, 'method', wrapper)

expect(wrapper).to.have.been.calledWith(method, 'method')
expect(obj.method()).to.equal('test')
})

it('should preserve symbols on the method', () => {
const sym = Symbol('foo')
const obj = { method: () => {} }
const wrapper = () => {}
const wrapper = () => () => {}

obj.method[sym] = 'bar'

instrumenter.wrap(obj, 'method', wrapper)

expect(shimmer.massWrap).to.have.been.calledWith([obj], ['method'], wrapper)
expect(obj.method).to.have.property(sym, 'bar')
})

it('should not override existing symbols on the shim', () => {
const sym = Symbol('foo')
const obj = { method: () => {} }
const shim = () => {}
const wrapper = () => shim

shim[sym] = 'invalid'
obj.method[sym] = 'bar'

instrumenter.wrap(obj, 'method', wrapper)

expect(obj.method).to.have.property(sym, 'invalid')
})

it('should throw if the method does not exist', () => {
Expand All @@ -317,11 +341,13 @@ describe('Instrumenter', () => {

describe('unwrap', () => {
it('should wrap the method on the object', () => {
const obj = { method: () => {} }
const obj = { method: () => 'foo' }
const wrapper = () => () => 'bar'

instrumenter.wrap(obj, 'method', wrapper)
instrumenter.unwrap(obj, 'method')

expect(shimmer.massUnwrap).to.have.been.calledWith([obj], ['method'])
expect(obj.method()).to.equal('foo')
})
})

Expand Down