Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure the label names are sanitised #2121

Merged
merged 8 commits into from
Apr 21, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,19 @@ function stringify(
let labelsStr = '';

for (const [key, val] of Object.entries(labels)) {
const santisedLabelName = sanitizePrometheusMetricName(key);
weyert marked this conversation as resolved.
Show resolved Hide resolved
hasLabel = true;
labelsStr += `${labelsStr.length > 0 ? ',' : ''}${key}="${escapeLabelValue(
val
)}"`;
labelsStr += `${
labelsStr.length > 0 ? ',' : ''
}${santisedLabelName}="${escapeLabelValue(val)}"`;
}
if (additionalLabels) {
for (const [key, val] of Object.entries(additionalLabels)) {
const santisedLabelName = sanitizePrometheusMetricName(key);
hasLabel = true;
labelsStr += `${
labelsStr.length > 0 ? ',' : ''
}${key}="${escapeLabelValue(val)}"`;
}${santisedLabelName}="${escapeLabelValue(val)}"`;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,34 @@ describe('PrometheusSerializer', () => {
`} 1 ${mockedHrTimeMs}\n`
);
});

it('should sanitise label names', async () => {
weyert marked this conversation as resolved.
Show resolved Hide resolved
const serializer = new PrometheusSerializer();

const meter = new MeterProvider({
processor: new ExactProcessor(SumAggregator),
}).getMeter('test');
const counter = meter.createCounter('test') as CounterMetric;
// if you try to use a label name like account-id prometheus will complain
// with an error like:
// error while linting: text format parsing error in line 282: expected '=' after label name, found '-'
counter
.bind(({
'account-id': '123456',
} as unknown) as Labels)
.add(1);
const records = await counter.getMetricRecord();
const record = records[0];

const result = serializer.serializeRecord(
record.descriptor.name,
record
);
assert.strictEqual(
result,
`test{account_id="123456"} 1 ${mockedHrTimeMs}\n`
);
});
});
});
});