mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Make prometheus value formatting more robust
- prometheus datasources passes its own interpolator function to the template server - that function relies on incoming values being strings - some template variables may be non-strings, e.g., `__interval_ms`, which throws an error This PR makes this more robust.
This commit is contained in:
@@ -17,11 +17,17 @@ export function alignRange(start, end, step) {
|
||||
}
|
||||
|
||||
export function prometheusRegularEscape(value) {
|
||||
return value.replace(/'/g, "\\\\'");
|
||||
if (typeof value === 'string') {
|
||||
return value.replace(/'/g, "\\\\'");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
export function prometheusSpecialRegexEscape(value) {
|
||||
return prometheusRegularEscape(value.replace(/\\/g, '\\\\\\\\').replace(/[$^*{}\[\]+?.()]/g, '\\\\$&'));
|
||||
if (typeof value === 'string') {
|
||||
return prometheusRegularEscape(value.replace(/\\/g, '\\\\\\\\').replace(/[$^*{}\[\]+?.()]/g, '\\\\$&'));
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
export class PrometheusDatasource {
|
||||
|
||||
@@ -166,6 +166,9 @@ describe('PrometheusDatasource', () => {
|
||||
});
|
||||
|
||||
describe('Prometheus regular escaping', function() {
|
||||
it('should not escape non-string', function() {
|
||||
expect(prometheusRegularEscape(12)).toEqual(12);
|
||||
});
|
||||
it('should not escape simple string', function() {
|
||||
expect(prometheusRegularEscape('cryptodepression')).toEqual('cryptodepression');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user