Alerting: Alertmanager datasource support for upstream Prometheus AM implementation (#39775)

This commit is contained in:
Domas
2021-10-01 16:24:56 +03:00
committed by GitHub
parent cc7f7e30e9
commit a1d4be0700
36 changed files with 767 additions and 342 deletions

View File

@@ -1,15 +1,49 @@
import { DataSourcePluginOptionsEditorProps } from '@grafana/data';
import { Alert, DataSourceHttpSettings } from '@grafana/ui';
import { DataSourcePluginOptionsEditorProps, SelectableValue } from '@grafana/data';
import { DataSourceHttpSettings, InlineFormLabel, Select } from '@grafana/ui';
import React from 'react';
import { AlertManagerDataSourceJsonData, AlertManagerImplementation } from './types';
export type Props = DataSourcePluginOptionsEditorProps;
export type Props = DataSourcePluginOptionsEditorProps<AlertManagerDataSourceJsonData>;
const IMPL_OPTIONS: SelectableValue[] = [
{
value: AlertManagerImplementation.cortex,
label: 'Cortex',
description: `https://cortexmetrics.io/`,
},
{
value: AlertManagerImplementation.prometheus,
label: 'Prometheus',
description:
'https://prometheus.io/. Does not support editing configuration via API, so contact points and notification policies are read-only.',
},
];
export const ConfigEditor: React.FC<Props> = ({ options, onOptionsChange }) => {
return (
<>
<Alert severity="info" title="Only Cortex alertmanager is supported">
Note that only Cortex implementation of alert manager is supported at this time.
</Alert>
<h3 className="page-heading">Alertmanager</h3>
<div className="gf-form-group">
<div className="gf-form-inline">
<div className="gf-form">
<InlineFormLabel width={13}>Implementation</InlineFormLabel>
<Select
width={40}
options={IMPL_OPTIONS}
value={options.jsonData.implementation || AlertManagerImplementation.cortex}
onChange={(value) =>
onOptionsChange({
...options,
jsonData: {
...options.jsonData,
implementation: value.value as AlertManagerImplementation,
},
})
}
/>
</div>
</div>
</div>
<DataSourceHttpSettings
defaultUrl={''}
dataSourceConfig={options}

View File

@@ -1,13 +1,14 @@
import { lastValueFrom, Observable, of } from 'rxjs';
import { DataQuery, DataQueryResponse, DataSourceApi, DataSourceInstanceSettings } from '@grafana/data';
import { BackendSrvRequest, getBackendSrv } from '@grafana/runtime';
import { AlertManagerDataSourceJsonData, AlertManagerImplementation } from './types';
export type AlertManagerQuery = {
query: string;
} & DataQuery;
export class AlertManagerDatasource extends DataSourceApi<AlertManagerQuery> {
constructor(public instanceSettings: DataSourceInstanceSettings) {
export class AlertManagerDatasource extends DataSourceApi<AlertManagerQuery, AlertManagerDataSourceJsonData> {
constructor(public instanceSettings: DataSourceInstanceSettings<AlertManagerDataSourceJsonData>) {
super(instanceSettings);
}
@@ -40,23 +41,38 @@ export class AlertManagerDatasource extends DataSourceApi<AlertManagerQuery> {
async testDatasource() {
let alertmanagerResponse;
let cortexAlertmanagerResponse;
try {
alertmanagerResponse = await this._request('/api/v2/status');
if (alertmanagerResponse && alertmanagerResponse?.status === 200) {
return {
status: 'error',
message:
'Only Cortex alert manager implementation is supported. A URL to cortex instance should be provided.',
};
}
} catch (e) {}
try {
cortexAlertmanagerResponse = await this._request('/alertmanager/api/v2/status');
} catch (e) {}
if (this.instanceSettings.jsonData.implementation === AlertManagerImplementation.prometheus) {
try {
alertmanagerResponse = await this._request('/alertmanager/api/v2/status');
if (alertmanagerResponse && alertmanagerResponse?.status === 200) {
return {
status: 'error',
message:
'It looks like you have chosen Prometheus implementation, but detected a Cortex endpoint. Please update implementation selection and try again.',
};
}
} catch (e) {}
try {
alertmanagerResponse = await this._request('/api/v2/status');
} catch (e) {}
} else {
try {
alertmanagerResponse = await this._request('/api/v2/status');
if (alertmanagerResponse && alertmanagerResponse?.status === 200) {
return {
status: 'error',
message:
'It looks like you have chosen Cortex implementation, but detected a Prometheus endpoint. Please update implementation selection and try again.',
};
}
} catch (e) {}
try {
alertmanagerResponse = await this._request('/alertmanager/api/v2/status');
} catch (e) {}
}
return cortexAlertmanagerResponse?.status === 200
return alertmanagerResponse?.status === 200
? {
status: 'success',
message: 'Health check passed.',

View File

@@ -1,6 +1,6 @@
{
"type": "datasource",
"name": "Alert Manager",
"name": "Alertmanager",
"id": "alertmanager",
"metrics": false,
"state": "alpha",

View File

@@ -1,5 +1,7 @@
//DOCS: https://prometheus.io/docs/alerting/latest/configuration/
import { DataSourceJsonData } from '@grafana/data';
export type AlertManagerCortexConfig = {
template_files: Record<string, string>;
alertmanager_config: AlertmanagerConfig;
@@ -246,3 +248,10 @@ export interface TestReceiversResult {
notified_at: string;
receivers: TestReceiversResultReceiver[];
}
export enum AlertManagerImplementation {
cortex = 'cortex',
prometheus = 'prometheus',
}
export type AlertManagerDataSourceJsonData = DataSourceJsonData & { implementation?: AlertManagerImplementation };