mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Prometheus: Run Explore range queries trough backend (#39133)
* Run Explore range queries trough backend * Remove trailing comma * Add timeRange step alignment to backend * Remove creation of instant query on backend as it is not supported ATM * Remove non-related frontend changes * Pass offset to calculate aligned range trough prom query * Update order in query error message * tableRefIds shouldn't contain undefined refIds * Remove cloning of dataframes when processing * Don't mutate response * Remove ordering of processed frames * Remove df because not needed
This commit is contained in:
@@ -8,7 +8,6 @@ import {
|
||||
DataQueryError,
|
||||
DataQueryRequest,
|
||||
DataQueryResponse,
|
||||
DataSourceApi,
|
||||
DataSourceInstanceSettings,
|
||||
dateMath,
|
||||
DateTime,
|
||||
@@ -17,7 +16,7 @@ import {
|
||||
ScopedVars,
|
||||
TimeRange,
|
||||
} from '@grafana/data';
|
||||
import { BackendSrvRequest, FetchError, FetchResponse, getBackendSrv } from '@grafana/runtime';
|
||||
import { BackendSrvRequest, FetchError, FetchResponse, getBackendSrv, DataSourceWithBackend } from '@grafana/runtime';
|
||||
|
||||
import { safeStringifyValue } from 'app/core/utils/explore';
|
||||
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
||||
@@ -26,7 +25,7 @@ import addLabelToQuery from './add_label_to_query';
|
||||
import PrometheusLanguageProvider from './language_provider';
|
||||
import { expandRecordingRules } from './language_utils';
|
||||
import { getInitHints, getQueryHints } from './query_hints';
|
||||
import { getOriginalMetricName, renderTemplate, transform } from './result_transformer';
|
||||
import { getOriginalMetricName, renderTemplate, transform, transformV2 } from './result_transformer';
|
||||
import {
|
||||
ExemplarTraceIdDestination,
|
||||
isFetchErrorResponse,
|
||||
@@ -47,12 +46,13 @@ export const ANNOTATION_QUERY_STEP_DEFAULT = '60s';
|
||||
const EXEMPLARS_NOT_AVAILABLE = 'Exemplars for this query are not available.';
|
||||
const GET_AND_POST_METADATA_ENDPOINTS = ['api/v1/query', 'api/v1/query_range', 'api/v1/series', 'api/v1/labels'];
|
||||
|
||||
export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions> {
|
||||
export class PrometheusDatasource extends DataSourceWithBackend<PromQuery, PromOptions> {
|
||||
type: string;
|
||||
editorSrc: string;
|
||||
ruleMappings: { [index: string]: string };
|
||||
url: string;
|
||||
directUrl: string;
|
||||
access: 'direct' | 'proxy';
|
||||
basicAuth: any;
|
||||
withCredentials: any;
|
||||
metricsNameCache = new LRU<string, string[]>(10);
|
||||
@@ -75,6 +75,7 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
|
||||
this.type = 'prometheus';
|
||||
this.editorSrc = 'app/features/prometheus/partials/query.editor.html';
|
||||
this.url = instanceSettings.url!;
|
||||
this.access = instanceSettings.access;
|
||||
this.basicAuth = instanceSettings.basicAuth;
|
||||
this.withCredentials = instanceSettings.withCredentials;
|
||||
this.interval = instanceSettings.jsonData.timeInterval || '15s';
|
||||
@@ -288,24 +289,52 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
|
||||
};
|
||||
};
|
||||
|
||||
prepareOptionsV2 = (options: DataQueryRequest<PromQuery>) => {
|
||||
const targets = options.targets.map((target) => {
|
||||
// We want to format Explore + range queries as time_series
|
||||
return {
|
||||
...target,
|
||||
instant: false,
|
||||
range: true,
|
||||
format: 'time_series',
|
||||
offsetSec: this.timeSrv.timeRange().to.utcOffset() * 60,
|
||||
};
|
||||
});
|
||||
|
||||
return { ...options, targets };
|
||||
};
|
||||
|
||||
query(options: DataQueryRequest<PromQuery>): Observable<DataQueryResponse> {
|
||||
const start = this.getPrometheusTime(options.range.from, false);
|
||||
const end = this.getPrometheusTime(options.range.to, true);
|
||||
const { queries, activeTargets } = this.prepareTargets(options, start, end);
|
||||
// WIP - currently we want to run trough backend only if all queries are explore + range queries
|
||||
const shouldRunBackendQuery =
|
||||
this.access === 'proxy' &&
|
||||
options.app === CoreApp.Explore &&
|
||||
!options.targets.some((query) => query.exemplar) &&
|
||||
!options.targets.some((query) => query.instant);
|
||||
|
||||
// No valid targets, return the empty result to save a round trip.
|
||||
if (!queries || !queries.length) {
|
||||
return of({
|
||||
data: [],
|
||||
state: LoadingState.Done,
|
||||
});
|
||||
if (shouldRunBackendQuery) {
|
||||
const newOptions = this.prepareOptionsV2(options);
|
||||
return super.query(newOptions).pipe(map((response) => transformV2(response, newOptions)));
|
||||
// Run queries trough browser/proxy
|
||||
} else {
|
||||
const start = this.getPrometheusTime(options.range.from, false);
|
||||
const end = this.getPrometheusTime(options.range.to, true);
|
||||
const { queries, activeTargets } = this.prepareTargets(options, start, end);
|
||||
|
||||
// No valid targets, return the empty result to save a round trip.
|
||||
if (!queries || !queries.length) {
|
||||
return of({
|
||||
data: [],
|
||||
state: LoadingState.Done,
|
||||
});
|
||||
}
|
||||
|
||||
if (options.app === CoreApp.Explore) {
|
||||
return this.exploreQuery(queries, activeTargets, end);
|
||||
}
|
||||
|
||||
return this.panelsQuery(queries, activeTargets, end, options.requestId, options.scopedVars);
|
||||
}
|
||||
|
||||
if (options.app === CoreApp.Explore) {
|
||||
return this.exploreQuery(queries, activeTargets, end);
|
||||
}
|
||||
|
||||
return this.panelsQuery(queries, activeTargets, end, options.requestId, options.scopedVars);
|
||||
}
|
||||
|
||||
private exploreQuery(queries: PromQueryRequest[], activeTargets: PromQuery[], end: number) {
|
||||
@@ -824,6 +853,27 @@ export class PrometheusDatasource extends DataSourceApi<PromQuery, PromOptions>
|
||||
getOriginalMetricName(labelData: { [key: string]: string }) {
|
||||
return getOriginalMetricName(labelData);
|
||||
}
|
||||
|
||||
// Used when running queries trough backend
|
||||
filterQuery(query: PromQuery): boolean {
|
||||
if (query.hide || !query.expr) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Used when running queries trough backend
|
||||
applyTemplateVariables(target: PromQuery, scopedVars: ScopedVars): Record<string, any> {
|
||||
const variables = cloneDeep(scopedVars);
|
||||
// We want to interpolate these variables on backend
|
||||
delete variables.__interval;
|
||||
delete variables.__interval_ms;
|
||||
|
||||
return {
|
||||
...target,
|
||||
expr: this.templateSrv.replace(target.expr, variables, this.interpolateQueryExpr),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -13,8 +13,12 @@ import {
|
||||
ScopedVars,
|
||||
TIME_SERIES_TIME_FIELD_NAME,
|
||||
TIME_SERIES_VALUE_FIELD_NAME,
|
||||
DataQueryResponse,
|
||||
DataQueryRequest,
|
||||
PreferredVisualisationType,
|
||||
} from '@grafana/data';
|
||||
import { FetchResponse, getDataSourceSrv, getTemplateSrv } from '@grafana/runtime';
|
||||
import { partition } from 'lodash';
|
||||
import { descending, deviation } from 'd3';
|
||||
import {
|
||||
ExemplarTraceIdDestination,
|
||||
@@ -37,6 +41,84 @@ interface TimeAndValue {
|
||||
[TIME_SERIES_VALUE_FIELD_NAME]: number;
|
||||
}
|
||||
|
||||
// V2 result trasnformer used to transform query results from queries that were run trough prometheus backend
|
||||
export function transformV2(response: DataQueryResponse, options: DataQueryRequest<PromQuery>) {
|
||||
// Get refIds that have table format as we need to process those to table reuslts
|
||||
const tableRefIds = options.targets.filter((target) => target.format === 'table').map((target) => target.refId);
|
||||
const [tableResults, otherResults]: [DataFrame[], DataFrame[]] = partition(response.data, (dataFrame) =>
|
||||
dataFrame.refId ? tableRefIds.includes(dataFrame.refId) : false
|
||||
);
|
||||
|
||||
// For table results, we need to transform data frames to table data frames
|
||||
const tableFrames = tableResults.map((dataFrame) => {
|
||||
const df = transformDFoTable(dataFrame, options.targets.length);
|
||||
return df;
|
||||
});
|
||||
|
||||
// Everything else is processed as time_series result and graph preferredVisualisationType
|
||||
const otherFrames = otherResults.map((dataFrame) => {
|
||||
const df = {
|
||||
...dataFrame,
|
||||
meta: {
|
||||
...dataFrame.meta,
|
||||
preferredVisualisationType: 'graph',
|
||||
},
|
||||
} as DataFrame;
|
||||
return df;
|
||||
});
|
||||
|
||||
return { ...response, data: [...otherFrames, ...tableFrames] };
|
||||
}
|
||||
|
||||
export function transformDFoTable(df: DataFrame, responseLength: number): DataFrame {
|
||||
if (df.length === 0) {
|
||||
return df;
|
||||
}
|
||||
|
||||
const timeField = df.fields[0];
|
||||
const valueField = df.fields[1];
|
||||
|
||||
// Create label fields
|
||||
const promLabels: PromMetric = valueField.labels ?? {};
|
||||
const labelFields = Object.keys(promLabels)
|
||||
.sort()
|
||||
.map((label) => {
|
||||
const numberField = label === 'le';
|
||||
return {
|
||||
name: label,
|
||||
config: { filterable: true },
|
||||
type: numberField ? FieldType.number : FieldType.string,
|
||||
values: new ArrayVector(),
|
||||
};
|
||||
});
|
||||
|
||||
// Fill labelFields with label values
|
||||
labelFields.forEach((field) => field.values.add(getLabelValue(promLabels, field.name)));
|
||||
|
||||
const tableDataFrame = {
|
||||
...df,
|
||||
name: undefined,
|
||||
meta: { ...df.meta, preferredVisualisationType: 'table' as PreferredVisualisationType },
|
||||
fields: [
|
||||
timeField,
|
||||
...labelFields,
|
||||
{
|
||||
...valueField,
|
||||
name: getValueText(responseLength, df.refId),
|
||||
labels: undefined,
|
||||
config: { ...valueField.config, displayNameFromDS: undefined },
|
||||
state: { ...valueField.state, displayName: undefined },
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
return tableDataFrame;
|
||||
}
|
||||
|
||||
function getValueText(responseLength: number, refId = '') {
|
||||
return responseLength > 1 ? `Value #${refId}` : 'Value';
|
||||
}
|
||||
|
||||
export function transform(
|
||||
response: FetchResponse<PromDataSuccessResponse>,
|
||||
transformOptions: {
|
||||
|
||||
@@ -10,6 +10,8 @@ export interface PromQuery extends DataQuery {
|
||||
hinting?: boolean;
|
||||
interval?: string;
|
||||
intervalFactor?: number;
|
||||
// Timezone offset to align start & end time on backend
|
||||
offsetSec?: number;
|
||||
legendFormat?: string;
|
||||
valueWithRefId?: boolean;
|
||||
requestId?: string;
|
||||
|
||||
Reference in New Issue
Block a user