Skip to content

Commit

Permalink
tests: update APM-related tests for zero-conf and run-context changes (
Browse files Browse the repository at this point in the history
…#110)

Two changes in [email protected] impacted tests here:
- "Zero configuration support" changed how the APM serviceName is
  handled for edge cases.
- Run context handling changes made it so `span.end()` actually removes
  the span from the current context, so log records after span.end() no
  longer have that span.id.
  • Loading branch information
trentm authored Nov 29, 2021
1 parent 222d6b3 commit a1156ca
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 20 deletions.
11 changes: 4 additions & 7 deletions loggers/pino/test/apm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,12 @@ test('can override service.name, event.dataset in base arg to constructor', t =>
})
})

test('unset APM serviceName does not set service.name, event.dataset, but also does not break', t => {
test('invalid APM serviceName does not set service.name or event.dataset, but also does not break', t => {
execFile(process.execPath, [
path.join(__dirname, 'use-apm-override-service-name.js')
// Leave <serviceName> arg empty.
path.join(__dirname, 'use-apm-override-service-name.js'),
'foo☃bar' // Use an invalid <serviceName>.
], {
timeout: 5000,
// Ensure the APM Agent's auto-configuration of `serviceName`, via looking
// up dirs for a package.json, does *not* work by execing from the root dir.
cwd: '/'
timeout: 5000
}, function (err, stdout, stderr) {
t.error(err)
const recs = stdout.trim().split(/\n/g).map(JSON.parse)
Expand Down
2 changes: 1 addition & 1 deletion loggers/pino/test/serve-one-http-req-with-apm.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ const server = http.createServer()
server.once('request', function handler (req, res) {
const span = apm.startSpan('auth')
setImmediate(function doneAuth () {
span.end()
res.end('ok')
log.info({ req, res }, 'handled request')
span.end()
server.close()
})
})
Expand Down
6 changes: 2 additions & 4 deletions loggers/winston/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,10 @@ function ecsTransform (info, opts) {
? ecsFields.service.name
: undefined)
if (!serviceName) {
// https://github.com/elastic/apm-agent-nodejs/pull/1949 is adding
// getServiceName() in v3.11.0. Fallback to private `apm._conf`.
// istanbul ignore next
serviceName = apm.getServiceName
? apm.getServiceName()
: apm._conf.serviceName
? apm.getServiceName() // added in [email protected]
: apm._conf.serviceName // fallback to private `_conf`
// A mis-configured APM Agent can be "started" but not have a
// "serviceName".
if (serviceName) {
Expand Down
11 changes: 4 additions & 7 deletions loggers/winston/test/apm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,15 +353,12 @@ test('can override service.name, event.dataset', t => {
})
})

test('unset APM serviceName does not set service.name, event.dataset, but also does not break', t => {
test('invalid APM serviceName does not set service.name or event.dataset, but also does not break', t => {
execFile(process.execPath, [
path.join(__dirname, 'use-apm-override-service-name.js')
// Leave <serviceName> arg empty.
path.join(__dirname, 'use-apm-override-service-name.js'),
'foo☃bar' // Use an invalid <serviceName>.
], {
timeout: 5000,
// Ensure the APM Agent's auto-configuration of `serviceName`, via looking
// up dirs for a package.json, does *not* work by execing from the root dir.
cwd: '/'
timeout: 5000
}, function (err, stdout, stderr) {
t.error(err)
const recs = stdout.trim().split(/\n/g).map(JSON.parse)
Expand Down
2 changes: 1 addition & 1 deletion loggers/winston/test/serve-one-http-req-with-apm.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ const server = http.createServer()
server.once('request', function handler (req, res) {
const span = apm.startSpan('auth')
setImmediate(function doneAuth () {
span.end()
res.end('ok')
log.info('handled request', { req, res })
span.end()
server.close()
})
})
Expand Down

0 comments on commit a1156ca

Please sign in to comment.