From 16503a719b71ec7251f761dfecfc7e80691f0c70 Mon Sep 17 00:00:00 2001 From: Brendan O'Handley Date: Tue, 28 Feb 2023 10:28:37 -0500 Subject: [PATCH] Prometheus: Handle jsonnet strings in variables.ts and fix types (#63875) handle jsonnet strings in variables.ts and fix types --- .../plugins/datasource/prometheus/types.ts | 1 - .../datasource/prometheus/variables.ts | 20 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/public/app/plugins/datasource/prometheus/types.ts b/public/app/plugins/datasource/prometheus/types.ts index c61bc4137b9..6c324f00233 100644 --- a/public/app/plugins/datasource/prometheus/types.ts +++ b/public/app/plugins/datasource/prometheus/types.ts @@ -22,7 +22,6 @@ export interface PromQuery extends DataQuery { showingTable?: boolean; /** Code, Builder or Explain */ editorMode?: QueryEditorMode; - query?: string; } export interface PromOptions extends DataSourceJsonData { diff --git a/public/app/plugins/datasource/prometheus/variables.ts b/public/app/plugins/datasource/prometheus/variables.ts index 5098147016d..a2d3c541353 100644 --- a/public/app/plugins/datasource/prometheus/variables.ts +++ b/public/app/plugins/datasource/prometheus/variables.ts @@ -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 { constructor( @@ -23,8 +23,22 @@ export class PrometheusVariableSupport extends CustomVariableSupport): Observable { - const query = request.targets[0].query; + query(request: DataQueryRequest): Observable { + // 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: [] }); }