Skip to content

Commit

Permalink
move string formatting to logger and improve output
Browse files Browse the repository at this point in the history
  • Loading branch information
rochdev committed Dec 19, 2024
1 parent 0058db1 commit 40422d7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
7 changes: 6 additions & 1 deletion packages/dd-trace/src/log/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const coalesce = require('koalas')
const { inspect } = require('util')
const { isTrue } = require('../util')
const { traceChannel, debugChannel, infoChannel, warnChannel, errorChannel } = require('./channels')
const logWriter = require('./writer')
Expand Down Expand Up @@ -63,7 +64,11 @@ const log = {
Error.captureStackTrace(logRecord, this.trace)

const fn = logRecord.stack.split('\n')[1].replace(/^\s+at ([^\s]+) .+/, '$1')
const params = args.map(a => JSON.stringify(a)).join(', ')
const params = args.map(a => {
return a && a.hasOwnProperty('toString') && typeof a.toString === 'function'
? a.toString()
: inspect(a, { depth: 5, breakLength: Infinity, compact: true })
}).join(', ')
const formatted = logRecord.stack.replace('Error: ', `Trace: ${fn}(${params})`)

traceChannel.publish(Log.parse(formatted))
Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/src/opentracing/span.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class DatadogSpan {

toString () {
const spanContext = this.context()
const resourceName = spanContext._tags['resource.name']
const resourceName = spanContext._tags['resource.name'] || ''
const resource = resourceName.length > 100
? `${resourceName.substring(0, 97)}...`
: resourceName
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.toString())
log.trace(span)
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.toString(), auto)
log.trace(span, 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.toString(), samplingPriority, mechanism)
log.trace(span, samplingPriority, mechanism)
this._addDecisionMaker(root)
}

Expand Down
8 changes: 6 additions & 2 deletions packages/dd-trace/test/log.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,14 @@ describe('log', () => {

it('should log to console after setting log level to trace', function foo () {
log.toggle(true, 'trace')
log.trace('argument', { hello: 'world' })
log.trace('argument', { hello: 'world' }, {
toString: () => 'string'
}, { foo: 'bar' })

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]).to.match(
/^Trace: Test.foo\('argument', { hello: 'world' }, string, { foo: 'bar' }\)/
)
expect(console.debug.firstCall.args[0].split('\n').length).to.be.gte(3)
})
})
Expand Down

0 comments on commit 40422d7

Please sign in to comment.