-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add correlation ID to all use cases and send it to backbone (#268)
* feat: add correlation id to all RESTClient calls * chore: add test for correlation id * test: add test * test: cleanup tests * Update packages/transport/test/core/backbone/CorrelationId.test.ts Co-authored-by: Julian König <[email protected]> * Update packages/transport/test/core/backbone/CorrelationId.test.ts Co-authored-by: Julian König <[email protected]> * chore: typo * feat: add correlation id to all useCases * refactor: rename correlationIdLib to correlator --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Julian König <[email protected]> Co-authored-by: Julian König <[email protected]>
- Loading branch information
1 parent
e6aebf7
commit 8510f91
Showing
8 changed files
with
179 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,73 @@ | ||
import { RESTClient } from "@nmshd/transport"; | ||
import { AxiosRequestConfig, AxiosResponse, Method } from "axios"; | ||
|
||
export class RequestInterceptor { | ||
protected _measuringRequests = true; | ||
public get measuringRequests(): boolean { | ||
return this._measuringRequests; | ||
} | ||
|
||
protected _requests: AxiosRequestConfig[] = []; | ||
public get requests(): AxiosRequestConfig[] { | ||
return this._requests; | ||
} | ||
|
||
protected _responses: AxiosResponse[] = []; | ||
public get responses(): AxiosResponse[] { | ||
return this._responses; | ||
} | ||
|
||
protected _client: RESTClient; | ||
public get controller(): RESTClient { | ||
return this._client; | ||
} | ||
|
||
public constructor(client: RESTClient) { | ||
this._client = client; | ||
this._measuringRequests = true; | ||
this.injectToClient(client); | ||
} | ||
|
||
private injectToClient(client: RESTClient) { | ||
const that = this; | ||
|
||
const axiosInstance = client["axiosInstance"]; | ||
axiosInstance.interceptors.request.use((req) => { | ||
if (!that._measuringRequests) return req; | ||
that._requests.push(req); | ||
return req; | ||
}); | ||
axiosInstance.interceptors.response.use((res) => { | ||
if (!that._measuringRequests) return res; | ||
that._responses.push(res); | ||
return res; | ||
}); | ||
} | ||
|
||
public start(): this { | ||
this._measuringRequests = true; | ||
this.reset(); | ||
return this; | ||
} | ||
|
||
private reset() { | ||
this._requests = []; | ||
this._responses = []; | ||
} | ||
|
||
public stop(): Communication { | ||
this._measuringRequests = false; | ||
return new Communication(this.requests, this.responses); | ||
} | ||
} | ||
|
||
class Communication { | ||
public constructor( | ||
public readonly requests: AxiosRequestConfig[], | ||
public readonly responses: AxiosResponse<any>[] | ||
) {} | ||
|
||
public getRequests(filter: { method: Method; urlSubstring: string }) { | ||
return this.requests.filter((r) => r.url!.toLowerCase().includes(filter.urlSubstring.toLowerCase()) && r.method?.toLowerCase() === filter.method.toLowerCase()); | ||
} | ||
} |
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,44 @@ | ||
import { AccountController } from "@nmshd/transport"; | ||
import correlator from "correlation-id"; | ||
import { Container } from "typescript-ioc"; | ||
import { RuntimeServiceProvider, TestRuntimeServices } from "../lib"; | ||
import { RequestInterceptor } from "../lib/RequestInterceptor"; | ||
|
||
const uuidRegex = new RegExp("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"); | ||
|
||
describe("CorrelationId", function () { | ||
let runtime: TestRuntimeServices; | ||
let runtimeServiceProvider: RuntimeServiceProvider; | ||
let interceptor: RequestInterceptor; | ||
|
||
beforeAll(async function () { | ||
runtimeServiceProvider = new RuntimeServiceProvider(); | ||
runtime = (await runtimeServiceProvider.launch(1))[0]; | ||
|
||
const accountController = Container.get(AccountController); | ||
interceptor = new RequestInterceptor((accountController as any).synchronization.client); | ||
}); | ||
|
||
afterAll(async function () { | ||
await runtimeServiceProvider.stop(); | ||
}); | ||
|
||
test("should send correlation id to the backbone when given", async function () { | ||
interceptor.start(); | ||
await correlator.withId("test-correlation-id", async () => { | ||
await runtime.transport.account.syncEverything(); | ||
}); | ||
|
||
const requests = interceptor.stop().requests; | ||
expect(requests.at(-1)!.headers!["x-correlation-id"]).toBe("test-correlation-id"); | ||
}); | ||
|
||
test("should send a generated correlation id to the backbone", async function () { | ||
interceptor.start(); | ||
|
||
await runtime.transport.account.syncEverything(); | ||
|
||
const requests = interceptor.stop().requests; | ||
expect(requests.at(-1)!.headers!["x-correlation-id"]).toMatch(uuidRegex); | ||
}); | ||
}); |
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
35 changes: 35 additions & 0 deletions
35
packages/transport/test/core/backbone/CorrelationId.test.ts
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,35 @@ | ||
import { IDatabaseConnection } from "@js-soft/docdb-access-abstractions"; | ||
import correlator from "correlation-id"; | ||
import { AccountController } from "../../../src"; | ||
import { RequestInterceptor } from "../../testHelpers/RequestInterceptor"; | ||
import { TestUtil } from "../../testHelpers/TestUtil"; | ||
|
||
describe("CorrelationId", function () { | ||
let connection: IDatabaseConnection; | ||
let testAccount: AccountController; | ||
let interceptor: RequestInterceptor; | ||
|
||
beforeAll(async function () { | ||
connection = await TestUtil.createDatabaseConnection(); | ||
const transport = TestUtil.createTransport(connection); | ||
await transport.init(); | ||
const accounts = await TestUtil.provideAccounts(transport, 1); | ||
testAccount = accounts[0]; | ||
interceptor = new RequestInterceptor((testAccount as any).synchronization.client); | ||
}); | ||
|
||
afterAll(async function () { | ||
await testAccount.close(); | ||
await connection.close(); | ||
}); | ||
|
||
test("should send correlation id to the backbone when given", async function () { | ||
interceptor.start(); | ||
await correlator.withId("test-correlation-id", async () => { | ||
await testAccount.syncEverything(); | ||
}); | ||
|
||
const requests = interceptor.stop().requests; | ||
expect(requests.at(-1)!.headers!["x-correlation-id"]).toBe("test-correlation-id"); | ||
}); | ||
}); |