Skip to content

Commit

Permalink
telemetry: make count logic faster
Browse files Browse the repository at this point in the history
  • Loading branch information
tlhunter committed Dec 13, 2024
1 parent 83c6928 commit 74ddbac
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 18 deletions.
1 change: 1 addition & 0 deletions packages/dd-trace/src/telemetry/logs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function onErrorLog (msg) {

const telLog = {
level: 'ERROR',
count: 1,

// existing log.error(err) without message will be reported as 'Generic Error'
message: message ?? 'Generic Error'
Expand Down
15 changes: 1 addition & 14 deletions packages/dd-trace/src/telemetry/logs/log-collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const logCollector = {
}
const hash = createHash(logEntry)
if (!logs.has(hash)) {
logs.set(hash, errorCopy(logEntry))
logs.set(hash, logEntry)
return true
} else {
logs.get(hash).count++
Expand Down Expand Up @@ -122,19 +122,6 @@ 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
6 changes: 2 additions & 4 deletions packages/dd-trace/test/telemetry/logs/log-collector.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,9 @@ describe('telemetry log collector', () => {
})

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

const err2 = new Error('foo buzz')
err2.level = 'ERROR'
const err2 = { message: 'foo buzz', level: 'ERROR', count: 1 }

logCollector.add(err1)
logCollector.add(err2)
Expand Down

0 comments on commit 74ddbac

Please sign in to comment.