Loki: Handle invalid query type values (#50755)

* loki: more robust query-type handling

* better comment

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>
This commit is contained in:
Gábor Farkas 2022-06-14 11:48:49 +02:00 committed by GitHub
parent 7566f800e6
commit a97f022612
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -97,4 +97,16 @@ describe('getNormalizedLokiQuery', () => {
it('handles new<>old conflict (new wins), instant', () => { it('handles new<>old conflict (new wins), instant', () => {
expectNormalized({ instant: true, range: false, queryType: LokiQueryType.Instant }, LokiQueryType.Instant); expectNormalized({ instant: true, range: false, queryType: LokiQueryType.Instant }, LokiQueryType.Instant);
}); });
it('handles invalid new, range', () => {
expectNormalized({ queryType: 'invalid' }, LokiQueryType.Range);
});
it('handles invalid new, when old-range exists, use old', () => {
expectNormalized({ instant: false, range: true, queryType: 'invalid' }, LokiQueryType.Range);
});
it('handles invalid new, when old-instant exists, use old', () => {
expectNormalized({ instant: true, range: false, queryType: 'invalid' }, LokiQueryType.Instant);
});
}); });

View File

@ -80,8 +80,13 @@ export function addParsedLabelToQuery(expr: string, key: string, value: string |
// - does not have `.instant` // - does not have `.instant`
// - does not have `.range` // - does not have `.range`
export function getNormalizedLokiQuery(query: LokiQuery): LokiQuery { export function getNormalizedLokiQuery(query: LokiQuery): LokiQuery {
// if queryType field contains invalid data we behave as if the queryType is empty
const { queryType } = query;
const hasValidQueryType =
queryType === LokiQueryType.Range || queryType === LokiQueryType.Instant || queryType === LokiQueryType.Stream;
// if queryType exists, it is respected // if queryType exists, it is respected
if (query.queryType !== undefined) { if (hasValidQueryType) {
const { instant, range, ...rest } = query; const { instant, range, ...rest } = query;
return rest; return rest;
} }