From 4b38cbdb34bea8c21ec02861791c8a6da324888e Mon Sep 17 00:00:00 2001 From: rozetko Date: Fri, 19 Aug 2022 18:40:48 +0300 Subject: [PATCH] Prometheus: support auth --- README.md | 6 +++--- spec/prometheus.jest.ts | 13 ++++++++++++- src/connectors/index.ts | 6 ++++++ src/connectors/prometheus.ts | 3 ++- src/services/query_service/direct.ts | 3 +-- src/tsdb-kit/index.ts | 15 ++++++++++++--- 6 files changed, 36 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e626c7d..060cdce 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # tsdb-kit -TSDB-kit is a node.js library and CLI-tool for querying timeseries-datasources. +TSDB-kit is a node.js library and CLI-tool for querying timeseries-datasources. ## Features @@ -12,7 +12,7 @@ TSDB-kit is a node.js library and CLI-tool for querying timeseries-datasources. ### Direct -* Prometheus (limited, auth is not supported yet) +* Prometheus ### Grafana @@ -34,7 +34,7 @@ For now, CLI supports only direct Prometheus queries For example: -`npx @corpglory/tsdb-kit -u http://localhost:9090 -q '100-(avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)'` +`npx @corpglory/tsdb-kit -U http://localhost:9090 -q '100-(avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)' -u my_user -p my_password` ## Development (TODO) diff --git a/spec/prometheus.jest.ts b/spec/prometheus.jest.ts index 456c24c..3df0134 100644 --- a/spec/prometheus.jest.ts +++ b/spec/prometheus.jest.ts @@ -7,7 +7,11 @@ import 'jest'; describe('Test Prometheus time range processing', function() { let datasource = { type: DatasourceType.PROMETHEUS, - url: 'api/datasources/proxy/4/api/v1/query_range?query=node_disk_io_time_ms&start=1543411320&end=1543432950&step=30' + url: 'api/datasources/proxy/4/api/v1/query_range?query=node_disk_io_time_ms&start=1543411320&end=1543432950&step=30', + auth: { + username: 'my_user', + password: 'my_password', + } } let targets = []; let prometheus = new PrometheusConnector(datasource, targets); @@ -19,4 +23,11 @@ describe('Test Prometheus time range processing', function() { expect(query.url.indexOf(`start=${Math.floor(from / 1000)}`) !== -1).toBeTruthy(); expect(query.url.indexOf(`end=${Math.floor(to / 1000)}`) !== -1).toBeTruthy(); }); + + it('check that username/password present in query', function() { + let query = prometheus.getQuery(0, 0, 1000, 0); + + expect(query.auth?.username).toBe('my_user'); + expect(query.auth?.password).toBe('my_password'); + }) }); diff --git a/src/connectors/index.ts b/src/connectors/index.ts index b15fadc..0f89291 100644 --- a/src/connectors/index.ts +++ b/src/connectors/index.ts @@ -12,6 +12,7 @@ export enum DatasourceType { MYSQL = 'mysql', } +// TODO: Datasource: type -> class export declare type Datasource = { url: string; type: DatasourceType; @@ -22,6 +23,7 @@ export declare type Datasource = { }; data?: any; datasourceId?: string; + auth?: any; }; export type DatasourceQuery = { @@ -29,6 +31,10 @@ export type DatasourceQuery = { method: string; schema: any; headers?: any; + auth?: { + username: string; + password: string; + }; } export type DataTable = { diff --git a/src/connectors/prometheus.ts b/src/connectors/prometheus.ts index 37ad0f1..b48f753 100644 --- a/src/connectors/prometheus.ts +++ b/src/connectors/prometheus.ts @@ -21,7 +21,8 @@ export class PrometheusConnector extends DatasourceConnector { method: 'GET', schema: { params: this.datasource.params - } + }, + auth: this.datasource.auth, } } diff --git a/src/services/query_service/direct.ts b/src/services/query_service/direct.ts index acf3102..5cd89f7 100644 --- a/src/services/query_service/direct.ts +++ b/src/services/query_service/direct.ts @@ -14,8 +14,7 @@ export class DirectQueryService extends QueryService { async query(query: DatasourceQuery): Promise> { // TODO: support auth let axiosQuery = { - url: query.url, - method: query.method, + ...query, }; _.defaults(axiosQuery, query.schema); diff --git a/src/tsdb-kit/index.ts b/src/tsdb-kit/index.ts index 161b314..47854a7 100644 --- a/src/tsdb-kit/index.ts +++ b/src/tsdb-kit/index.ts @@ -9,10 +9,12 @@ import * as _ from 'lodash'; const parser = new ArgumentParser(); parser.add_argument('-v', '--version', { action: 'version', version }); -parser.add_argument('-u', '--url', { help: 'Datasource URL', required: true }); +parser.add_argument('-U', '--url', { help: 'Datasource URL', required: true }); parser.add_argument('-q', '--query', { help: 'Query Template', required: true }); parser.add_argument('-f', '--from', { help: 'From timestamp (ms), e.g. 1660670020000. If not specified, `now-5m` is used' }); parser.add_argument('-t', '--to', { help: 'To timestamp (ms), e.g. 1660670026000. If not specified, `now` is used' }); +parser.add_argument('-u', '--username', { help: 'Basic Auth Username' }); +parser.add_argument('-p', '--password', { help: 'Basic Auth Password' }); const args = parser.parse_args(); @@ -22,12 +24,19 @@ const PROMETHEUS_URL = args.url; const QUERY = args.query; const FROM = args.from || timeNowInMs - 5 * 60 * 1000; const TO = args.to || timeNowInMs; +const USERNAME = args.username; +const PASSWORD = args.password; +let auth; +if(USERNAME && PASSWORD) { + auth = { username: USERNAME, password: PASSWORD }; +} const datasource = { type: DatasourceType.PROMETHEUS, // TODO: remove PROMETHEUS_URL from here - url: `${PROMETHEUS_URL}/api/v1/query_range?query=${QUERY}&start=1543411320&end=1543432950&step=30` -} + url: `${PROMETHEUS_URL}/api/v1/query_range?query=${QUERY}&start=1543411320&end=1543432950&step=30`, + auth, +}; const targets = []; const queryConfig = new QueryConfig(QueryType.DIRECT, datasource, targets); queryByConfig(queryConfig, PROMETHEUS_URL, FROM, TO)