Adding tags for Datadog browser logs

Viewed 1212

I am using Datadog to monitor my browser console logs. I need different tags in for datadog logs. The only option I fount is to add attributes to my logger using,

DD_LOGS.addContext('referrer', document.referrer);

Is there any way for the frontend client application to have tags in datadog? Or is the attribute and tags are same in Datadog

1 Answers

In Datadog Logs, there's a difference between the Tags associated with the execution environment, and Attributes set on Log entry content.

From this section in the docs:

Context refers to the infrastructure and application context in which the log has been generated. Information is gathered from tags—whether automatically attached (host name, container name, log file name, serverless function name, etc.)—or added through custom tags (team in charge, environment, application version, etc.) on the log by the Datadog Agent or Log Forwarder.

And looking into the source for the browser SDK, we can see:

...
  const tags =
    `sdk_version:${conf.sdkVersion}` +
    `${conf.env ? `,env:${conf.env}` : ''}` +
    `${conf.service ? `,service:${conf.service}` : ''}` +
    `${conf.version ? `,version:${conf.version}` : ''}`
  const datadogHost = `${type}-http-intake.logs.${domain}`
  const host = conf.proxyHost ? conf.proxyHost : datadogHost
  const proxyParameter = conf.proxyHost ? `ddhost=${datadogHost}&` : ''

  return `https://${host}/v1/input/${conf.clientToken}?${proxyParameter}ddsource=${source || 'browser'}&ddtags=${tags}`
...

This shows us that the tags query string parameter being submitted is being calculated based on configuration, and only provides a small amount of user-configurable entries, like env, service - these were released very recently in version 1.11.5 - here's the change introducing them.

So you may not be able to set tags for a specific log entry - rather you can set attributes per log entry, like in the example you shared, which is setting Attributes for the logger instance as a whole. Attributes are part of the log Content - which will be viewable in the body of the log entry.

Yes, this is confusing since the function used is named addContext/setContext - and these don't set the same thing as the documentation's "Context" - rather they modify the Attributes that are associated with the log entry.

In that event, you may want to have either custom logger instances that provide specific attributes for that logger, or add context inline to the log entry, like this:

DD_LOGS.logger.info('Page Viewed', { referrer: document.referrer });

Here's the docs on this approach which show what other default attributes are being set per log entry.

Related