-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
ff79c34
commit 198e9e5
Showing
18 changed files
with
276 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* inspired by https://mathiasbynens.be/notes/globalthis | ||
*/ | ||
|
||
export function getGlobalObject<T>(): T { | ||
if (typeof globalThis === 'object') { | ||
return globalThis as unknown as T | ||
} | ||
Object.defineProperty(Object.prototype, '_dd_temp_', { | ||
get() { | ||
return this as object | ||
}, | ||
configurable: true, | ||
}) | ||
// @ts-ignore _dd_temp is defined using defineProperty | ||
let globalObject: unknown = _dd_temp_ | ||
// @ts-ignore _dd_temp is defined using defineProperty | ||
delete Object.prototype._dd_temp_ | ||
if (typeof globalObject !== 'object') { | ||
// on safari _dd_temp_ is available on window but not globally | ||
// fallback on other browser globals check | ||
if (typeof self === 'object') { | ||
globalObject = self | ||
} else if (typeof window === 'object') { | ||
globalObject = window | ||
} else { | ||
globalObject = {} | ||
} | ||
} | ||
return globalObject as T | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { display } from './display' | ||
import { CustomerDataType, CUSTOMER_DATA_BYTES_LIMIT, warnIfCustomerDataLimitReached } from './heavyCustomerDataWarning' | ||
|
||
describe('warnIfCustomerDataLimitReached', () => { | ||
let displaySpy: jasmine.Spy<typeof display.warn> | ||
beforeEach(() => { | ||
displaySpy = spyOn(display, 'warn') | ||
}) | ||
|
||
it('should warn when the customer data reach the limit', () => { | ||
const warned = warnIfCustomerDataLimitReached(CUSTOMER_DATA_BYTES_LIMIT + 1, CustomerDataType.User) | ||
expect(warned).toEqual(true) | ||
expect(displaySpy).toHaveBeenCalledWith( | ||
"The user data is over 3KiB. On low connectivity, the SDK has the potential to exhaust the user's upload bandwidth." | ||
) | ||
}) | ||
|
||
it('should not warn when the customer data does not reach the limit', () => { | ||
const warned = warnIfCustomerDataLimitReached(CUSTOMER_DATA_BYTES_LIMIT - 1, CustomerDataType.User) | ||
expect(warned).toEqual(false) | ||
expect(displaySpy).not.toHaveBeenCalled() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { display } from './display' | ||
import { ONE_KIBI_BYTE } from './utils' | ||
|
||
// RUM and logs batch bytes limit is 16KB | ||
// ensure that we leave room for other event attributes and maintain a decent amount of event per batch | ||
// (3KB (customer data) + 1KB (other attributes)) * 4 (events per batch) = 16KB | ||
export const CUSTOMER_DATA_BYTES_LIMIT = 3 * ONE_KIBI_BYTE | ||
|
||
export const enum CustomerDataType { | ||
FeatureFlag = 'feature flag evaluation', | ||
User = 'user', | ||
GlobalContext = 'global context', | ||
LoggerContext = 'logger context', | ||
} | ||
|
||
export function warnIfCustomerDataLimitReached(bytesCount: number, customerDataType: CustomerDataType): boolean { | ||
if (bytesCount > CUSTOMER_DATA_BYTES_LIMIT) { | ||
display.warn( | ||
`The ${customerDataType} data is over ${ | ||
CUSTOMER_DATA_BYTES_LIMIT / ONE_KIBI_BYTE | ||
}KiB. On low connectivity, the SDK has the potential to exhaust the user's upload bandwidth.` | ||
) | ||
return true | ||
} | ||
return false | ||
} |
Oops, something went wrong.