Prometheus: avoid unnecessary network requests (#83342)

* perf: avoid unnecessary network requests

* test: restore mocks to undo `jest.replaceProperty`

* chore: mirror updates for Prometheus library
This commit is contained in:
Nick Richmond 2024-02-26 11:56:40 -05:00 committed by GitHub
parent 9f88a88303
commit b3363543ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 66 additions and 14 deletions

View File

@ -11,7 +11,7 @@ import {
QueryHint,
TimeRange,
} from '@grafana/data';
import { TemplateSrv } from '@grafana/runtime';
import { config, TemplateSrv } from '@grafana/runtime';
import { PrometheusDatasource } from '../../datasource';
import PromQlLanguageProvider from '../../language_provider';
@ -59,6 +59,10 @@ const bugQuery: PromVisualQuery = {
],
};
afterEach(() => {
jest.restoreAllMocks();
});
describe('PromQueryBuilder', () => {
it('shows empty just with metric selected', async () => {
setup();
@ -104,6 +108,28 @@ describe('PromQueryBuilder', () => {
await waitFor(() => expect(datasource.getVariables).toBeCalled());
});
it('checks if the LLM plugin is enabled when the `prometheusPromQAIL` feature is enabled', async () => {
jest.replaceProperty(config, 'featureToggles', {
prometheusPromQAIL: true,
});
const mockIsLLMPluginEnabled = jest.fn();
mockIsLLMPluginEnabled.mockResolvedValue(true);
jest.spyOn(require('./promQail/state/helpers'), 'isLLMPluginEnabled').mockImplementation(mockIsLLMPluginEnabled);
setup();
await waitFor(() => expect(mockIsLLMPluginEnabled).toHaveBeenCalledTimes(1));
});
it('does not check if the LLM plugin is enabled when the `prometheusPromQAIL` feature is disabled', async () => {
jest.replaceProperty(config, 'featureToggles', {
prometheusPromQAIL: false,
});
const mockIsLLMPluginEnabled = jest.fn();
mockIsLLMPluginEnabled.mockResolvedValue(true);
jest.spyOn(require('./promQail/state/helpers'), 'isLLMPluginEnabled').mockImplementation(mockIsLLMPluginEnabled);
setup();
await waitFor(() => expect(mockIsLLMPluginEnabled).toHaveBeenCalledTimes(0));
});
// <LegacyPrometheus>
it('tries to load labels when metric selected', async () => {
const { languageProvider } = setup();

View File

@ -36,15 +36,12 @@ export interface PromQueryBuilderProps {
showExplain: boolean;
}
// initial commit for hackathon-2023-08-promqail
// AI/ML + Prometheus
const prometheusPromQAIL = config.featureToggles.prometheusPromQAIL;
export const PromQueryBuilder = React.memo<PromQueryBuilderProps>((props) => {
const { datasource, query, onChange, onRunQuery, data, showExplain } = props;
const [highlightedOp, setHighlightedOp] = useState<QueryBuilderOperation | undefined>();
const [showDrawer, setShowDrawer] = useState<boolean>(false);
const [llmAppEnabled, updateLlmAppEnabled] = useState<boolean>(false);
const { prometheusPromQAIL } = config.featureToggles; // AI/ML + Prometheus
const lang = { grammar: promqlGrammar, name: 'promql' };
@ -55,8 +52,11 @@ export const PromQueryBuilder = React.memo<PromQueryBuilderProps>((props) => {
const check = await isLLMPluginEnabled();
updateLlmAppEnabled(check);
}
checkLlms();
}, []);
if (prometheusPromQAIL) {
checkLlms();
}
}, [prometheusPromQAIL]);
return (
<>

View File

@ -11,7 +11,7 @@ import {
QueryHint,
TimeRange,
} from '@grafana/data';
import { TemplateSrv } from '@grafana/runtime';
import { config, TemplateSrv } from '@grafana/runtime';
import { PrometheusDatasource } from '../../datasource';
import PromQlLanguageProvider from '../../language_provider';
@ -59,6 +59,10 @@ const bugQuery: PromVisualQuery = {
],
};
afterEach(() => {
jest.restoreAllMocks();
});
describe('PromQueryBuilder', () => {
it('shows empty just with metric selected', async () => {
setup();
@ -104,6 +108,28 @@ describe('PromQueryBuilder', () => {
await waitFor(() => expect(datasource.getVariables).toBeCalled());
});
it('checks if the LLM plugin is enabled when the `prometheusPromQAIL` feature is enabled', async () => {
jest.replaceProperty(config, 'featureToggles', {
prometheusPromQAIL: true,
});
const mockIsLLMPluginEnabled = jest.fn();
mockIsLLMPluginEnabled.mockResolvedValue(true);
jest.spyOn(require('./promQail/state/helpers'), 'isLLMPluginEnabled').mockImplementation(mockIsLLMPluginEnabled);
setup();
await waitFor(() => expect(mockIsLLMPluginEnabled).toHaveBeenCalledTimes(1));
});
it('does not check if the LLM plugin is enabled when the `prometheusPromQAIL` feature is disabled', async () => {
jest.replaceProperty(config, 'featureToggles', {
prometheusPromQAIL: false,
});
const mockIsLLMPluginEnabled = jest.fn();
mockIsLLMPluginEnabled.mockResolvedValue(true);
jest.spyOn(require('./promQail/state/helpers'), 'isLLMPluginEnabled').mockImplementation(mockIsLLMPluginEnabled);
setup();
await waitFor(() => expect(mockIsLLMPluginEnabled).toHaveBeenCalledTimes(0));
});
// <LegacyPrometheus>
it('tries to load labels when metric selected', async () => {
const { languageProvider } = setup();

View File

@ -36,15 +36,12 @@ export interface Props {
showExplain: boolean;
}
// initial commit for hackathon-2023-08-promqail
// AI/ML + Prometheus
const prometheusPromQAIL = config.featureToggles.prometheusPromQAIL;
export const PromQueryBuilder = React.memo<Props>((props) => {
const { datasource, query, onChange, onRunQuery, data, showExplain } = props;
const [highlightedOp, setHighlightedOp] = useState<QueryBuilderOperation | undefined>();
const [showDrawer, setShowDrawer] = useState<boolean>(false);
const [llmAppEnabled, updateLlmAppEnabled] = useState<boolean>(false);
const { prometheusPromQAIL } = config.featureToggles; // AI/ML + Prometheus
const lang = { grammar: promqlGrammar, name: 'promql' };
@ -55,8 +52,11 @@ export const PromQueryBuilder = React.memo<Props>((props) => {
const check = await isLLMPluginEnabled();
updateLlmAppEnabled(check);
}
checkLlms();
}, []);
if (prometheusPromQAIL) {
checkLlms();
}
}, [prometheusPromQAIL]);
return (
<>