The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/public/app/plugins/datasource/alertmanager/DataSource.ts

63 lines
1.7 KiB

import { DataQuery, DataQueryResponse, DataSourceApi, DataSourceInstanceSettings } from '@grafana/data';
import { BackendSrvRequest, getBackendSrv } from '@grafana/runtime';
import { Observable, of } from 'rxjs';
export type AlertManagerQuery = {
query: string;
} & DataQuery;
export class AlertManagerDatasource extends DataSourceApi<AlertManagerQuery> {
constructor(public instanceSettings: DataSourceInstanceSettings) {
super(instanceSettings);
}
// `query()` has to be implemented but we actually don't use it, just need this
// data source to proxy requests.
// @ts-ignore
query(): Observable<DataQueryResponse> {
return of({
data: [],
});
}
_request(url: string) {
const options: BackendSrvRequest = {
headers: {},
method: 'GET',
url: this.instanceSettings.url + url,
};
if (this.instanceSettings.basicAuth || this.instanceSettings.withCredentials) {
this.instanceSettings.withCredentials = true;
}
if (this.instanceSettings.basicAuth) {
options.headers!.Authorization = this.instanceSettings.basicAuth;
}
return getBackendSrv().fetch<any>(options).toPromise();
}
async testDatasource() {
let alertmanagerResponse;
let cortexAlertmanagerResponse;
try {
alertmanagerResponse = await this._request('/api/v2/status');
} catch (e) {}
try {
cortexAlertmanagerResponse = await this._request('/alertmanager/api/v2/status');
} catch (e) {}
return alertmanagerResponse?.status === 200 || cortexAlertmanagerResponse?.status === 200
? {
status: 'success',
message: 'Health check passed.',
}
: {
status: 'error',
message: 'Health check failed.',
};
}
}