Skip to content

Instantly share code, notes, and snippets.

@polarnik
Created September 1, 2024 16:51
Show Gist options
  • Save polarnik/60db2a4b530b29426559a1b25dd31d86 to your computer and use it in GitHub Desktop.
Save polarnik/60db2a4b530b29426559a1b25dd31d86 to your computer and use it in GitHub Desktop.
k6 functions for sending summary metrics to influxdb
// "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";
//import { htmlReport } from "./ext_k6_reporter.js";
// "../ext/ext_k6-sammary-0.0.2.js"
import { jUnit, textSummary } from "https://jslib.k6.io/k6-summary/0.0.2/index.js";
import { influxLineSummary } from './k6_summary_2_influx_line.js';
import http from 'k6/http';
export function do_handleSummary(data) {
let filteredData = JSON.parse(JSON.stringify(data), function(key, value){
if(key=='cookies')
return null;
else
return value;
});
let tags = {};
let test_info_fields = {};
let time_stamp = Date.now();
if(data.setup_data) {
const keys = ["testConfig", "JOB_NAME", "BUILD_ID", "testEnvironment", "TEST_NAME"];
keys.forEach(function (currentValue, index, array) {
if(data.setup_data[currentValue]) {
test_info_fields[currentValue] = data.setup_data[currentValue];
tags[currentValue] = data.setup_data[currentValue];
}
});
const exclude_keys = keys;
exclude_keys.push("cookies");
exclude_keys.push("currentTimeNow");
const setup_data_keys = Object.keys(data.setup_data);
setup_data_keys.forEach(function (currentValue, index, array) {
const val = data.setup_data[currentValue].toString();
if(!(exclude_keys.includes(currentValue) || val.startsWith("[") || val.startsWith("{"))) {
test_info_fields[currentValue] = val;
}
});
if(data.setup_data.currentTimeNow) time_stamp = data.setup_data.currentTimeNow;
}
return {
'./summary.influx.txt': influxLineSummary(data, tags, test_info_fields, time_stamp),
'./summary.txt': textSummary(data, { indent: ' ', enableColors: false }), // Show the text summary to stdout...
'stdout': textSummary(data, { indent: ' ', enableColors: false }), // Show the text summary to stdout...
'./debug.json': JSON.stringify(filteredData, null, 4),
};
}
var forEach = function (obj, callback) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (callback(key, obj[key])) {
break
}
}
}
}
function escapeInfluxQL(str) {
return str
.replace(/[(]/g, "")
.replace(/[)]/g, "")
.replace(/[ ]/g, "\\ ")
.replace(/[=]/g, "\\=")
.replace(/,/g, "_")
//.replace(".", "_")
;
}
function addMetricToString(inluxLineBuilder, key_metricName, tags, global_tags, metricValues, time_stamp) {
inluxLineBuilder.push('\n');
inluxLineBuilder.push(escapeInfluxQL(key_metricName));
if(Object.keys(tags).length > 0) {
const keys = Object.keys(tags).sort();
inluxLineBuilder.push("_by");
forEach(keys, function (tag_index, tag) {
inluxLineBuilder.push("_");
inluxLineBuilder.push(tag);
});
forEach(keys, function (tag_index, tag) {
const tagValue = tags[tag];
inluxLineBuilder.push(",");
inluxLineBuilder.push(escapeInfluxQL(tag));
inluxLineBuilder.push('=');
inluxLineBuilder.push(escapeInfluxQL(tagValue));
});
}
if(Object.keys(global_tags).length > 0) {
const keys = Object.keys(global_tags).sort();
forEach(keys, function (tag_index, tag) {
const tagValue = global_tags[tag];
inluxLineBuilder.push(",");
inluxLineBuilder.push(escapeInfluxQL(tag));
inluxLineBuilder.push('=');
inluxLineBuilder.push(escapeInfluxQL(tagValue));
});
}
inluxLineBuilder.push(' ');
let isFirstField = true;
forEach(Object.keys(metricValues), function (valueName_index, valueName) {
if (!isFirstField) inluxLineBuilder.push(',');
if (isFirstField) isFirstField = false;
inluxLineBuilder.push(escapeInfluxQL(valueName));
inluxLineBuilder.push('=');
if(valueName=="count" || valueName=="value" || valueName=="fails" || valueName=="passes") {
inluxLineBuilder.push(Math.round(metricValues[valueName]));
inluxLineBuilder.push('u');
} else {
inluxLineBuilder.push(metricValues[valueName]);
}
});
inluxLineBuilder.push(' ');
inluxLineBuilder.push(time_stamp);
}
function test_info_2_influx(test_info_fields, test_tags, time_stamp) {
let inluxLineBuilder = [];
inluxLineBuilder.push('\n');
inluxLineBuilder.push('test_info');
forEach(test_tags, function (key, value) {
inluxLineBuilder.push(',');
inluxLineBuilder.push(escapeInfluxQL(key));
inluxLineBuilder.push('=');
inluxLineBuilder.push(escapeInfluxQL(value));
});
const prefix = inluxLineBuilder.join('')
inluxLineBuilder = [];
forEach(test_info_fields, function (key, value) {
inluxLineBuilder.push(prefix);
inluxLineBuilder.push(',');
inluxLineBuilder.push("test_info_name");
inluxLineBuilder.push('=');
inluxLineBuilder.push(escapeInfluxQL(key));
inluxLineBuilder.push(' ');
inluxLineBuilder.push("value");
inluxLineBuilder.push('="');
inluxLineBuilder.push(escapeInfluxQL(value).replace(/["]/g, '\\"'));
inluxLineBuilder.push('"');
inluxLineBuilder.push(' ');
inluxLineBuilder.push(time_stamp);
});
return inluxLineBuilder;
}
function summarizeMetrics(data, test_tags, time_stamp) {
const inluxLineBuilder = [];
forEach(data.metrics, function (key, metricValues) {
let key_metricName = key;
let tags = {};
if(key.endsWith("}")) {
const metric_name_parts = key.split("{");
key_metricName = metric_name_parts[0];
const all_tags_string = metric_name_parts[1].substring(0, metric_name_parts[1].length-1);
const all_tags_string_parts = all_tags_string.split(",");
forEach(all_tags_string_parts,
function (tag_string_index, tag_string) {
const tag_string_parts = tag_string.split(":");
tags[tag_string_parts[0]] = tag_string_parts[1];
}
);
}
let metric_tags = {};
if(metricValues.type) metric_tags['type'] = metricValues.type;
if(metricValues.contains) metric_tags['contains'] = metricValues.contains;
let global_tags = Object.assign({}, metric_tags, test_tags)
addMetricToString(inluxLineBuilder, key_metricName, tags, global_tags, metricValues.values, time_stamp);
})
return inluxLineBuilder;
}
export function k6_summary_2_influx_line(data, test_tags = {}, test_info_fields = {}, time_stamp = Date.now()) {
let lines = [];
try {
Array.prototype.push.apply(lines, test_info_2_influx(test_info_fields, test_tags, time_stamp));
Array.prototype.push.apply(lines, summarizeMetrics(data, test_tags, time_stamp));
} catch (error) {
console.error(error);
}
return lines.join('')
}
exports.influxLineSummary = k6_summary_2_influx_line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment