mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -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 enabledAlertmanagers: AlertmanagerConfig[] = [];
|
||||||
const droppedAlertmanagers: AlertmanagerConfig[] = discoveredAlertmanagers?.droppedAlertManagers.map((am) => ({
|
const droppedAlertmanagers: AlertmanagerConfig[] = discoveredAlertmanagers.droppedAlertManagers.map((am) => ({
|
||||||
url: am.url.replace(SUFFIX_REGEX, ''),
|
url: am.url.replace(SUFFIX_REGEX, ''),
|
||||||
status: 'dropped',
|
status: 'dropped',
|
||||||
actualUrl: am.url,
|
actualUrl: am.url,
|
||||||
@ -32,26 +32,22 @@ export function useExternalAmSelector(): AlertmanagerConfig[] | [] {
|
|||||||
actualUrl: '',
|
actualUrl: '',
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
let found = false;
|
const matchingActiveAM = discoveredAlertmanagers.activeAlertManagers.find(
|
||||||
for (const activeAM of discoveredAlertmanagers.activeAlertManagers) {
|
(am) => am.url === `${url}/api/v2/alerts`
|
||||||
if (activeAM.url === `${url}/api/v2/alerts`) {
|
);
|
||||||
found = true;
|
matchingActiveAM
|
||||||
enabledAlertmanagers.push({
|
? enabledAlertmanagers.push({
|
||||||
url: activeAM.url.replace(SUFFIX_REGEX, ''),
|
url: matchingActiveAM.url.replace(SUFFIX_REGEX, ''),
|
||||||
status: 'active',
|
status: 'active',
|
||||||
actualUrl: activeAM.url,
|
actualUrl: matchingActiveAM.url,
|
||||||
});
|
})
|
||||||
}
|
: enabledAlertmanagers.push({
|
||||||
}
|
|
||||||
if (!found) {
|
|
||||||
enabledAlertmanagers.push({
|
|
||||||
url: url,
|
url: url,
|
||||||
status: 'pending',
|
status: 'pending',
|
||||||
actualUrl: '',
|
actualUrl: '',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return [...enabledAlertmanagers, ...droppedAlertmanagers];
|
return [...enabledAlertmanagers, ...droppedAlertmanagers];
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user