Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
- 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
- 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:
Expand Down
6 changes: 3 additions & 3 deletions lib/countly-bulk-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
37 changes: 37 additions & 0 deletions lib/countly-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions lib/countly.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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();
Expand All @@ -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,
},
});
Expand Down
17 changes: 17 additions & 0 deletions test/tests_bulk_fixes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
});
23 changes: 23 additions & 0 deletions test/tests_internal_limits.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
19 changes: 19 additions & 0 deletions test/tests_request_queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading