Skip to content

Commit 105ec44

Browse files
chore: fix the timestamp in instana logger from epoch time to ISO string
1 parent dd557cc commit 105ec44

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

packages/collector/src/logger.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ exports.init = function init(userConfig = {}) {
9595
parentLogger = uninstrumentedLogger({
9696
name: '@instana/collector',
9797
level: 'info',
98-
base: { threadId, pid: process.pid, hostname: os.hostname() }
98+
base: { threadId, pid: process.pid, hostname: os.hostname() },
99+
// Set ISO timestamp format for the default logger
100+
timestamp: () => `,"time":"${new Date().toISOString()}"`
99101
});
100102
}
101103

@@ -133,12 +135,17 @@ exports.init = function init(userConfig = {}) {
133135

134136
// CASE: We copy any settings from the pino instance such as custom formats.
135137
if (symbols && Array.isArray(symbols)) {
138+
// If there's a custom timestamp function from the userConfig logger , we'll respect that
136139
const timeSym = symbols.find(sym => String(sym) === 'Symbol(pino.time)');
137140
// @ts-ignore
138141
const timestampFn = instance[timeSym];
139142

140-
if (timestampFn) {
143+
// Only use the user's timestamp function if it's from a user-provided logger
144+
if (timestampFn && userConfig.logger) {
141145
pinoOpts.timestamp = timestampFn;
146+
} else if (!pinoOpts.timestamp) {
147+
// If no timestamp function is set yet, fallback to ISO format
148+
pinoOpts.timestamp = () => `,"time":"${new Date().toISOString()}"`;
142149
}
143150

144151
const formattersSym = symbols.find(sym => String(sym) === 'Symbol(pino.formatters)');

packages/collector/test/logger_test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,32 @@ describe('logger', () => {
139139
stream => expect(stream.level).to.equal('info')
140140
);
141141
});
142+
143+
it('should use ISO timestamp format for pino logger', () => {
144+
const logger = log.init({});
145+
146+
// Create a mock write stream to capture the log output
147+
const capturedLogs = [];
148+
const mockStream = {
149+
write: function (chunk) {
150+
capturedLogs.push(JSON.parse(chunk));
151+
}
152+
};
153+
154+
// Replace the stream temporarily
155+
const originalStream = logger.logger[uninstrumentedLogger.symbols.streamSym];
156+
logger.logger[uninstrumentedLogger.symbols.streamSym] = mockStream;
157+
158+
logger.info('Test log message');
159+
160+
// Restore the original stream
161+
logger.logger[uninstrumentedLogger.symbols.streamSym] = originalStream;
162+
163+
// Verify the timestamp format in the captured log
164+
expect(capturedLogs.length).to.equal(1);
165+
expect(capturedLogs[0]).to.have.property('time');
166+
expect(capturedLogs[0].time).to.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/);
167+
});
142168
});
143169

144170
/**

0 commit comments

Comments
 (0)