-
Notifications
You must be signed in to change notification settings - Fork 627
UN-3444 [FEAT] Propagate frontend request ID and surface it on error notifications #1955
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
Open
Deepak-Kesavan
wants to merge
8
commits into
main
Choose a base branch
from
UN-3444-implement-and-propagate-request-id-from-frontend-to-backend
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d242835
UN-3444 [FEAT] Propagate frontend request ID and surface it on error …
Deepak-Kesavan 0fa515b
UN-3444 [FIX] Address PR review on request-ID helpers
Deepak-Kesavan 5be4c0b
Merge branch 'main' into UN-3444-implement-and-propagate-request-id-f…
Deepak-Kesavan 381a1d9
UN-3444 [FEAT] Append request ID to error log rows in the bottom Logs…
Deepak-Kesavan 076316f
Merge branch 'main' into UN-3444-implement-and-propagate-request-id-f…
Deepak-Kesavan a07d548
UN-3444 [FIX] Address review on request-ID helpers
Deepak-Kesavan adc2a47
UN-3444 [FIX] Sonar cleanup on requestId.test.js
Deepak-Kesavan 81381b8
Merge branch 'main' into UN-3444-implement-and-propagate-request-id-f…
Deepak-Kesavan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,30 @@ | ||
| import { v4 as uuidv4 } from "uuid"; | ||
|
|
||
| const REQUEST_ID_HEADER = "X-Request-ID"; | ||
|
|
||
| const setHeaderIfMissing = (headers, value) => { | ||
| if (typeof headers.set === "function") { | ||
| headers.set(REQUEST_ID_HEADER, value, false); | ||
| return; | ||
| } | ||
| if (!headers[REQUEST_ID_HEADER]) { | ||
| headers[REQUEST_ID_HEADER] = value; | ||
| } | ||
| }; | ||
|
|
||
| const attachRequestIdInterceptor = (axiosInstance) => { | ||
|
vishnuszipstack marked this conversation as resolved.
|
||
| return axiosInstance.interceptors.request.use((config) => { | ||
| config.headers ??= {}; | ||
| setHeaderIfMissing(config.headers, uuidv4()); | ||
| return config; | ||
| }); | ||
| }; | ||
|
|
||
| const getRequestIdFromError = (err) => { | ||
| return ( | ||
| err?.response?.headers?.[REQUEST_ID_HEADER.toLowerCase()] ?? | ||
|
vishnuszipstack marked this conversation as resolved.
|
||
| err?.config?.headers?.[REQUEST_ID_HEADER] | ||
| ); | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| }; | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
|
|
||
| export { REQUEST_ID_HEADER, attachRequestIdInterceptor, getRequestIdFromError }; | ||
This file contains hidden or 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,95 @@ | ||
| import axios from "axios"; | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { | ||
| attachRequestIdInterceptor, | ||
| getRequestIdFromError, | ||
| REQUEST_ID_HEADER, | ||
| } from "./requestId"; | ||
|
|
||
| const UUID_V4_REGEX = | ||
| /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; | ||
|
|
||
| const runRequestInterceptors = async (instance, config = {}) => { | ||
| let current = { ...config, headers: { ...config.headers } }; | ||
| for (const handler of instance.interceptors.request.handlers) { | ||
| if (handler?.fulfilled) { | ||
| current = await handler.fulfilled(current); | ||
| } | ||
| } | ||
| return current; | ||
| }; | ||
|
|
||
| describe("attachRequestIdInterceptor", () => { | ||
| it("injects a v4 UUID when the header is absent", async () => { | ||
| const instance = axios.create(); | ||
| attachRequestIdInterceptor(instance); | ||
|
|
||
| const result = await runRequestInterceptors(instance); | ||
|
|
||
| expect(result.headers[REQUEST_ID_HEADER]).toMatch(UUID_V4_REGEX); | ||
| }); | ||
|
|
||
| it("does NOT overwrite a caller-supplied X-Request-ID", async () => { | ||
| const instance = axios.create(); | ||
| attachRequestIdInterceptor(instance); | ||
|
|
||
| const supplied = "caller-provided-id"; | ||
| const result = await runRequestInterceptors(instance, { | ||
| headers: { [REQUEST_ID_HEADER]: supplied }, | ||
| }); | ||
|
|
||
| expect(result.headers[REQUEST_ID_HEADER]).toBe(supplied); | ||
| }); | ||
|
|
||
| it("generates a distinct ID per request", async () => { | ||
| const instance = axios.create(); | ||
| attachRequestIdInterceptor(instance); | ||
|
|
||
| const first = await runRequestInterceptors(instance); | ||
| const second = await runRequestInterceptors(instance); | ||
|
|
||
| expect(first.headers[REQUEST_ID_HEADER]).not.toBe( | ||
| second.headers[REQUEST_ID_HEADER], | ||
| ); | ||
| }); | ||
|
|
||
| it("creates a headers object when one is missing on the config", async () => { | ||
| const instance = axios.create(); | ||
| attachRequestIdInterceptor(instance); | ||
|
|
||
| let current = {}; | ||
| for (const handler of instance.interceptors.request.handlers) { | ||
| if (handler?.fulfilled) { | ||
| current = await handler.fulfilled(current); | ||
| } | ||
| } | ||
|
|
||
| expect(current.headers[REQUEST_ID_HEADER]).toMatch(UUID_V4_REGEX); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getRequestIdFromError", () => { | ||
| it("reads the lowercased response header", () => { | ||
| const err = { | ||
| response: { headers: { "x-request-id": "from-response" } }, | ||
| config: { headers: { [REQUEST_ID_HEADER]: "from-config" } }, | ||
| }; | ||
|
|
||
| expect(getRequestIdFromError(err)).toBe("from-response"); | ||
| }); | ||
|
|
||
| it("falls back to the request config header when no response header exists", () => { | ||
| const err = { | ||
| config: { headers: { [REQUEST_ID_HEADER]: "from-config" } }, | ||
| }; | ||
|
|
||
| expect(getRequestIdFromError(err)).toBe("from-config"); | ||
| }); | ||
|
|
||
| it("returns undefined for null/empty error inputs without throwing", () => { | ||
| expect(getRequestIdFromError(null)).toBeUndefined(); | ||
| expect(getRequestIdFromError(undefined)).toBeUndefined(); | ||
| expect(getRequestIdFromError({})).toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ const STORE_VARIABLES = { | |
| title: "", | ||
| duration: DEFAULT_DURATION, | ||
| key: null, | ||
| requestId: null, | ||
| }, | ||
| }; | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.