-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add request ID threading (#78)
- Loading branch information
Showing
6 changed files
with
273 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as uuid from 'uuid' | ||
|
||
export const requestIdHeader = 'Request-Id' | ||
|
||
// tslint:disable-next-line: no-unnecessary-class | ||
export class RequestId { | ||
static ids: string[] = [] | ||
|
||
static track(...ids: string[]) { | ||
const tracked = RequestId.ids | ||
ids = ids.filter(id => !(tracked.includes(id))) | ||
RequestId.ids = [...ids, ...tracked] | ||
return RequestId.ids | ||
} | ||
|
||
static create(): string[] { | ||
const tracked = RequestId.ids | ||
const generatedId = RequestId._generate() | ||
RequestId.ids = [generatedId, ...tracked] | ||
return RequestId.ids | ||
} | ||
|
||
static empty(): void { | ||
RequestId.ids = [] | ||
} | ||
|
||
static get headerValue() { | ||
return RequestId.ids.join(',') | ||
} | ||
|
||
static _generate() { | ||
return uuid.v4() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import {expect} from 'chai' | ||
import * as sinon from 'sinon' | ||
|
||
import {RequestId} from '../src/request-id' | ||
|
||
describe('getRequestId', () => { | ||
let generateStub: any | ||
|
||
beforeEach(function () { | ||
RequestId.empty() | ||
generateStub = sinon.stub(RequestId, '_generate').returns('randomly-generated-uuid') | ||
}) | ||
|
||
afterEach(function () { | ||
generateStub.restore() | ||
}) | ||
|
||
it('can create random uuids', () => { | ||
expect(RequestId.ids.length).to.equal(0) | ||
expect(generateStub.called).to.be.false | ||
const ids = RequestId.create() | ||
expect(ids).to.deep.equal(RequestId.ids) | ||
expect(ids).to.deep.equal(['randomly-generated-uuid']) | ||
expect(RequestId.ids.length).to.equal(1) | ||
expect(generateStub.called).to.be.true | ||
}) | ||
|
||
it('can can track ids', () => { | ||
expect(RequestId.ids.length).to.equal(0) | ||
RequestId.track('tracked-id', 'another-tracked-id') | ||
expect(RequestId.ids).to.deep.equal(['tracked-id', 'another-tracked-id']) | ||
}) | ||
|
||
it('can empty the tracked ids', () => { | ||
expect(RequestId.ids.length).to.equal(0) | ||
RequestId.create() | ||
expect(RequestId.ids.length).to.equal(1) | ||
RequestId.empty() | ||
expect(RequestId.ids.length).to.equal(0) | ||
}) | ||
|
||
it('can generate a header value', () => { | ||
RequestId.create() | ||
RequestId.track('incoming-header-id') | ||
expect(RequestId.headerValue).to.equal('incoming-header-id,randomly-generated-uuid') | ||
}) | ||
|
||
it('create and track uuids together putting latest in front', () => { | ||
expect(RequestId.ids.length).to.equal(0) | ||
|
||
generateStub.returns('random') | ||
let ids = RequestId.create() | ||
expect(ids).to.deep.equal(['random']) | ||
|
||
ids = RequestId.track('tracked') | ||
expect(ids).to.deep.equal(['tracked', 'random']) | ||
|
||
generateStub.returns('another-random') | ||
ids = RequestId.create() | ||
expect(RequestId.ids).to.deep.equal(['another-random', 'tracked', 'random']) | ||
expect(RequestId.headerValue).to.deep.equal('another-random,tracked,random') | ||
}) | ||
|
||
it('tracked ids are not added if they are already included', function () { | ||
expect(RequestId.ids).to.deep.equal([]) | ||
|
||
RequestId.track('tracked') | ||
expect(RequestId.ids).to.deep.equal(['tracked']) | ||
|
||
RequestId.track('tracked') | ||
expect(RequestId.ids).to.deep.equal(['tracked']) | ||
|
||
RequestId.track('other-tracked', 'tracked', 'another-tracked') | ||
expect(RequestId.ids).to.deep.equal(['other-tracked', 'another-tracked', 'tracked']) | ||
}) | ||
}) |
Oops, something went wrong.