Commit f2574f49 by Andrej Ocenas Committed by GitHub

Prometheus: Fix caching for default labels request (#20718)

parent 130fb02b
import Plain from 'slate-plain-serializer';
import { Editor as SlateEditor } from 'slate';
import LanguageProvider from '../language_provider';
import { PrometheusDatasource } from '../datasource';
import LanguageProvider from './language_provider';
import { PrometheusDatasource } from './datasource';
import { HistoryItem } from '@grafana/data';
import { PromQuery } from '../types';
import { PromQuery } from './types';
import Mock = jest.Mock;
describe('Language completion provider', () => {
const datasource: PrometheusDatasource = ({
......@@ -411,5 +412,28 @@ describe('Language completion provider', () => {
},
]);
});
it('does not re-fetch default labels', async () => {
const datasource: PrometheusDatasource = ({
metadataRequest: jest.fn(() => ({ data: { data: [] as any[] } })),
getTimeRange: jest.fn(() => ({ start: 0, end: 1 })),
} as any) as PrometheusDatasource;
const instance = new LanguageProvider(datasource);
const value = Plain.deserialize('{}');
const ed = new SlateEditor({ value });
const valueWithSelection = ed.moveForward(1).value;
const args = {
text: '',
prefix: '',
wrapperClasses: ['context-labels'],
value: valueWithSelection,
};
await instance.provideCompletionItems(args);
// one call for 2 default labels job, instance
expect((datasource.metadataRequest as Mock).mock.calls.length).toBe(2);
await instance.provideCompletionItems(args);
expect((datasource.metadataRequest as Mock).mock.calls.length).toBe(2);
});
});
});
......@@ -308,12 +308,17 @@ export default class PromQlLanguageProvider extends LanguageProvider {
const existingKeys = parsedSelector ? parsedSelector.labelKeys : [];
// Query labels for selector
if (selector && (!this.labelValues[selector] || this.timeRangeChanged())) {
if (selector) {
if (selector === EMPTY_SELECTOR) {
// Query label values for default labels
await Promise.all(DEFAULT_KEYS.map(key => this.fetchLabelValues(key)));
// For empty selector we do not need to check range
if (!this.labelValues[selector]) {
// Query label values for default labels
await Promise.all(DEFAULT_KEYS.map(key => this.fetchLabelValues(key)));
}
} else {
await this.fetchSeriesLabels(selector, !containsMetric);
if (!this.labelValues[selector] || this.timeRangeChanged()) {
await this.fetchSeriesLabels(selector, !containsMetric);
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment