From 7d9b5c7860ae25a2c94ef85d2264fcd5d9df606d Mon Sep 17 00:00:00 2001 From: turtledreams <62231246+turtledreams@users.noreply.github.com> Date: Tue, 17 Nov 2026 18:40:48 +0900 Subject: [PATCH 1/2] fix: apply the internal limits to custom APM metrics report_trace cut trace.app_metrics, while traces carry apm_metrics, so custom metrics were never limited. Custom metrics now keep to the key, value and segmentation limits, and the reserved metrics that the server reads, such as duration and response_time, are sent whole. A trace without metrics is still refused, and a refused bulk trace returns the bulk user for chaining. --- CHANGELOG.md | 1 + lib/countly-bulk-user.js | 6 +++--- lib/countly-common.js | 37 +++++++++++++++++++++++++++++++++++ lib/countly.js | 4 ++-- test/tests_bulk_fixes.js | 17 ++++++++++++++++ test/tests_internal_limits.js | 23 ++++++++++++++++++++++ 6 files changed, 83 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad9ddf5..8c35646 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - Mitigated an issue where `add_event` and `end_event` threw an error when called without an event - Mitigated an issue where events added to `CountlyBulk` ignored its `max_key_length`, `max_value_size` and `max_segmentation_values` limits - Mitigated an issue where bulk users ignored the limits set for `CountlyBulk` and used the default limits instead +- Mitigated an issue where the custom metrics of `report_trace` ignored the `max_key_length`, `max_value_size` and `max_segmentation_values` limits ## 26.8.0 - ! Minor breaking change ! SDK behavior settings are now enabled by default. Changes made in SDK Manager on your server affect the SDK's behavior directly and take precedence over the matching init options: diff --git a/lib/countly-bulk-user.js b/lib/countly-bulk-user.js index 337718d..2af653a 100644 --- a/lib/countly-bulk-user.js +++ b/lib/countly-bulk-user.js @@ -528,23 +528,23 @@ function CountlyBulkUser(conf) { * @param {string} trace.name - url or view of the trace * @param {number} trace.stz - start timestamp * @param {number} trace.etz - end timestamp - * @param {Object} trace.app_metrics - key/value metrics like duration, to report with trace where value is number + * @param {Object} trace.apm_metrics - key/value metrics like duration, to report with trace where value is number. Custom metrics keep to the internal limits, the reserved metrics such as duration and response_time are sent whole * @param {Object=} trace.apm_attr - object profiling attributes (not yet supported) * @returns {module:lib/countly-bulk-user} instance */ this.report_trace = function(trace) { if (this.check_consent("apm")) { trace.name = cc.truncateSingleValue(trace.name, conf.maxKeyLength, "report_trace"); - trace.app_metrics = cc.truncateObject(trace.app_metrics, conf.maxKeyLength, conf.maxValueSize, conf.maxSegmentationValues, "report_trace"); var props = ["type", "name", "stz", "etz", "apm_metrics", "apm_attr"]; for (var i = 0; i < props.length; i++) { if (props[i] !== "apm_attr" && typeof trace[props[i]] === "undefined") { cc.log(cc.logLevelEnums.ERROR, `CountlyBulkUser report_trace, APM trace must have a: [${props[i]}] param.`); - return; + return this; } } var e = cc.getProperties(trace, props); + e.apm_metrics = cc.truncateApmMetrics(e.apm_metrics, conf.maxKeyLength, conf.maxValueSize, conf.maxSegmentationValues, "report_trace"); e.timestamp = trace.stz; var date = new Date(); e.hour = date.getHours(); diff --git a/lib/countly-common.js b/lib/countly-common.js index 0197b9e..0a43bba 100644 --- a/lib/countly-common.js +++ b/lib/countly-common.js @@ -198,6 +198,43 @@ var cc = { return this.makeJsonSafe(ob, errorLog); }, + // APM metric keys the server reads itself, which truncateApmMetrics keeps whole + apmReservedMetrics: ["response_time", "response_payload_size", "response_code", "request_payload_size", "duration", "slow_rendering_frames", "frozen_frames"], + + /** + * Applies the internal limits to the custom metrics of an APM trace. The reserved metrics are kept whole and do + * not count towards the segmentation limit, and a custom metric cut to a reserved key does not replace it. + * @param {Object} metrics - apm_metrics of a trace + * @param {Number} keyLimit - limit for key length + * @param {Number} valueLimit - limit for value length + * @param {Number} segmentLimit - limit for the number of custom metrics + * @param {string} errorLog - prefix for error log + * @param {String=} area - health check feature area, see truncateObject + * @returns {Object} the metrics within the limits + */ + truncateApmMetrics: function truncateApmMetrics(metrics, keyLimit, valueLimit, segmentLimit, errorLog, area) { + if (!metrics || typeof metrics !== "object") { + return metrics; + } + var result = {}; + var custom = {}; + Object.keys(metrics).forEach((key) => { + if (this.apmReservedMetrics.indexOf(key) !== -1) { + result[key] = metrics[key]; + } + else { + custom[key] = metrics[key]; + } + }); + var limited = this.truncateObject(custom, keyLimit, valueLimit, segmentLimit, errorLog, area); + Object.keys(limited).forEach((key) => { + if (typeof result[key] === "undefined") { + result[key] = limited[key]; + } + }); + return this.makeJsonSafe(result, errorLog); + }, + /** * Returns a copy of a value that JSON.stringify can always serialize, with the same JSON as the * original wherever the original could be serialized. BigInt numbers become strings, so database diff --git a/lib/countly.js b/lib/countly.js index 854dabb..9e58f89 100644 --- a/lib/countly.js +++ b/lib/countly.js @@ -1874,13 +1874,12 @@ Countly.Bulk = Bulk; * @param {string} trace.name - url or view of the trace * @param {number} trace.stz - start timestamp * @param {number} trace.etz - end timestamp - * @param {Object} trace.app_metrics - key/value metrics like duration, to report with trace where value is number + * @param {Object} trace.apm_metrics - key/value metrics like duration, to report with trace where value is number. Custom metrics keep to the internal limits, the reserved metrics such as duration and response_time are sent whole * @param {Object=} trace.apm_attr - object profiling attributes (not yet supported) */ Countly.report_trace = function(trace) { if (Countly.check_consent("apm")) { trace.name = cc.truncateSingleValue(trace.name, Countly.maxKeyLength, "report_trace", "w201"); - trace.app_metrics = cc.truncateObject(trace.app_metrics, Countly.maxKeyLength, Countly.maxValueSize, Countly.maxSegmentationValues, "report_trace", ""); var props = ["type", "name", "stz", "etz", "apm_metrics", "apm_attr"]; for (var i = 0; i < props.length; i++) { if (props[i] !== "apm_attr" && typeof trace[props[i]] === "undefined") { @@ -1891,6 +1890,7 @@ Countly.Bulk = Bulk; } var e = cc.getProperties(trace, props); + e.apm_metrics = cc.truncateApmMetrics(e.apm_metrics, Countly.maxKeyLength, Countly.maxValueSize, Countly.maxSegmentationValues, "report_trace", ""); e.timestamp = trace.stz; var date = new Date(); e.hour = date.getHours(); diff --git a/test/tests_bulk_fixes.js b/test/tests_bulk_fixes.js index 906662e..9ac4b95 100644 --- a/test/tests_bulk_fixes.js +++ b/test/tests_bulk_fixes.js @@ -285,4 +285,21 @@ describe("Bulk fix tests", () => { const request = bulk._getBulkRequestQueue().find((r) => r.device_id === "own-limits-user"); assert.deepStrictEqual(request.user_details, { name: "Long nam", custom: {} }); }); + + it("17- custom APM metrics of a bulk user keep to the internal limits, and the reserved metrics stay whole", () => { + const bulk = new CountlyBulk({ app_key: appKey, url: "http://127.0.0.1:1", max_key_length: 8, max_segmentation_values: 2 }); + const user = bulk.add_user({ device_id: "apm-limits-user" }); + user.report_trace({ + type: "network", + name: "/checkout", + stz: 1700000000000, + etz: 1700000001330, + apm_metrics: { response_time: 1330, response_payload_size: 120, response_code: 200, request_payload_size: 70, cpu_usage_percent: 45, memory_used_mb: 512, disk_reads_count: 7 }, + }); + assert.strictEqual(user.report_trace({ type: "device", name: "no_metrics", stz: 1700000000000, etz: 1700000001000 }), user); + const traces = bulk._getBulkRequestQueue().filter((r) => r.device_id === "apm-limits-user" && r.apm).map((r) => JSON.parse(r.apm)); + assert.equal(traces.length, 1, "a trace without metrics was queued"); + assert.equal(traces[0].name, "/checkou"); + assert.deepStrictEqual(traces[0].apm_metrics, { response_time: 1330, response_payload_size: 120, response_code: 200, request_payload_size: 70, cpu_usag: 45, memory_u: 512 }); + }); }); diff --git a/test/tests_internal_limits.js b/test/tests_internal_limits.js index 17406cd..d8d9cb9 100644 --- a/test/tests_internal_limits.js +++ b/test/tests_internal_limits.js @@ -252,4 +252,27 @@ describe("Testing internal limits", () => { done(); }, hp.sWait); }); + it("6. Check APM trace truncation", (done) => { + // init Countly + initLimitsMain(); + // custom metrics keep to the limits, the reserved network metrics stay whole + Countly.report_trace({ + type: "network", + name: "/checkout/payment", + stz: 1700000000000, + etz: 1700000001330, + apm_metrics: { response_time: 1330, response_payload_size: 120, response_code: 200, request_payload_size: 70, cpu_usage_percent: 45, memory_used_mb: 512, disk_reads_count: 7, net_errors_count: 2 }, + }); + // a trace without metrics is still refused + Countly.report_trace({ type: "device", name: "no_metrics", stz: 1700000000000, etz: 1700000001000 }); + // test + setTimeout(() => { + var rq = hp.readRequestQueue(); + assert.equal(rq.length, 1); + var apm = JSON.parse(rq[0].apm); + assert.equal(apm.name, "/checkou"); + assert.deepStrictEqual(apm.apm_metrics, { response_time: 1330, response_payload_size: 120, response_code: 200, request_payload_size: 70, cpu_usag: 45, memory_u: 512, disk_rea: 7 }); + done(); + }, hp.sWait); + }); }); From a931672ddf5904b7759b08d9384e929861ad9096 Mon Sep 17 00:00:00 2001 From: turtledreams <62231246+turtledreams@users.noreply.github.com> Date: Tue, 17 Nov 2026 18:41:13 +0900 Subject: [PATCH 2/2] fix: send the app start trace report_app_start put the duration under app_metrics, so report_trace refused the trace and nothing was sent. Its start time was also the process uptime instead of a timestamp. It now sends the "app_start" trace from the process start until the call, as the APM guide names it. --- CHANGELOG.md | 1 + lib/countly.js | 8 ++++---- test/tests_request_queue.js | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c35646..c732227 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - Mitigated an issue where events added to `CountlyBulk` ignored its `max_key_length`, `max_value_size` and `max_segmentation_values` limits - Mitigated an issue where bulk users ignored the limits set for `CountlyBulk` and used the default limits instead - Mitigated an issue where the custom metrics of `report_trace` ignored the `max_key_length`, `max_value_size` and `max_segmentation_values` limits +- Mitigated an issue where `report_app_start` sent no app start trace ## 26.8.0 - ! Minor breaking change ! SDK behavior settings are now enabled by default. Changes made in SDK Manager on your server affect the SDK's behavior directly and take precedence over the matching init options: diff --git a/lib/countly.js b/lib/countly.js index 9e58f89..84b3e58 100644 --- a/lib/countly.js +++ b/lib/countly.js @@ -1903,21 +1903,21 @@ Countly.Bulk = Bulk; }; /** - * Report time passed since app start trace + * Report the time from the start of the process until now as the "app_start" trace */ Countly.report_app_start = function() { cc.log(cc.logLevelEnums.INFO, "report_app_start, Reporting app start."); // do on next tick to allow synchronous code to load process.nextTick(() => { - var start = Math.floor(process.uptime() * 1000); var end = Date.now(); + var start = end - Math.floor(process.uptime() * 1000); Countly.report_trace({ type: "device", - name: process.title || process.argv.join(" "), + name: "app_start", stz: start, etz: end, - app_metrics: { + apm_metrics: { duration: end - start, }, }); diff --git a/test/tests_request_queue.js b/test/tests_request_queue.js index 0480916..234c1b8 100644 --- a/test/tests_request_queue.js +++ b/test/tests_request_queue.js @@ -126,6 +126,25 @@ describe("Request queue tests", () => { done(); }, hp.sWait); }); + it("App start is reported as an app_start trace that lasts from the process start until the call", (done) => { + // initialize SDK + initMain(); + var before = Date.now(); + Countly.report_app_start(); + // read request queue + setTimeout(() => { + var rq = hp.readRequestQueue(); + assert.equal(rq.length, 1); + hp.requestBaseParamValidator(rq[0]); + var apm = JSON.parse(rq[0].apm); + assert.equal(apm.type, "device"); + assert.equal(apm.name, "app_start"); + assert.ok(apm.etz >= before && apm.etz <= Date.now()); + assert.equal(apm.apm_metrics.duration, apm.etz - apm.stz); + assert.ok(Math.abs(apm.apm_metrics.duration - (process.uptime() * 1000)) < 1000, `duration ${apm.apm_metrics.duration} is not the process uptime`); + done(); + }, hp.sWait); + }); it("Request queue persists order across halt and re-init", (done) => { // initialize SDK initMain();