microsoft / ApplicationInsights-node.js

Microsoft Application Insights SDK for Node.js
MIT License
320 stars 138 forks source link

Do not know how to serialize a BigInt #1249

Open brianpham93 opened 7 months ago

brianpham93 commented 7 months ago

I use winston and winston-azure-application-insights to log directly to application insights I'm getting this error when log an object that contains BigInt data

ApplicationInsights:Failed to serialize payload [
  TypeError: Do not know how to serialize a BigInt
      at JSON.stringify (<anonymous>)
      at Util.stringify (./my-project/node_modules/applicationinsights/out/Library/Util.js:373:25)
      at EnvelopeFactory.truncateProperties (./my-project/node_modules/applicationinsights/out/Library/EnvelopeFactory.js:135:60)
      at EnvelopeFactory.createTraceData (./my-project/node_modules/applicationinsights/out/Library/EnvelopeFactory.js:153:33)
      at EnvelopeFactory.createEnvelope (./my-project/node_modules/applicationinsights/out/Library/EnvelopeFactory.js:24:40)
      at TelemetryClient.track (./my-project/node_modules/applicationinsights/out/Library/TelemetryClient.js:134:44)
      at TelemetryClient.trackTrace (./my-project/node_modules/applicationinsights/out/Library/TelemetryClient.js:60:14)
      at ./my-project/node_modules/applicationinsights/out/AutoCollection/diagnostic-channel/winston.sub.js:55:20
      at Array.forEach (<anonymous>)
      at subscriber (./my-project/node_modules/applicationinsights/out/AutoCollection/diagnostic-channel/winston.sub.js:40:13)

Does ApplicationInsights support BigInt or is there anything wrong with the way i'm logging data ?

Version: 2.9.1 Host: macOS 14, Apple Silicon

hectorhdzg commented 7 months ago

@brianpham93 looks like the issue is in JSON.stringify, after a quick internet search looks like some type replacement can be done to prevent this error, can you share some sample winston log we can use to reproduce?

JacksonWeber commented 2 months ago

@brianpham93 Did you have any luck with your serialization issue?

brianpham93 commented 2 months ago

@brianpham93 Did you have any luck with your serialization issue?

i'm using this working around temporarily

BigInt.prototype.toJSON = function () {
    return this.toString();
};
JacksonWeber commented 2 months ago

@brianpham93 Is there anything about your scenario that makes converting the Big Int to a string not work for you in the long term?

brianpham93 commented 2 months ago

@JacksonWeber So far i haven't found out any issue with current approach

JacksonWeber commented 2 months ago

@brianpham93 Awesome! Mind if I mark this issue as complete in that case?

brianpham93 commented 2 months ago

@JacksonWeber No issue. It would be better if this is supported natively in app insights via a separate PR