Skip to content

Commit 52f102e

Browse files
fix: resolved issue in setLogger function with winston logger (#1522)
refs INSTA-24448
1 parent 1a8f05b commit 52f102e

File tree

4 files changed

+114
-24
lines changed

4 files changed

+114
-24
lines changed

packages/collector/src/logger.js

+32-24
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,33 @@ exports.init = function init(config, isReInit) {
5555
// This consoleStream creates a destination stream for the logger that writes log data to the standard output.
5656
// Since we are using multistream here, this needs to be specified explicitly
5757

58-
const consoleStream = uninstrumentedLogger.destination(parentLogger.destination);
59-
60-
const multiStream = {
61-
/**
62-
* Custom write method to send logs to multiple destinations
63-
* @param {string} chunk
64-
*/
65-
write(chunk) {
66-
consoleStream.write(chunk);
67-
68-
loggerToAgentStream.write(chunk);
69-
}
70-
};
71-
72-
parentLogger = uninstrumentedLogger(
73-
{
74-
...parentLogger.levels,
75-
level: parentLogger.level || 'info',
76-
base: parentLogger.bindings(),
77-
timestamp: () => `,"time":"${new Date().toISOString()}"`
78-
},
79-
multiStream
80-
);
58+
try {
59+
const consoleStream = uninstrumentedLogger.destination(parentLogger.destination);
60+
61+
const multiStream = {
62+
/**
63+
* Custom write method to send logs to multiple destinations
64+
* @param {string} chunk
65+
*/
66+
write(chunk) {
67+
consoleStream.write(chunk);
68+
69+
loggerToAgentStream.write(chunk);
70+
}
71+
};
72+
73+
parentLogger = uninstrumentedLogger(
74+
{
75+
...parentLogger.levels,
76+
level: parentLogger.level || 'info',
77+
base: parentLogger.bindings(),
78+
timestamp: () => `,"time":"${new Date().toISOString()}"`
79+
},
80+
multiStream
81+
);
82+
} catch (error) {
83+
parentLogger.debug(`An issue occurred while modifying the current logger: ${error.message}`);
84+
}
8185
} else if (parentLogger && parentLogger.addStream) {
8286
// in case we are using a bunyan logger we also forward logs to the agent
8387
parentLogger.addStream({
@@ -174,6 +178,10 @@ function setLoggerLevel(_logger, level) {
174178
*/
175179
function isPinoLogger(_logger) {
176180
return (
177-
_logger && typeof _logger === 'object' && typeof _logger.child === 'function' && typeof _logger.level === 'string'
181+
_logger &&
182+
typeof _logger === 'object' &&
183+
typeof _logger.child === 'function' &&
184+
typeof _logger.level === 'string' &&
185+
typeof _logger.bindings === 'function'
178186
);
179187
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* (c) Copyright IBM Corp. 2025
3+
*/
4+
5+
/* eslint-disable no-console */
6+
7+
'use strict';
8+
9+
// NOTE: c8 bug https://github.com/bcoe/c8/issues/166
10+
process.on('SIGTERM', () => {
11+
process.disconnect();
12+
process.exit(0);
13+
});
14+
15+
const agentPort = process.env.AGENT_PORT;
16+
const winston = require('winston');
17+
const instana = require('../../../..')({
18+
agentPort,
19+
level: 'warn',
20+
tracing: {
21+
enabled: process.env.TRACING_ENABLED !== 'false',
22+
forceTransmissionStartingAt: 1
23+
}
24+
});
25+
26+
instana.setLogger(
27+
winston.createLogger({
28+
level: 'info'
29+
})
30+
);
31+
32+
let instanaLogger;
33+
instanaLogger = require('../../../../src/logger').getLogger('test-module-name', newLogger => {
34+
instanaLogger = newLogger;
35+
});
36+
37+
const bodyParser = require('body-parser');
38+
const express = require('express');
39+
const morgan = require('morgan');
40+
const port = require('../../../test_util/app-port')();
41+
const app = express();
42+
const logPrefix = `winston App [Instana receives winston logger] (${process.pid}):\t`;
43+
44+
if (process.env.WITH_STDOUT) {
45+
app.use(morgan(`${logPrefix}:method :url :status`));
46+
}
47+
48+
app.use(bodyParser.json());
49+
50+
app.get('/', (req, res) => {
51+
res.sendStatus(200);
52+
});
53+
54+
app.get('/trigger', (req, res) => {
55+
instanaLogger.error('An error logged by Instana - this must not be traced');
56+
res.sendStatus(200);
57+
});
58+
59+
app.listen(port, () => {
60+
log(`Listening on port: ${port}`);
61+
});
62+
63+
function log() {
64+
const args = Array.prototype.slice.call(arguments);
65+
args[0] = logPrefix + args[0];
66+
console.log.apply(console, args);
67+
}

packages/collector/test/tracing/logger/instana-logger/controls.js

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ exports.registerTestHooks = (opts = {}) => {
3434
case 'instana-receives-bunyan-logger':
3535
appName = 'app-instana-receives-bunyan-logger.js';
3636
break;
37+
case 'instana-receives-winston-logger':
38+
appName = 'app-instana-receives-winston-logger.js';
39+
break;
3740
default:
3841
throw new Error(`Unknown instanaLoggingMode: ${opts.instanaLoggingMode}`);
3942
}

packages/collector/test/tracing/logger/instana-logger/test.js

+12
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ mochaSuiteFn('tracing/instana-logger', function () {
5757

5858
it('log calls are not traced', () => verifyInstanaLoggingIsNotTraced());
5959
});
60+
61+
describe('Instana receives a winston logger', () => {
62+
appControls.registerTestHooks({
63+
instanaLoggingMode: 'instana-receives-winston-logger'
64+
});
65+
66+
it('log calls are not traced', () => verifyInstanaLoggingIsNotTraced());
67+
});
6068
});
6169

6270
function verifyInstanaLoggingIsNotTraced() {
@@ -81,6 +89,10 @@ mochaSuiteFn('tracing/instana-logger', function () {
8189
// verify that nothing logged by Instana has been traced with bunyan
8290
const allBunyanSpans = testUtils.getSpansByName(spans, 'log.bunyan');
8391
expect(allBunyanSpans).to.be.empty;
92+
93+
// verify that nothing logged by Instana has been traced with winston
94+
const allWinstonSpans = testUtils.getSpansByName(spans, 'log.winston');
95+
expect(allWinstonSpans).to.be.empty;
8496
})
8597
);
8698
});

0 commit comments

Comments
 (0)