From 82e58225d62d7656fd01a435bf54546b44959811 Mon Sep 17 00:00:00 2001 From: Eric Khun Date: Tue, 25 Feb 2020 02:11:44 +0000 Subject: [PATCH 1/3] try dd-trace logging auto-inject --- bufflog.ts | 17 +---------------- index.ts | 2 +- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/bufflog.ts b/bufflog.ts index bb4aed3..5faa7dc 100644 --- a/bufflog.ts +++ b/bufflog.ts @@ -1,9 +1,7 @@ -import tracer from "dd-trace"; -import formats from "dd-trace/ext/formats"; export class BuffLog { pinoLogger: any; - defaultLevel = String.prototype.toLowerCase.apply(process.env.LOG_LEVEL) || "notice";; + defaultLevel = process.env.LOG_LEVEL ? String.prototype.toLowerCase.apply(process.env.LOG_LEVEL) : "notice"; constructor() { this.pinoLogger = require('pino')({ @@ -16,19 +14,6 @@ export class BuffLog { // soon: remove the `v` field https://github.com/pinojs/pino/issues/620 base: {}, - mixin () { - // Check here if a current trace exist to inject it in the log - // `tracer` is a singleton, will no-op if no tracer was initialized - var span = tracer.scope().active() - if (span) { - const traceInfo = {} - tracer.inject(span.context(), formats.LOG, traceInfo); - return traceInfo; - } else { - return {} - } - }, - // notice doesn't exist in pino, let's add it customLevels: { notice: 35 diff --git a/index.ts b/index.ts index 44be4b8..08a27c9 100644 --- a/index.ts +++ b/index.ts @@ -1,6 +1,6 @@ import tracer from "dd-trace"; import express from 'express'; -import bufflog from './bufflog'; +import bufflog from './bufflog'; tracer.init({ hostname: "dd-agent-hostname", From b2212f3ea6aaac8dd555c1adf8a118c4346ced2b Mon Sep 17 00:00:00 2001 From: Eric Khun Date: Tue, 25 Feb 2020 02:13:56 +0000 Subject: [PATCH 2/3] switch logInjection to true --- index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.ts b/index.ts index 08a27c9..89f27bb 100644 --- a/index.ts +++ b/index.ts @@ -4,7 +4,8 @@ import bufflog from './bufflog'; tracer.init({ hostname: "dd-agent-hostname", - logInjection: false + // will automatically append the traces to BuffLog + logInjection: true }); bufflog.info('hello info'); From 0d8c909c61bcd3f3574b65e5119f8f048857e0d1 Mon Sep 17 00:00:00 2001 From: Eric Khun Date: Tue, 25 Feb 2020 02:20:45 +0000 Subject: [PATCH 3/3] more info around tracing --- README.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 76a1fd5..3bfa987 100644 --- a/README.md +++ b/README.md @@ -23,4 +23,25 @@ If you wish to see more logs, simply set the `LOG_LEVEL` to the desired level. H | NOTICE | Logs that track the general flow of the application. This is the default level | | | WARNING | Logs that highlight an abnormal or unexpected event in the application flow, but do not otherwise cause the application execution to stop. | | | ERROR | Logs that highlight when the current flow of execution is stopped due to a failure. These should indicate a failure in the current activity, not an application-wide failure. | | -| CRITICAL | Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires immediate attention. | | \ No newline at end of file +| CRITICAL | Logs that describe an unrecoverable application or system crash, or a catastrophic failure that requires immediate attention. | | + + +## Add traces to log + +A great feature of Datadog is to correlate traces and logs to make troubleshooting easier. + +To take advantage of this, you will need to: +- install the `dd-trace` package +- import it and init it with `logInjection:true`. +- BuffLog will append automatically the traces to the logs (only within a request) + +```js +// make sure to put those lines at the very beginning of your service +import tracer from "dd-trace"; +tracer.init({ + // will automatically append the traces to BuffLog + logInjection: true + + // ... all other options... +}); +```