Skip to content

Commit 1efe34e

Browse files
committed
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.
1 parent 09c3569 commit 1efe34e

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

public/app/plugins/datasource/prometheus/datasource.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@ export function alignRange(start, end, step) {
1717
}
1818

1919
export function prometheusRegularEscape(value) {
20-
return value.replace(/'/g, "\\\\'");
20+
if (typeof value === 'string') {
21+
return value.replace(/'/g, "\\\\'");
22+
}
23+
return value;
2124
}
2225

2326
export function prometheusSpecialRegexEscape(value) {
24-
return prometheusRegularEscape(value.replace(/\\/g, '\\\\\\\\').replace(/[$^*{}\[\]+?.()]/g, '\\\\$&'));
27+
if (typeof value === 'string') {
28+
return prometheusRegularEscape(value.replace(/\\/g, '\\\\\\\\').replace(/[$^*{}\[\]+?.()]/g, '\\\\$&'));
29+
}
30+
return value;
2531
}
2632

2733
export class PrometheusDatasource {

public/app/plugins/datasource/prometheus/specs/datasource.jest.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ describe('PrometheusDatasource', () => {
166166
});
167167

168168
describe('Prometheus regular escaping', function() {
169+
it('should not escape non-string', function() {
170+
expect(prometheusRegularEscape(12)).toEqual(12);
171+
});
169172
it('should not escape simple string', function() {
170173
expect(prometheusRegularEscape('cryptodepression')).toEqual('cryptodepression');
171174
});

0 commit comments

Comments
 (0)