Skip to content

Commit

Permalink
feat(utils): internal metrics (#1762)
Browse files Browse the repository at this point in the history
* add module for internal metrics

* eslint

* add module

* bump versions

* Trigger Build
  • Loading branch information
pepoviola authored Apr 7, 2024
1 parent b1ed94b commit f8260ac
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 23 deletions.
2 changes: 1 addition & 1 deletion javascript/packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"dependencies": {
"@zombienet/dsl-parser-wrapper": "^0.1.10",
"@zombienet/orchestrator": "^0.0.81",
"@zombienet/utils": "^0.0.24",
"@zombienet/utils": "^0.0.25",
"cli-progress": "^3.12.0",
"commander": "^11.1.0",
"debug": "^4.3.4",
Expand Down
2 changes: 1 addition & 1 deletion javascript/packages/orchestrator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@polkadot/api": "^10.12.4",
"@polkadot/keyring": "^12.6.2",
"@polkadot/util-crypto": "^12.6.1",
"@zombienet/utils": "^0.0.24",
"@zombienet/utils": "^0.0.25",
"chai": "^4.3.10",
"debug": "^4.3.4",
"execa": "^5.1.1",
Expand Down
22 changes: 2 additions & 20 deletions javascript/packages/orchestrator/src/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
series,
setLogType,
sleep,
registerSpawnElapsedTimeSecs,
} from "@zombienet/utils";
import fs from "fs";
import tmp from "tmp-promise";
Expand Down Expand Up @@ -541,26 +542,7 @@ export async function start(
const spawnEnd = performance.now();
const spawnElapsedSecs = Math.round((spawnEnd - spawnStart) / 1000);
debug(`\t 🕰 [Spawn] elapsed time: ${spawnElapsedSecs} secs`);

if (
options?.inCI &&
process.env["PUSHGATEWAY_URL"] &&
process.env["CI_JOB_NAME"]
) {
const jobId = process.env["CI_JOB_ID"];
const jobName = process.env["CI_JOB_NAME"];
const projectName = process.env["CI_PROJECT_NAME"] || "";
const metricName = "zombie_network_ready_secs";
const help = `# HELP ${metricName} Elapsed time to spawn the network in seconds`;
const type = `# TYPE ${metricName} gauge`;
const metricString = `${metricName}{job_id="${jobId}", job_name="${jobName}", project_name="${projectName}"} ${spawnElapsedSecs}`;
const body = [help, type, metricString, "\n"].join("\n");
debug!(`Sending metric with content:\n ${body}`);
await fetch(process.env["PUSHGATEWAY_URL"], {
method: "POST",
body,
});
}
if (options?.inCI) await registerSpawnElapsedTimeSecs(spawnElapsedSecs);

// clean cache before dump the info.
network.cleanMetricsCache();
Expand Down
8 changes: 8 additions & 0 deletions javascript/packages/orchestrator/src/test-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
setLogType,
sleep,
LogType,
registerTotalElapsedTimeSecs,
} from "@zombienet/utils";
import fs from "fs";
import Mocha from "mocha";
Expand Down Expand Up @@ -40,6 +41,7 @@ export async function run(
dir: string | undefined,
force: boolean = false,
) {
const testStart = performance.now();
logType && setLogType(logType);
let network: Network;
const backchannelMap: BackchannelMap = {};
Expand Down Expand Up @@ -131,6 +133,12 @@ export async function run(
suite.afterAll("teardown", async function () {
this.timeout(180 * 1000);
if (network && !network.wasRunning) {
// report metric
const testEnd = performance.now();
const elapsedSecs = Math.round((testEnd - testStart) / 1000);
debug(`\t 🕰 [Test] elapsed time: ${elapsedSecs} secs`);
if (inCI) await registerTotalElapsedTimeSecs(elapsedSecs);

const logsPath = await network.dumpLogs(false);
const tests = this.test?.parent?.tests;

Expand Down
2 changes: 1 addition & 1 deletion javascript/packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zombienet/utils",
"version": "0.0.24",
"version": "0.0.25",
"description": "Useful utilities for ZombieNet Framework",
"main": "dist/index.js",
"author": "Parity Technologies <admin@parity.io>",
Expand Down
1 change: 1 addition & 0 deletions javascript/packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from "./net";
export * from "./nunjucksRelativeLoader";
export * from "./promiseSeries";
export * from "./tableCli";
export * from "./zombieMetrics";
42 changes: 42 additions & 0 deletions javascript/packages/utils/src/zombieMetrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function canSend() {
return process.env["PUSHGATEWAY_URL"] && process.env["CI_JOB_NAME"];
}

function getFromCI() {
return [
process.env["CI_JOB_ID"],
process.env["CI_JOB_NAME"],
process.env["CI_PROJECT_NAME"] || "",
process.env["PUSHGATEWAY_URL"],
];
}

export async function registerSpawnElapsedTimeSecs(elapsed: number) {
if (canSend()) {
const [jobId, jobName, projectName, pushGatewayUrl] = getFromCI();
const metricName = "zombie_network_ready_secs";
const help = `# HELP ${metricName} Elapsed time to spawn the network in seconds`;
const type = `# TYPE ${metricName} gauge`;
const metricString = `${metricName}{job_id="${jobId}", job_name="${jobName}", project_name="${projectName}"} ${elapsed}`;
const body = [help, type, metricString, "\n"].join("\n");
await fetch(pushGatewayUrl!, {
method: "POST",
body,
});
}
}

export async function registerTotalElapsedTimeSecs(elapsed: number) {
if (canSend()) {
const [jobId, jobName, projectName, pushGatewayUrl] = getFromCI();
const metricName = "zombie_test_complete_secs";
const help = `# HELP ${metricName} Elapsed time to complete the test job in seconds (including spawning, but not teardown)`;
const type = `# TYPE ${metricName} gauge`;
const metricString = `${metricName}{job_id="${jobId}", job_name="${jobName}", project_name="${projectName}"} ${elapsed}`;
const body = [help, type, metricString, "\n"].join("\n");
await fetch(pushGatewayUrl!, {
method: "POST",
body,
});
}
}

0 comments on commit f8260ac

Please sign in to comment.