pino-datadog-transport
A pino v7+ transport for sending logs to Datadog.
It uses datadog-api-client-typescript to
send logs using the client v2.LogsApi#submitLog method.
- Performs batch sending of logs on a periodic basis, or when log capacity is reached in overall log batch size or count.
- Will retry failed sends.
- Can disable batch sending and always send for each log entry.
Table of Contents
Installation
yarn add pino-datadog-transport
import { LoggerOptions, pino } from 'pino';
const pinoConf: LoggerOptions = {
level: 'trace',
}
const logger = pino(pinoConf, pino.transport({
target: 'pino-datadog-transport',
options: {
ddClientConf: {
authMethods: {
apiKeyAuth: <your datadog API key>
}
},
ddServerConf: {
// Note: This must match the site you use for your DataDog login - See below for more info
site: "datadoghq.eu"
}
}
}))
Configuration options
export interface DDTransportOptions {
ddClientConf: ConfigurationParameters
ddServerConf?: {
site?: string
subdomain?: string
protocol?: string
}
ddsource?: string
ddtags?: string
service?: string
onInit?: () => void
onError?: (err: any, logs?: Array<Record<string, any>>) => void
onDebug?: (msg: string) => void
retries?: number
sendIntervalMs?: number
sendImmediate?: boolean
}
Implementing the onError()
/ onDebug()
callback
You cannot specify the callbacks directly as they are not serializable.
Doing so will result in the following error:
DOMException [DataCloneError]: (e)=>{
} could not be cloned.
Instead, you need to create another file that implements it:
const p = pino({}, pino.transport({
target: join(__dirname, 'pino-datadog-logger.js'),
options: {
ddClientConf: {
authMethods: {
apiKeyAuth: <your datadog API key>
}
},
}
}))
module.exports = (opts) => {
return require('pino-datadog-logger')({
...opts,
onError: (data, logItems) => {
}
})
}
Note: Log entries can only be a maximum of 1MB in size. This is a Datadog imposed limit.
This library will call onError() if a log entry is 0.95MB (to account for
serialization and metadata).
Sending logs to Datadog with pino-socket instead
It is possible to send logs to Datadog using raw TCP instead of HTTPS. Datadog
recommends HTTPS as the logs are compress-able, whereas TCP-sent logs are not.
HTTPS also has content length limits, whereas TCP does not.
Datadog recommends sending logs over HTTPS instead of raw TCP, as the latest
version of the Datadog agent uses HTTPS with a TCP fallback:
// https://docs.datadoghq.com/agent/logs/log_transport?tab=https
For Agent v6.19+/v7.19+, the default transport used for your logs is
compressed HTTPS instead of TCP for the previous versions.
When the Agent starts, if log collection is enabled, it runs a
HTTPS connectivity test. If successful, then the Agent uses
the compressed HTTPS transport, otherwise the Agent falls back to a TCP transport.
However, you can send logs using raw TCP + TLS using pino-socket
.
See instructions here on how to do this.