Skip to content
Closed
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
88 changes: 39 additions & 49 deletions lib/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,64 +21,18 @@ const {
validateMetricName,
validateLabelName
} = require('./validation');
const Metric = require('./metric');

class Counter {
class Counter extends Metric {
/**
* Counter
* @param {string} name - Name of the metric
* @param {string} help - Help description for the metric
* @param {Array.<string>} labels - Labels
*/
constructor(name, help, labels) {
let config;
if (isObject(name)) {
config = Object.assign(
{
labelNames: []
},
name
);

if (!config.registers) {
config.registers = [globalRegistry];
}
} else {
printDeprecationObjectConstructor();

config = {
name,
help,
labelNames: labels,
registers: [globalRegistry]
};
}

if (!config.help) {
throw new Error('Missing mandatory help parameter');
}
if (!config.name) {
throw new Error('Missing mandatory name parameter');
}
if (!validateMetricName(config.name)) {
throw new Error('Invalid metric name');
}

if (!validateLabelName(config.labelNames)) {
throw new Error('Invalid label name');
}

this.name = config.name;

this.labelNames = config.labelNames || [];

super(getConfig(name, help, labels), type);
this.reset();

this.help = config.help;
this.aggregator = config.aggregator || 'sum';

config.registers.forEach(registryInstance =>
registryInstance.registerMetric(this)
);
}

/**
Expand Down Expand Up @@ -177,4 +131,40 @@ function setValue(hashMap, value, timestamp, labels, hash) {
return hashMap;
}

function getConfig(name, help, labels) {
let config;
if (isObject(name)) {
config = Object.assign(
{
labelNames: []
},
name
);
if (!config.registers) {
config.registers = [globalRegistry];
}
} else {
printDeprecationObjectConstructor();
config = {
name,
help,
labelNames: labels,
registers: [globalRegistry]
};
}
if (!config.help) {
throw new Error('Missing mandatory help parameter');
}
if (!config.name) {
throw new Error('Missing mandatory name parameter');
}
if (!validateMetricName(config.name)) {
throw new Error('Invalid metric name');
}
if (!validateLabelName(config.labelNames)) {
throw new Error('Invalid label name');
}
return config;
}

module.exports = Counter;
75 changes: 75 additions & 0 deletions lib/formatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';

const generateFunction = require('generate-function');

function getFormatter(metric, defaultLabels) {
const defaultLabelNames = Object.keys(defaultLabels);
const name = escapeString(metric.name);
const help = escapeString(metric.help);
const type = metric.type;
const labelsCode = getLabelsCode(metric, defaultLabelNames, defaultLabels);

const gen = generateFunction();
gen(`
function format(item, escapeLabelValue, getValueAsString, timestamps) {
const info = '# HELP ${name} ${help}\\n# TYPE ${name} ${type}\\n';
let values = '';
for (const val of item.values || []) {
val.labels = val.labels || {};
let metricName = val.metricName || '${name}';
const labels = \`${labelsCode}\`;
const hasLabels = labels.length > 0;
metricName += \`\${hasLabels ? '{' : ''}\${labels}\${hasLabels ? '}' : ''}\`;
let line = \`\${metricName} \${getValueAsString(val.value)}\`;
if (timestamps && val.timestamp) line += \` \${val.timestamp}\`;
values += \`\${line}\\n\`;
}
return info + values;
}`);
return gen.toFunction();
}

function getLabelsCode(metric, defaultLabelNames, defaultLabels) {
let labelString = '';
const labelNames = getLabelNames(metric, defaultLabelNames);
for (let index = 0; index < labelNames.length; index++) {
const labelName = labelNames[index];
if (labelName === 'quantile') {
labelString += `\${val.labels.quantile != null ? \`quantile="\${escapeLabelValue(val.labels.quantile)}"\` : ''}`;
} else if (labelName === 'le') {
labelString += `\${val.labels.le != null ? \`le="\${escapeLabelValue(val.labels.le)}"\` : ''}`;
} else {
labelString += `${labelName}="\${val.labels['${labelName}'] ? escapeLabelValue(val.labels['${labelName}']) : '${
defaultLabels[labelName]
}'}"`;
}
if (index !== labelNames.length - 1 && labelString.length > 0) {
labelString += ',';
}
}
return labelString;
}

function getLabelNames(metric, defaultLabelNames) {
const labelNames = [...metric.labelNames];
for (const labelName of defaultLabelNames) {
if (!labelNames.includes(labelName)) {
labelNames.push(labelName);
}
}
if (metric.type === 'summary') {
labelNames.push('quantile');
}
if (metric.type === 'histogram') {
labelNames.push('le');
}
return labelNames;
}

function escapeString(str) {
return str.replace(/\n/g, '\\\\n').replace(/\\(?!n)/g, '\\\\\\\\');
}

module.exports = {
getFormatter
};
83 changes: 39 additions & 44 deletions lib/gauge.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,59 +22,18 @@ const {
validateLabel,
validateLabelName
} = require('./validation');
const Metric = require('./metric');

class Gauge {
class Gauge extends Metric {
/**
* Gauge
* @param {string} name - Name of the metric
* @param {string} help - Help for the metric
* @param {Array.<string>} labels - Array with strings, all label keywords supported
*/
constructor(name, help, labels) {
let config;
if (isObject(name)) {
config = Object.assign(
{
labelNames: []
},
name
);

if (!config.registers) {
config.registers = [globalRegistry];
}
} else {
printDeprecationObjectConstructor();
config = {
name,
help,
labelNames: labels,
registers: [globalRegistry]
};
}

if (!config.help) {
throw new Error('Missing mandatory help parameter');
}
if (!config.name) {
throw new Error('Missing mandatory name parameter');
}
if (!validateMetricName(config.name)) {
throw new Error('Invalid metric name');
}
if (!validateLabelName(config.labelNames)) {
throw new Error('Invalid label name');
}

this.name = config.name;
this.labelNames = config.labelNames || [];
super(getConfig(name, help, labels), type);
this.reset();
this.help = config.help;
this.aggregator = config.aggregator || 'sum';

config.registers.forEach(registryInstance =>
registryInstance.registerMetric(this)
);
}

/**
Expand Down Expand Up @@ -261,4 +220,40 @@ function convertLabelsAndValues(labels, value) {
};
}

function getConfig(name, help, labels) {
let config;
if (isObject(name)) {
config = Object.assign(
{
labelNames: []
},
name
);
if (!config.registers) {
config.registers = [globalRegistry];
}
} else {
printDeprecationObjectConstructor();
config = {
name,
help,
labelNames: labels,
registers: [globalRegistry]
};
}
if (!config.help) {
throw new Error('Missing mandatory help parameter');
}
if (!config.name) {
throw new Error('Missing mandatory name parameter');
}
if (!validateMetricName(config.name)) {
throw new Error('Invalid metric name');
}
if (!validateLabelName(config.labelNames)) {
throw new Error('Invalid label name');
}
return config;
}

module.exports = Gauge;
Loading