Skip to content
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

Add datadog tracing support #4

Merged
merged 5 commits into from
Feb 24, 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
16 changes: 15 additions & 1 deletion bufflog.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import tracer from "dd-trace";
import formats from "dd-trace/ext/formats";

export class BuffLog {
pinoLogger: any;
Expand All @@ -12,7 +14,19 @@ export class BuffLog {

// Define "base" fields
// soon: remove the `v` field https://github.com/pinojs/pino/issues/620
base: {
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
Expand Down
24 changes: 24 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import tracer from "dd-trace";
import express from 'express';
import {BuffLog} from './bufflog';

tracer.init({
hostname: "dd-agent-hostname",
logInjection: false

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @erickhun,

Curious to know what differences you found between using logInjection: true which supports pino and doing the injection manually like we are doing in this PR. Is the injected data different?

Copy link
Collaborator Author

@erickhun erickhun Feb 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@esclapes good one, i've tried to add the traceID with switching the logInjection to true as you said, but it didn't work out of the box 🤔. It's why i've ended up with the manual tracing.

I'm not too sure how we can make it detect by the dd-trace plugin out of the box. but if you have any idea to make that work that'd be awesome.

Copy link

@esclapes esclapes Feb 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erickhun 🤔 my hunch is that it's an issue with the order of the imports. I believe the .init call needs to happen before anything else is imported so that dd-trace has time to monkey patch the supported libraries.

We usually place all the init code in a separate file and then import that file at the top of the index. This is a recent example

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@esclapes oh thanks! i'll try this in an other branch. Sorry if that might look obvious, still trying to get up to speed with js 😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@esclapes implemented in #8

It also worked if we don't do it in an other file 🤔. I think I've perhaps not switched the autoinject parameter properly 🙈

Thanks for your feedbacks, definitely make it easier to use!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad to know that it works! It feels nice to use the dd-trace feature for this, as it will probably be updated with newer features and bug fixes that may not be obvious to us.

💯

});

let logger = new BuffLog();

logger.info('hello info');
Expand All @@ -8,3 +15,20 @@ logger.notice('hello notice');
logger.warning('hello warning');
logger.error('hello error');
logger.critical('hello critical');

const app = express();

app.listen(4000, () => {
console.log(`Server is listening on port 4000`);
});

app.get('/', (req, res) => {
var logger = new BuffLog();
logger.notice("Notice log via endpoint");
logger.info('hello info');
logger.debug('hello debug');
logger.notice('hello notice');
logger.warning('hello warning');
logger.error('hello error');
logger.critical('hello critical');
});
Loading