Skip to content

Commit

Permalink
feat: Add warning for large payloads (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
metal-messiah authored Jul 27, 2023
1 parent dc4fb1b commit d616f64
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/common/harvest/harvest.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import { applyFnToProps } from '../util/traverse'
import { SharedContext } from '../context/shared-context'
import { VERSION } from '../constants/env'
import { isWorkerScope, isIE } from '../constants/runtime'
import { warn } from '../util/console'

const warnings = {}

/**
* @typedef {import('./types.js').NetworkSendSpec} NetworkSendSpec
Expand All @@ -24,7 +27,6 @@ import { isWorkerScope, isIE } from '../constants/runtime'
* @typedef {import('./types.js').FeatureHarvestCallback} FeatureHarvestCallback
* @typedef {import('./types.js').FeatureHarvestCallbackOptions} FeatureHarvestCallbackOptions
*/

export class Harvest extends SharedContext {
constructor (parent) {
super(parent) // gets any allowed properties from the parent and stores them in `sharedContext`
Expand Down Expand Up @@ -115,6 +117,8 @@ export class Harvest extends SharedContext {
} else {
body = stringify(body)
}
/** Warn --once per endpoint-- if the agent tries to send large payloads */
if (body.length > 750000 && (warnings[endpoint] = (warnings?.[endpoint] || 0) + 1) === 1) warn(`The Browser Agent is attempting to send a very large payload to /${endpoint}. This is usually tied to large amounts of custom attributes. Please check your configurations.`)
}

if (!body || body.length === 0 || body === '{}' || body === '[]') {
Expand Down
15 changes: 15 additions & 0 deletions src/common/harvest/harvest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { faker } from '@faker-js/faker'
import * as encodeModule from '../url/encode'
import * as submitDataModule from '../util/submit-data'
import * as configModule from '../config/config'
import { warn } from '../util/console'
import { applyFnToProps } from '../util/traverse'

import { Harvest } from './harvest'
Expand Down Expand Up @@ -288,6 +289,20 @@ describe('_send', () => {
})
})

test('should warn (once) if payload is large', () => {
spec.payload.body = 'x'.repeat(1024 * 1024) // ~1mb string

const result = harvestInstance._send(spec)

expect(result).toEqual(true)
expect(warn).toHaveBeenCalledWith(expect.stringContaining('The Browser Agent is attempting to send a very large payload'))
expect(warn).toHaveBeenCalledTimes(1)

const result2 = harvestInstance._send(spec)
expect(result2).toEqual(true)
expect(warn).toHaveBeenCalledTimes(1)
})

test('should set body to events when endpoint is events', () => {
spec.endpoint = 'events'
spec.payload.body.e = faker.lorem.sentence()
Expand Down

0 comments on commit d616f64

Please sign in to comment.