From 043bb59593cd12b41f609da734a24e34e5d1c936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thierry=20Sall=C3=A9?= Date: Wed, 18 Sep 2019 18:59:23 +0200 Subject: [PATCH] Prometheus: datasource config with custom parameters string (#19121) --- .../features/datasources/prometheus.md | 21 ++++++++++--------- .../datasource/prometheus/datasource.ts | 14 +++++++++++++ .../prometheus/partials/config.html | 20 ++++++++++++++++++ .../plugins/datasource/prometheus/types.ts | 1 + 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/docs/sources/features/datasources/prometheus.md b/docs/sources/features/datasources/prometheus.md index 5ac3543cb31..e0124d76f01 100644 --- a/docs/sources/features/datasources/prometheus.md +++ b/docs/sources/features/datasources/prometheus.md @@ -25,16 +25,17 @@ Grafana includes built-in support for Prometheus. ## Data source options -| Name | Description | -| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------- | -| _Name_ | The data source name. This is how you refer to the data source in panels & queries. | -| _Default_ | Default data source means that it will be pre-selected for new panels. | -| _Url_ | The http protocol, ip and port of you Prometheus server (default port is usually 9090) | -| _Access_ | Server (default) = URL needs to be accessible from the Grafana backend/server, Browser = URL needs to be accessible from the browser. | -| _Basic Auth_ | Enable basic authentication to the Prometheus data source. | -| _User_ | Name of your Prometheus user | -| _Password_ | Database user's password | -| _Scrape interval_ | This will be used as a lower limit for the Prometheus step query parameter. Default value is 15s. | +| Name | Description | +| ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | +| _Name_ | The data source name. This is how you refer to the data source in panels & queries. | +| _Default_ | Default data source means that it will be pre-selected for new panels. | +| _Url_ | The http protocol, ip and port of you Prometheus server (default port is usually 9090) | +| _Access_ | Server (default) = URL needs to be accessible from the Grafana backend/server, Browser = URL needs to be accessible from the browser. | +| _Basic Auth_ | Enable basic authentication to the Prometheus data source. | +| _User_ | Name of your Prometheus user | +| _Password_ | Database user's password | +| _Scrape interval_ | This will be used as a lower limit for the Prometheus step query parameter. Default value is 15s. | +| _CustomQueryParameters_ | Add Custom parameters to Prometheus query url. For example `timeout`, `partial_response`, `dedup` or `max_source_resolution`. | ## Query editor diff --git a/public/app/plugins/datasource/prometheus/datasource.ts b/public/app/plugins/datasource/prometheus/datasource.ts index 24203998046..bc98a80a16d 100644 --- a/public/app/plugins/datasource/prometheus/datasource.ts +++ b/public/app/plugins/datasource/prometheus/datasource.ts @@ -55,6 +55,7 @@ export class PrometheusDatasource extends DataSourceApi httpMethod: string; languageProvider: PrometheusLanguageProvider; resultTransformer: ResultTransformer; + customQueryParameters: any; /** @ngInject */ constructor( @@ -78,6 +79,7 @@ export class PrometheusDatasource extends DataSourceApi this.resultTransformer = new ResultTransformer(templateSrv); this.ruleMappings = {}; this.languageProvider = new PrometheusLanguageProvider(this); + this.customQueryParameters = new URLSearchParams(instanceSettings.jsonData.customQueryParameters); } init = () => { @@ -348,6 +350,12 @@ export class PrometheusDatasource extends DataSourceApi data['timeout'] = this.queryTimeout; } + for (const [key, value] of this.customQueryParameters) { + if (data[key] == null) { + data[key] = value; + } + } + return this._request(url, data, { requestId: query.requestId, headers: query.headers }).catch((err: any) => { if (err.cancelled) { return err; @@ -368,6 +376,12 @@ export class PrometheusDatasource extends DataSourceApi data['timeout'] = this.queryTimeout; } + for (const [key, value] of this.customQueryParameters) { + if (data[key] == null) { + data[key] = value; + } + } + return this._request(url, data, { requestId: query.requestId, headers: query.headers }).catch((err: any) => { if (err.cancelled) { return err; diff --git a/public/app/plugins/datasource/prometheus/partials/config.html b/public/app/plugins/datasource/prometheus/partials/config.html index c0ee73719fe..1f050cafd13 100644 --- a/public/app/plugins/datasource/prometheus/partials/config.html +++ b/public/app/plugins/datasource/prometheus/partials/config.html @@ -48,3 +48,23 @@ +

Misc

+
+
+
+ Custom query parameters + + + Add Custom parameters to Prometheus or Thanos queries. + +
+
+
+ + diff --git a/public/app/plugins/datasource/prometheus/types.ts b/public/app/plugins/datasource/prometheus/types.ts index 387b9c9e3d0..fba388bf1c2 100644 --- a/public/app/plugins/datasource/prometheus/types.ts +++ b/public/app/plugins/datasource/prometheus/types.ts @@ -25,6 +25,7 @@ export interface PromOptions extends DataSourceJsonData { queryTimeout: string; httpMethod: string; directUrl: string; + customQueryParameters?: string; } export interface PromQueryRequest extends PromQuery {