Loki: improve backend-compatibility (#47579)

* loki: annotation queries: make them ready for the backend-mode

* removed unused code
This commit is contained in:
Gábor Farkas 2022-04-12 18:23:19 +02:00 committed by GitHub
parent 712b239d5a
commit a15c2021fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,6 +8,7 @@ import Prism from 'prismjs';
import { import {
AnnotationEvent, AnnotationEvent,
AnnotationQueryRequest, AnnotationQueryRequest,
CoreApp,
DataFrame, DataFrame,
DataFrameView, DataFrameView,
DataQueryError, DataQueryError,
@ -31,6 +32,7 @@ import {
QueryResultMeta, QueryResultMeta,
ScopedVars, ScopedVars,
TimeRange, TimeRange,
rangeUtil,
} from '@grafana/data'; } from '@grafana/data';
import { BackendSrvRequest, FetchError, getBackendSrv, config, DataSourceWithBackend } from '@grafana/runtime'; import { BackendSrvRequest, FetchError, getBackendSrv, config, DataSourceWithBackend } from '@grafana/runtime';
import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv'; import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv';
@ -78,6 +80,21 @@ const DEFAULT_QUERY_PARAMS: Partial<LokiRangeQueryRequest> = {
query: '', query: '',
}; };
function makeRequest(query: LokiQuery, range: TimeRange, app: CoreApp, requestId: string): DataQueryRequest<LokiQuery> {
const intervalInfo = rangeUtil.calculateInterval(range, 1);
return {
targets: [query],
requestId,
interval: intervalInfo.interval,
intervalMs: intervalInfo.intervalMs,
range: range,
scopedVars: {},
timezone: 'UTC',
app,
startTime: Date.now(),
};
}
export class LokiDatasource export class LokiDatasource
extends DataSourceWithBackend<LokiQuery, LokiOptions> extends DataSourceWithBackend<LokiQuery, LokiOptions>
implements implements
@ -652,32 +669,25 @@ export class LokiDatasource
} }
async annotationQuery(options: any): Promise<AnnotationEvent[]> { async annotationQuery(options: any): Promise<AnnotationEvent[]> {
const { const { expr, maxLines, instant, tagKeys = '', titleFormat = '', textFormat = '' } = options.annotation;
expr,
maxLines,
instant,
stepInterval,
tagKeys = '',
titleFormat = '',
textFormat = '',
} = options.annotation;
if (!expr) { if (!expr) {
return []; return [];
} }
const interpolatedExpr = this.templateSrv.replace(expr, {}, this.interpolateQueryExpr); const id = `annotation-${options.annotation.name}`;
const query = {
refId: `annotation-${options.annotation.name}`, const query: LokiQuery = {
expr: interpolatedExpr, refId: id,
expr,
maxLines, maxLines,
instant, instant,
stepInterval,
queryType: instant ? LokiQueryType.Instant : LokiQueryType.Range, queryType: instant ? LokiQueryType.Instant : LokiQueryType.Range,
}; };
const { data } = instant
? await lastValueFrom(this.runInstantQuery(query, options as any)) const request = makeRequest(query, options.range, CoreApp.Dashboard, id);
: await lastValueFrom(this.runRangeQuery(query, options as any));
const { data } = await lastValueFrom(this.query(request));
const annotations: AnnotationEvent[] = []; const annotations: AnnotationEvent[] = [];
const splitKeys: string[] = tagKeys.split(',').filter((v: string) => v !== ''); const splitKeys: string[] = tagKeys.split(',').filter((v: string) => v !== '');