Browse Source

Prometheus: support auth

pull/8/head
rozetko 2 years ago
parent
commit
4b38cbdb34
  1. 4
      README.md
  2. 13
      spec/prometheus.jest.ts
  3. 6
      src/connectors/index.ts
  4. 3
      src/connectors/prometheus.ts
  5. 3
      src/services/query_service/direct.ts
  6. 15
      src/tsdb-kit/index.ts

4
README.md

@ -12,7 +12,7 @@ TSDB-kit is a node.js library and CLI-tool for querying timeseries-datasources.
### Direct ### Direct
* Prometheus (limited, auth is not supported yet) * Prometheus
### Grafana ### Grafana
@ -34,7 +34,7 @@ For now, CLI supports only direct Prometheus queries
For example: 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) ## Development (TODO)

13
spec/prometheus.jest.ts

@ -7,7 +7,11 @@ import 'jest';
describe('Test Prometheus time range processing', function() { describe('Test Prometheus time range processing', function() {
let datasource = { let datasource = {
type: DatasourceType.PROMETHEUS, 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 targets = [];
let prometheus = new PrometheusConnector(datasource, 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(`start=${Math.floor(from / 1000)}`) !== -1).toBeTruthy();
expect(query.url.indexOf(`end=${Math.floor(to / 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');
})
}); });

6
src/connectors/index.ts

@ -12,6 +12,7 @@ export enum DatasourceType {
MYSQL = 'mysql', MYSQL = 'mysql',
} }
// TODO: Datasource: type -> class
export declare type Datasource = { export declare type Datasource = {
url: string; url: string;
type: DatasourceType; type: DatasourceType;
@ -22,6 +23,7 @@ export declare type Datasource = {
}; };
data?: any; data?: any;
datasourceId?: string; datasourceId?: string;
auth?: any;
}; };
export type DatasourceQuery = { export type DatasourceQuery = {
@ -29,6 +31,10 @@ export type DatasourceQuery = {
method: string; method: string;
schema: any; schema: any;
headers?: any; headers?: any;
auth?: {
username: string;
password: string;
};
} }
export type DataTable = { export type DataTable = {

3
src/connectors/prometheus.ts

@ -21,7 +21,8 @@ export class PrometheusConnector extends DatasourceConnector {
method: 'GET', method: 'GET',
schema: { schema: {
params: this.datasource.params params: this.datasource.params
} },
auth: this.datasource.auth,
} }
} }

3
src/services/query_service/direct.ts

@ -14,8 +14,7 @@ export class DirectQueryService extends QueryService {
async query(query: DatasourceQuery): Promise<AxiosResponse<any>> { async query(query: DatasourceQuery): Promise<AxiosResponse<any>> {
// TODO: support auth // TODO: support auth
let axiosQuery = { let axiosQuery = {
url: query.url, ...query,
method: query.method,
}; };
_.defaults(axiosQuery, query.schema); _.defaults(axiosQuery, query.schema);

15
src/tsdb-kit/index.ts

@ -9,10 +9,12 @@ import * as _ from 'lodash';
const parser = new ArgumentParser(); const parser = new ArgumentParser();
parser.add_argument('-v', '--version', { action: 'version', version }); 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('-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('-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('-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(); const args = parser.parse_args();
@ -22,12 +24,19 @@ const PROMETHEUS_URL = args.url;
const QUERY = args.query; const QUERY = args.query;
const FROM = args.from || timeNowInMs - 5 * 60 * 1000; const FROM = args.from || timeNowInMs - 5 * 60 * 1000;
const TO = args.to || timeNowInMs; 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 = { const datasource = {
type: DatasourceType.PROMETHEUS, type: DatasourceType.PROMETHEUS,
// TODO: remove PROMETHEUS_URL from here // 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 targets = [];
const queryConfig = new QueryConfig(QueryType.DIRECT, datasource, targets); const queryConfig = new QueryConfig(QueryType.DIRECT, datasource, targets);
queryByConfig(queryConfig, PROMETHEUS_URL, FROM, TO) queryByConfig(queryConfig, PROMETHEUS_URL, FROM, TO)

Loading…
Cancel
Save