Skip to content

Commit

Permalink
Adding Pino Integration-test (#2002)
Browse files Browse the repository at this point in the history
  • Loading branch information
bojbrook authored and khanayan123 committed Jan 2, 2024
1 parent aff6d02 commit 3ce04b0
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
72 changes: 72 additions & 0 deletions integration-tests/pino.spec.js
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'])
})
})
})
32 changes: 32 additions & 0 deletions integration-tests/pino/index.js
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 })
})

0 comments on commit 3ce04b0

Please sign in to comment.