Browse Source

Prometheus: support auth

pull/8/head
rozetko 2 years ago
parent
commit
4b38cbdb34
  1. 6
      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

6
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)

13
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');
})
});

6
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 = {

3
src/connectors/prometheus.ts

@ -21,7 +21,8 @@ export class PrometheusConnector extends DatasourceConnector {
method: 'GET',
schema: {
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>> {
// TODO: support auth
let axiosQuery = {
url: query.url,
method: query.method,
...query,
};
_.defaults(axiosQuery, query.schema);

15
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)

Loading…
Cancel
Save