Loki: Add option to issue forward queries (#91181)

* Loki: Add option to issue forward queries

* Loki: Fix spelling of "backward"/"forward"
This commit is contained in:
Sven Grossmann 2024-07-31 12:53:59 +02:00 committed by GitHub
parent b2314dfb55
commit 14db32cc9f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 13 deletions

View File

@ -9,7 +9,7 @@ import { config } from '@grafana/runtime';
import { InlineFormLabel, RadioButtonGroup, InlineField, Input, Select, Stack } from '@grafana/ui'; import { InlineFormLabel, RadioButtonGroup, InlineField, Input, Select, Stack } from '@grafana/ui';
import { getLokiQueryType } from '../queryUtils'; import { getLokiQueryType } from '../queryUtils';
import { LokiQuery, LokiQueryType } from '../types'; import { LokiQuery, LokiQueryDirection, LokiQueryType } from '../types';
export interface LokiOptionFieldsProps { export interface LokiOptionFieldsProps {
lineLimitValue: string; lineLimitValue: string;
@ -29,6 +29,15 @@ export const queryTypeOptions: Array<SelectableValue<LokiQueryType>> = [
}, },
]; ];
export const queryDirections: Array<SelectableValue<LokiQueryDirection>> = [
{ value: LokiQueryDirection.Backward, label: 'Backward', description: 'Search in backward direction.' },
{
value: LokiQueryDirection.Forward,
label: 'Forward',
description: 'Search in forward direction.',
},
];
if (config.featureToggles.lokiExperimentalStreaming) { if (config.featureToggles.lokiExperimentalStreaming) {
queryTypeOptions.push({ queryTypeOptions.push({
value: LokiQueryType.Stream, value: LokiQueryType.Stream,

View File

@ -7,9 +7,14 @@ import { EditorField, EditorRow, QueryOptionGroup } from '@grafana/experimental'
import { config, reportInteraction } from '@grafana/runtime'; import { config, reportInteraction } from '@grafana/runtime';
import { Alert, AutoSizeInput, RadioButtonGroup, Select } from '@grafana/ui'; import { Alert, AutoSizeInput, RadioButtonGroup, Select } from '@grafana/ui';
import { preprocessMaxLines, queryTypeOptions, RESOLUTION_OPTIONS } from '../../components/LokiOptionFields'; import {
preprocessMaxLines,
queryDirections,
queryTypeOptions,
RESOLUTION_OPTIONS,
} from '../../components/LokiOptionFields';
import { getLokiQueryType, isLogsQuery } from '../../queryUtils'; import { getLokiQueryType, isLogsQuery } from '../../queryUtils';
import { LokiQuery, LokiQueryType, QueryStats } from '../../types'; import { LokiQuery, LokiQueryDirection, LokiQueryType, QueryStats } from '../../types';
export interface Props { export interface Props {
query: LokiQuery; query: LokiQuery;
@ -29,6 +34,11 @@ export const LokiQueryBuilderOptions = React.memo<Props>(
onRunQuery(); onRunQuery();
}; };
const onQueryDirectionChange = (value: LokiQueryDirection) => {
onChange({ ...query, direction: value });
onRunQuery();
};
const onResolutionChange = (option: SelectableValue<number>) => { const onResolutionChange = (option: SelectableValue<number>) => {
reportInteraction('grafana_loki_resolution_clicked', { reportInteraction('grafana_loki_resolution_clicked', {
app, app,
@ -73,6 +83,8 @@ export const LokiQueryBuilderOptions = React.memo<Props>(
? queryTypeOptions.filter((o) => o.value !== LokiQueryType.Instant) ? queryTypeOptions.filter((o) => o.value !== LokiQueryType.Instant)
: queryTypeOptions; : queryTypeOptions;
const queryDirection = query.direction ?? LokiQueryDirection.Backward;
// if the state's queryType is still Instant, trigger a change to range for log queries // if the state's queryType is still Instant, trigger a change to range for log queries
if (isLogQuery && queryType === LokiQueryType.Instant) { if (isLogQuery && queryType === LokiQueryType.Instant) {
onChange({ ...query, queryType: LokiQueryType.Range }); onChange({ ...query, queryType: LokiQueryType.Range });
@ -111,6 +123,7 @@ export const LokiQueryBuilderOptions = React.memo<Props>(
</EditorField> </EditorField>
)} )}
{isLogQuery && ( {isLogQuery && (
<>
<EditorField label="Line limit" tooltip="Upper limit for number of log lines returned by query."> <EditorField label="Line limit" tooltip="Upper limit for number of log lines returned by query.">
<AutoSizeInput <AutoSizeInput
className="width-4" className="width-4"
@ -121,6 +134,10 @@ export const LokiQueryBuilderOptions = React.memo<Props>(
onCommitChange={onMaxLinesChange} onCommitChange={onMaxLinesChange}
/> />
</EditorField> </EditorField>
<EditorField label="Direction" tooltip="Direction to search for logs.">
<RadioButtonGroup options={queryDirections} value={queryDirection} onChange={onQueryDirectionChange} />
</EditorField>
</>
)} )}
{!isLogQuery && ( {!isLogQuery && (
<> <>