Skip to content

Commit

Permalink
fix(core): serialize bigint in metadata to string (#619)
Browse files Browse the repository at this point in the history
* fix(core): serialize bigint in metadata to string

* chore: extracted replacer into SegmentUtils

* Update subsegment.d.ts

Co-authored-by: Carol Abadeer <60774943+carolabadeer@users.noreply.github.com>

* Update segment.d.ts

Co-authored-by: Carol Abadeer <60774943+carolabadeer@users.noreply.github.com>

---------

Co-authored-by: Andrea Amorosi <aamorosi@amazon.es>
Co-authored-by: Carol Abadeer <60774943+carolabadeer@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 21, 2023
1 parent 4e50705 commit 45a180b
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 4 deletions.
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(subsegment?: 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(segment?: 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;
});
});

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"}');
});
});
});

0 comments on commit 45a180b

Please sign in to comment.