Skip to content

Commit

Permalink
fix: resolved issue in setLogger function with winston logger (#1522)
Browse files Browse the repository at this point in the history
refs INSTA-24448
  • Loading branch information
abhilash-sivan authored Jan 17, 2025
1 parent 1a8f05b commit 52f102e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 24 deletions.
56 changes: 32 additions & 24 deletions packages/collector/src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,33 @@ exports.init = function init(config, isReInit) {
// This consoleStream creates a destination stream for the logger that writes log data to the standard output.
// Since we are using multistream here, this needs to be specified explicitly

const consoleStream = uninstrumentedLogger.destination(parentLogger.destination);

const multiStream = {
/**
* Custom write method to send logs to multiple destinations
* @param {string} chunk
*/
write(chunk) {
consoleStream.write(chunk);

loggerToAgentStream.write(chunk);
}
};

parentLogger = uninstrumentedLogger(
{
...parentLogger.levels,
level: parentLogger.level || 'info',
base: parentLogger.bindings(),
timestamp: () => `,"time":"${new Date().toISOString()}"`
},
multiStream
);
try {
const consoleStream = uninstrumentedLogger.destination(parentLogger.destination);

const multiStream = {
/**
* Custom write method to send logs to multiple destinations
* @param {string} chunk
*/
write(chunk) {
consoleStream.write(chunk);

loggerToAgentStream.write(chunk);
}
};

parentLogger = uninstrumentedLogger(
{
...parentLogger.levels,
level: parentLogger.level || 'info',
base: parentLogger.bindings(),
timestamp: () => `,"time":"${new Date().toISOString()}"`
},
multiStream
);
} catch (error) {
parentLogger.debug(`An issue occurred while modifying the current logger: ${error.message}`);
}
} else if (parentLogger && parentLogger.addStream) {
// in case we are using a bunyan logger we also forward logs to the agent
parentLogger.addStream({
Expand Down Expand Up @@ -174,6 +178,10 @@ function setLoggerLevel(_logger, level) {
*/
function isPinoLogger(_logger) {
return (
_logger && typeof _logger === 'object' && typeof _logger.child === 'function' && typeof _logger.level === 'string'
_logger &&
typeof _logger === 'object' &&
typeof _logger.child === 'function' &&
typeof _logger.level === 'string' &&
typeof _logger.bindings === 'function'
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* (c) Copyright IBM Corp. 2025
*/

/* eslint-disable no-console */

'use strict';

// NOTE: c8 bug https://github.com/bcoe/c8/issues/166
process.on('SIGTERM', () => {
process.disconnect();
process.exit(0);
});

const agentPort = process.env.AGENT_PORT;
const winston = require('winston');
const instana = require('../../../..')({
agentPort,
level: 'warn',
tracing: {
enabled: process.env.TRACING_ENABLED !== 'false',
forceTransmissionStartingAt: 1
}
});

instana.setLogger(
winston.createLogger({
level: 'info'
})
);

let instanaLogger;
instanaLogger = require('../../../../src/logger').getLogger('test-module-name', newLogger => {
instanaLogger = newLogger;
});

const bodyParser = require('body-parser');
const express = require('express');
const morgan = require('morgan');
const port = require('../../../test_util/app-port')();
const app = express();
const logPrefix = `winston App [Instana receives winston logger] (${process.pid}):\t`;

if (process.env.WITH_STDOUT) {
app.use(morgan(`${logPrefix}:method :url :status`));
}

app.use(bodyParser.json());

app.get('/', (req, res) => {
res.sendStatus(200);
});

app.get('/trigger', (req, res) => {
instanaLogger.error('An error logged by Instana - this must not be traced');
res.sendStatus(200);
});

app.listen(port, () => {
log(`Listening on port: ${port}`);
});

function log() {
const args = Array.prototype.slice.call(arguments);
args[0] = logPrefix + args[0];
console.log.apply(console, args);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ exports.registerTestHooks = (opts = {}) => {
case 'instana-receives-bunyan-logger':
appName = 'app-instana-receives-bunyan-logger.js';
break;
case 'instana-receives-winston-logger':
appName = 'app-instana-receives-winston-logger.js';
break;
default:
throw new Error(`Unknown instanaLoggingMode: ${opts.instanaLoggingMode}`);
}
Expand Down
12 changes: 12 additions & 0 deletions packages/collector/test/tracing/logger/instana-logger/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ mochaSuiteFn('tracing/instana-logger', function () {

it('log calls are not traced', () => verifyInstanaLoggingIsNotTraced());
});

describe('Instana receives a winston logger', () => {
appControls.registerTestHooks({
instanaLoggingMode: 'instana-receives-winston-logger'
});

it('log calls are not traced', () => verifyInstanaLoggingIsNotTraced());
});
});

function verifyInstanaLoggingIsNotTraced() {
Expand All @@ -81,6 +89,10 @@ mochaSuiteFn('tracing/instana-logger', function () {
// verify that nothing logged by Instana has been traced with bunyan
const allBunyanSpans = testUtils.getSpansByName(spans, 'log.bunyan');
expect(allBunyanSpans).to.be.empty;

// verify that nothing logged by Instana has been traced with winston
const allWinstonSpans = testUtils.getSpansByName(spans, 'log.winston');
expect(allWinstonSpans).to.be.empty;
})
);
});
Expand Down

0 comments on commit 52f102e

Please sign in to comment.