Compare commits
35 Commits
dependabot
...
master
37 changed files with 1725 additions and 5281 deletions
@ -1,10 +1,10 @@ |
|||||||
{ |
{ |
||||||
"compilerOptions": { |
"compilerOptions": { |
||||||
|
"moduleResolution": "node", |
||||||
"sourceMap": true, |
"sourceMap": true, |
||||||
"target": "es2015", |
"target": "es6", |
||||||
"declaration": false, |
"declaration": false, |
||||||
"outFile": "bin/tsdb-kit.js" |
"skipLibCheck": true |
||||||
}, |
}, |
||||||
"include": [ "src/**/*.ts" ], |
"include": [ "src/**/*.ts" ] |
||||||
"exclude": [ "src/index.ts" ] |
|
||||||
} |
} |
||||||
|
@ -0,0 +1,31 @@ |
|||||||
|
import { InfluxdbConnector } from './influxdb'; |
||||||
|
import { GraphiteConnector } from './graphite'; |
||||||
|
import { DatasourceConnector, DatasourceType } from '.'; |
||||||
|
import { PrometheusConnector } from './prometheus'; |
||||||
|
import { PostgresConnector } from './postgres'; |
||||||
|
import { ElasticsearchConnector } from './elasticsearch'; |
||||||
|
import { MysqlConnector } from './mysql'; |
||||||
|
|
||||||
|
import { QueryConfig } from '../models/query_config'; |
||||||
|
|
||||||
|
|
||||||
|
export function connectorFactory( |
||||||
|
queryConfig: QueryConfig, |
||||||
|
): DatasourceConnector { |
||||||
|
const classMap = { |
||||||
|
[DatasourceType.INFLUXDB]: InfluxdbConnector, |
||||||
|
[DatasourceType.GRAPHITE]: GraphiteConnector, |
||||||
|
[DatasourceType.PROMETHEUS]: PrometheusConnector, |
||||||
|
[DatasourceType.POSTGRES]: PostgresConnector, |
||||||
|
[DatasourceType.ELASTICSEARCH]: ElasticsearchConnector, |
||||||
|
[DatasourceType.MYSQL]: MysqlConnector, |
||||||
|
}; |
||||||
|
const datasource = queryConfig.datasource; |
||||||
|
const targets = queryConfig.targets; |
||||||
|
if(classMap[datasource.type] === undefined) { |
||||||
|
console.error(`Datasources of type ${datasource.type} are not supported currently`); |
||||||
|
throw new Error(`Datasources of type ${datasource.type} are not supported currently`); |
||||||
|
} else { |
||||||
|
return new classMap[datasource.type](datasource, targets); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,58 @@ |
|||||||
|
export enum QueryType { |
||||||
|
DIRECT = 'direct', |
||||||
|
GRAFANA = 'grafana', |
||||||
|
} |
||||||
|
|
||||||
|
export enum DatasourceType { |
||||||
|
INFLUXDB = 'influxdb', |
||||||
|
GRAPHITE = 'graphite', |
||||||
|
PROMETHEUS = 'prometheus', |
||||||
|
POSTGRES = 'postgres', |
||||||
|
ELASTICSEARCH = 'elasticsearch', |
||||||
|
MYSQL = 'mysql', |
||||||
|
} |
||||||
|
|
||||||
|
// TODO: Datasource: type -> class
|
||||||
|
export declare type Datasource = { |
||||||
|
url: string; |
||||||
|
type: DatasourceType; |
||||||
|
params?: { |
||||||
|
db: string; |
||||||
|
q: string; |
||||||
|
epoch: string; |
||||||
|
}; |
||||||
|
data?: any; |
||||||
|
datasourceId?: string; |
||||||
|
auth?: any; |
||||||
|
}; |
||||||
|
|
||||||
|
export type DatasourceQuery = { |
||||||
|
url: string; |
||||||
|
method: string; |
||||||
|
schema: any; |
||||||
|
headers?: any; |
||||||
|
auth?: { |
||||||
|
username: string; |
||||||
|
password: string; |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
export type DataTable = { |
||||||
|
values: (number | null)[][]; |
||||||
|
columns: string[]; |
||||||
|
} |
||||||
|
|
||||||
|
export abstract class DatasourceConnector { |
||||||
|
constructor( |
||||||
|
public datasource: Datasource, |
||||||
|
// TODO: Target type
|
||||||
|
public targets: any[], |
||||||
|
) {} |
||||||
|
/* |
||||||
|
from / to - timestamp in ms |
||||||
|
limit - max number of items in result |
||||||
|
offset - number of items to skip from timerange start |
||||||
|
*/ |
||||||
|
abstract getQuery(from: number, to: number, limit: number, offset: number): DatasourceQuery; |
||||||
|
abstract parseResponse(res): DataTable; |
||||||
|
} |
@ -0,0 +1,5 @@ |
|||||||
|
import { SqlConnector } from './sql'; |
||||||
|
|
||||||
|
export class MysqlConnector extends SqlConnector { |
||||||
|
|
||||||
|
} |
@ -0,0 +1,5 @@ |
|||||||
|
import { SqlConnector } from './sql'; |
||||||
|
|
||||||
|
export class PostgresConnector extends SqlConnector { |
||||||
|
|
||||||
|
} |
@ -1,134 +0,0 @@ |
|||||||
import { Metric } from './metrics/metrics_factory'; |
|
||||||
import { MetricQuery, Datasource } from './metrics/metric'; |
|
||||||
|
|
||||||
import { URL } from 'url'; |
|
||||||
import axios from 'axios'; |
|
||||||
import * as _ from 'lodash'; |
|
||||||
|
|
||||||
export class DataKitError extends Error { |
|
||||||
constructor( |
|
||||||
message: string, |
|
||||||
public datasourceType?: string, |
|
||||||
public datasourceUrl?: string |
|
||||||
) { |
|
||||||
super(message); |
|
||||||
} |
|
||||||
}; |
|
||||||
|
|
||||||
export class BadRange extends DataKitError {}; |
|
||||||
export class GrafanaUnavailable extends DataKitError {}; |
|
||||||
export class DatasourceUnavailable extends DataKitError {}; |
|
||||||
|
|
||||||
const CHUNK_SIZE = 50000; |
|
||||||
|
|
||||||
|
|
||||||
/** |
|
||||||
* @param metric to query to Grafana |
|
||||||
* @returns { values: [time, value][], columns: string[] } |
|
||||||
*/ |
|
||||||
export async function queryByMetric( |
|
||||||
metric: Metric, url: string, from: number, to: number, apiKey: string |
|
||||||
): Promise<{ values: [number, number][], columns: string[] }> { |
|
||||||
|
|
||||||
if(from > to) { |
|
||||||
throw new BadRange( |
|
||||||
`Data-kit got wrong range: from ${from} > to ${to}`, |
|
||||||
metric.datasource.type, |
|
||||||
url |
|
||||||
); |
|
||||||
} |
|
||||||
|
|
||||||
if(from === to) { |
|
||||||
console.warn(`Data-kit got from === to`); |
|
||||||
} |
|
||||||
|
|
||||||
const grafanaUrl = getGrafanaUrl(url); |
|
||||||
|
|
||||||
let data = { |
|
||||||
values: [], |
|
||||||
columns: [] |
|
||||||
}; |
|
||||||
|
|
||||||
while(true) { |
|
||||||
let query = metric.metricQuery.getQuery(from, to, CHUNK_SIZE, data.values.length); |
|
||||||
query.url = `${grafanaUrl}/${query.url}`; |
|
||||||
let res = await queryGrafana(query, apiKey, metric.datasource); |
|
||||||
let chunk = metric.metricQuery.getResults(res); |
|
||||||
let values = chunk.values; |
|
||||||
data.values = data.values.concat(values); |
|
||||||
data.columns = chunk.columns; |
|
||||||
|
|
||||||
if(values.length < CHUNK_SIZE) { |
|
||||||
// because if we get less that we could, then there is nothing more
|
|
||||||
break; |
|
||||||
} |
|
||||||
} |
|
||||||
return data; |
|
||||||
} |
|
||||||
|
|
||||||
async function queryGrafana(query: MetricQuery, apiKey: string, datasource: Datasource) { |
|
||||||
let headers = { Authorization: `Bearer ${apiKey}` }; |
|
||||||
|
|
||||||
if(query.headers !== undefined) { |
|
||||||
_.merge(headers, query.headers); |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
let axiosQuery = { |
|
||||||
headers, |
|
||||||
url: query.url, |
|
||||||
method: query.method, |
|
||||||
}; |
|
||||||
|
|
||||||
_.defaults(axiosQuery, query.schema); |
|
||||||
|
|
||||||
try { |
|
||||||
var res = await axios(axiosQuery); |
|
||||||
} catch (e) { |
|
||||||
const msg = `Data kit: fail while request data: ${e.message}`; |
|
||||||
const parsedUrl = new URL(query.url); |
|
||||||
const queryUrl = `query url: ${JSON.stringify(parsedUrl.pathname)}`; |
|
||||||
console.error(`${msg} ${queryUrl}`); |
|
||||||
if(e.errno === 'ECONNREFUSED') { |
|
||||||
throw new GrafanaUnavailable(e.message); |
|
||||||
} |
|
||||||
if(e.response !== undefined) { |
|
||||||
console.error(`Response: \ |
|
||||||
status: ${e.response.status}, \ |
|
||||||
response data: ${JSON.stringify(e.response.data)}, \ |
|
||||||
headers: ${JSON.stringify(e.response.headers)} |
|
||||||
`);
|
|
||||||
if(e.response.status === 401) { |
|
||||||
throw new Error(`Unauthorized. Check the API_KEY. ${e.message}`); |
|
||||||
} |
|
||||||
if(e.response.status === 502) { |
|
||||||
let datasourceError = new DatasourceUnavailable( |
|
||||||
`datasource ${parsedUrl.pathname} unavailable, message: ${e.message}`, |
|
||||||
datasource.type, |
|
||||||
query.url |
|
||||||
); |
|
||||||
throw datasourceError; |
|
||||||
} |
|
||||||
} |
|
||||||
throw new Error(msg); |
|
||||||
} |
|
||||||
|
|
||||||
return res; |
|
||||||
} |
|
||||||
|
|
||||||
function getGrafanaUrl(url: string) { |
|
||||||
const parsedUrl = new URL(url); |
|
||||||
const path = parsedUrl.pathname; |
|
||||||
const panelUrl = path.match(/^\/*([^\/]*)\/d\//); |
|
||||||
if(panelUrl === null) { |
|
||||||
return url; |
|
||||||
} |
|
||||||
|
|
||||||
const origin = parsedUrl.origin; |
|
||||||
const grafanaSubPath = panelUrl[1]; |
|
||||||
if(grafanaSubPath.length > 0) { |
|
||||||
return `${origin}/${grafanaSubPath}`; |
|
||||||
} |
|
||||||
|
|
||||||
return origin; |
|
||||||
} |
|
@ -1,3 +1,55 @@ |
|||||||
export { Metric } from './metrics/metrics_factory'; |
import { DataTable } from './connectors'; |
||||||
export { Datasource } from './metrics/metric' |
import { QueryConfig } from './models/query_config'; |
||||||
export { queryByMetric, GrafanaUnavailable, DatasourceUnavailable } from './grafana_service'; |
import { BadRange } from './types'; |
||||||
|
|
||||||
|
export { QueryConfig } from './models/query_config'; |
||||||
|
export { Datasource, DatasourceType, DataTable } from './connectors' |
||||||
|
export { DatasourceUnavailable } from './types'; |
||||||
|
export { GrafanaUnavailable } from './services/query_service/grafana'; |
||||||
|
|
||||||
|
const CHUNK_SIZE = 50000; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* @param queryConfig |
||||||
|
* @returns { values: [time, value][], columns: string[] } |
||||||
|
*/ |
||||||
|
export async function queryByConfig( |
||||||
|
// TODO: check how did we wanna use `url` field
|
||||||
|
queryConfig: QueryConfig, url: string, from: number, to: number, |
||||||
|
// TODO: we need an abstract DatasourceConfig class which will differ in direct and grafana queries
|
||||||
|
apiKey?: string |
||||||
|
): Promise<DataTable> { |
||||||
|
|
||||||
|
if(from > to) { |
||||||
|
throw new BadRange( |
||||||
|
`TSDB-kit got wrong range: from ${from} > to ${to}`, |
||||||
|
queryConfig.datasource.type, |
||||||
|
url |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
if(from === to) { |
||||||
|
console.warn(`TSDB-kit got from === to`); |
||||||
|
} |
||||||
|
|
||||||
|
let data: DataTable = { |
||||||
|
values: [], |
||||||
|
columns: [] |
||||||
|
}; |
||||||
|
|
||||||
|
while(true) { |
||||||
|
let query = queryConfig.datasourceConnector.getQuery(from, to, CHUNK_SIZE, data.values.length); |
||||||
|
const res = await queryConfig.queryService.query(query, apiKey); |
||||||
|
let chunk = queryConfig.datasourceConnector.parseResponse(res); |
||||||
|
let values = chunk.values; |
||||||
|
data.values = data.values.concat(values); |
||||||
|
data.columns = chunk.columns; |
||||||
|
|
||||||
|
if(values.length < CHUNK_SIZE) { |
||||||
|
// because if we get less that we could, then there is nothing more
|
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
return data; |
||||||
|
} |
||||||
|
@ -1,40 +0,0 @@ |
|||||||
export declare type Datasource = { |
|
||||||
url: string; |
|
||||||
type: string; |
|
||||||
params?: { |
|
||||||
db: string; |
|
||||||
q: string; |
|
||||||
epoch: string; |
|
||||||
}; |
|
||||||
data?: any; |
|
||||||
datasourceId?: string; |
|
||||||
}; |
|
||||||
|
|
||||||
export type MetricQuery = { |
|
||||||
url: string; |
|
||||||
method: string; |
|
||||||
schema: any; |
|
||||||
headers?: any; |
|
||||||
} |
|
||||||
|
|
||||||
export type MetricResults = { |
|
||||||
values: any; |
|
||||||
columns: any; |
|
||||||
} |
|
||||||
|
|
||||||
export type MetricId = string; |
|
||||||
|
|
||||||
export abstract class AbstractMetric { |
|
||||||
constructor( |
|
||||||
public datasource: Datasource, |
|
||||||
public targets: any[], |
|
||||||
public id?: MetricId |
|
||||||
) {}; |
|
||||||
abstract getQuery(from: number, to: number, limit: number, offset: number): MetricQuery; |
|
||||||
/* |
|
||||||
from / to - timestamp in ms |
|
||||||
limit - max number of items in result |
|
||||||
offset - number of items to skip from timerange start |
|
||||||
*/ |
|
||||||
abstract getResults(res): MetricResults; |
|
||||||
} |
|
@ -1,78 +0,0 @@ |
|||||||
import { InfluxdbMetric } from './influxdb_metric'; |
|
||||||
import { GraphiteMetric } from './graphite_metric'; |
|
||||||
import { AbstractMetric, Datasource, MetricId } from './metric'; |
|
||||||
import { PrometheusMetric } from './prometheus_metric'; |
|
||||||
import { PostgresMetric } from './postgres_metric'; |
|
||||||
import { ElasticsearchMetric } from './elasticsearch_metric'; |
|
||||||
import { MysqlMetric } from './mysql_metric'; |
|
||||||
|
|
||||||
export function metricFactory( |
|
||||||
datasource: Datasource, |
|
||||||
targets: any[], |
|
||||||
id?: MetricId |
|
||||||
): AbstractMetric { |
|
||||||
|
|
||||||
let classMap = { |
|
||||||
'influxdb': InfluxdbMetric, |
|
||||||
'graphite': GraphiteMetric, |
|
||||||
'prometheus': PrometheusMetric, |
|
||||||
'postgres': PostgresMetric, |
|
||||||
'elasticsearch': ElasticsearchMetric, |
|
||||||
'mysql': MysqlMetric, |
|
||||||
}; |
|
||||||
if(classMap[datasource.type] === undefined) { |
|
||||||
console.error(`Datasources of type ${datasource.type} are not supported currently`); |
|
||||||
throw new Error(`Datasources of type ${datasource.type} are not supported currently`); |
|
||||||
} else { |
|
||||||
return new classMap[datasource.type](datasource, targets, id); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
export class Metric { |
|
||||||
datasource: Datasource; |
|
||||||
targets: any[]; |
|
||||||
id?: MetricId; |
|
||||||
private _metricQuery: AbstractMetric = undefined; |
|
||||||
|
|
||||||
constructor(datasource: Datasource, targets: any[], id?: MetricId) { |
|
||||||
if(datasource === undefined) { |
|
||||||
throw new Error('datasource is undefined'); |
|
||||||
} |
|
||||||
if(targets === undefined) { |
|
||||||
throw new Error('targets is undefined'); |
|
||||||
} |
|
||||||
if(targets.length === 0) { |
|
||||||
throw new Error('targets is empty'); |
|
||||||
} |
|
||||||
this.datasource = datasource; |
|
||||||
this.targets = targets; |
|
||||||
this.id = id; |
|
||||||
} |
|
||||||
|
|
||||||
public get metricQuery() { |
|
||||||
if(this._metricQuery === undefined) { |
|
||||||
this._metricQuery = metricFactory(this.datasource, this.targets, this.id); |
|
||||||
} |
|
||||||
return this._metricQuery; |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
public toObject() { |
|
||||||
return { |
|
||||||
datasource: this.datasource, |
|
||||||
targets: this.targets, |
|
||||||
_id: this.id |
|
||||||
}; |
|
||||||
} |
|
||||||
|
|
||||||
static fromObject(obj: any): Metric { |
|
||||||
if(obj === undefined) { |
|
||||||
throw new Error('obj is undefined'); |
|
||||||
} |
|
||||||
return new Metric( |
|
||||||
obj.datasource, |
|
||||||
obj.targets, |
|
||||||
obj._id |
|
||||||
); |
|
||||||
} |
|
||||||
} |
|
@ -1,5 +0,0 @@ |
|||||||
import { SqlMetric } from './sql_metric'; |
|
||||||
|
|
||||||
export class MysqlMetric extends SqlMetric { |
|
||||||
|
|
||||||
} |
|
@ -1,5 +0,0 @@ |
|||||||
import { SqlMetric } from './sql_metric'; |
|
||||||
|
|
||||||
export class PostgresMetric extends SqlMetric { |
|
||||||
|
|
||||||
} |
|
@ -0,0 +1,62 @@ |
|||||||
|
import { Datasource, DatasourceConnector, QueryType } from '../connectors'; |
||||||
|
import { connectorFactory } from '../connectors/connector_factory'; |
||||||
|
import { QueryService } from '../services/query_service/base'; |
||||||
|
import { queryServiceFactory } from '../services/query_service/query_service_factory'; |
||||||
|
|
||||||
|
|
||||||
|
export class QueryConfig { |
||||||
|
queryType: QueryType; |
||||||
|
datasource: Datasource; |
||||||
|
// TODO: Target type (depends on datasource type)
|
||||||
|
targets: any[]; |
||||||
|
private _datasourceConnector?: DatasourceConnector; |
||||||
|
private _queryService?: QueryService; |
||||||
|
|
||||||
|
constructor(queryType: QueryType, datasource: Datasource, targets: any[]) { |
||||||
|
if(queryType === undefined) { |
||||||
|
throw new Error('queryType is undefined'); |
||||||
|
} |
||||||
|
if(datasource === undefined) { |
||||||
|
throw new Error('datasource is undefined'); |
||||||
|
} |
||||||
|
if(targets === undefined) { |
||||||
|
throw new Error('targets is undefined'); |
||||||
|
} |
||||||
|
this.queryType = queryType; |
||||||
|
this.datasource = datasource; |
||||||
|
this.targets = targets; |
||||||
|
} |
||||||
|
|
||||||
|
get datasourceConnector(): DatasourceConnector { |
||||||
|
if(this._datasourceConnector === undefined) { |
||||||
|
this._datasourceConnector = connectorFactory(this); |
||||||
|
} |
||||||
|
return this._datasourceConnector; |
||||||
|
} |
||||||
|
|
||||||
|
get queryService(): QueryService { |
||||||
|
if(this._queryService === undefined) { |
||||||
|
this._queryService = queryServiceFactory(this); |
||||||
|
} |
||||||
|
return this._queryService; |
||||||
|
} |
||||||
|
|
||||||
|
public toObject() { |
||||||
|
return { |
||||||
|
queryType: this.queryType, |
||||||
|
datasource: this.datasource, |
||||||
|
targets: this.targets, |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
static fromObject(obj: any): QueryConfig { |
||||||
|
if(obj === undefined) { |
||||||
|
throw new Error('obj is undefined'); |
||||||
|
} |
||||||
|
return new QueryConfig( |
||||||
|
obj.queryType, |
||||||
|
obj.datasource, |
||||||
|
obj.targets, |
||||||
|
); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,11 @@ |
|||||||
|
import { Datasource, DatasourceQuery } from '../../connectors'; |
||||||
|
|
||||||
|
import { AxiosResponse } from 'axios'; |
||||||
|
|
||||||
|
|
||||||
|
export abstract class QueryService { |
||||||
|
constructor(protected _datasource: Datasource) { } |
||||||
|
|
||||||
|
// TODO: we don't need `apiKey` here, we need some abstract auth config for both Direct and Grafana queries
|
||||||
|
abstract query(query: DatasourceQuery, apiKey?: string): Promise<AxiosResponse<any>>; |
||||||
|
} |
@ -0,0 +1,52 @@ |
|||||||
|
import { QueryService } from './base'; |
||||||
|
import { DatasourceUnavailable } from '../../types'; |
||||||
|
import { Datasource, DatasourceQuery } from '../../connectors'; |
||||||
|
|
||||||
|
import axios, { AxiosResponse } from 'axios'; |
||||||
|
import * as _ from 'lodash'; |
||||||
|
|
||||||
|
|
||||||
|
export class DirectQueryService extends QueryService { |
||||||
|
constructor(datasource: Datasource) { |
||||||
|
super(datasource); |
||||||
|
} |
||||||
|
|
||||||
|
async query(query: DatasourceQuery): Promise<AxiosResponse<any>> { |
||||||
|
// TODO: support auth
|
||||||
|
let axiosQuery = { |
||||||
|
...query, |
||||||
|
}; |
||||||
|
|
||||||
|
_.defaults(axiosQuery, query.schema); |
||||||
|
|
||||||
|
try { |
||||||
|
return axios(axiosQuery); |
||||||
|
} catch(e) { |
||||||
|
// TODO: seems like this error handler can be used for both Grafana and Direct queries
|
||||||
|
const msg = `TSDB-kit: fail while request data: ${e.message}`; |
||||||
|
const parsedUrl = new URL(query.url); |
||||||
|
const queryUrl = `query url: ${JSON.stringify(parsedUrl.pathname)}`; |
||||||
|
console.error(`${msg} ${queryUrl}`); |
||||||
|
|
||||||
|
if(e.response !== undefined) { |
||||||
|
console.error(`Response: \ |
||||||
|
status: ${e.response.status}, \ |
||||||
|
response data: ${JSON.stringify(e.response.data)}, \ |
||||||
|
headers: ${JSON.stringify(e.response.headers)} |
||||||
|
`);
|
||||||
|
if(e.response.status === 401) { |
||||||
|
throw new Error(`Unauthorized. Check credentials. ${e.message}`); |
||||||
|
} |
||||||
|
if(e.response.status === 502) { |
||||||
|
let datasourceError = new DatasourceUnavailable( |
||||||
|
`datasource ${parsedUrl.pathname} unavailable, message: ${e.message}`, |
||||||
|
this._datasource.type, |
||||||
|
query.url |
||||||
|
); |
||||||
|
throw datasourceError; |
||||||
|
} |
||||||
|
} |
||||||
|
throw new Error(msg); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,64 @@ |
|||||||
|
import { QueryService } from './base'; |
||||||
|
import { Datasource, DatasourceQuery } from '../../connectors'; |
||||||
|
import { TsdbKitError, DatasourceUnavailable } from '../../types'; |
||||||
|
|
||||||
|
import axios, { AxiosResponse } from 'axios'; |
||||||
|
import * as _ from 'lodash'; |
||||||
|
|
||||||
|
|
||||||
|
export class GrafanaUnavailable extends TsdbKitError { }; |
||||||
|
|
||||||
|
export class GrafanaQueryService extends QueryService { |
||||||
|
constructor(datasource: Datasource) { |
||||||
|
super(datasource); |
||||||
|
} |
||||||
|
|
||||||
|
async query(query: DatasourceQuery, apiKey: string): Promise<AxiosResponse<any>> { |
||||||
|
let headers = { Authorization: `Bearer ${apiKey}` }; |
||||||
|
|
||||||
|
if(query.headers !== undefined) { |
||||||
|
_.merge(headers, query.headers); |
||||||
|
} |
||||||
|
|
||||||
|
let axiosQuery = { |
||||||
|
headers, |
||||||
|
url: query.url, |
||||||
|
method: query.method, |
||||||
|
}; |
||||||
|
|
||||||
|
_.defaults(axiosQuery, query.schema); |
||||||
|
|
||||||
|
try { |
||||||
|
const resp = await axios(axiosQuery); |
||||||
|
return resp; |
||||||
|
} catch (e) { |
||||||
|
// TODO: seems like this error handler can be used for both Grafana and Direct queries
|
||||||
|
const msg = `TSDB-kit: fail while request data: ${e.message}`; |
||||||
|
const parsedUrl = new URL(query.url); |
||||||
|
const queryUrl = `query url: ${JSON.stringify(parsedUrl.pathname)}`; |
||||||
|
console.error(`${msg} ${queryUrl}`); |
||||||
|
if(e.errno === 'ECONNREFUSED') { |
||||||
|
throw new GrafanaUnavailable(e.message); |
||||||
|
} |
||||||
|
if(e.response !== undefined) { |
||||||
|
console.error(`Response: \ |
||||||
|
status: ${e.response.status}, \ |
||||||
|
response data: ${JSON.stringify(e.response.data)}, \ |
||||||
|
headers: ${JSON.stringify(e.response.headers)} |
||||||
|
`);
|
||||||
|
if(e.response.status === 401) { |
||||||
|
throw new Error(`Unauthorized. Check the API_KEY. ${e.message}`); |
||||||
|
} |
||||||
|
if(e.response.status === 502) { |
||||||
|
let datasourceError = new DatasourceUnavailable( |
||||||
|
`datasource ${parsedUrl.pathname} unavailable, message: ${e.message}`, |
||||||
|
this._datasource.type, |
||||||
|
query.url |
||||||
|
); |
||||||
|
throw datasourceError; |
||||||
|
} |
||||||
|
} |
||||||
|
throw new Error(msg); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,23 @@ |
|||||||
|
import { QueryService } from './base'; |
||||||
|
import { DirectQueryService } from './direct'; |
||||||
|
import { GrafanaQueryService } from './grafana'; |
||||||
|
import { QueryType } from '../../connectors'; |
||||||
|
import { QueryConfig } from '../../models/query_config'; |
||||||
|
|
||||||
|
|
||||||
|
export function queryServiceFactory( |
||||||
|
queryConfig: QueryConfig, |
||||||
|
): QueryService { |
||||||
|
const classMap = { |
||||||
|
[QueryType.DIRECT]: DirectQueryService, |
||||||
|
[QueryType.GRAFANA]: GrafanaQueryService, |
||||||
|
}; |
||||||
|
const queryType = queryConfig.queryType; |
||||||
|
const datasource = queryConfig.datasource; |
||||||
|
if(classMap[queryType] === undefined) { |
||||||
|
console.error(`Queries of type ${queryType} are not supported currently`); |
||||||
|
throw new Error(`Queries of type ${queryType} are not supported currently`); |
||||||
|
} else { |
||||||
|
return new classMap[queryType](datasource); |
||||||
|
} |
||||||
|
} |
@ -1 +1,49 @@ |
|||||||
console.log('Hello world'); |
import { queryByConfig, QueryConfig } from '..'; |
||||||
|
|
||||||
|
import { DatasourceType, QueryType } from '../connectors'; |
||||||
|
|
||||||
|
const { version } = require('../../package.json') |
||||||
|
import { ArgumentParser } from 'argparse'; |
||||||
|
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('-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(); |
||||||
|
|
||||||
|
const timeNowInMs = new Date().getTime(); |
||||||
|
|
||||||
|
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`, |
||||||
|
auth, |
||||||
|
}; |
||||||
|
const targets = []; |
||||||
|
const queryConfig = new QueryConfig(QueryType.DIRECT, datasource, targets); |
||||||
|
queryByConfig(queryConfig, PROMETHEUS_URL, FROM, TO) |
||||||
|
.then(res => { |
||||||
|
console.log(res); |
||||||
|
}) |
||||||
|
.catch(err => { |
||||||
|
console.error('Query error: ', err); |
||||||
|
}); |
||||||
|
|
||||||
|
@ -0,0 +1,15 @@ |
|||||||
|
import { DatasourceType } from './connectors'; |
||||||
|
|
||||||
|
|
||||||
|
export class TsdbKitError extends Error { |
||||||
|
constructor( |
||||||
|
message: string, |
||||||
|
public datasourceType?: DatasourceType, |
||||||
|
public datasourceUrl?: string |
||||||
|
) { |
||||||
|
super(message); |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
export class BadRange extends TsdbKitError {}; |
||||||
|
export class DatasourceUnavailable extends TsdbKitError {}; |
@ -0,0 +1,33 @@ |
|||||||
|
const webpack = require('webpack'); |
||||||
|
const path = require('path'); |
||||||
|
|
||||||
|
|
||||||
|
module.exports = { |
||||||
|
mode: 'development', |
||||||
|
target: 'node', |
||||||
|
devtool: 'inline-source-map', |
||||||
|
entry: { |
||||||
|
main: './src/tsdb-kit/index.ts', |
||||||
|
}, |
||||||
|
output: { |
||||||
|
path: path.resolve(__dirname, './bin'), |
||||||
|
filename: 'tsdb-kit.js' |
||||||
|
}, |
||||||
|
plugins: [ |
||||||
|
new webpack.BannerPlugin({ banner: "#!/usr/bin/env node", raw: true }), |
||||||
|
], |
||||||
|
resolve: { |
||||||
|
extensions: ['.ts', '.js'], |
||||||
|
}, |
||||||
|
module: { |
||||||
|
rules: [ |
||||||
|
{ |
||||||
|
test: /.ts$/, |
||||||
|
loader: 'ts-loader', |
||||||
|
options: { |
||||||
|
configFile: 'bin.tsconfig.json' |
||||||
|
} |
||||||
|
} |
||||||
|
] |
||||||
|
} |
||||||
|
}; |
Loading…
Reference in new issue