Skip to content

Commit

Permalink
Adds tracing (#80)
Browse files Browse the repository at this point in the history
Adds tracing
  • Loading branch information
christopherjturner authored Dec 11, 2024
1 parent e258d3b commit da6cce0
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 16 deletions.
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export default {
'<rootDir>/.server',
'index.js'
],
coverageDirectory: '<rootDir>/coverage'
coverageDirectory: '<rootDir>/coverage',
transformIgnorePatterns: ['/node_modules(?!/(@defra/hapi-tracing))'] // ESM only modules
}

/**
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"license": "OGL-UK-3.0",
"dependencies": {
"@babel/runtime": "^7.26.0",
"@defra/hapi-tracing": "^0.1.1",
"@elastic/ecs-pino-format": "^1.5.0",
"@hapi/boom": "^10.0.1",
"@hapi/hapi": "^21.3.12",
Expand Down
24 changes: 15 additions & 9 deletions src/api/common/helpers/logging/logger-options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ecsFormat } from '@elastic/ecs-pino-format'
import { config } from '~/src/config/index.js'
import { getTraceId } from '@defra/hapi-tracing'

const logConfig = config.get('log')
const serviceName = config.get('serviceName')
Expand All @@ -10,14 +11,10 @@ const serviceVersion = config.get('serviceVersion')
*/
const formatters = {
ecs: {
...ecsFormat(),
base: {
service: {
name: serviceName,
type: 'nodeJs',
version: serviceVersion
}
}
...ecsFormat({
serviceVersion,
serviceName
})
},
'pino-pretty': { transport: { target: 'pino-pretty' } }
}
Expand All @@ -33,7 +30,16 @@ export const loggerOptions = {
remove: true
},
level: logConfig.level,
...formatters[logConfig.format]
...formatters[logConfig.format],
nesting: true,
mixin() {
const mixinValues = {}
const traceId = getTraceId()
if (traceId) {
mixinValues.trace = { id: traceId }
}
return mixinValues
}
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/api/common/helpers/request-tracing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { tracing } from '@defra/hapi-tracing'
import { config } from '~/src/config/index.js'

export const requestTracing = {
plugin: tracing.plugin,
options: {
tracingHeader: config.get('tracing.header')
}
}
21 changes: 15 additions & 6 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { mongoDb } from '~/src/api/common/helpers/mongodb.js'
import { failAction } from '~/src/api/common/helpers/fail-action.js'
import { secureContext } from '~/src/api/common/helpers/secure-context/index.js'
import { pulse } from '~/src/api/common/helpers/pulse.js'
import { requestTracing } from '~/src/api/common/helpers/request-tracing.js'

async function createServer() {
const server = hapi.server({
Expand Down Expand Up @@ -39,12 +40,20 @@ async function createServer() {
})

// Hapi Plugins:
// requestLogger - automatically logs incoming requests
// secureContext - loads CA certificates from environment config
// pulse - provides shutdown handlers
// mongoDb - sets up mongo connection pool and attaches to `server` and `request` objects
// router - routes used in the app
await server.register([requestLogger, secureContext, pulse, mongoDb, router])
// requestLogger - automatically logs incoming requests
// requestTracing - trace header logging and propagation
// secureContext - loads CA certificates from environment config
// pulse - provides shutdown handlers
// mongoDb - sets up mongo connection pool and attaches to `server` and `request` objects
// router - routes used in the app
await server.register([
requestLogger,
requestTracing,
secureContext,
pulse,
mongoDb,
router
])

return server
}
Expand Down
8 changes: 8 additions & 0 deletions src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ const config = convict({
format: Boolean,
default: isProduction,
env: 'ENABLE_METRICS'
},
tracing: {
header: {
doc: 'Which header to track',
format: String,
default: 'x-cdp-request-id',
env: 'TRACING_HEADER'
}
}
})

Expand Down

0 comments on commit da6cce0

Please sign in to comment.