Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
feat(pino.js): add trace id to pino logger (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranrib authored Oct 15, 2020
1 parent 792ee2a commit 31cccde
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,9 @@ Epsagon provides out-of-the-box instrumentation (tracing) for many popular frame
|@azure/storage-blob |`>=12.2.0` |
|ldapjs |`>=2.1.0` |
|ws |`>=7.3.1` |
|pino |`>=6.0.0` |
|bunyan |`>=1.8.0` |
|winston |`>=2.4.5` |
Expand Down
42 changes: 42 additions & 0 deletions src/events/pino.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const tracer = require('../tracer.js');
const moduleUtils = require('./module_utils.js');


/**
* Wrap pino logs
* @param {Function} wrappedFunction The function to wrap from winston
* @returns {function} asJson wrapper function
*/
function logWrapper(wrappedFunction) {
return function internalLogWrapper(obj, msg, num, time) {
if (!tracer.isLoggingTracingEnabled()) {
return wrappedFunction.apply(this, [obj, msg, num, time]);
}
const traceId = tracer.getTraceId();
if (!traceId) {
return wrappedFunction.apply(this, [obj, msg, num, time]);
}

/* eslint-disable no-param-reassign */
obj.epsagon = {
trace_id: traceId,
};

tracer.addLoggingTracingEnabledMetadata();

return wrappedFunction.apply(this, [obj, msg, num, time]);
};
}

module.exports = {
/**
* Initializes the pino log tracer
*/
init() {
moduleUtils.patchModule(
'pino/lib/tools',
'asJson',
logWrapper
);
},
};
2 changes: 2 additions & 0 deletions src/patcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const mqttPatcher = require('./events/mqtt.js');
const kafkajsPatcher = require('./events/kafkajs.js');
const kafkaNodePatcher = require('./events/kafka-node.js');
const bunyanPatcher = require('./events/bunyan.js');
const pinoPatcher = require('./events/pino.js');
const azureSdkPatcher = require('./events/azure_sdk.js');
const winstonCloudwatchPatcher = require('./events/winston_cloudwatch.js');
const winstonPatcher = require('./events/winston.js');
Expand Down Expand Up @@ -63,6 +64,7 @@ if (!config.getConfig().isEpsagonPatchDisabled) {
kafkajsPatcher,
kafkaNodePatcher,
bunyanPatcher,
pinoPatcher,
azureSdkPatcher,
winstonCloudwatchPatcher,
winstonPatcher,
Expand Down

0 comments on commit 31cccde

Please sign in to comment.