From f4bb56fe328f7055faaa10cf69d96c5e1d643e3b Mon Sep 17 00:00:00 2001 From: Dominik Broj Date: Mon, 11 Mar 2024 10:23:16 +0100 Subject: [PATCH] Fix passing additional headers to custom fetch (#4038) # What this PR does Fix passing additional headers to custom fetch ## Which issue(s) this PR closes Closes https://github.com/grafana/support-escalations/issues/9630 ## Checklist - [ ] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] Added the relevant release notes label (see labels prefixed w/ `release:`). These labels dictate how your PR will show up in the autogenerated release notes. --- .../network/oncall-api/http-client.test.ts | 6 ++-- .../src/network/oncall-api/http-client.ts | 28 ++++++++----------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/grafana-plugin/src/network/oncall-api/http-client.test.ts b/grafana-plugin/src/network/oncall-api/http-client.test.ts index d7e315a7..a3acc789 100644 --- a/grafana-plugin/src/network/oncall-api/http-client.test.ts +++ b/grafana-plugin/src/network/oncall-api/http-client.test.ts @@ -21,10 +21,10 @@ jest.mock('openapi-fetch', () => ({ const fetchMock = jest.fn().mockResolvedValue(true); +const HEADERS = new Headers(); +HEADERS.set('Content-Type', 'application/json'); const REQUEST_CONFIG = { - headers: { - 'Content-Type': 'application/json', - }, + headers: HEADERS, }; const URL = 'https://someurl.com'; const SUCCESSFUL_RESPONSE_MOCK = { ok: true }; diff --git a/grafana-plugin/src/network/oncall-api/http-client.ts b/grafana-plugin/src/network/oncall-api/http-client.ts index 4d5b4a20..ef0d630e 100644 --- a/grafana-plugin/src/network/oncall-api/http-client.ts +++ b/grafana-plugin/src/network/oncall-api/http-client.ts @@ -20,24 +20,20 @@ const showApiError = (errorResponse: Response) => { export const getCustomFetchFn = ({ withGlobalErrorHandler }: { withGlobalErrorHandler: boolean }) => - async (url: string, reqConfig: Parameters[1] = {}): Promise => { + async (url: string, requestConfig: Parameters[1] & { headers?: Headers } = {}): Promise => { const { faro } = FaroHelper; const otel = faro?.api?.getOTEL(); - const requestConfig = { - ...reqConfig, - headers: { - ...reqConfig.headers, - 'Content-Type': 'application/json', - /** - * In short, this header will tell the Grafana plugin proxy, a Go service which use Go's HTTP Transport, - * to retry POST requests (and other non-idempotent requests). This doesn't necessarily make these requests - * idempotent, but it will make them retry-able from Go's (read: net/http) perspective. - * - * https://stackoverflow.com/questions/42847294/how-to-catch-http-server-closed-idle-connection-error/62292758#62292758 - * https://raintank-corp.slack.com/archives/C01C4K8DETW/p1692280544382739?thread_ts=1692279329.797149&cid=C01C4K8DETW - */ 'X-Idempotency-Key': `${Date.now()}-${Math.random()}`, - }, - }; + + /** + * In short, this header will tell the Grafana plugin proxy, a Go service which use Go's HTTP Transport, + * to retry POST requests (and other non-idempotent requests). This doesn't necessarily make these requests + * idempotent, but it will make them retry-able from Go's (read: net/http) perspective. + * + * https://stackoverflow.com/questions/42847294/how-to-catch-http-server-closed-idle-connection-error/62292758#62292758 + * https://raintank-corp.slack.com/archives/C01C4K8DETW/p1692280544382739?thread_ts=1692279329.797149&cid=C01C4K8DETW + */ + requestConfig.headers.set('X-Idempotency-Key', `${Date.now()}-${Math.random()}`); + requestConfig.headers.set('Content-Type', 'application/json'); if (faro && otel) { const tracer = otel.trace.getTracer('default');