Skip to content

Commit

Permalink
fix service name when tracer is imported by a dependency (#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
rochdev committed Oct 25, 2019
1 parent 2774893 commit b2f8b3d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,4 @@ versions
build
prebuilds
docs/test.js
!packages/*/test/node_modules
!packages/*/test/**/node_modules
18 changes: 17 additions & 1 deletion packages/dd-trace/src/platform/node/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,25 @@ function service () {
const callerPath = parentModule()
const parentPath = parentModule(callerPath)
const cwd = path.dirname(parentPath || callerPath)
const pkg = readPkgUp.sync({ cwd }).pkg || {}
const pkg = findPkg(cwd)

return pkg.name
}

function findPkg (cwd) {
let up = readPkgUp.sync({ cwd })

while (up && isDependency(up.path)) {
cwd = path.resolve(path.dirname(up.path), '..')
up = readPkgUp.sync({ cwd })
}

return up && up.pkg ? up.pkg : {}
}

function isDependency (filepath) {
const expr = new RegExp(`\\${path.sep}node_modules\\${path.sep}`)
return expr.test(filepath)
}

module.exports = service
6 changes: 6 additions & 0 deletions packages/dd-trace/test/platform/node/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ describe('Platform', () => {

expect(name).to.equal('foo')
})

it('should work even in dependencies', () => {
const name = require('./load/node_modules/dep')

expect(name).to.equal('foo')
})
})

describe('request', () => {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b2f8b3d

Please sign in to comment.