Skip to content

🐛 [RUM-5645] Add a ff to avoid using "fetch keepalive" #3640

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/src/tools/experimentalFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export enum ExperimentalFeature {
TRACK_INTAKE_REQUESTS = 'track_intake_requests',
USER_ACCOUNT_TRACE_HEADER = 'user_account_trace_header',
WRITABLE_RESOURCE_GRAPHQL = 'writable_resource_graphql',
AVOID_FETCH_KEEPALIVE = 'avoid_fetch_keepalive',
}

const enabledExperimentalFeatures: Set<ExperimentalFeature> = new Set()
Expand Down
46 changes: 46 additions & 0 deletions packages/core/src/transport/httpRequest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import type { Request } from '../../test'
import type { EndpointBuilder } from '../domain/configuration'
import { createEndpointBuilder } from '../domain/configuration'
import { addExperimentalFeatures, resetExperimentalFeatures, ExperimentalFeature } from '../tools/experimentalFeatures'
import { noop } from '../tools/utils/functionUtils'
import { createHttpRequest, fetchKeepAliveStrategy, fetchStrategy } from './httpRequest'
import type { HttpRequest } from './httpRequest'
Expand Down Expand Up @@ -263,3 +264,48 @@ describe('httpRequest intake parameters', () => {
expect(requests.length).toEqual(2)
})
})

describe('httpRequest with AVOID_FETCH_KEEPALIVE feature flag', () => {
const BATCH_BYTES_LIMIT = 100
const ENDPOINT_URL = 'http://my.website'
let interceptor: ReturnType<typeof interceptRequests>
let requests: Request[]
let endpointBuilder: EndpointBuilder
let request: HttpRequest

beforeEach(() => {
interceptor = interceptRequests()
requests = interceptor.requests
endpointBuilder = mockEndpointBuilder(ENDPOINT_URL)
})

afterEach(() => {
resetExperimentalFeatures()
})

it('should use regular fetch (without keepalive) when feature flag is enabled', async () => {
addExperimentalFeatures([ExperimentalFeature.AVOID_FETCH_KEEPALIVE])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: just use mockExperimentalFeatures, so you don't need to reset them

request = createHttpRequest(endpointBuilder, BATCH_BYTES_LIMIT, noop)

request.send({ data: '{"foo":"bar"}', bytesCount: 10 })
await interceptor.waitForAllFetchCalls()

expect(requests.length).toEqual(1)
expect(requests[0].type).toBe('fetch')
expect(requests[0].url).toContain(ENDPOINT_URL)
})

it('should use fetch keepalive when feature flag is not enabled', async () => {
if (!interceptor.isFetchKeepAliveSupported()) {
pending('no fetch keepalive support')
}

request = createHttpRequest(endpointBuilder, BATCH_BYTES_LIMIT, noop)

request.send({ data: '{"foo":"bar"}', bytesCount: 10 })
await interceptor.waitForAllFetchCalls()

expect(requests.length).toEqual(1)
expect(requests[0].type).toBe('fetch-keepalive')
})
})
10 changes: 8 additions & 2 deletions packages/core/src/transport/httpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { EndpointBuilder } from '../domain/configuration'
import type { Context } from '../tools/serialisation/context'
import { monitor, monitorError } from '../tools/monitor'
import type { RawError } from '../domain/error/error.types'
import { isExperimentalFeatureEnabled, ExperimentalFeature } from '../tools/experimentalFeatures'
import { newRetryState, sendWithRetryStrategy } from './sendWithRetryStrategy'

/**
Expand Down Expand Up @@ -38,8 +39,13 @@ export function createHttpRequest(
reportError: (error: RawError) => void
) {
const retryState = newRetryState()
const sendStrategyForRetry = (payload: Payload, onResponse: (r: HttpResponse) => void) =>
fetchKeepAliveStrategy(endpointBuilder, bytesLimit, payload, onResponse)
const sendStrategyForRetry = (payload: Payload, onResponse: (r: HttpResponse) => void) => {
if (isExperimentalFeatureEnabled(ExperimentalFeature.AVOID_FETCH_KEEPALIVE)) {
fetchStrategy(endpointBuilder, payload, onResponse)
} else {
fetchKeepAliveStrategy(endpointBuilder, bytesLimit, payload, onResponse)
}
}

return {
send: (payload: Payload) => {
Expand Down