Skip to content

Take advantage of dd-trace autoinject feature #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. | |
| 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...
});
```
17 changes: 1 addition & 16 deletions bufflog.ts
Original file line number Diff line number Diff line change
@@ -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')({
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import tracer from "dd-trace";
import express from 'express';
import bufflog from './bufflog';
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');
Expand Down