Skip to content

Commit

Permalink
fix(rum-core): ensure apmRequest is called for events (#1055)
Browse files Browse the repository at this point in the history
* fix(rum-core): ensure apmRequest is called for events

* chore: fix cors and tests

* chore: allow headers from apm server
  • Loading branch information
vigneshshanmugam authored Jul 15, 2021
1 parent 53dced5 commit 7ae7e39
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
1 change: 1 addition & 0 deletions dev-utils/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ services:
apm-server -e
-E apm-server.rum.enabled=true
-E apm-server.rum.event_rate.limit=100000
-E apm-server.rum.allow_headers=["x-custom-header"]
-E apm-server.host=0.0.0.0:8200
-E apm-server.read_timeout=1m
-E apm-server.shutdown_timeout=2m
Expand Down
5 changes: 3 additions & 2 deletions packages/rum-core/src/common/apm-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,16 @@ class ApmServer {
'Content-Type': 'application/x-ndjson'
}
const apmRequest = this._configService.get('apmRequest')
return compressPayload(payload, headers)
const params = { payload, headers, beforeSend: apmRequest }
return compressPayload(params)
.catch(error => {
if (__DEV__) {
this._loggingService.debug(
'Compressing the payload using CompressionStream API failed',
error.message
)
}
return { payload, headers, beforeSend: apmRequest }
return params
})
.then(result => this._makeHttpRequest('POST', endPoint, result))
.then(({ responseText }) => responseText)
Expand Down
8 changes: 4 additions & 4 deletions packages/rum-core/src/common/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,17 +294,17 @@ export function compressMetricsets(breakdowns) {
* CompressionStream spec supported only in Chromium browsers
* Spec : https://wicg.github.io/compression/
*/
export function compressPayload(payload, headers, type = 'gzip') {
export function compressPayload(params, type = 'gzip') {
const isCompressionStreamSupported = typeof CompressionStream === 'function'
return new Promise(resolve => {
/**
* Resolve with unmodified payload if the compression stream
* is not supported in browser
*/
if (!isCompressionStreamSupported) {
return resolve({ payload, headers })
return resolve(params)
}

const { payload, headers, beforeSend } = params
/**
* create a blob with the original payload data and convert it
* as readable stream
Expand All @@ -323,7 +323,7 @@ export function compressPayload(payload, headers, type = 'gzip') {
*/
return new Response(compressedStream).blob().then(payload => {
headers['Content-Encoding'] = type
return resolve({ payload, headers })
return resolve({ payload, headers, beforeSend })
})
})
}
5 changes: 3 additions & 2 deletions packages/rum-core/test/benchmarks/compress.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ suite('Compress', () => {
.join('')

benchmark('compress payload', async () => {
await compressPayload(ndjsonPayload, {
'Content-Type': 'application/x-ndjson'
await compressPayload({
payload: ndjsonPayload,
headers: { 'Content-Type': 'application/x-ndjson' }
})
})
})
8 changes: 4 additions & 4 deletions packages/rum-core/test/common/compress.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,10 @@ describe('Compress', function () {
.join('')
const isCompressionStreamSupported = typeof CompressionStream === 'function'
const originalHeaders = { 'Content-Type': 'application/x-ndjson' }
let { payload, headers } = await compressPayload(
ndjsonPayload,
originalHeaders
)
let { payload, headers } = await compressPayload({
payload: ndjsonPayload,
headers: originalHeaders
})
if (isCompressionStreamSupported) {
const decompressedBlob = await decompressPayload(payload)
payload = await view(decompressedBlob)
Expand Down
4 changes: 2 additions & 2 deletions packages/rum/test/e2e/general-usecase/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ const elasticApm = createApmBase({
pageLoadSpanId: 'bbd8bcc3be14d814',
pageLoadSampled: true,
session: true,
apmRequest(xhr) {
xhr.setRequestHeader('custom', 'value')
apmRequest: ({ xhr }) => {
xhr.setRequestHeader('x-custom-header', 'foo')
return true
}
})
Expand Down

0 comments on commit 7ae7e39

Please sign in to comment.