Skip to content

Commit

Permalink
Merge branch 'master' into new_user_collection
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-id authored Dec 13, 2024
2 parents 172da58 + 4304684 commit b1ba64a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
8 changes: 6 additions & 2 deletions packages/datadog-shimmer/src/shimmer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ const log = require('../../dd-trace/src/log')
const unwrappers = new WeakMap()

function copyProperties (original, wrapped) {
Object.setPrototypeOf(wrapped, original)

// TODO getPrototypeOf is not fast. Should we instead do this in specific
// instrumentations where needed?
const proto = Object.getPrototypeOf(original)
if (proto !== Function.prototype) {
Object.setPrototypeOf(wrapped, proto)
}
const props = Object.getOwnPropertyDescriptors(original)
const keys = Reflect.ownKeys(props)

Expand Down
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 b1ba64a

Please sign in to comment.