Connecting NodeJS Logs and Traces
Datadog の調査レポート: サーバーレスの状態 レポート: サーバーレスの状態

Connecting NodeJS Logs and Traces

このページは日本語には対応しておりません。随時翻訳に取り組んでいます。翻訳に関してご質問やご意見ございましたら、お気軽にご連絡ください。

Automatically Inject Trace and Span IDs

Enable injection with the environment variable DD_LOGS_INJECTION=true or by configuring the tracer directly:

const tracer = require('dd-trace').init({
    logInjection: true
});

This enables automatic trace ID injection for bunyan, paperplane, pino, and winston.

If you haven’t done so already, configure the NodeJS tracer with DD_ENV, DD_SERVICE, and DD_VERSION. This will provide the best experience for adding env, service, and version (see Unified Service Tagging for more details).

Note: Automatic injection only works for logs formatted as JSON.

Manually Inject Trace and Span IDs

If you are using a logging library not supported for automatic injection but are using JSON format, it’s possible to do manual injection directly in your code.

Example using console as the underlying logger:

const tracer = require('dd-trace');
const formats = require('dd-trace/ext/formats');

class Logger {
    log(level, message) {
        const span = tracer.scope().active();
        const time = new Date().toISOString();
        const record = { time, level, message };

        if (span) {
            tracer.inject(span.context(), formats.LOG, record);
        }

        console.log(JSON.stringify(record));
    }
}

module.exports = Logger;

Further Reading