microsoft / ApplicationInsights-node.js

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

[Beta] - "ApplicationInsights:Invalid attribute value set for key: req []" #1168

Open marcus13371337 opened 1 year ago

marcus13371337 commented 1 year ago

Hello,

I just bumped to 3.0.0-beta.6 (from 3.0.0-beta.4) and started noticing a lot of errors in my application:

ApplicationInsights:Invalid attribute value set for key: req []
ApplicationInsights:Invalid attribute value set for key: res []

What's up with that? Am I doing something wrong?

hectorhdzg commented 1 year ago

@marcus13371337 can you share the actual telemetry event that is triggering this?, I can see there is some OpenTelemetry that add the warning when the value of the attribute is null, you may want to sanitize the attributes before sending the telemetry

marcus13371337 commented 1 year ago

@hectorhdzg I can't reproduce it locally unfortunately. However, it seems as if objects and errors are sent with toString instead of JSON-serialized (which worked in the earlier version). Screenshot 2023-07-04 at 08 28 29

I think it's an error on our side, we have a logging to trace converter which was implemented this way:

  const getLogProperties = (entry: LogEntry) => {
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    const { msg, ...rest } = entry;

    return {
      ...rest,
    };
  };

  const insertTrace = (entry: LogEntry) => {
    const inserts: Promise<void>[] = [];

    inserts.push(
      logHandler.trackTrace({
        message: entry.msg,
        severity: getLogSeverityName(entry.level),
        properties: getLogProperties(entry),
      })
    );

    if (entry.level >= 50) {
      const err = new Error(entry.msg);
      err.stack = entry.stack || "";

      inserts.push(
        logHandler.trackException({
          exception: err,
          properties: getLogProperties(entry),
        })
      );
    }

    return Promise.all(inserts);
  };

We had to fix that with the following change:

  const getLogProperties = (entry: LogEntry) => {
    const { msg, ...rest } = entry;

    return Object.entries(rest).reduce(
      (acc, [key, value]) => ({ ...acc, [key]: JSON.stringify(value) }), // Apperently you need to JSON-stringify in the new version
      {}
    );
  };

  const insertTrace = (entry: LogEntry) => {
    const inserts: Promise<void>[] = [];

    inserts.push(
      logHandler.trackTrace({
        message: entry.msg,
        severity: getLogSeverityName(entry.level),
        properties: getLogProperties(entry),
      })
    );

    if (entry.level >= 50) {
      const err = new Error(entry.msg);
      err.stack = entry.stack || "";

      inserts.push(
        logHandler.trackException({
          exception: err,
          properties: getLogProperties(entry),
        })
      );
    }

    return Promise.all(inserts);
  };

Note the difference in getLogProperties. I'm not sure if it's a bug or a feature :)

marcus13371337 commented 1 year ago

The fix above fixed the issue reported in the logs i.e "ApplicationInsights:Invalid attribute value set for key: req"

JacksonWeber commented 1 month ago

@marcus13371337 I believe the above should be solved for you now as of the fix for better serialization handling of values passed to loggers.