Skip to content

Commit

Permalink
Fix big with label="undefined"
Browse files Browse the repository at this point in the history
  • Loading branch information
doochik committed May 18, 2020
1 parent 3c4c748 commit a1a3675
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
27 changes: 19 additions & 8 deletions lib/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,33 @@ function getFormatter(metric, defaultLabels) {
}

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

function getLabelNames(metric, defaultLabelNames) {
Expand Down
26 changes: 26 additions & 0 deletions test/registerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,32 @@ describe('register', () => {
expect(output).toEqual('test_metric{testLabel="testValue"} 1');
});

it('should handle a metric with default labels with value 0', () => {
register.setDefaultLabels({ testLabel: 0 });
const counter = new Counter({
name: 'test_metric',
help: 'A test metric',
});
counter.inc(1);
register.registerMetric(counter);

const output = register.metrics().split('\n')[2];
expect(output).toEqual('test_metric{testLabel="0"} 1');
});

it('should handle a metrics with labels subset', () => {
const gauge = new Gauge({
name: 'test_metric',
help: 'help',
labelNames: ['code', 'success'],
});
gauge.inc({ code: '200' }, 10);
register.registerMetric(gauge);

const output = register.metrics().split('\n')[2];
expect(output).toEqual('test_metric{code="200"} 10');
});

it('labeled metrics should take precidence over defaulted', () => {
register.setDefaultLabels({ testLabel: 'testValue' });
const counter = new Counter({
Expand Down

0 comments on commit a1a3675

Please sign in to comment.