function isAsyncFunction(fn) {
if (typeof fn !== 'function') return false;
var ctor = fn.constructor;
return !!ctor && (ctor.name === 'AsyncFunction' || ctor.displayName === 'AsyncFunction');
}
function group(name, fn) {
if (typeof fn === 'function' && isAsyncFunction(fn)) {
throw new TypeError('group() does not support async callbacks (k6 rejects them)');
}
if (typeof __tropel_trp_group_start === 'function') {
__tropel_trp_group_start(name);
var startTime = Date.now();
try {
if (typeof fn === 'function') {
return fn();
}
} finally {
var duration = Date.now() - startTime;
__tropel_trp_group_end(name, duration);
}
} else {
if (typeof fn === 'function') {
return fn();
}
}
}
function check(val, conds, tags) {
if (conds === null || conds === undefined || typeof conds !== 'object') {
throw new TypeError('check() requires an object as its second argument');
}
var allPassed = true;
var tagsJson = '';
if (tags && typeof tags === 'object') {
try { tagsJson = JSON.stringify(tags); } catch (e) { tagsJson = ''; }
}
var names = Object.keys(conds);
for (var i = 0; i < names.length; i++) {
var name = names[i];
var condition = conds[name];
var passed = false;
if (typeof condition === 'function') {
if (isAsyncFunction(condition)) {
throw new TypeError(
'check() condition "' + name + '" is an async function; k6 rejects async conditions'
);
}
try {
passed = !!condition(val);
} catch (e) {
if (typeof __tropel_trp_test === 'function') {
__tropel_trp_test(name, false, tagsJson);
}
throw e;
}
} else {
passed = !!condition;
}
if (typeof __tropel_trp_test === 'function') {
__tropel_trp_test(name, passed, tagsJson);
}
if (!passed) {
allPassed = false;
}
}
return allPassed;
}
function Counter(name) {
if (!name || typeof name !== 'string') {
throw new Error('Counter requires a metric name');
}
this._name = name;
this._type = 'counter';
this._isTime = false;
}
Object.defineProperty(Counter.prototype, 'name', {
configurable: false,
enumerable: true,
get: function () { return this._name; },
set: function () { },
});
Counter.prototype.add = function (value, tags) {
var v = Number(value);
if (!isFinite(v)) {
return false;
}
if (typeof __tropel_trp_custom_metric_add === 'function') {
var tagsStr = tags ? JSON.stringify(tags) : '{}';
__tropel_trp_custom_metric_add(this._name, v, tagsStr, this._type, this._isTime);
}
return true;
};
function Gauge(name) {
if (!name || typeof name !== 'string') {
throw new Error('Gauge requires a metric name');
}
this._name = name;
this._type = 'gauge';
this._isTime = false;
}
Object.defineProperty(Gauge.prototype, 'name', {
configurable: false,
enumerable: true,
get: function () { return this._name; },
set: function () { },
});
Gauge.prototype.add = function (value, tags) {
var v = Number(value);
if (!isFinite(v)) {
return false;
}
if (typeof __tropel_trp_custom_metric_add === 'function') {
var tagsStr = tags ? JSON.stringify(tags) : '{}';
__tropel_trp_custom_metric_add(this._name, v, tagsStr, this._type, this._isTime);
}
return true;
};
function Rate(name) {
if (!name || typeof name !== 'string') {
throw new Error('Rate requires a metric name');
}
this._name = name;
this._type = 'rate';
this._isTime = false;
}
Object.defineProperty(Rate.prototype, 'name', {
configurable: false,
enumerable: true,
get: function () { return this._name; },
set: function () { },
});
Rate.prototype.add = function (value, tags) {
var v = Number(value);
if (!isFinite(v)) {
return false;
}
if (typeof __tropel_trp_custom_metric_add === 'function') {
var tagsStr = tags ? JSON.stringify(tags) : '{}';
__tropel_trp_custom_metric_add(this._name, v, tagsStr, this._type, this._isTime);
}
return true;
};
function Trend(name, isTime) {
if (!name || typeof name !== 'string') {
throw new Error('Trend requires a metric name');
}
this._name = name;
this._type = 'trend';
this._isTime = isTime === true;
}
Object.defineProperty(Trend.prototype, 'name', {
configurable: false,
enumerable: true,
get: function () { return this._name; },
set: function () { },
});
Trend.prototype.add = function (value, tags) {
var v = Number(value);
if (!isFinite(v)) {
return false;
}
if (typeof __tropel_trp_custom_metric_add === 'function') {
var tagsStr = tags ? JSON.stringify(tags) : '{}';
__tropel_trp_custom_metric_add(this._name, v, tagsStr, this._type, this._isTime);
}
return true;
};
if (typeof globalThis !== 'undefined') {
globalThis.check = check;
globalThis.group = group;
globalThis.Counter = Counter;
globalThis.Gauge = Gauge;
globalThis.Rate = Rate;
globalThis.Trend = Trend;
}