grafana / faro-web-sdk

The Grafana Faro Web SDK, part of the Grafana Faro project, is a highly configurable web SDK for real user monitoring (RUM) that instruments browser frontend applications to capture observability signals. Frontend telemetry can then be correlated with backend and infrastructure data for full-stack observability.
https://grafana.com/oss/faro/
Apache License 2.0
690 stars 62 forks source link

`pushLog` argument serializer #564

Closed mxab closed 2 months ago

mxab commented 2 months ago

Description

Currently the args array for the pushLog function maps each arg to a string.

{
...
  message: args
    .map((arg) => {
      try {
        return String(arg);
      } catch (err) {
        return '';
      }
    })
    .join(' '),
}

This works but makes logs ugly as soon as someone write console.log("something happened" , {foo: "bar"}) as it yields something happened [object Object]

Proposed solution

Extend the config with a LogMessageSerializer which is used instead of the hard coded String(arg);.

{
...
  message: args
    .map((arg) => {
      return config.logMessageSerializer.serialize(arg)
    })
    .join(' '),
}

Use current behaviour as a default StringLogMessageSerializer in the config but it could be overwritten with a e.g. JSONLogMessageSerializer that would do something like JSON.stringify(arg)