diff --git a/packages/dd-trace/src/log/index.js b/packages/dd-trace/src/log/index.js index 2f9d07ef63c..7e0fb85679b 100644 --- a/packages/dd-trace/src/log/index.js +++ b/packages/dd-trace/src/log/index.js @@ -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') @@ -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)) diff --git a/packages/dd-trace/src/opentracing/span.js b/packages/dd-trace/src/opentracing/span.js index 00fd51da027..23f885bbabd 100644 --- a/packages/dd-trace/src/opentracing/span.js +++ b/packages/dd-trace/src/opentracing/span.js @@ -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 diff --git a/packages/dd-trace/src/priority_sampler.js b/packages/dd-trace/src/priority_sampler.js index 58808fe43a6..3a89f71f664 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.toString()) + log.trace(span) 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.toString(), auto) + log.trace(span, auto) const tag = this._getPriorityFromTags(context._tags, context) @@ -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) } diff --git a/packages/dd-trace/test/log.spec.js b/packages/dd-trace/test/log.spec.js index a2012be64bb..16682f97db8 100644 --- a/packages/dd-trace/test/log.spec.js +++ b/packages/dd-trace/test/log.spec.js @@ -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) }) })