Prometheus: Handle jsonnet strings in variables.ts and fix types (#63875)

handle jsonnet strings in variables.ts and fix types
This commit is contained in:
Brendan O'Handley 2023-02-28 10:28:37 -05:00 committed by GitHub
parent 2f8ee38c5e
commit 16503a719b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 4 deletions

View File

@ -22,7 +22,6 @@ export interface PromQuery extends DataQuery {
showingTable?: boolean;
/** Code, Builder or Explain */
editorMode?: QueryEditorMode;
query?: string;
}
export interface PromOptions extends DataSourceJsonData {

View File

@ -9,7 +9,7 @@ import { getTimeSrv, TimeSrv } from '../../../features/dashboard/services/TimeSr
import { PromVariableQueryEditor } from './components/VariableQueryEditor';
import { PrometheusDatasource } from './datasource';
import PrometheusMetricFindQuery from './metric_find_query';
import { PromQuery } from './types';
import { PromVariableQuery } from './types';
export class PrometheusVariableSupport extends CustomVariableSupport<PrometheusDatasource> {
constructor(
@ -23,8 +23,22 @@ export class PrometheusVariableSupport extends CustomVariableSupport<PrometheusD
editor = PromVariableQueryEditor;
query(request: DataQueryRequest<PromQuery>): Observable<DataQueryResponse> {
const query = request.targets[0].query;
query(request: DataQueryRequest<PromVariableQuery>): Observable<DataQueryResponse> {
// Handling grafana as code from jsonnet variable queries which are strings and not objects
// Previously, when using StandardVariableSupport
// the variable query string was changed to be on the expr attribute
// Now, using CustomVariableSupport,
// the variable query is changed to the query attribute.
// So, without standard variable support changing the query string to the expr attribute,
// the variable query string is coming in as it is written in jsonnet,
// where it is just a string. Here is where we handle that.
let query: string | undefined;
if (typeof request.targets[0] === 'string') {
query = request.targets[0];
} else {
query = request.targets[0].query;
}
if (!query) {
return of({ data: [] });
}