Skip to content

Commit 4a2e140

Browse files
authored
Merge b3e3567 into 4e50705
2 parents 4e50705 + b3e3567 commit 4a2e140

File tree

8 files changed

+51
-4
lines changed

8 files changed

+51
-4
lines changed

packages/core/lib/segments/attributes/subsegment.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ declare class Subsegment {
6161
toString(): string;
6262

6363
toJSON(): { [key: string]: any };
64+
65+
serialize(subsegment?: Subsegment): string;
6466
}
6567

6668
export = Subsegment;

packages/core/lib/segments/attributes/subsegment.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,15 +401,15 @@ Subsegment.prototype.format = function format() {
401401
this.trace_id = this.segment.trace_id;
402402
}
403403

404-
return JSON.stringify(this);
404+
return this.serialize();
405405
};
406406

407407
/**
408408
* Returns the formatted subsegment JSON string.
409409
*/
410410

411411
Subsegment.prototype.toString = function toString() {
412-
return JSON.stringify(this);
412+
return this.serialize();
413413
};
414414

415415
Subsegment.prototype.toJSON = function toJSON() {
@@ -428,4 +428,14 @@ Subsegment.prototype.toJSON = function toJSON() {
428428
return thisCopy;
429429
};
430430

431+
/**
432+
* Returns the serialized subsegment JSON string, replacing any BigInts with strings.
433+
*/
434+
Subsegment.prototype.serialize = function serialize(object) {
435+
return JSON.stringify(
436+
object ?? this,
437+
SegmentUtils.getJsonStringifyReplacer()
438+
);
439+
};
440+
431441
module.exports = Subsegment;

packages/core/lib/segments/segment.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ declare class Segment {
6464
format(): string;
6565

6666
toString(): string;
67+
68+
serialize(segment?: Segment): string;
6769
}
6870

6971
export = Segment;

packages/core/lib/segments/segment.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,11 +431,18 @@ Segment.prototype.format = function format() {
431431
false
432432
);
433433

434-
return JSON.stringify(thisCopy);
434+
return this.serialize(thisCopy);
435435
};
436436

437437
Segment.prototype.toString = function toString() {
438-
return JSON.stringify(this);
438+
return this.serialize();
439+
};
440+
441+
Segment.prototype.serialize = function serialize(object) {
442+
return JSON.stringify(
443+
object ?? this,
444+
SegmentUtils.getJsonStringifyReplacer()
445+
);
439446
};
440447

441448
module.exports = Segment;

packages/core/lib/segments/segment_utils.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ export function setStreamingThreshold(threshold: number): void;
1717
export function getStreamingThreshold(): number;
1818

1919
export function getHttpResponseData(res: http.ServerResponse): object;
20+
21+
export function getJsonStringifyReplacer(): (key: string, value: any) => any;

packages/core/lib/segments/segment_utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ var utils = {
6767
ret.content_length = safeParseInt(res.headers['content-length']);
6868
}
6969
return ret;
70+
},
71+
72+
getJsonStringifyReplacer: () => (_, value) => {
73+
if (typeof value === 'bigint') {
74+
return value.toString();
75+
}
76+
77+
return value;
7078
}
7179
};
7280

packages/core/test/unit/segments/attributes/subsegment.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,12 @@ describe('Subsegment', function() {
332332
child.flush();
333333
emitStub.should.have.been.called;
334334
});
335+
336+
it('should stringify bigint objects correctly', function() {
337+
child.addMetadata('key', BigInt(9007199254740991));
338+
child.flush();
339+
emitStub.should.have.been.calledOnce;
340+
});
335341
});
336342

337343
describe('#streamSubsegments', function() {

packages/core/test/unit/segments/segment_utils.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,14 @@ describe('SegmentUtils', function() {
4343
assert.deepEqual(emptyRes, {});
4444
});
4545
});
46+
47+
describe('#getJsonStringifyReplacer', () => {
48+
it('should stringify BigInts', () => {
49+
const obj = {foo: 1n, bar: BigInt(2)};
50+
const replacer = SegmentUtils.getJsonStringifyReplacer();
51+
const result = JSON.stringify(obj, replacer);
52+
53+
assert.equal(result, '{"foo":"1","bar":"2"}');
54+
});
55+
});
4656
});

0 commit comments

Comments
 (0)