From 0058db188bb15d561199f23a1cf33598e6fbb5c6 Mon Sep 17 00:00:00 2001 From: rochdev Date: Thu, 19 Dec 2024 15:30:01 -0500 Subject: [PATCH] fix invalid output for log.trace --- packages/dd-trace/src/log/index.js | 9 +++++++-- packages/dd-trace/src/log/writer.js | 7 ++++--- packages/dd-trace/src/priority_sampler.js | 6 +++--- packages/dd-trace/test/log.spec.js | 12 ++++++------ 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/packages/dd-trace/src/log/index.js b/packages/dd-trace/src/log/index.js index 213b6ccc8e6..2f9d07ef63c 100644 --- a/packages/dd-trace/src/log/index.js +++ b/packages/dd-trace/src/log/index.js @@ -59,9 +59,14 @@ const log = { trace (...args) { if (traceChannel.hasSubscribers) { const logRecord = {} + Error.captureStackTrace(logRecord, this.trace) - const stack = logRecord.stack.split('\n')[1].replace(/^\s+at ([^\s]) .+/, '$1') - traceChannel.publish(Log.parse('Trace', args, { stack })) + + const fn = logRecord.stack.split('\n')[1].replace(/^\s+at ([^\s]+) .+/, '$1') + const params = args.map(a => JSON.stringify(a)).join(', ') + const formatted = logRecord.stack.replace('Error: ', `Trace: ${fn}(${params})`) + + traceChannel.publish(Log.parse(formatted)) } return this }, diff --git a/packages/dd-trace/src/log/writer.js b/packages/dd-trace/src/log/writer.js index a721f7f9e35..322c703b2b3 100644 --- a/packages/dd-trace/src/log/writer.js +++ b/packages/dd-trace/src/log/writer.js @@ -4,7 +4,6 @@ const { storage } = require('../../../datadog-core') const { LogChannel } = require('./channels') const { Log } = require('./log') const defaultLogger = { - trace: msg => console.trace(msg), /* eslint-disable-line no-console */ debug: msg => console.debug(msg), /* eslint-disable-line no-console */ info: msg => console.info(msg), /* eslint-disable-line no-console */ warn: msg => console.warn(msg), /* eslint-disable-line no-console */ @@ -91,8 +90,10 @@ function onDebug (log) { function onTrace (log) { const { formatted, cause } = getErrorLog(log) - if (formatted) withNoop(() => logger.trace(formatted)) - if (cause) withNoop(() => logger.trace(cause)) + // Using logger.debug() because not all loggers have trace level, + // and console.trace() has a completely different meaning. + if (formatted) withNoop(() => logger.debug(formatted)) + if (cause) withNoop(() => logger.debug(cause)) } function error (...args) { diff --git a/packages/dd-trace/src/priority_sampler.js b/packages/dd-trace/src/priority_sampler.js index 3a89f71f664..58808fe43a6 100644 --- a/packages/dd-trace/src/priority_sampler.js +++ b/packages/dd-trace/src/priority_sampler.js @@ -57,7 +57,7 @@ class PrioritySampler { isSampled (span) { const priority = this._getPriorityFromAuto(span) - log.trace(span) + log.trace(span.toString()) return priority === USER_KEEP || priority === AUTO_KEEP } @@ -71,7 +71,7 @@ class PrioritySampler { if (context._sampling.priority !== undefined) return if (!root) return // noop span - log.trace(span, auto) + log.trace(span.toString(), auto) const tag = this._getPriorityFromTags(context._tags, context) @@ -126,7 +126,7 @@ class PrioritySampler { const root = context._trace.started[0] - log.trace(span, samplingPriority, mechanism) + log.trace(span.toString(), samplingPriority, mechanism) this._addDecisionMaker(root) } diff --git a/packages/dd-trace/test/log.spec.js b/packages/dd-trace/test/log.spec.js index ac2feea9f7a..a2012be64bb 100644 --- a/packages/dd-trace/test/log.spec.js +++ b/packages/dd-trace/test/log.spec.js @@ -86,7 +86,6 @@ describe('log', () => { sinon.stub(console, 'error') sinon.stub(console, 'warn') sinon.stub(console, 'debug') - sinon.stub(console, 'trace') error = new Error() @@ -105,7 +104,6 @@ describe('log', () => { console.error.restore() console.warn.restore() console.debug.restore() - console.trace.restore() }) it('should support chaining', () => { @@ -145,14 +143,16 @@ describe('log', () => { it('should not log to console by default', () => { log.trace('trace') - expect(console.trace).to.not.have.been.called + expect(console.debug).to.not.have.been.called }) - it('should log to console after setting log level to trace', () => { + it('should log to console after setting log level to trace', function foo () { log.toggle(true, 'trace') - log.trace('argument') + log.trace('argument', { hello: 'world' }) - expect(console.trace).to.have.been.calledTwice + expect(console.debug).to.have.been.calledOnce + expect(console.debug.firstCall.args[0]).to.match(/^Trace: Test.foo\("argument", {"hello":"world"}\)/) + expect(console.debug.firstCall.args[0].split('\n').length).to.be.gte(3) }) })