DataDog / browser-sdk

Datadog Browser SDK
Apache License 2.0
279 stars 130 forks source link

`beforeSend` not firing #2622

Open austinlogo opened 4 months ago

austinlogo commented 4 months ago

Describe the bug `BeforeSend is being sent on init configurations, but we're still seeing the logs make their way through.

To Reproduce

Begin Logging with the following beforeSend config

datadogLogs.init({
      beforeSend: (event) => {
        if (event.message.includes('DD_LOGS is already initialized')) return false;
        if (event.http?.status_code === 0) {
          webLogger.log('swallowing event error', event);
          return false;
        }

        return true;
      },
    });

Expected behavior I would expect aborted requests with a 0 status code to not show up in the DD logs.

N-Boutaib commented 4 months ago

Hello @austinlogo ! Thanks for reaching out.

I tried the code snippet you provided (I removed webLogger.log('swallowing event error', event);) and it is filtering out all the events that have 0 as a status_code.

Could you please check if you can reproduce it on your app ? You can use the browser-sdk extension to see which events (logs) are being sent.

Otherwise, we suspect that perhaps some old version of your app, still using the configuration without beforeSend, could be the origin of those event, or some error might be thrown when trying to log using webLogger when the status_code is 0.

austinlogo commented 4 months ago

Hey @N-Boutaib, thanks for getting back. We looked into this more and still see it. Will the datadogLogs.init() update configurations if run again. We were also seeing Datadog Browser SDK: DD_RUM is already initialized. errors.

Questions:

image

BenoitZugmeyer commented 4 months ago

Does the config update if run again (i.e. on refresh), if not How can we update the configurations?

No, it won't. For beforeSend, you could use a reference like this:

let currentBeforeSend = (event) => { ... }
datadogLogs.init({
  beforeSend: (event) => currentBeforeSend(event)
})

// later
currentBeforeSend = (event) => { ...something else... }

Can we filter these out in the pipeline? We tried that but it seems like the pipeline is read-only.

You should be able to create a new pipeline, and use source:browser as a filter to focus on browser logs.