Skip to content

Commit

Permalink
🐛 Fix dd-request-id endpoint query param
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque committed Aug 30, 2021
1 parent e50faf3 commit ce160d3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
5 changes: 2 additions & 3 deletions packages/core/src/domain/configuration/endpointBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ export function createEndpointBuilder(
if (shouldUseIntakeV2(endpointType)) {
parameters +=
`&dd-api-key=${clientToken}&` +
`dd-evp-origin-version=${sdkVersion}&` +
`dd-evp-origin=browser&` +
`dd-request-id=${generateUUID()}`
`dd-evp-origin-version=${encodeURIComponent(sdkVersion)}&` +
`dd-evp-origin=browser`
}

return `${buildIntakeUrl(endpointType)}?${parameters}`
Expand Down
48 changes: 48 additions & 0 deletions packages/core/src/transport/httpRequest.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable @typescript-eslint/unbound-method */
import sinon from 'sinon'
import { BuildEnv, BuildMode } from '../boot/init'
import { createEndpointBuilder } from '../domain/configuration/endpointBuilder'
import { HttpRequest } from './httpRequest'

describe('httpRequest', () => {
Expand Down Expand Up @@ -63,3 +65,49 @@ describe('httpRequest', () => {
expect(server.requests.length).toEqual(1)
})
})

describe('httpRequest parameters', () => {
const BATCH_BYTES_LIMIT = 100
const clientToken = 'some_client_token'
const buildEnv: BuildEnv = {
buildMode: BuildMode.RELEASE,
sdkVersion: 'some_version',
}
let server: sinon.SinonFakeServer
let sendBeaconSpy: jasmine.Spy<(url: string, data?: BodyInit | null) => boolean>
beforeEach(() => {
server = sinon.fakeServer.create()
sendBeaconSpy = jasmine.createSpy()
navigator.sendBeacon = sendBeaconSpy
})

afterEach(() => {
server.restore()
})

it('should add batch_time', () => {
const request = new HttpRequest('https://my.website', BATCH_BYTES_LIMIT, true)

request.send('{"foo":"bar1"}', 10)

expect(sendBeaconSpy.calls.argsFor(0)[0]).toContain(`batch_time=`)
})

it('should add dd-request-id query attribute when the intake v2 enabled', () => {
const endpointBuilder = createEndpointBuilder({ clientToken, intakeApiVersion: 2 }, buildEnv, true)
const request = new HttpRequest(endpointBuilder.build('rum'), BATCH_BYTES_LIMIT)

request.send('{"foo":"bar1"}', 10)

expect(sendBeaconSpy.calls.argsFor(0)[0]).toContain(`&dd-request-id=`)
})

it('should not add dd-request-id query attribute when the intake v1 enabled', () => {
const endpointBuilder = createEndpointBuilder({ clientToken }, buildEnv, true)
const request = new HttpRequest(endpointBuilder.build('rum'), BATCH_BYTES_LIMIT)

request.send('{"foo":"bar1"}', 10)

expect(sendBeaconSpy.calls.argsFor(0)[0]).not.toContain(`&dd-request-id=`)
})
})
20 changes: 19 additions & 1 deletion packages/core/src/transport/httpRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { monitor, addErrorToMonitoringBatch, addMonitoringMessage } from '../domain/internalMonitoring'
import { generateUUID, includes } from '../tools/utils'

let hasReportedXhrError = false

Expand All @@ -14,7 +15,16 @@ export class HttpRequest {
constructor(private endpointUrl: string, private bytesLimit: number, private withBatchTime: boolean = false) {}

send(data: string | FormData, size: number) {
const url = this.withBatchTime ? addBatchTime(this.endpointUrl) : this.endpointUrl
let url = this.endpointUrl

if (isEndpointIntakeV2(url)) {
url = addRequestId(url)
}

if (this.withBatchTime) {
url = addBatchTime(url)
}

const tryBeacon = !!navigator.sendBeacon && size < this.bytesLimit
if (tryBeacon) {
try {
Expand Down Expand Up @@ -67,6 +77,14 @@ function addBatchTime(url: string) {
return `${url}${url.indexOf('?') === -1 ? '?' : '&'}batch_time=${new Date().getTime()}`
}

function addRequestId(url: string) {
return `${url}${url.indexOf('?') === -1 ? '?' : '&'}dd-request-id=${generateUUID()}`
}

function isEndpointIntakeV2(url: string) {
return includes(url, '/api/v2')
}

let hasReportedBeaconError = false
function reportBeaconError(e: unknown) {
if (!hasReportedBeaconError) {
Expand Down

0 comments on commit ce160d3

Please sign in to comment.