Skip to content

Commit

Permalink
fix: replace bunyan and set pino as default loggger
Browse files Browse the repository at this point in the history
  • Loading branch information
abhilash-sivan committed Nov 19, 2024
1 parent e1b685a commit 719f4e2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
3 changes: 1 addition & 2 deletions packages/collector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,14 @@
"dependencies": {
"@instana/core": "4.0.1",
"@instana/shared-metrics": "4.0.1",
"bunyan": "^1.8.15",
"pino": "^9.5.0",
"semver": "^7.5.4",
"serialize-error": "^8.1.0"
},
"optionalDependencies": {
"@instana/autoprofile": "4.0.1"
},
"devDependencies": {
"@types/bunyan": "^1.8.8",
"opentracing": "^0.14.5",
"typeorm": "^0.3.20"
}
Expand Down
57 changes: 28 additions & 29 deletions packages/collector/src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ try {
// thread (0).
}

const bunyan = require('bunyan');
const pino = require('pino');
const { logger } = require('@instana/core');
const pinoToAgentStream = require('./agent/loggerToAgentStream');

const bunyanToAgentStream = require('./agent/bunyanToAgentStream');

/** @type {bunyan | import('@instana/core/src/core').GenericLogger} */
/** @type {pino.Logger | import('@instana/core/src/core').GenericLogger} */
let parentLogger = null;
/** @type {Object.<string, (logger: import('@instana/core/src/core').GenericLogger) => *>} */
const registry = {};
Expand All @@ -31,40 +30,39 @@ const registry = {};
* @param {boolean} [isReInit]
*/
exports.init = function init(config, isReInit) {
if (config.logger && typeof config.logger.child === 'function') {
// A bunyan or pino logger has been provided via config. In either case we create a child logger directly under the
// given logger which serves as the parent for all loggers we create later on.
if (config.logger && isPino(config.logger)) {
// A pino logger has been provided via config; create a child logger directly under it.
parentLogger = config.logger.child({
module: 'instana-nodejs-logger-parent',
__in: 1
});
} else if (config.logger && hasLoggingFunctions(config.logger)) {
// A custom non-bunyan logger has been provided via config. We use it as is.
// A custom logger has been provided; use it as is.
parentLogger = config.logger;
} else {
// No custom logger has been provided via config, we create a new bunyan logger as the parent logger for all loggers
// we create later on.
parentLogger = bunyan.createLogger({
// No custom logger has been provided; create a new pino logger as the parent logger for all loggers
parentLogger = pino({
name: '@instana/collector',
thread: threadId,
__in: 1
});
}
if (isBunyan(parentLogger)) {
// in case we are using a bunyan logger we also forward logs to the agent
/** @type {bunyan} */ (parentLogger).addStream({
type: 'raw',
stream: bunyanToAgentStream,
level: 'info'
level: 'info',
base: {
thread: threadId,
__in: 1
},
transport: {
target: 'pino/file',
options: {
destination: pinoToAgentStream,
sync: false
}
}
});

if (process.env['INSTANA_DEBUG']) {
/** @type {bunyan} */ (parentLogger).level('debug');
parentLogger.level = 'debug';
} else if (config.level) {
/** @type {bunyan} */ (parentLogger).level(/** @type {import('bunyan').LogLevel} */ (config.level));
parentLogger.level = config.level;
} else if (process.env['INSTANA_LOG_LEVEL']) {
/** @type {bunyan} */ (parentLogger).level(
/** @type {import('bunyan').LogLevel} */ (process.env['INSTANA_LOG_LEVEL'].toLowerCase())
);
parentLogger.level = process.env['INSTANA_LOG_LEVEL'].toLowerCase();
}
}

Expand Down Expand Up @@ -110,11 +108,12 @@ exports.getLogger = function getLogger(loggerName, reInitFn) {
};

/**
* @param {import('bunyan') | *} _logger
* @param {import('pino').Logger | *} _logger
* @returns {boolean}
*/
function isBunyan(_logger) {
return _logger instanceof bunyan;
function isPino(_logger) {
// _logger.hasOwnProperty('pino')
return _logger && _logger[Symbol.for('pino.logger')] === true;
}

/**
Expand Down

0 comments on commit 719f4e2

Please sign in to comment.