Loki: Cancel inflight /stats requests when new requests are issued (#78654)

* add requestId to stats requests

* add `exploreId`

* change from `exploreId` to `useId`

* change ref id

* add comment
This commit is contained in:
Sven Grossmann 2023-11-27 19:36:53 +01:00 committed by GitHub
parent 4aea1107b3
commit a73be4b963
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 4 deletions

View File

@ -1,5 +1,5 @@
import { isEqual } from 'lodash';
import React, { SyntheticEvent, useCallback, useEffect, useState } from 'react';
import React, { SyntheticEvent, useCallback, useEffect, useId, useState } from 'react';
import { usePrevious } from 'react-use';
import { CoreApp, LoadingState } from '@grafana/data';
@ -29,6 +29,7 @@ export const testIds = {
};
export const LokiQueryEditor = React.memo<LokiQueryEditorProps>((props) => {
const id = useId();
const { onChange, onRunQuery, onAddQuery, data, app, queries, datasource, range: timeRange } = props;
const [parseModalOpen, setParseModalOpen] = useState(false);
const [queryPatternsModalOpen, setQueryPatternsModalOpen] = useState(false);
@ -106,12 +107,13 @@ export const LokiQueryEditor = React.memo<LokiQueryEditorProps>((props) => {
);
if (shouldUpdate && timeRange) {
const makeAsyncRequest = async () => {
const stats = await datasource.getStats(query, timeRange);
// overwriting the refId that is later used to cancel inflight queries with the same ID.
const stats = await datasource.getStats({ ...query, refId: `${id}_${query.refId}` }, timeRange);
setQueryStats(stats);
};
makeAsyncRequest();
}
}, [datasource, timeRange, previousTimeRange, query, previousQueryExpr, previousQueryType, setQueryStats]);
}, [datasource, timeRange, previousTimeRange, query, previousQueryExpr, previousQueryType, setQueryStats, id]);
return (
<>

View File

@ -1592,6 +1592,21 @@ describe('LokiDatasource', () => {
expect(ds.statsMetadataRequest).toHaveBeenCalled();
expect(result).toEqual({ streams: 2, chunks: 2, bytes: 2, entries: 2 });
});
it('calls statsMetadataRequest with the right properties', async () => {
query.expr = 'count_over_time({foo="bar"}[1m])';
await ds.getQueryStats(query, mockTimeRange);
expect(ds.statsMetadataRequest).toHaveBeenCalledWith(
`index/stats`,
{
start: 0,
end: 1000000,
query: '{foo="bar"}',
},
{ requestId: 'log-stats-A', showErrorAlert: false }
);
});
});
describe('statsMetadataRequest', () => {

View File

@ -108,6 +108,8 @@ export const REF_ID_STARTER_ANNOTATION = 'annotation-';
export const REF_ID_STARTER_LOG_ROW_CONTEXT = 'log-row-context-query-';
export const REF_ID_STARTER_LOG_VOLUME = 'log-volume-';
export const REF_ID_STARTER_LOG_SAMPLE = 'log-sample-';
export const REF_ID_STARTER_STATS = 'log-stats-';
const NS_IN_MS = 1000000;
export function makeRequest(
@ -560,7 +562,7 @@ export class LokiDatasource
start: start,
end: end,
},
{ showErrorAlert: false }
{ showErrorAlert: false, requestId: `${REF_ID_STARTER_STATS}${query.refId}` }
);
statsForAll = {