From 74ddbac09b2297cb42160ddbdaaf3e3a7c6ef639 Mon Sep 17 00:00:00 2001 From: Thomas Hunter II Date: Fri, 13 Dec 2024 09:22:53 -0800 Subject: [PATCH] telemetry: make count logic faster --- packages/dd-trace/src/telemetry/logs/index.js | 1 + .../dd-trace/src/telemetry/logs/log-collector.js | 15 +-------------- .../test/telemetry/logs/log-collector.spec.js | 6 ++---- 3 files changed, 4 insertions(+), 18 deletions(-) diff --git a/packages/dd-trace/src/telemetry/logs/index.js b/packages/dd-trace/src/telemetry/logs/index.js index d8fa1969e55..199b5fb7943 100644 --- a/packages/dd-trace/src/telemetry/logs/index.js +++ b/packages/dd-trace/src/telemetry/logs/index.js @@ -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' diff --git a/packages/dd-trace/src/telemetry/logs/log-collector.js b/packages/dd-trace/src/telemetry/logs/log-collector.js index c43e25c8dc4..a2ee9d06f4a 100644 --- a/packages/dd-trace/src/telemetry/logs/log-collector.js +++ b/packages/dd-trace/src/telemetry/logs/log-collector.js @@ -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++ @@ -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 diff --git a/packages/dd-trace/test/telemetry/logs/log-collector.spec.js b/packages/dd-trace/test/telemetry/logs/log-collector.spec.js index e3d4126c4c9..57600dcb441 100644 --- a/packages/dd-trace/test/telemetry/logs/log-collector.spec.js +++ b/packages/dd-trace/test/telemetry/logs/log-collector.spec.js @@ -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)