Skip to content

Commit 8d4594f

Browse files
committed
sdk-core: reduce breadcrumb size PR changes
1 parent cb99dbf commit 8d4594f

File tree

7 files changed

+46
-18
lines changed

7 files changed

+46
-18
lines changed

packages/sdk-core/src/common/jsonSize.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
type JsonReplacer = (this: unknown, key: string, value: unknown) => unknown;
2+
13
function stringifiedSize<T>(value: T): number {
24
return JSON.stringify(value).length;
35
}
@@ -15,7 +17,7 @@ const booleanSize = (value: boolean) => (value ? 4 : 5);
1517
const undefinedSize = 0;
1618
const nullSize = 'null'.length;
1719

18-
function arraySize(array: unknown[], replacer?: (this: unknown, key: string, value: unknown) => unknown): number {
20+
function arraySize(array: unknown[], replacer?: JsonReplacer): number {
1921
const bracketLength = 2;
2022
const commaLength = array.length - 1;
2123
let elementsLength = 0;
@@ -35,7 +37,7 @@ function arraySize(array: unknown[], replacer?: (this: unknown, key: string, val
3537
return bracketLength + commaLength + elementsLength;
3638
}
3739

38-
const objectSize = (obj: object, replacer?: (this: unknown, key: string, value: unknown) => unknown): number => {
40+
const objectSize = (obj: object, replacer?: JsonReplacer): number => {
3941
const entries = Object.entries(obj);
4042
const bracketLength = 2;
4143

@@ -49,9 +51,12 @@ const objectSize = (obj: object, replacer?: (this: unknown, key: string, value:
4951
}
5052

5153
entryCount++;
54+
55+
// +1 adds the comma size
5256
entriesLength += keySize(k) + valueSize + 1;
5357
}
5458

59+
// -1 removes previously added last comma size (there is no trailing comma)
5560
const commaLength = Math.max(0, entryCount - 1);
5661

5762
return bracketLength + commaLength + entriesLength;
@@ -74,18 +79,13 @@ function keySize(key: unknown): number {
7479
case 'boolean':
7580
return booleanSize(key) + QUOTE_SIZE;
7681
case 'symbol':
77-
return 0; // key not used in JSON
82+
return symbolSize; // key not used in JSON
7883
default:
7984
return stringSize(key.toString());
8085
}
8186
}
8287

83-
function _jsonSize(
84-
parent: unknown,
85-
key: string,
86-
value: unknown,
87-
replacer?: (this: unknown, key: string, value: unknown) => unknown,
88-
): number {
88+
function _jsonSize(parent: unknown, key: string, value: unknown, replacer?: JsonReplacer): number {
8989
if (value && typeof value === 'object' && 'toJSON' in value && typeof value.toJSON === 'function') {
9090
value = value.toJSON() as object;
9191
}
@@ -132,6 +132,6 @@ function _jsonSize(
132132
* @param replacer A function that transforms the results as in `JSON.stringify`.
133133
* @returns Final string length.
134134
*/
135-
export function jsonSize(value: unknown, replacer?: (this: unknown, key: string, value: unknown) => unknown): number {
135+
export function jsonSize(value: unknown, replacer?: JsonReplacer): number {
136136
return _jsonSize(undefined, '', value, replacer);
137137
}

packages/sdk-core/src/dataStructures/OverwritingArray.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@ export class OverwritingArray<T> {
3434
items?: T[],
3535
) {
3636
this._array = new Array(capacity);
37+
38+
// Head must be always between 0 and capacity.
39+
// If lower than 0, it needs to go from the end
40+
// If larger than capacity, it needs to go from the start
41+
// Wrapping solves this
3742
this._headConstraint = wrapped(0, capacity);
43+
44+
// Length must be always no less than 0 and no larger than capacity
3845
this._lengthConstraint = clamped(0, capacity);
3946

4047
if (items) {

packages/sdk-core/src/dataStructures/numbers.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
export type ConstrainedNumber = (value: number) => number;
22

3+
/**
4+
* Constrains `value` to `min` and `max` values, wrapping not matching values around.
5+
* @param min minimum value to allow
6+
* @param max maximum value to allow
7+
* @returns function accepting `value`
8+
*
9+
* @example
10+
* const wrap = wrapped(10, 20);
11+
* console.log(wrap(15)); // 15
12+
* console.log(wrap(21)); // 10, wrapped around
13+
* console.log(wrap(8)); // 18, wrapped around
14+
*/
315
export function wrapped(min: number, max: number): ConstrainedNumber {
416
function wrapped(value: number) {
517
const range = max - min;
@@ -23,6 +35,18 @@ export function wrapped(min: number, max: number): ConstrainedNumber {
2335
return wrapped;
2436
}
2537

38+
/**
39+
* Constrains `value` to `min` and `max` values.
40+
* @param min minimum value to allow
41+
* @param max maximum value to allow
42+
* @returns function accepting `value`
43+
*
44+
* @example
45+
* const clamp = clamped(10, 20);
46+
* console.log(wrap(15)); // 15
47+
* console.log(wrap(21)); // 20
48+
* console.log(wrap(8)); // 10
49+
*/
2650
export function clamped(min: number, max: number): ConstrainedNumber {
2751
function clamped(value: number) {
2852
return Math.max(min, Math.min(value, max));

packages/sdk-core/src/modules/breadcrumbs/BreadcrumbsManager.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,7 @@ export class BreadcrumbsManager implements BacktraceBreadcrumbs, BacktraceModule
182182
}
183183

184184
if (this._limits.maximumBreadcrumbMessageLength !== undefined) {
185-
rawBreadcrumb = {
186-
...rawBreadcrumb,
187-
message: rawBreadcrumb.message.substring(0, this._limits.maximumBreadcrumbMessageLength),
188-
};
185+
rawBreadcrumb.message = rawBreadcrumb.message.substring(0, this._limits.maximumBreadcrumbMessageLength);
189186
}
190187

191188
let limitedBreadcrumb: RawBreadcrumb | LimitedRawBreadcrumb;

packages/sdk-core/src/modules/breadcrumbs/storage/BreadcrumbsStorage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface BreadcrumbsStorageLimits {
1717
* Specifies maximum breadcrumbs size in bytes.
1818
* If the size is exceeded, oldest breadcrumbs will be skipped.
1919
*/
20-
readonly maximumBreadcrumbsSize?: number;
20+
readonly maximumTotalBreadcrumbsSize?: number;
2121
}
2222

2323
export type BreadcrumbsStorageFactory = (options: BreadcrumbsStorageOptions) => BreadcrumbsStorage;

packages/sdk-core/src/modules/breadcrumbs/storage/InMemoryBreadcrumbsStorage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ export class InMemoryBreadcrumbsStorage implements BreadcrumbsStorage, Backtrace
7070

7171
this._breadcrumbs.add(breadcrumb);
7272

73-
if (this._limits.maximumBreadcrumbsSize) {
73+
if (this._limits.maximumTotalBreadcrumbsSize) {
7474
const size = jsonSize(breadcrumb, jsonEscaper());
7575
this._breadcrumbSizes.add(size);
7676

7777
let totalSize = this.totalSize();
78-
while (totalSize > this._limits.maximumBreadcrumbsSize) {
78+
while (totalSize > this._limits.maximumTotalBreadcrumbsSize) {
7979
this._breadcrumbs.shift();
8080
const removedSize = this._breadcrumbSizes.shift() ?? 0;
8181

packages/sdk-core/tests/breadcrumbs/InMemoryBreadcrumbsStorage.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ describe('InMemoryBreadcrumbsStorage', () => {
162162
const size = JSON.stringify(expected).length;
163163
const storage = new InMemoryBreadcrumbsStorage({
164164
maximumBreadcrumbs: 100,
165-
maximumBreadcrumbsSize: size,
165+
maximumTotalBreadcrumbsSize: size,
166166
});
167167

168168
for (const breadcrumb of breadcrumbs) {

0 commit comments

Comments
 (0)