Skip to content

Commit

Permalink
fix invalid output for log.trace
Browse files Browse the repository at this point in the history
  • Loading branch information
rochdev committed Dec 19, 2024
1 parent 4e2e716 commit 0058db1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
9 changes: 7 additions & 2 deletions packages/dd-trace/src/log/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
7 changes: 4 additions & 3 deletions packages/dd-trace/src/log/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions packages/dd-trace/src/priority_sampler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)

Expand Down Expand Up @@ -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)
}

Expand Down
12 changes: 6 additions & 6 deletions packages/dd-trace/test/log.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -105,7 +104,6 @@ describe('log', () => {
console.error.restore()
console.warn.restore()
console.debug.restore()
console.trace.restore()
})

it('should support chaining', () => {
Expand Down Expand Up @@ -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)
})
})

Expand Down

0 comments on commit 0058db1

Please sign in to comment.