Skip to content

Commit

Permalink
ROC-1105: Add possibility to return origin request id (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlewan authored Dec 18, 2017
1 parent 417c2bc commit 86beda4
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
17 changes: 15 additions & 2 deletions log/tracing/requestTracing.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ function notUUID (uuidString) {
return !UUID_REGEX.test(uuidString)
}

function getHeaderValue (headerName) {
const initialRequest = RequestTracing.getInitialRequest()
if (initialRequest) {
return initialRequest.headers[headerName]
} else {
return undefined
}
}

class RequestTracing {
static middleware (req, res, next) {
if (tracingHeadersNotPresentOrInvalid(req)) {
Expand All @@ -35,12 +44,16 @@ class RequestTracing {
return LocalStorage.retrieveInitialRequest()
}

static getOriginRequestId () {
return getHeaderValue(ORIGIN_REQUEST_ID_HEADER)
}

static getRootRequestId () {
return RequestTracing.getInitialRequest().headers[ROOT_REQUEST_ID_HEADER]
return getHeaderValue(ROOT_REQUEST_ID_HEADER)
}

static getCurrentRequestId () {
return RequestTracing.getInitialRequest().headers[REQUEST_ID_HEADER]
return getHeaderValue(REQUEST_ID_HEADER)
}

static createNextRequestId () {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hmcts/nodejs-logging",
"version": "2.0.0",
"version": "2.1.0",
"description": "",
"main": "Logger.js",
"scripts": {
Expand Down
71 changes: 68 additions & 3 deletions test/unit/tracing/requestTracing.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict'
/* global describe, context, it, beforeEach */
/* global describe, context, it, beforeEach, afterEach */

const uuid = require('uuid')

Expand Down Expand Up @@ -157,8 +157,22 @@ describe('RequestTracing', () => {
it(`should return the ${REQUEST_ID_HEADER}`, (done) => {
function createNextFunction (done) {
return function () {
const rootRequestId = RequestTracing.getCurrentRequestId()
expect(rootRequestId).to.equal(req.headers[REQUEST_ID_HEADER])
const requestId = RequestTracing.getCurrentRequestId()
expect(requestId).to.equal(req.headers[REQUEST_ID_HEADER])
done()
}
}

RequestTracing.middleware(req, req, createNextFunction(done))
})
})

describe('getOriginRequestId', () => {
it(`should return the ${ORIGIN_REQUEST_ID_HEADER}`, (done) => {
function createNextFunction (done) {
return function () {
const originRequestId = RequestTracing.getOriginRequestId()
expect(originRequestId).to.equal(req.headers[ORIGIN_REQUEST_ID_HEADER])
done()
}
}
Expand All @@ -173,4 +187,55 @@ describe('RequestTracing', () => {
expect(nextRequestId).to.match(UUID_REGEX)
})
})

context('when initial request is undefined', () => {
let sinonSandbox

beforeEach(() => {
sinonSandbox = sinon.sandbox.create()
sinonSandbox.stub(RequestTracing, 'getInitialRequest').callsFake(() => {
return undefined
})
})

afterEach(() => {
sinonSandbox.restore()
})

it('should return undefined root request id', (done) => {
function createNextFunction (done) {
return function () {
const rootRequestId = RequestTracing.getRootRequestId()
expect(rootRequestId).to.be.undefined
done()
}
}

RequestTracing.middleware(req, req, createNextFunction(done))
})

it('should return undefined current request id', (done) => {
function createNextFunction (done) {
return function () {
const requestId = RequestTracing.getCurrentRequestId()
expect(requestId).to.be.undefined
done()
}
}

RequestTracing.middleware(req, req, createNextFunction(done))
})

it('should return undefined origin request id', (done) => {
function createNextFunction (done) {
return function () {
const originRequestId = RequestTracing.getOriginRequestId()
expect(originRequestId).to.be.undefined
done()
}
}

RequestTracing.middleware(req, req, createNextFunction(done))
})
})
})

0 comments on commit 86beda4

Please sign in to comment.