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 service name when tracer is imported by a dependency #724

Merged
merged 3 commits into from
Oct 25, 2019
Merged
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,5 @@ versions
build
prebuilds
docs/test.js
!packages/*/test/node_modules
!packages/*/test/**/node_modules
dist
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 @@ -13,9 +13,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 @@ -177,6 +177,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.