Skip to content

fix(core): serialize bigint in metadata to string #619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/lib/segments/attributes/subsegment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ declare class Subsegment {
toString(): string;

toJSON(): { [key: string]: any };

serialize(object?: Subsegment): string;
}

export = Subsegment;
14 changes: 12 additions & 2 deletions packages/core/lib/segments/attributes/subsegment.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,15 +401,15 @@ Subsegment.prototype.format = function format() {
this.trace_id = this.segment.trace_id;
}

return JSON.stringify(this);
return this.serialize();
};

/**
* Returns the formatted subsegment JSON string.
*/

Subsegment.prototype.toString = function toString() {
return JSON.stringify(this);
return this.serialize();
};

Subsegment.prototype.toJSON = function toJSON() {
Expand All @@ -428,4 +428,14 @@ Subsegment.prototype.toJSON = function toJSON() {
return thisCopy;
};

/**
* Returns the serialized subsegment JSON string, replacing any BigInts with strings.
*/
Subsegment.prototype.serialize = function serialize(object) {
return JSON.stringify(
object ?? this,
SegmentUtils.getJsonStringifyReplacer()
);
};

module.exports = Subsegment;
2 changes: 2 additions & 0 deletions packages/core/lib/segments/segment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ declare class Segment {
format(): string;

toString(): string;

serialize(object?: Segment): string;
}

export = Segment;
11 changes: 9 additions & 2 deletions packages/core/lib/segments/segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,18 @@ Segment.prototype.format = function format() {
false
);

return JSON.stringify(thisCopy);
return this.serialize(thisCopy);
};

Segment.prototype.toString = function toString() {
return JSON.stringify(this);
return this.serialize();
};

Segment.prototype.serialize = function serialize(object) {
return JSON.stringify(
object ?? this,
SegmentUtils.getJsonStringifyReplacer()
);
};

module.exports = Segment;
2 changes: 2 additions & 0 deletions packages/core/lib/segments/segment_utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ export function setStreamingThreshold(threshold: number): void;
export function getStreamingThreshold(): number;

export function getHttpResponseData(res: http.ServerResponse): object;

export function getJsonStringifyReplacer(): (key: string, value: any) => any;
8 changes: 8 additions & 0 deletions packages/core/lib/segments/segment_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ var utils = {
ret.content_length = safeParseInt(res.headers['content-length']);
}
return ret;
},

getJsonStringifyReplacer: () => (_, value) => {
if (typeof value === 'bigint') {
return value.toString();
}

return value;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ describe('Subsegment', function() {
child.flush();
emitStub.should.have.been.called;
});

it('should stringify bigint objects correctly', function() {
child.addMetadata('key', BigInt(9007199254740991));
child.flush();
emitStub.should.have.been.calledOnce;
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see a similar test for Segment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing test harness for Segment stops at mocking the Segment.flush() method and asserting that it's been called. Given that segment is serialized within the flush method, the serialization is never done.

Adding this test would mean rearchitecting the tests and requires knowledge of the codebase that I don't have.

If the maintainers want to add this, I'd suggest them to either: 1/ address this in a future PR, 2/ contribute to this PR and provide the test fixture.

});

describe('#streamSubsegments', function() {
Expand Down
10 changes: 10 additions & 0 deletions packages/core/test/unit/segments/segment_utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,14 @@ describe('SegmentUtils', function() {
assert.deepEqual(emptyRes, {});
});
});

describe('#getJsonStringifyReplacer', () => {
it('should stringify BigInts', () => {
const obj = {foo: 1n, bar: BigInt(2)};
const replacer = SegmentUtils.getJsonStringifyReplacer();
const result = JSON.stringify(obj, replacer);

assert.equal(result, '{"foo":"1","bar":"2"}');
});
});
});