Prometheus: Allow sub-second step in the prometheus datasource (#21861)

This commit is contained in:
Ed Welch 2020-02-06 09:28:20 -05:00 committed by GitHub
parent e2b06d9228
commit f2c3d23140
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 5 deletions

View File

@ -990,14 +990,14 @@ describe('PrometheusDatasource', () => {
expect(res.url).toBe(urlExpected);
});
it('step should never go below 1', async () => {
it('step should be fractional for sub second intervals', async () => {
const query = {
// 6 minute range
range: { from: time({ minutes: 1 }), to: time({ minutes: 7 }) },
targets: [{ expr: 'test' }],
interval: '100ms',
};
const urlExpected = 'proxied/api/v1/query_range?query=test&start=60&end=420&step=1';
const urlExpected = 'proxied/api/v1/query_range?query=test&start=60&end=420&step=0.1';
datasourceRequestMock.mockImplementation(() => Promise.resolve(response));
ds.query(query as any);
const res = datasourceRequestMock.mock.calls[0][0];

View File

@ -396,8 +396,13 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
adjustInterval(interval: number, minInterval: number, range: number, intervalFactor: number) {
// Prometheus will drop queries that might return more than 11000 data points.
// Calculate a safe interval as an additional minimum to take into account.
const safeInterval = Math.ceil(range / 11000);
return Math.max(interval * intervalFactor, minInterval, safeInterval, 1);
// Fractional safeIntervals are allowed, however serve little purpose if the interval is greater than 1
// If this is the case take the ceil of the value.
let safeInterval = range / 11000;
if (safeInterval > 1) {
safeInterval = Math.ceil(safeInterval);
}
return Math.max(interval * intervalFactor, minInterval, safeInterval);
}
performTimeSeriesQuery(query: PromQueryRequest, start: number, end: number) {

View File

@ -46,7 +46,7 @@ export class ResultTransformer {
metricLabel = this.createMetricLabel(metricData.metric, options);
const stepMs = parseInt(options.step, 10) * 1000;
const stepMs = parseFloat(options.step) * 1000;
let baseTimestamp = start * 1000;
if (metricData.values === undefined) {