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

Adds tracing #80

Merged
merged 4 commits into from
Dec 11, 2024
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
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
Loading