Skip to content

Commit

Permalink
Integrate thomas.lebeau/xhr-allow-untrested-events (#3123) into stagi…
Browse files Browse the repository at this point in the history
…ng-46

Integrated commit sha: 5f3f019

Co-authored-by: Thomas Lebeau <thomas.lebeau@datadoghq.com>
  • Loading branch information
dd-mergequeue[bot] and thomas-lebeau authored Nov 12, 2024
2 parents 08b2ab4 + 5f3f019 commit 6f3a76b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 35 deletions.
15 changes: 4 additions & 11 deletions packages/core/src/transport/httpRequest.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { collectAsyncCalls, mockEndpointBuilder, interceptRequests } from '../../test'
import type { Request } from '../../test'
import type { EndpointBuilder, Configuration } from '../domain/configuration'
import type { EndpointBuilder } from '../domain/configuration'
import { createEndpointBuilder } from '../domain/configuration'
import { noop } from '../tools/utils/functionUtils'
import { createHttpRequest, fetchKeepAliveStrategy, sendXHR } from './httpRequest'
Expand All @@ -13,14 +13,12 @@ describe('httpRequest', () => {
let requests: Request[]
let endpointBuilder: EndpointBuilder
let request: HttpRequest
let configuration: Configuration

beforeEach(() => {
configuration = {} as Configuration
interceptor = interceptRequests()
requests = interceptor.requests
endpointBuilder = mockEndpointBuilder(ENDPOINT_URL)
request = createHttpRequest(configuration, endpointBuilder, BATCH_BYTES_LIMIT, noop)
request = createHttpRequest(endpointBuilder, BATCH_BYTES_LIMIT, noop)
})

describe('send', () => {
Expand Down Expand Up @@ -105,7 +103,6 @@ describe('httpRequest', () => {
interceptor.withFetch(() => Promise.resolve({ status: 429, type: 'cors' }))

fetchKeepAliveStrategy(
configuration,
endpointBuilder,
BATCH_BYTES_LIMIT,
{ data: '{"foo":"bar1"}\n{"foo":"bar2"}', bytesCount: 10 },
Expand All @@ -129,7 +126,6 @@ describe('httpRequest', () => {
})

fetchKeepAliveStrategy(
configuration,
endpointBuilder,
BATCH_BYTES_LIMIT,
{ data: '{"foo":"bar1"}\n{"foo":"bar2"}', bytesCount: 10 },
Expand All @@ -148,7 +144,6 @@ describe('httpRequest', () => {
})

fetchKeepAliveStrategy(
configuration,
endpointBuilder,
BATCH_BYTES_LIMIT,
{ data: '{"foo":"bar1"}\n{"foo":"bar2"}', bytesCount: BATCH_BYTES_LIMIT },
Expand Down Expand Up @@ -177,7 +172,7 @@ describe('httpRequest', () => {
})
})

sendXHR(configuration, 'foo', '', onResponseSpy)
sendXHR('foo', '', onResponseSpy)

setTimeout(() => {
expect(onResponseSpy).toHaveBeenCalledTimes(1)
Expand Down Expand Up @@ -257,14 +252,12 @@ describe('httpRequest intake parameters', () => {
let requests: Request[]
let endpointBuilder: EndpointBuilder
let request: HttpRequest
let configuration: Configuration

beforeEach(() => {
configuration = {} as Configuration
interceptor = interceptRequests()
requests = interceptor.requests
endpointBuilder = createEndpointBuilder({ clientToken }, 'logs', [])
request = createHttpRequest(configuration, endpointBuilder, BATCH_BYTES_LIMIT, noop)
request = createHttpRequest(endpointBuilder, BATCH_BYTES_LIMIT, noop)
})

it('should have a unique request id', () => {
Expand Down
30 changes: 9 additions & 21 deletions packages/core/src/transport/httpRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EndpointBuilder, Configuration } from '../domain/configuration'
import type { EndpointBuilder } from '../domain/configuration'
import { addTelemetryError } from '../domain/telemetry'
import type { Context } from '../tools/serialisation/context'
import { monitor } from '../tools/monitor'
Expand Down Expand Up @@ -35,14 +35,13 @@ export interface RetryInfo {
}

export function createHttpRequest(
configuration: Configuration,
endpointBuilder: EndpointBuilder,
bytesLimit: number,
reportError: (error: RawError) => void
) {
const retryState = newRetryState()
const sendStrategyForRetry = (payload: Payload, onResponse: (r: HttpResponse) => void) =>
fetchKeepAliveStrategy(configuration, endpointBuilder, bytesLimit, payload, onResponse)
fetchKeepAliveStrategy(endpointBuilder, bytesLimit, payload, onResponse)

return {
send: (payload: Payload) => {
Expand All @@ -53,17 +52,12 @@ export function createHttpRequest(
* keep using sendBeaconStrategy on exit
*/
sendOnExit: (payload: Payload) => {
sendBeaconStrategy(configuration, endpointBuilder, bytesLimit, payload)
sendBeaconStrategy(endpointBuilder, bytesLimit, payload)
},
}
}

function sendBeaconStrategy(
configuration: Configuration,
endpointBuilder: EndpointBuilder,
bytesLimit: number,
payload: Payload
) {
function sendBeaconStrategy(endpointBuilder: EndpointBuilder, bytesLimit: number, payload: Payload) {
const canUseBeacon = !!navigator.sendBeacon && payload.bytesCount < bytesLimit
if (canUseBeacon) {
try {
Expand All @@ -79,7 +73,7 @@ function sendBeaconStrategy(
}

const xhrUrl = endpointBuilder.build('xhr', payload)
sendXHR(configuration, xhrUrl, payload.data)
sendXHR(xhrUrl, payload.data)
}

let hasReportedBeaconError = false
Expand All @@ -92,7 +86,6 @@ function reportBeaconError(e: unknown) {
}

export function fetchKeepAliveStrategy(
configuration: Configuration,
endpointBuilder: EndpointBuilder,
bytesLimit: number,
payload: Payload,
Expand All @@ -106,12 +99,12 @@ export function fetchKeepAliveStrategy(
monitor(() => {
const xhrUrl = endpointBuilder.build('xhr', payload)
// failed to queue the request
sendXHR(configuration, xhrUrl, payload.data, onResponse)
sendXHR(xhrUrl, payload.data, onResponse)
})
)
} else {
const xhrUrl = endpointBuilder.build('xhr', payload)
sendXHR(configuration, xhrUrl, payload.data, onResponse)
sendXHR(xhrUrl, payload.data, onResponse)
}
}

Expand All @@ -124,12 +117,7 @@ function isKeepAliveSupported() {
}
}

export function sendXHR(
configuration: Configuration,
url: string,
data: Payload['data'],
onResponse?: (r: HttpResponse) => void
) {
export function sendXHR(url: string, data: Payload['data'], onResponse?: (r: HttpResponse) => void) {
const request = new XMLHttpRequest()
request.open('POST', url, true)
if (data instanceof Blob) {
Expand All @@ -139,7 +127,7 @@ export function sendXHR(
request.setRequestHeader('Content-Type', data.type)
}
addEventListener(
configuration,
{ allowUntrustedEvents: true },
request,
'loadend',
() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/transport/startBatchWithReplica.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function startBatchWithReplica<T extends Context>(
function createBatchFromConfig(configuration: Configuration, { endpoint, encoder }: BatchConfiguration) {
return batchFactoryImp({
encoder,
request: createHttpRequest(configuration, endpoint, configuration.batchBytesLimit, reportError),
request: createHttpRequest(endpoint, configuration.batchBytesLimit, reportError),
flushController: createFlushController({
messagesLimit: configuration.batchMessagesLimit,
bytesLimit: configuration.batchBytesLimit,
Expand Down
3 changes: 1 addition & 2 deletions packages/rum/src/boot/startRecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ export function startRecording(
}

const replayRequest =
httpRequest ||
createHttpRequest(configuration, configuration.sessionReplayEndpointBuilder, SEGMENT_BYTES_LIMIT, reportError)
httpRequest || createHttpRequest(configuration.sessionReplayEndpointBuilder, SEGMENT_BYTES_LIMIT, reportError)

let addRecord: (record: BrowserRecord) => void

Expand Down

0 comments on commit 6f3a76b

Please sign in to comment.