mirror of
https://github.com/grafana/grafana.git
synced 2025-01-01 03:37:24 -06:00
Alerting: Fix external alertmanager duplication (#49980)
* Fix external alertmanager duplication * Add tests
This commit is contained in:
parent
bdff63d4a8
commit
9da41140aa
@ -127,4 +127,41 @@ describe('useExternalAmSelector', () => {
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('The number of alert managers should match config entries when there are multiple entries of the same url', () => {
|
||||
useSelectorMock.mockImplementation((callback) => {
|
||||
return callback(
|
||||
createMockStoreState(
|
||||
[
|
||||
{ url: 'same/url/to/am/api/v2/alerts' },
|
||||
{ url: 'same/url/to/am/api/v2/alerts' },
|
||||
{ url: 'same/url/to/am/api/v2/alerts' },
|
||||
],
|
||||
[],
|
||||
['same/url/to/am', 'same/url/to/am', 'same/url/to/am']
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
const alertmanagers = useExternalAmSelector();
|
||||
|
||||
expect(alertmanagers.length).toBe(3);
|
||||
expect(alertmanagers).toEqual([
|
||||
{
|
||||
url: 'same/url/to/am',
|
||||
actualUrl: 'same/url/to/am/api/v2/alerts',
|
||||
status: 'active',
|
||||
},
|
||||
{
|
||||
url: 'same/url/to/am',
|
||||
actualUrl: 'same/url/to/am/api/v2/alerts',
|
||||
status: 'active',
|
||||
},
|
||||
{
|
||||
url: 'same/url/to/am',
|
||||
actualUrl: 'same/url/to/am/api/v2/alerts',
|
||||
status: 'active',
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
@ -18,7 +18,7 @@ export function useExternalAmSelector(): AlertmanagerConfig[] | [] {
|
||||
}
|
||||
|
||||
const enabledAlertmanagers: AlertmanagerConfig[] = [];
|
||||
const droppedAlertmanagers: AlertmanagerConfig[] = discoveredAlertmanagers?.droppedAlertManagers.map((am) => ({
|
||||
const droppedAlertmanagers: AlertmanagerConfig[] = discoveredAlertmanagers.droppedAlertManagers.map((am) => ({
|
||||
url: am.url.replace(SUFFIX_REGEX, ''),
|
||||
status: 'dropped',
|
||||
actualUrl: am.url,
|
||||
@ -32,24 +32,20 @@ export function useExternalAmSelector(): AlertmanagerConfig[] | [] {
|
||||
actualUrl: '',
|
||||
});
|
||||
} else {
|
||||
let found = false;
|
||||
for (const activeAM of discoveredAlertmanagers.activeAlertManagers) {
|
||||
if (activeAM.url === `${url}/api/v2/alerts`) {
|
||||
found = true;
|
||||
enabledAlertmanagers.push({
|
||||
url: activeAM.url.replace(SUFFIX_REGEX, ''),
|
||||
const matchingActiveAM = discoveredAlertmanagers.activeAlertManagers.find(
|
||||
(am) => am.url === `${url}/api/v2/alerts`
|
||||
);
|
||||
matchingActiveAM
|
||||
? enabledAlertmanagers.push({
|
||||
url: matchingActiveAM.url.replace(SUFFIX_REGEX, ''),
|
||||
status: 'active',
|
||||
actualUrl: activeAM.url,
|
||||
actualUrl: matchingActiveAM.url,
|
||||
})
|
||||
: enabledAlertmanagers.push({
|
||||
url: url,
|
||||
status: 'pending',
|
||||
actualUrl: '',
|
||||
});
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
enabledAlertmanagers.push({
|
||||
url: url,
|
||||
status: 'pending',
|
||||
actualUrl: '',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user