@zombienet/utils
Version:
Useful utilities for ZombieNet Framework
68 lines (67 loc) • 3.15 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerSpawnElapsedTimeSecs = registerSpawnElapsedTimeSecs;
exports.registerTotalElapsedTimeSecs = registerTotalElapsedTimeSecs;
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"],
];
}
function registerSpawnElapsedTimeSecs(elapsed) {
return __awaiter(this, void 0, void 0, function* () {
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");
try {
yield fetch(pushGatewayUrl, {
method: "POST",
body,
});
}
catch (e) {
console.log("Error sending spawn elapsed time...", e);
}
}
});
}
function registerTotalElapsedTimeSecs(elapsed, success) {
return __awaiter(this, void 0, void 0, function* () {
if (canSend()) {
const status = success ? "pass" : "fail";
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}"}, status="${status}" ${elapsed}`;
const body = [help, type, metricString, "\n"].join("\n");
try {
yield fetch(pushGatewayUrl, {
method: "POST",
body,
});
}
catch (e) {
console.log("Error sending total elapsed time...", e);
}
}
});
}