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

<!--
*Note*: if you have more than one GitHub issue that this PR closes, be
sure to preface
each issue link with a [closing
keyword](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/using-keywords-in-issues-and-pull-requests#linking-a-pull-request-to-an-issue).
This ensures that the issue(s) are auto-closed once the PR has been
merged.
-->

## 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.
This commit is contained in:
Dominik Broj 2024-03-11 10:23:16 +01:00 committed by GitHub
parent 6680abb60d
commit f4bb56fe32
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 19 deletions

View file

@ -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 };

View file

@ -20,24 +20,20 @@ const showApiError = (errorResponse: Response) => {
export const getCustomFetchFn =
({ withGlobalErrorHandler }: { withGlobalErrorHandler: boolean }) =>
async (url: string, reqConfig: Parameters<typeof fetch>[1] = {}): Promise<Response> => {
async (url: string, requestConfig: Parameters<typeof fetch>[1] & { headers?: Headers } = {}): Promise<Response> => {
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');