-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Pino Integration-test (#2002)
- Loading branch information
1 parent
aff6d02
commit 3ce04b0
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* eslint-disable comma-dangle */ | ||
'use strict' | ||
|
||
const { FakeAgent, spawnProc, createSandbox, curl } = require('./helpers') | ||
const path = require('path') | ||
const { assert } = require('chai') | ||
const { once } = require('events') | ||
|
||
describe('pino test', () => { | ||
let agent | ||
let proc | ||
let sandbox | ||
let cwd | ||
let startupTestFile | ||
|
||
before(async () => { | ||
sandbox = await createSandbox(['pino']) | ||
cwd = sandbox.folder | ||
startupTestFile = path.join(cwd, 'pino/index.js') | ||
}) | ||
|
||
after(async () => { | ||
await sandbox.remove() | ||
}) | ||
|
||
context('Log injection', () => { | ||
beforeEach(async () => { | ||
agent = await new FakeAgent().start() | ||
}) | ||
|
||
afterEach(async () => { | ||
proc.kill() | ||
await agent.stop() | ||
}) | ||
|
||
it('Log injection enabled', async () => { | ||
proc = await spawnProc(startupTestFile, { | ||
cwd, | ||
env: { | ||
AGENT_PORT: agent.port, | ||
lOG_INJECTION: true, | ||
}, | ||
stdio: 'pipe', | ||
}) | ||
const [data] = await Promise.all([once(proc.stdout, 'data'), curl(proc)]) | ||
const stdoutData = JSON.parse(data.toString()) | ||
assert.containsAllKeys(stdoutData, ['dd']) | ||
assert.containsAllKeys(stdoutData.dd, ['trace_id', 'span_id']) | ||
assert.strictEqual( | ||
stdoutData['dd']['trace_id'], | ||
stdoutData['custom']['trace_id'] | ||
) | ||
assert.strictEqual( | ||
stdoutData['dd']['span_id'], | ||
stdoutData['custom']['span_id'] | ||
) | ||
}) | ||
|
||
it('Log injection disabled', async () => { | ||
proc = await spawnProc(startupTestFile, { | ||
cwd, | ||
env: { | ||
AGENT_PORT: agent.port, | ||
}, | ||
stdio: 'pipe', | ||
}) | ||
const [data] = await Promise.all([once(proc.stdout, 'data'), curl(proc)]) | ||
const stdoutData = JSON.parse(data.toString()) | ||
assert.doesNotHaveAnyKeys(stdoutData, ['dd']) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict' | ||
|
||
const options = {} | ||
|
||
if (process.env.AGENT_PORT) { | ||
options.port = process.env.AGENT_PORT | ||
} | ||
|
||
if (process.env.lOG_INJECTION) { | ||
options.logInjection = process.env.lOG_INJECTION | ||
} | ||
|
||
const tracer = require('dd-trace').init(options) | ||
|
||
const http = require('http') | ||
const logger = require('pino')() | ||
|
||
const server = http | ||
.createServer((req, res) => { | ||
const span = tracer.scope().active() | ||
const contextTraceId = span.context().toTraceId() | ||
const contextSpanId = span.context().toSpanId() | ||
logger.info( | ||
{ custom: { trace_id: contextTraceId, span_id: contextSpanId } }, | ||
'Creating server' | ||
) | ||
res.end('hello, world\n') | ||
}) | ||
.listen(0, () => { | ||
const port = server.address().port | ||
process.send({ port }) | ||
}) |