2021-04-15 06:53:40 -05:00
|
|
|
import { DataSourceApi, DataSourceInstanceSettings, DataSourcePluginMeta, ScopedVars } from '@grafana/data';
|
2021-08-10 02:59:48 -05:00
|
|
|
import {
|
|
|
|
GrafanaAlertStateDecision,
|
|
|
|
GrafanaRuleDefinition,
|
|
|
|
PromAlertingRuleState,
|
|
|
|
PromRuleType,
|
|
|
|
RulerGrafanaRuleDTO,
|
|
|
|
} from 'app/types/unified-alerting-dto';
|
2021-04-07 00:42:43 -05:00
|
|
|
import { AlertingRule, Alert, RecordingRule, RuleGroup, RuleNamespace } from 'app/types/unified-alerting';
|
2021-04-15 06:53:40 -05:00
|
|
|
import DatasourceSrv from 'app/features/plugins/datasource_srv';
|
|
|
|
import { DataSourceSrv, GetDataSourceListFilters } from '@grafana/runtime';
|
2021-06-23 11:32:42 -05:00
|
|
|
import {
|
2021-07-07 18:17:26 -05:00
|
|
|
AlertmanagerAlert,
|
2021-06-23 11:32:42 -05:00
|
|
|
AlertManagerCortexConfig,
|
2021-07-07 18:17:26 -05:00
|
|
|
AlertmanagerGroup,
|
2021-06-23 11:32:42 -05:00
|
|
|
AlertmanagerStatus,
|
2021-07-07 18:17:26 -05:00
|
|
|
AlertState,
|
2021-06-23 11:32:42 -05:00
|
|
|
GrafanaManagedReceiverConfig,
|
|
|
|
} from 'app/plugins/datasource/alertmanager/types';
|
2021-04-07 00:42:43 -05:00
|
|
|
|
|
|
|
let nextDataSourceId = 1;
|
|
|
|
|
2021-07-12 16:10:13 -05:00
|
|
|
export const mockDataSource = (
|
|
|
|
partial: Partial<DataSourceInstanceSettings> = {},
|
|
|
|
meta: Partial<DataSourcePluginMeta> = {}
|
2021-08-10 02:59:48 -05:00
|
|
|
): DataSourceInstanceSettings<any> => {
|
2021-04-07 00:42:43 -05:00
|
|
|
const id = partial.id ?? nextDataSourceId++;
|
|
|
|
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
uid: `mock-ds-${nextDataSourceId}`,
|
|
|
|
type: 'prometheus',
|
|
|
|
name: `Prometheus-${id}`,
|
2021-08-06 02:24:35 -05:00
|
|
|
access: 'proxy',
|
2021-04-07 00:42:43 -05:00
|
|
|
jsonData: {},
|
|
|
|
meta: ({
|
|
|
|
info: {
|
|
|
|
logos: {
|
|
|
|
small: 'https://prometheus.io/assets/prometheus_logo_grey.svg',
|
|
|
|
large: 'https://prometheus.io/assets/prometheus_logo_grey.svg',
|
|
|
|
},
|
|
|
|
},
|
2021-07-12 16:10:13 -05:00
|
|
|
...meta,
|
2021-04-07 00:42:43 -05:00
|
|
|
} as any) as DataSourcePluginMeta,
|
|
|
|
...partial,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const mockPromAlert = (partial: Partial<Alert> = {}): Alert => ({
|
|
|
|
activeAt: '2021-03-18T13:47:05.04938691Z',
|
|
|
|
annotations: {
|
|
|
|
message: 'alert with severity "warning"',
|
|
|
|
},
|
|
|
|
labels: {
|
|
|
|
alertname: 'myalert',
|
|
|
|
severity: 'warning',
|
|
|
|
},
|
|
|
|
state: PromAlertingRuleState.Firing,
|
|
|
|
value: '1e+00',
|
|
|
|
...partial,
|
|
|
|
});
|
|
|
|
|
2021-08-10 02:59:48 -05:00
|
|
|
export const mockRulerGrafanaRule = (
|
|
|
|
partial: Partial<RulerGrafanaRuleDTO> = {},
|
|
|
|
partialDef: Partial<GrafanaRuleDefinition> = {}
|
|
|
|
): RulerGrafanaRuleDTO => {
|
|
|
|
return {
|
|
|
|
for: '1m',
|
|
|
|
grafana_alert: {
|
|
|
|
uid: '123',
|
|
|
|
title: 'myalert',
|
|
|
|
namespace_uid: '123',
|
|
|
|
namespace_id: 1,
|
|
|
|
condition: 'A',
|
|
|
|
no_data_state: GrafanaAlertStateDecision.Alerting,
|
|
|
|
exec_err_state: GrafanaAlertStateDecision.Alerting,
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
datasourceUid: '123',
|
|
|
|
refId: 'A',
|
|
|
|
queryType: 'huh',
|
|
|
|
model: {} as any,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
...partialDef,
|
|
|
|
},
|
|
|
|
annotations: {
|
|
|
|
message: 'alert with severity "{{.warning}}}"',
|
|
|
|
},
|
|
|
|
labels: {
|
|
|
|
severity: 'warning',
|
|
|
|
},
|
|
|
|
...partial,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-04-07 00:42:43 -05:00
|
|
|
export const mockPromAlertingRule = (partial: Partial<AlertingRule> = {}): AlertingRule => {
|
|
|
|
return {
|
|
|
|
type: PromRuleType.Alerting,
|
|
|
|
alerts: [mockPromAlert()],
|
|
|
|
name: 'myalert',
|
|
|
|
query: 'foo > 1',
|
|
|
|
lastEvaluation: '2021-03-23T08:19:05.049595312Z',
|
|
|
|
evaluationTime: 0.000395601,
|
|
|
|
annotations: {
|
|
|
|
message: 'alert with severity "{{.warning}}}"',
|
|
|
|
},
|
|
|
|
labels: {
|
|
|
|
severity: 'warning',
|
|
|
|
},
|
|
|
|
state: PromAlertingRuleState.Firing,
|
|
|
|
health: 'OK',
|
|
|
|
...partial,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const mockPromRecordingRule = (partial: Partial<RecordingRule> = {}): RecordingRule => {
|
|
|
|
return {
|
|
|
|
type: PromRuleType.Recording,
|
|
|
|
query: 'bar < 3',
|
|
|
|
labels: {
|
|
|
|
cluster: 'eu-central',
|
|
|
|
},
|
|
|
|
health: 'OK',
|
|
|
|
name: 'myrecordingrule',
|
|
|
|
lastEvaluation: '2021-03-23T08:19:05.049595312Z',
|
|
|
|
evaluationTime: 0.000395601,
|
|
|
|
...partial,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const mockPromRuleGroup = (partial: Partial<RuleGroup> = {}): RuleGroup => {
|
|
|
|
return {
|
|
|
|
name: 'mygroup',
|
|
|
|
interval: 60,
|
|
|
|
rules: [mockPromAlertingRule()],
|
|
|
|
...partial,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const mockPromRuleNamespace = (partial: Partial<RuleNamespace> = {}): RuleNamespace => {
|
|
|
|
return {
|
|
|
|
dataSourceName: 'Prometheus-1',
|
|
|
|
name: 'default',
|
|
|
|
groups: [mockPromRuleGroup()],
|
|
|
|
...partial,
|
|
|
|
};
|
|
|
|
};
|
2021-04-15 06:53:40 -05:00
|
|
|
|
2021-07-07 18:17:26 -05:00
|
|
|
export const mockAlertmanagerAlert = (partial: Partial<AlertmanagerAlert> = {}): AlertmanagerAlert => {
|
|
|
|
return {
|
|
|
|
annotations: {
|
|
|
|
summary: 'US-Central region is on fire',
|
|
|
|
},
|
|
|
|
endsAt: '2021-06-22T21:49:28.562Z',
|
|
|
|
fingerprint: '88e013643c3df34ac3',
|
|
|
|
receivers: [{ name: 'pagerduty' }],
|
|
|
|
startsAt: '2021-06-21T17:25:28.562Z',
|
|
|
|
status: { inhibitedBy: [], silencedBy: [], state: AlertState.Active },
|
|
|
|
updatedAt: '2021-06-22T21:45:28.564Z',
|
|
|
|
generatorURL: 'https://play.grafana.com/explore',
|
|
|
|
labels: { severity: 'warning', region: 'US-Central' },
|
|
|
|
...partial,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const mockAlertGroup = (partial: Partial<AlertmanagerGroup> = {}): AlertmanagerGroup => {
|
|
|
|
return {
|
|
|
|
labels: {
|
|
|
|
severity: 'warning',
|
|
|
|
region: 'US-Central',
|
|
|
|
},
|
|
|
|
receiver: {
|
|
|
|
name: 'pagerduty',
|
|
|
|
},
|
|
|
|
alerts: [
|
|
|
|
mockAlertmanagerAlert(),
|
|
|
|
mockAlertmanagerAlert({
|
|
|
|
status: { state: AlertState.Suppressed, silencedBy: ['123456abcdef'], inhibitedBy: [] },
|
2021-08-19 11:22:52 -05:00
|
|
|
labels: { severity: 'warning', region: 'US-Central', foo: 'bar', ...partial.labels },
|
2021-07-07 18:17:26 -05:00
|
|
|
}),
|
|
|
|
],
|
|
|
|
...partial,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-04-15 06:53:40 -05:00
|
|
|
export class MockDataSourceSrv implements DataSourceSrv {
|
2021-07-12 16:10:13 -05:00
|
|
|
datasources: Record<string, DataSourceApi> = {};
|
2021-04-15 06:53:40 -05:00
|
|
|
// @ts-ignore
|
|
|
|
private settingsMapByName: Record<string, DataSourceInstanceSettings> = {};
|
|
|
|
private settingsMapByUid: Record<string, DataSourceInstanceSettings> = {};
|
|
|
|
private settingsMapById: Record<string, DataSourceInstanceSettings> = {};
|
|
|
|
// @ts-ignore
|
|
|
|
private templateSrv = {
|
|
|
|
getVariables: () => [],
|
|
|
|
replace: (name: any) => name,
|
|
|
|
};
|
2021-07-12 16:10:13 -05:00
|
|
|
defaultName = '';
|
2021-04-15 06:53:40 -05:00
|
|
|
|
|
|
|
constructor(datasources: Record<string, DataSourceInstanceSettings>) {
|
2021-07-12 16:10:13 -05:00
|
|
|
this.datasources = {};
|
2021-04-15 06:53:40 -05:00
|
|
|
this.settingsMapByName = Object.values(datasources).reduce<Record<string, DataSourceInstanceSettings>>(
|
|
|
|
(acc, ds) => {
|
|
|
|
acc[ds.name] = ds;
|
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
for (const dsSettings of Object.values(this.settingsMapByName)) {
|
|
|
|
this.settingsMapByUid[dsSettings.uid] = dsSettings;
|
|
|
|
this.settingsMapById[dsSettings.id] = dsSettings;
|
2021-07-12 16:10:13 -05:00
|
|
|
if (dsSettings.isDefault) {
|
|
|
|
this.defaultName = dsSettings.name;
|
|
|
|
}
|
2021-04-15 06:53:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get(name?: string | null, scopedVars?: ScopedVars): Promise<DataSourceApi> {
|
2021-07-12 16:10:13 -05:00
|
|
|
return DatasourceSrv.prototype.get.call(this, name, scopedVars);
|
|
|
|
//return Promise.reject(new Error('not implemented'));
|
2021-04-15 06:53:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of data sources
|
|
|
|
*/
|
|
|
|
getList(filters?: GetDataSourceListFilters): DataSourceInstanceSettings[] {
|
|
|
|
return DatasourceSrv.prototype.getList.call(this, filters);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get settings and plugin metadata by name or uid
|
|
|
|
*/
|
|
|
|
getInstanceSettings(nameOrUid: string | null | undefined): DataSourceInstanceSettings | undefined {
|
2021-04-16 03:08:26 -05:00
|
|
|
return (
|
|
|
|
DatasourceSrv.prototype.getInstanceSettings.call(this, nameOrUid) ||
|
|
|
|
(({ meta: { info: { logos: {} } } } as unknown) as DataSourceInstanceSettings)
|
|
|
|
);
|
2021-04-15 06:53:40 -05:00
|
|
|
}
|
2021-07-12 16:10:13 -05:00
|
|
|
|
|
|
|
async loadDatasource(name: string): Promise<DataSourceApi<any, any>> {
|
|
|
|
return DatasourceSrv.prototype.loadDatasource.call(this, name);
|
|
|
|
}
|
2021-04-15 06:53:40 -05:00
|
|
|
}
|
2021-05-06 04:32:45 -05:00
|
|
|
|
|
|
|
export const mockGrafanaReceiver = (
|
|
|
|
type: string,
|
|
|
|
overrides: Partial<GrafanaManagedReceiverConfig> = {}
|
|
|
|
): GrafanaManagedReceiverConfig => ({
|
|
|
|
type: type,
|
|
|
|
name: type,
|
|
|
|
disableResolveMessage: false,
|
|
|
|
settings: {},
|
|
|
|
...overrides,
|
|
|
|
});
|
|
|
|
|
|
|
|
export const someGrafanaAlertManagerConfig: AlertManagerCortexConfig = {
|
|
|
|
template_files: {
|
|
|
|
'first template': 'first template content',
|
|
|
|
'second template': 'second template content',
|
|
|
|
'third template': 'third template',
|
|
|
|
},
|
|
|
|
alertmanager_config: {
|
|
|
|
route: {
|
|
|
|
receiver: 'default',
|
|
|
|
},
|
|
|
|
receivers: [
|
|
|
|
{
|
|
|
|
name: 'default',
|
|
|
|
grafana_managed_receiver_configs: [mockGrafanaReceiver('email')],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'critical',
|
|
|
|
grafana_managed_receiver_configs: [mockGrafanaReceiver('slack'), mockGrafanaReceiver('pagerduty')],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-06-23 11:32:42 -05:00
|
|
|
export const someCloudAlertManagerStatus: AlertmanagerStatus = {
|
|
|
|
cluster: {
|
|
|
|
peers: [],
|
|
|
|
status: 'ok',
|
|
|
|
},
|
|
|
|
uptime: '10 hours',
|
|
|
|
versionInfo: {
|
|
|
|
branch: '',
|
|
|
|
version: '',
|
|
|
|
goVersion: '',
|
|
|
|
buildDate: '',
|
|
|
|
buildUser: '',
|
|
|
|
revision: '',
|
|
|
|
},
|
|
|
|
config: {
|
|
|
|
route: {
|
|
|
|
receiver: 'default-email',
|
|
|
|
},
|
|
|
|
receivers: [
|
|
|
|
{
|
|
|
|
name: 'default-email',
|
|
|
|
email_configs: [
|
|
|
|
{
|
|
|
|
to: 'example@example.com',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-05-06 04:32:45 -05:00
|
|
|
export const someCloudAlertManagerConfig: AlertManagerCortexConfig = {
|
|
|
|
template_files: {
|
|
|
|
'foo template': 'foo content',
|
|
|
|
},
|
|
|
|
alertmanager_config: {
|
|
|
|
route: {
|
|
|
|
receiver: 'cloud-receiver',
|
|
|
|
},
|
|
|
|
receivers: [
|
|
|
|
{
|
|
|
|
name: 'cloud-receiver',
|
|
|
|
email_configs: [
|
|
|
|
{
|
|
|
|
to: 'domas.lapinskas@grafana.com',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
slack_configs: [
|
|
|
|
{
|
|
|
|
api_url: 'http://slack1',
|
|
|
|
channel: '#mychannel',
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
text: 'action1text',
|
|
|
|
type: 'action1type',
|
|
|
|
url: 'http://action1',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
fields: [
|
|
|
|
{
|
|
|
|
title: 'field1',
|
|
|
|
value: 'text1',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'field2',
|
|
|
|
value: 'text2',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|