diff --git a/public/app/plugins/datasource/loki/datasource.ts b/public/app/plugins/datasource/loki/datasource.ts index 69d9b771cc5..41a71d3709f 100644 --- a/public/app/plugins/datasource/loki/datasource.ts +++ b/public/app/plugins/datasource/loki/datasource.ts @@ -8,6 +8,7 @@ import Prism from 'prismjs'; import { AnnotationEvent, AnnotationQueryRequest, + CoreApp, DataFrame, DataFrameView, DataQueryError, @@ -31,6 +32,7 @@ import { QueryResultMeta, ScopedVars, TimeRange, + rangeUtil, } from '@grafana/data'; import { BackendSrvRequest, FetchError, getBackendSrv, config, DataSourceWithBackend } from '@grafana/runtime'; import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_srv'; @@ -78,6 +80,21 @@ const DEFAULT_QUERY_PARAMS: Partial = { query: '', }; +function makeRequest(query: LokiQuery, range: TimeRange, app: CoreApp, requestId: string): DataQueryRequest { + 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 extends DataSourceWithBackend implements @@ -652,32 +669,25 @@ export class LokiDatasource } async annotationQuery(options: any): Promise { - const { - expr, - maxLines, - instant, - stepInterval, - tagKeys = '', - titleFormat = '', - textFormat = '', - } = options.annotation; + const { expr, maxLines, instant, tagKeys = '', titleFormat = '', textFormat = '' } = options.annotation; if (!expr) { return []; } - const interpolatedExpr = this.templateSrv.replace(expr, {}, this.interpolateQueryExpr); - const query = { - refId: `annotation-${options.annotation.name}`, - expr: interpolatedExpr, + const id = `annotation-${options.annotation.name}`; + + const query: LokiQuery = { + refId: id, + expr, maxLines, instant, - stepInterval, queryType: instant ? LokiQueryType.Instant : LokiQueryType.Range, }; - const { data } = instant - ? await lastValueFrom(this.runInstantQuery(query, options as any)) - : await lastValueFrom(this.runRangeQuery(query, options as any)); + + const request = makeRequest(query, options.range, CoreApp.Dashboard, id); + + const { data } = await lastValueFrom(this.query(request)); const annotations: AnnotationEvent[] = []; const splitKeys: string[] = tagKeys.split(',').filter((v: string) => v !== '');