Alerting: Fix slack double pound and email summary (#82333)

This commit is contained in:
Gilles De Mey 2024-02-14 13:12:04 +01:00 committed by GitHub
parent 2938f891dd
commit 70aa8fe6b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 176 additions and 11 deletions

View File

@ -0,0 +1,157 @@
import { GrafanaManagedContactPoint } from 'app/plugins/datasource/alertmanager/types';
import { ReceiverTypes } from '../receivers/grafanaAppReceivers/onCall/onCall';
import { RECEIVER_META_KEY, RECEIVER_PLUGIN_META_KEY } from './useContactPoints';
import {
ReceiverConfigWithMetadata,
getReceiverDescription,
isAutoGeneratedPolicy,
isProvisioned,
summarizeEmailAddresses,
} from './utils';
describe('isProvisioned', () => {
it('should return true when at least one receiver is provisioned', () => {
const contactPoint: GrafanaManagedContactPoint = {
name: 'my-contact-point',
grafana_managed_receiver_configs: [
{ name: 'email', provenance: 'api', type: 'email', disableResolveMessage: false, settings: {} },
],
};
expect(isProvisioned(contactPoint)).toBe(true);
});
it('should return false when no receiver was provisioned', () => {
const contactPoint: GrafanaManagedContactPoint = {
name: 'my-contact-point',
grafana_managed_receiver_configs: [
{ name: 'email', provenance: undefined, type: 'email', disableResolveMessage: false, settings: {} },
],
};
expect(isProvisioned(contactPoint)).toBe(false);
});
});
describe('isAutoGeneratedPolicy', () => {
it('should return false when not enabled', () => {
expect(isAutoGeneratedPolicy({})).toBe(false);
});
});
describe('getReceiverDescription', () => {
it('should show multiple email addresses', () => {
const receiver: ReceiverConfigWithMetadata = {
name: 'email',
provenance: undefined,
type: 'email',
disableResolveMessage: false,
settings: { addresses: 'test1@test.com,test2@test.com,test3@test.com,test4@test.com' },
[RECEIVER_META_KEY]: {
name: 'Email',
description: 'The email receiver',
},
};
expect(getReceiverDescription(receiver)).toBe('test1@test.com, test2@test.com, test3@test.com, +1 more');
});
it('should work for Slack', () => {
const output = '#channel';
const receiver1: ReceiverConfigWithMetadata = {
name: 'slack',
provenance: undefined,
type: 'slack',
disableResolveMessage: false,
settings: { recipient: '#channel' },
[RECEIVER_META_KEY]: {
name: 'Slack',
description: 'The Slack receiver',
},
};
const receiver2: ReceiverConfigWithMetadata = {
name: 'slack',
provenance: undefined,
type: 'slack',
disableResolveMessage: false,
settings: { recipient: 'channel' },
[RECEIVER_META_KEY]: {
name: 'Slack',
description: 'The Slack receiver',
},
};
expect(getReceiverDescription(receiver1)).toBe(output);
expect(getReceiverDescription(receiver2)).toBe(output);
});
it('should work for OnCall', () => {
const output = 'The OnCall receiver';
const input: ReceiverConfigWithMetadata = {
name: 'my oncall',
provenance: undefined,
type: ReceiverTypes.OnCall,
disableResolveMessage: false,
settings: {},
[RECEIVER_PLUGIN_META_KEY]: {
description: output,
icon: '',
title: '',
},
[RECEIVER_META_KEY]: {
name: '',
},
};
expect(getReceiverDescription(input)).toBe(output);
});
it('should work for any type', () => {
const output = 'Some description of the receiver';
const input: ReceiverConfigWithMetadata = {
name: 'some receiver',
provenance: undefined,
type: 'some',
disableResolveMessage: false,
settings: {},
[RECEIVER_META_KEY]: {
name: 'Some Receiver',
description: output,
},
};
expect(getReceiverDescription(input)).toBe(output);
});
it('should work for any type with no description', () => {
const input: ReceiverConfigWithMetadata = {
name: 'some receiver',
provenance: undefined,
type: 'some',
disableResolveMessage: false,
settings: {},
[RECEIVER_META_KEY]: {
name: 'Some Receiver',
},
};
expect(getReceiverDescription(input)).toBe(undefined);
});
});
describe('summarizeEmailAddresses', () => {
it('should work with one email address', () => {
expect(summarizeEmailAddresses('test@test.com')).toBe('test@test.com');
});
it('should work with multiple types of separators', () => {
const output = 'foo@foo.com, bar@bar.com';
expect(summarizeEmailAddresses('foo@foo.com, bar@bar.com')).toBe(output);
expect(summarizeEmailAddresses(' foo@foo.com; bar@bar.com')).toBe(output);
expect(summarizeEmailAddresses('foo@foo.com\n bar@bar.com ')).toBe(output);
});
});

View File

@ -1,4 +1,4 @@
import { countBy, split, trim, upperFirst } from 'lodash';
import { countBy, difference, take, trim, upperFirst } from 'lodash';
import { ReactNode } from 'react';
import { config } from '@grafana/runtime';
@ -40,15 +40,21 @@ export function getReceiverDescription(receiver: ReceiverConfigWithMetadata): Re
return hasEmailAddresses ? summarizeEmailAddresses(receiver.settings['addresses']) : undefined;
}
case 'slack': {
const channelName = receiver.settings['recipient'];
return channelName ? `#${channelName}` : undefined;
const recipient: string | undefined = receiver.settings['recipient'];
if (!recipient) {
return;
}
// Slack channel name might have a "#" in the recipient already
const channelName = recipient.replace(/^#/, '');
return `#${channelName}`;
}
case 'kafka': {
const topicName = receiver.settings['kafkaTopic'];
const topicName: string | undefined = receiver.settings['kafkaTopic'];
return topicName;
}
case 'webhook': {
const url = receiver.settings['url'];
const url: string | undefined = receiver.settings['url'];
return url;
}
case ReceiverTypes.OnCall: {
@ -61,20 +67,22 @@ export function getReceiverDescription(receiver: ReceiverConfigWithMetadata): Re
// input: foo+1@bar.com, foo+2@bar.com, foo+3@bar.com, foo+4@bar.com
// output: foo+1@bar.com, foo+2@bar.com, +2 more
function summarizeEmailAddresses(addresses: string): string {
export function summarizeEmailAddresses(addresses: string): string {
const MAX_ADDRESSES_SHOWN = 3;
const SUPPORTED_SEPARATORS = /,|;|\n+/g;
// split all email addresses
const emails = addresses.trim().split(SUPPORTED_SEPARATORS).map(trim);
const notShown = emails.length - MAX_ADDRESSES_SHOWN;
// grab the first 3 and the rest
const summary = take(emails, MAX_ADDRESSES_SHOWN);
const rest = difference(emails, summary);
const truncatedAddresses = split(addresses, SUPPORTED_SEPARATORS, MAX_ADDRESSES_SHOWN);
if (notShown > 0) {
truncatedAddresses.push(`+${notShown} more`);
if (rest.length) {
summary.push(`+${rest.length} more`);
}
return truncatedAddresses.join(', ');
return summary.join(', ');
}
// Grafana Managed contact points have receivers with additional diagnostics