Skip to content

Commit

Permalink
telemetry: increment .count when deduping telemetry logs (#5001)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlhunter authored Dec 12, 2024
1 parent e6ad5b3 commit d0ba71d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
19 changes: 17 additions & 2 deletions packages/dd-trace/src/telemetry/logs/log-collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const log = require('../../log')
const { calculateDDBasePath } = require('../../util')

const logs = new Map()
const logs = new Map() // hash -> log

// NOTE: Is this a reasonable number?
let maxEntries = 10000
Expand Down Expand Up @@ -79,8 +79,10 @@ const logCollector = {
}
const hash = createHash(logEntry)
if (!logs.has(hash)) {
logs.set(hash, logEntry)
logs.set(hash, errorCopy(logEntry))
return true
} else {
logs.get(hash).count++
}
} catch (e) {
log.error('Unable to add log to logCollector: %s', e.message)
Expand Down Expand Up @@ -120,6 +122,19 @@ const logCollector = {
}
}

// clone an Error object to later serialize and transmit
// { ...error } doesn't work
// also users can add arbitrary fields to an error
function errorCopy (error) {
const keys = Object.getOwnPropertyNames(error)
const obj = {}
for (const key of keys) {
obj[key] = error[key]
}
obj.count = 1
return obj
}

logCollector.reset()

module.exports = logCollector
2 changes: 2 additions & 0 deletions packages/dd-trace/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ function globMatch (pattern, subject) {
return true
}

// TODO: this adds stack traces relative to packages/
// shouldn't paths be relative to the root of dd-trace?
function calculateDDBasePath (dirname) {
const dirSteps = dirname.split(path.sep)
const packagesIndex = dirSteps.lastIndexOf('packages')
Expand Down
19 changes: 19 additions & 0 deletions packages/dd-trace/test/telemetry/logs/log-collector.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,24 @@ describe('telemetry log collector', () => {
expect(logs.length).to.be.equal(4)
expect(logs[3]).to.deep.eq({ message: 'Omitted 2 entries due to overflowing', level: 'ERROR' })
})

it('duplicated errors should send incremented count values', () => {
const err1 = new Error('oh no')
err1.level = 'ERROR'

const err2 = new Error('foo buzz')
err2.level = 'ERROR'

logCollector.add(err1)
logCollector.add(err2)
logCollector.add(err1)
logCollector.add(err2)
logCollector.add(err1)

const drainedErrors = logCollector.drain()
expect(drainedErrors.length).to.be.equal(2)
expect(drainedErrors[0].count).to.be.equal(3)
expect(drainedErrors[1].count).to.be.equal(2)
})
})
})

0 comments on commit d0ba71d

Please sign in to comment.