Skip to content
This repository has been archived by the owner on Dec 14, 2022. It is now read-only.

How to send JSON to datadog as the message #33

Open
dcurletti opened this issue May 30, 2019 · 2 comments
Open

How to send JSON to datadog as the message #33

dcurletti opened this issue May 30, 2019 · 2 comments

Comments

@dcurletti
Copy link

Wondering if you have experience trying to send logs to datadog with a message that is json rather than a string? I'd like to do this because I want to be able to search on all of the message fields rather than sending the JSON as metadata and then creating a facet.

any help is appreciated, thanks!

@dcurletti dcurletti changed the title How to send JSON logs to datadog in such a way that the metadata fields are searchable without creating a facet How to send JSON to datadog as the message May 30, 2019
@vladholubiev
Copy link
Member

Hey Davide!

I personally didn't try to send a JSON message and always send it as a metadata.

Can you please describe what happens right now when you do so? Does it send a log message at all, prints error, etc?

@wyantb
Copy link

wyantb commented Jan 15, 2020

Hey @dcurletti ! I had no problems sending JSON objects to datadog. Code snippet incoming:

const fmt = process.env.LOGGER_FORMAT === 'simple' ?
  format.combine(format.timestamp(), format.colorize(), format.simple()) :
  format.combine(format.timestamp(), format.json());

const ourTransports = [new transports.Console()];
if (process.env.DD_API_KEY) {
  ourTransports.push(new DatadogTransport({
    apiKey: process.env.DD_API_KEY,
    metadata: {
      source: 'nodejs', // not sure this does anything useful
      sourcecategory: 'sourcecode', // not sure this does anything useful
      service: process.env.DD_SERVICE,
      environment: process.env.YOUR_ENV,
    },
  }));
}

const logger = createLogger({
  format: fmt,
  level: process.env.LOGGER_LEVEL || 'info',
  transports: ourTransports,
});

Basically, for devs running the server w/ a winston logger locally, they set LOGGER_FORMAT of simple to get a plain, simple formatted logger w/ colors. In production, though, DD_API_KEY is set and the format used is format.combine(format.timestamp(), format.json()) - meaning that the logger itself will spit out JSON messages to both the console and datadog. All of our logger instances used in specific classes, then, use a simple logger.child({ class: 'FILENAME HERE' }) call so that we know the source of a particular logger call, and that class field present in the JSON is one of our facets for searching in datadog. It all works pretty well in my experience.

Hope that helps!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

3 participants