DataSource.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { lastValueFrom, Observable, of } from 'rxjs';
  2. import { DataQuery, DataQueryResponse, DataSourceApi, DataSourceInstanceSettings } from '@grafana/data';
  3. import { BackendSrvRequest, getBackendSrv } from '@grafana/runtime';
  4. import { AlertManagerDataSourceJsonData, AlertManagerImplementation } from './types';
  5. export type AlertManagerQuery = {
  6. query: string;
  7. } & DataQuery;
  8. export class AlertManagerDatasource extends DataSourceApi<AlertManagerQuery, AlertManagerDataSourceJsonData> {
  9. constructor(public instanceSettings: DataSourceInstanceSettings<AlertManagerDataSourceJsonData>) {
  10. super(instanceSettings);
  11. }
  12. // `query()` has to be implemented but we actually don't use it, just need this
  13. // data source to proxy requests.
  14. // @ts-ignore
  15. query(): Observable<DataQueryResponse> {
  16. return of({
  17. data: [],
  18. });
  19. }
  20. _request(url: string) {
  21. const options: BackendSrvRequest = {
  22. headers: {},
  23. method: 'GET',
  24. url: this.instanceSettings.url + url,
  25. };
  26. if (this.instanceSettings.basicAuth || this.instanceSettings.withCredentials) {
  27. this.instanceSettings.withCredentials = true;
  28. }
  29. if (this.instanceSettings.basicAuth) {
  30. options.headers!.Authorization = this.instanceSettings.basicAuth;
  31. }
  32. return lastValueFrom(getBackendSrv().fetch<any>(options));
  33. }
  34. async testDatasource() {
  35. let alertmanagerResponse;
  36. if (this.instanceSettings.jsonData.implementation === AlertManagerImplementation.prometheus) {
  37. try {
  38. alertmanagerResponse = await this._request('/alertmanager/api/v2/status');
  39. if (alertmanagerResponse && alertmanagerResponse?.status === 200) {
  40. return {
  41. status: 'error',
  42. message:
  43. 'It looks like you have chosen Prometheus implementation, but detected a Mimir or Cortex endpoint. Please update implementation selection and try again.',
  44. };
  45. }
  46. } catch (e) {}
  47. try {
  48. alertmanagerResponse = await this._request('/api/v2/status');
  49. } catch (e) {}
  50. } else {
  51. try {
  52. alertmanagerResponse = await this._request('/api/v2/status');
  53. if (alertmanagerResponse && alertmanagerResponse?.status === 200) {
  54. return {
  55. status: 'error',
  56. message:
  57. 'It looks like you have chosen a Mimir or Cortex implementation, but detected a Prometheus endpoint. Please update implementation selection and try again.',
  58. };
  59. }
  60. } catch (e) {}
  61. try {
  62. alertmanagerResponse = await this._request('/alertmanager/api/v2/status');
  63. } catch (e) {}
  64. }
  65. return alertmanagerResponse?.status === 200
  66. ? {
  67. status: 'success',
  68. message: 'Health check passed.',
  69. }
  70. : {
  71. status: 'error',
  72. message: 'Health check failed.',
  73. };
  74. }
  75. }