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
2 changes: 1 addition & 1 deletion lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ function flattenSharedLabels(labels) {
}
function escapeLabelValue(str) {
if (typeof str !== 'string') {
return str;
str = String(str);
}
return escapeString(str).replace(/"/g, '\\"');
}
Expand Down
23 changes: 23 additions & 0 deletions test/registerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,29 @@ describe('Register', () => {
expect(escapedResult).toMatch(/\\"/);
});

it('should stringify and escape non-string label values', async () => {
register.registerMetric({
async get() {
return {
name: 'test_metric',
type: 'counter',
help: 'A test metric',
values: [
{
value: 12,
labels: {
label: ['say "hi"\nand \\back'],
},
},
],
};
},
});

const escapedResult = await register.metrics();
expect(escapedResult).toContain('label="say \\"hi\\"\\nand \\\\back"');
});

describe('should output metrics as JSON', () => {
it('should output metrics as JSON', async () => {
register.registerMetric(getMetric());
Expand Down
Loading