Skip to content

Commit

Permalink
Migration deploy (#603)
Browse files Browse the repository at this point in the history
* Renamed health check end point from /ping.xml to /pingdom/ping.xml. (#499)

Also call the updated end point on the Data Hub API.
Test to be written as part of TET-664

* Use ASIM formatted logger (#518)

* Use ASIM formatted logger on production environments.

* add initial build configuration

* Fix linting errors

* Update healthcheck in test

* Skip pingdom test

---------

Co-authored-by: Marijn Kampf <marijn.kampf@digital.trade.gov.uk>
Co-authored-by: Lawrence Goldstien <lawrence.goldstein@digital.trade.gov.uk>
  • Loading branch information
3 people authored Jul 15, 2024
1 parent 8872e9b commit e0f60a9
Show file tree
Hide file tree
Showing 11 changed files with 361 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .copilot/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
repository: omis
builder:
name: paketobuildpacks/builder-jammy-full
version: 0.3.339
9 changes: 9 additions & 0 deletions .copilot/phases/pre_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

# Exit early if something goes wrong
set -e

# Add commands below to run as part of the pre_build phase

# The build system requires a Procfile be present
echo "web: npm start" > Procfile
127 changes: 127 additions & 0 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"clean": "del .build",
"build": "npm run clean && webpack",
"test": "npm run test:unit",
"test:unit": "NODE_ENV=development; LOG_LEVEL=error; JASMINE_CONFIG_PATH=src/test/app/jasmine.json jasmine",
"test:unit": "NODE_ENV=development LOG_LEVEL=info JASMINE_CONFIG_PATH=src/test/app/jasmine.json jasmine",
"test:e2e": "CYPRESS_coverage=false cypress run --config '{\"specPattern\":[\"test/end-to-end/cypress/*.js\"]}' --browser chrome",
"test:e2e:watch": "CYPRESS_coverage=false cypress open --config '{\"specPattern\":[\"test/end-to-end/cypress/*.js\"]}' --e2e --browser chrome",
"lint": "npm-run-all lint:all:*",
Expand Down Expand Up @@ -78,6 +78,8 @@
"sass": "^1.77.6",
"sass-loader": "^14.2.1",
"serve-favicon": "^2.5.0",
"sinon": "^17.0.1",
"std-mocks": "^2.0.0",
"style-loader": "^4.0.0",
"webpack": "^5.92.1",
"webpack-assets-manifest": "^5.2.1",
Expand Down
12 changes: 9 additions & 3 deletions src/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ function startApp() {
`Listening at http://${serverConfig.host}:${serverConfig.port}`
)

logger.info(messages.join(' '))
logger.info(messages.join(' '), {
eventType: logger.eventTypes.expressStartup,
})
})
}

if (isClustered) {
// if this is the master then create the workers
if (cluster.isMaster) {
logger.info('Runing in cluster mode.')
logger.info('Runing in cluster mode.', {
eventType: logger.eventTypes.expressStartup,
})
for (let i = 0; i < numberOfWorkers; i++) {
cluster.fork()
}
Expand All @@ -43,6 +47,8 @@ if (isClustered) {
startApp()
}
} else {
logger.info('Not running in cluster mode.')
logger.info('Not running in cluster mode.', {
eventType: logger.eventTypes.expressStartup,
})
startApp()
}
2 changes: 1 addition & 1 deletion src/app/controllers/healthcheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const logger = require('../lib/logger')
const serviceDependencies = [
{
name: 'leeloo',
healthCheck: () => axios.get(`${api.root}/ping.xml`),
healthCheck: () => axios.get(`${api.root}/pingdom/ping.xml`),
},
]

Expand Down
73 changes: 71 additions & 2 deletions src/app/lib/logger.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,79 @@
const winston = require('winston')

const { createLogger, transports, format } = winston

const config = require('../../../config')

const logger = new winston.createLogger({
const eventTypes = {
expressStartup: 'express.startup',
expressRequest: 'express.request',
}

const asimFormat = format.printf(
({ level, message, timestamp, eventType = eventTypes.expressRequest }) => {
return JSON.stringify({
EventMessage: message,
EventCount: 1,
EventStartTime: timestamp,
EventEndTime: timestamp,
EventType: eventType,
EventResult: 'NA',
EventSeverity: getEventSeverity(level),
EventOriginalSeverity: level,
EventSchema: 'ProcessEvent',
EventSchemaVersion: '0.1.4',
ActingAppType: 'Express',
AdditionalFields: {
CustomASIMFormatter: true,
TraceHeaders: {},
},
})
}
)

const getEventSeverity = (level) => {
return {
debug: 'Informational',
info: 'Informational',
warn: 'Low',
error: 'Medium',
critical: 'High',
}[level]
}

const loggerConfiguration = {
level: config.logLevel,
transports: [new winston.transports.Console({ colorize: config.isDev })],
})
}

if (config.isProd) {
// Only send asim formatted logs on production as other types will mess up processing.
loggerConfiguration.transports = [
new transports.Console({
handleExceptions: true,
humanReadableUnhandledException: true,
json: true,
format: format.combine(
winston.format.splat(),
format.simple(),
format.timestamp(),
asimFormat
),
}),
]
}

const logger = createLogger(loggerConfiguration)

if (config.isDev) {
logger.exceptions.handle(
new transports.Console({
format: format.combine(format.timestamp(), format.json()),
})
)
}

// Add event types to logger to allow call of constants.
logger.eventTypes = eventTypes

module.exports = logger
4 changes: 3 additions & 1 deletion src/app/middleware/set-locals.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ let webpackManifest = {}
try {
webpackManifest = require(`${config.buildDir}/manifest.json`)
} catch (err) {
logger.error('Manifest file is not found. Ensure assets are built.')
logger.error('Manifest file is not found. Ensure assets are built.', {
eventType: logger.eventTypes.expressStartup,
})
}

module.exports = function setLocals(req, res, next) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/routers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { renderPingdomXml } = require('../controllers/healthcheck')
const { fetchOrderDetails } = require('../middleware/order')

// NOTE: ping has to be defined before the auth token middleware
router.get('/ping.xml', renderPingdomXml)
router.get('/pingdom/ping.xml', renderPingdomXml)
router.get('/cookies', renderCookies)
router.get('/', indexController)

Expand Down
Loading

0 comments on commit e0f60a9

Please sign in to comment.