Search in sources :

Example 21 with AlertManagerConfig

use of io.hops.hopsworks.alerting.config.dto.AlertManagerConfig in project hopsworks by logicalclocks.

the class AlertManagerConfiguration method restoreFromBackup.

public void restoreFromBackup() throws AlertManagerConfigCtrlCreateException, AlertManagerConfigReadException, IOException, AlertManagerUnreachableException, AlertManagerConfigUpdateException, AlertManagerClientCreateException {
    AlertManagerConfig alertManagerConfig = read();
    Optional<AlertManagerConfigEntity> optionalAlertManagerConfigEntity = alertManagerConfigFacade.getLatest();
    ObjectMapper objectMapper = new ObjectMapper();
    JSONObject jsonAlertManagerConfig = alertManagerConfig != null ? new JSONObject(objectMapper.writeValueAsString(alertManagerConfig)) : null;
    JSONObject jsonAlertManagerConfigBackup = optionalAlertManagerConfigEntity.map(AlertManagerConfigEntity::getContent).orElse(null);
    AlertManagerConfig alertManagerConfigBackup = jsonAlertManagerConfigBackup != null ? objectMapper.readValue(jsonAlertManagerConfigBackup.toString(), AlertManagerConfig.class) : null;
    if (jsonAlertManagerConfig != null && jsonAlertManagerConfigBackup != null) {
        if (!JsonObjectHelper.similar(jsonAlertManagerConfig, jsonAlertManagerConfigBackup)) {
            boolean updated = merge(alertManagerConfig, alertManagerConfigBackup);
            if (updated) {
                writeAndReload(alertManagerConfig);
                LOGGER.log(Level.INFO, "Fixed Alert manager config from backup.");
            } else {
                // Not similar but not updated then save new backup
                saveToDatabase(jsonAlertManagerConfig);
                LOGGER.log(Level.INFO, "Alert manager config backup saved.");
            }
        } else {
            LOGGER.log(Level.INFO, "Alert manager config is up to date with backup.");
        }
    } else if (jsonAlertManagerConfig == null && jsonAlertManagerConfigBackup != null) {
        writeAndReload(alertManagerConfigBackup);
        LOGGER.log(Level.INFO, "Replace Alert manager config with backup.");
    } else if (jsonAlertManagerConfig != null) {
        saveToDatabase(jsonAlertManagerConfig);
        LOGGER.log(Level.INFO, "Alert manager config backup saved.");
    }
}
Also used : AlertManagerConfig(io.hops.hopsworks.alerting.config.dto.AlertManagerConfig) JSONObject(org.json.JSONObject) AlertManagerConfigEntity(io.hops.hopsworks.persistence.entity.alertmanager.AlertManagerConfigEntity) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 22 with AlertManagerConfig

use of io.hops.hopsworks.alerting.config.dto.AlertManagerConfig in project hopsworks by logicalclocks.

the class TestAlertManagerConfigTimer method testRestoreBackup.

@Test
public void testRestoreBackup() throws AlertManagerUnreachableException, AlertManagerConfigCtrlCreateException, AlertManagerConfigUpdateException, IOException, AlertManagerConfigReadException, AlertManagerClientCreateException {
    Mockito.doAnswer((Answer<Void>) invocation -> {
        Object[] args = invocation.getArguments();
        if (args != null && args.length > 0 && args[0] != null) {
            AlertManagerConfigEntity alertManagerConfigEntity = (AlertManagerConfigEntity) args[0];
            AlertManagerConfig alertManagerConfig = fromJson(alertManagerConfigEntity.getContent());
            LOGGER.log(Level.INFO, "Update database routes={0}, receivers={1}.", new Object[] { alertManagerConfig.getRoute().getRoutes().size(), alertManagerConfig.getReceivers().size() });
            assert alertManagerConfig.getRoute().getRoutes().size() == 15;
            assert alertManagerConfig.getReceivers().size() == 9;
        }
        return null;
    }).when(alertManagerConfigFacade).update(Mockito.any());
    alertManagerConfiguration.restoreFromBackup();
    AlertManagerConfig alertManagerConfig = alertManagerConfigController.read();
    assert alertManagerConfig.getRoute().getRoutes().size() == 15;
    assert alertManagerConfig.getReceivers().size() == 9;
}
Also used : YAMLGenerator(com.fasterxml.jackson.dataformat.yaml.YAMLGenerator) AlertType(io.hops.hopsworks.persistence.entity.alertmanager.AlertType) FeatureGroupAlert(io.hops.hopsworks.persistence.entity.featurestore.featuregroup.datavalidation.alert.FeatureGroupAlert) Date(java.util.Date) Random(java.util.Random) AlertSeverity(io.hops.hopsworks.persistence.entity.alertmanager.AlertSeverity) AlertManagerConfigController(io.hops.hopsworks.alerting.config.AlertManagerConfigController) AlertManagerClient(io.hops.hopsworks.alerting.api.AlertManagerClient) JSONObject(org.json.JSONObject) AlertManagerConfiguration(io.hops.hopsworks.alert.AlertManagerConfiguration) After(org.junit.After) JobAlert(io.hops.hopsworks.persistence.entity.jobs.description.JobAlert) AlertManagerConfigFacade(io.hops.hopsworks.alert.dao.AlertManagerConfigFacade) AlertManagerUnreachableException(io.hops.hopsworks.alert.exception.AlertManagerUnreachableException) ValidationRuleAlertStatus(io.hops.hopsworks.persistence.entity.featurestore.featuregroup.datavalidation.alert.ValidationRuleAlertStatus) SlackConfig(io.hops.hopsworks.alerting.config.dto.SlackConfig) AlertManagerConfigReadException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigReadException) Jobs(io.hops.hopsworks.persistence.entity.jobs.description.Jobs) Featuregroup(io.hops.hopsworks.persistence.entity.featurestore.featuregroup.Featuregroup) Logger(java.util.logging.Logger) ServiceDiscoveryException(com.logicalclocks.servicediscoverclient.exceptions.ServiceDiscoveryException) ProjectServiceAlert(io.hops.hopsworks.persistence.entity.project.alert.ProjectServiceAlert) List(java.util.List) AlertManagerConfigCtrlCreateException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigCtrlCreateException) JobAlertStatus(io.hops.hopsworks.persistence.entity.jobs.description.JobAlertStatus) Optional(java.util.Optional) AlertReceiver(io.hops.hopsworks.persistence.entity.alertmanager.AlertReceiver) EmailConfig(io.hops.hopsworks.alerting.config.dto.EmailConfig) ProjectServiceEnum(io.hops.hopsworks.persistence.entity.project.service.ProjectServiceEnum) Receiver(io.hops.hopsworks.alerting.config.dto.Receiver) Project(io.hops.hopsworks.persistence.entity.project.Project) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Answer(org.mockito.stubbing.Answer) AlertManagerConfigFileNotFoundException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigFileNotFoundException) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) AlertManagerConfigUpdateException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigUpdateException) PagerdutyConfig(io.hops.hopsworks.alerting.config.dto.PagerdutyConfig) Before(org.junit.Before) AlertManagerConfigEntity(io.hops.hopsworks.persistence.entity.alertmanager.AlertManagerConfigEntity) AlertManagerConfig(io.hops.hopsworks.alerting.config.dto.AlertManagerConfig) AlertManagerClientCreateException(io.hops.hopsworks.alerting.exceptions.AlertManagerClientCreateException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test) IOException(java.io.IOException) Featurestore(io.hops.hopsworks.persistence.entity.featurestore.Featurestore) ProjectServiceAlertStatus(io.hops.hopsworks.persistence.entity.project.alert.ProjectServiceAlertStatus) File(java.io.File) Mockito(org.mockito.Mockito) AlertReceiverFacade(io.hops.hopsworks.alert.dao.AlertReceiverFacade) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) AlertManagerConfig(io.hops.hopsworks.alerting.config.dto.AlertManagerConfig) JSONObject(org.json.JSONObject) AlertManagerConfigEntity(io.hops.hopsworks.persistence.entity.alertmanager.AlertManagerConfigEntity) Test(org.junit.Test)

Example 23 with AlertManagerConfig

use of io.hops.hopsworks.alerting.config.dto.AlertManagerConfig in project hopsworks by logicalclocks.

the class TestAlertManagerConfigTimer method testReceiverFix.

@Test
public void testReceiverFix() throws AlertManagerUnreachableException, AlertManagerConfigCtrlCreateException, AlertManagerConfigUpdateException, IOException, AlertManagerConfigReadException, AlertManagerClientCreateException {
    alertManagerConfiguration.runFixConfig();
    AlertManagerConfig alertManagerConfig = alertManagerConfigController.read();
    assert alertManagerConfig.getRoute().getRoutes().size() == 15;
    assert alertManagerConfig.getReceivers().size() == 6;
}
Also used : AlertManagerConfig(io.hops.hopsworks.alerting.config.dto.AlertManagerConfig) Test(org.junit.Test)

Example 24 with AlertManagerConfig

use of io.hops.hopsworks.alerting.config.dto.AlertManagerConfig in project hopsworks by logicalclocks.

the class TestAlertManagerConfigTimer method setUp.

@Before
public void setUp() throws ServiceDiscoveryException, AlertManagerConfigFileNotFoundException, AlertManagerConfigReadException, JsonProcessingException {
    String alertmanagerConfigPath = TestAlertManagerConfigTimer.class.getResource("/alertmanager.yml").getPath();
    alertManagerConfigDBPath = TestAlertManagerConfigTimer.class.getResource("/alertmanagerDB.yml").getPath();
    client = Mockito.mock(AlertManagerClient.class);
    alertManagerConfigController = new AlertManagerConfigController.Builder().withConfigPath(alertmanagerConfigPath).withClient(client).build();
    alertManagerConfigFacade = Mockito.mock(AlertManagerConfigFacade.class);
    Mockito.doAnswer((Answer<Void>) invocation -> {
        Object[] args = invocation.getArguments();
        LOGGER.log(Level.INFO, "Save to database: {0}.", args);
        return null;
    }).when(alertManagerConfigFacade).save(Mockito.any());
    Mockito.doAnswer((Answer<Void>) invocation -> {
        Object[] args = invocation.getArguments();
        LOGGER.log(Level.INFO, "Update database: {0}.", args);
        return null;
    }).when(alertManagerConfigFacade).update(Mockito.any());
    AlertManagerConfig alertManagerConfig = read(new File(alertManagerConfigDBPath));
    AlertManagerConfigEntity alertManagerConfigEntity = new AlertManagerConfigEntity();
    alertManagerConfigEntity.setId(1);
    alertManagerConfigEntity.setCreated(new Date());
    alertManagerConfigEntity.setContent(toJson(alertManagerConfig));
    Mockito.when(alertManagerConfigFacade.getLatest()).thenReturn(Optional.of(alertManagerConfigEntity));
    alertReceiverFacade = Mockito.mock(AlertReceiverFacade.class);
    List<AlertReceiver> receivers = createReceivers();
    Mockito.when(alertReceiverFacade.findAll()).thenReturn(receivers);
    Mockito.doAnswer((Answer<Optional<AlertReceiver>>) invocation -> {
        Object[] args = invocation.getArguments();
        Optional<AlertReceiver> alertReceiver = receivers.stream().filter(receiver -> receiver.getName().equals(args[0])).findFirst();
        return alertReceiver;
    }).when(alertReceiverFacade).findByName(Mockito.any());
    alertManagerConfiguration = new AlertManagerConfiguration(client, alertManagerConfigController, alertManagerConfigFacade, alertReceiverFacade);
    alertManagerConfigBackup = alertManagerConfigController.read();
    alertManagerConfigBackupDB = read(new File(alertManagerConfigDBPath));
}
Also used : YAMLGenerator(com.fasterxml.jackson.dataformat.yaml.YAMLGenerator) AlertType(io.hops.hopsworks.persistence.entity.alertmanager.AlertType) FeatureGroupAlert(io.hops.hopsworks.persistence.entity.featurestore.featuregroup.datavalidation.alert.FeatureGroupAlert) Date(java.util.Date) Random(java.util.Random) AlertSeverity(io.hops.hopsworks.persistence.entity.alertmanager.AlertSeverity) AlertManagerConfigController(io.hops.hopsworks.alerting.config.AlertManagerConfigController) AlertManagerClient(io.hops.hopsworks.alerting.api.AlertManagerClient) JSONObject(org.json.JSONObject) AlertManagerConfiguration(io.hops.hopsworks.alert.AlertManagerConfiguration) After(org.junit.After) JobAlert(io.hops.hopsworks.persistence.entity.jobs.description.JobAlert) AlertManagerConfigFacade(io.hops.hopsworks.alert.dao.AlertManagerConfigFacade) AlertManagerUnreachableException(io.hops.hopsworks.alert.exception.AlertManagerUnreachableException) ValidationRuleAlertStatus(io.hops.hopsworks.persistence.entity.featurestore.featuregroup.datavalidation.alert.ValidationRuleAlertStatus) SlackConfig(io.hops.hopsworks.alerting.config.dto.SlackConfig) AlertManagerConfigReadException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigReadException) Jobs(io.hops.hopsworks.persistence.entity.jobs.description.Jobs) Featuregroup(io.hops.hopsworks.persistence.entity.featurestore.featuregroup.Featuregroup) Logger(java.util.logging.Logger) ServiceDiscoveryException(com.logicalclocks.servicediscoverclient.exceptions.ServiceDiscoveryException) ProjectServiceAlert(io.hops.hopsworks.persistence.entity.project.alert.ProjectServiceAlert) List(java.util.List) AlertManagerConfigCtrlCreateException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigCtrlCreateException) JobAlertStatus(io.hops.hopsworks.persistence.entity.jobs.description.JobAlertStatus) Optional(java.util.Optional) AlertReceiver(io.hops.hopsworks.persistence.entity.alertmanager.AlertReceiver) EmailConfig(io.hops.hopsworks.alerting.config.dto.EmailConfig) ProjectServiceEnum(io.hops.hopsworks.persistence.entity.project.service.ProjectServiceEnum) Receiver(io.hops.hopsworks.alerting.config.dto.Receiver) Project(io.hops.hopsworks.persistence.entity.project.Project) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Answer(org.mockito.stubbing.Answer) AlertManagerConfigFileNotFoundException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigFileNotFoundException) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) AlertManagerConfigUpdateException(io.hops.hopsworks.alerting.exceptions.AlertManagerConfigUpdateException) PagerdutyConfig(io.hops.hopsworks.alerting.config.dto.PagerdutyConfig) Before(org.junit.Before) AlertManagerConfigEntity(io.hops.hopsworks.persistence.entity.alertmanager.AlertManagerConfigEntity) AlertManagerConfig(io.hops.hopsworks.alerting.config.dto.AlertManagerConfig) AlertManagerClientCreateException(io.hops.hopsworks.alerting.exceptions.AlertManagerClientCreateException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test) IOException(java.io.IOException) Featurestore(io.hops.hopsworks.persistence.entity.featurestore.Featurestore) ProjectServiceAlertStatus(io.hops.hopsworks.persistence.entity.project.alert.ProjectServiceAlertStatus) File(java.io.File) Mockito(org.mockito.Mockito) AlertReceiverFacade(io.hops.hopsworks.alert.dao.AlertReceiverFacade) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) AlertManagerConfig(io.hops.hopsworks.alerting.config.dto.AlertManagerConfig) Optional(java.util.Optional) AlertReceiver(io.hops.hopsworks.persistence.entity.alertmanager.AlertReceiver) AlertManagerConfigFacade(io.hops.hopsworks.alert.dao.AlertManagerConfigFacade) Date(java.util.Date) AlertManagerClient(io.hops.hopsworks.alerting.api.AlertManagerClient) AlertReceiverFacade(io.hops.hopsworks.alert.dao.AlertReceiverFacade) File(java.io.File) AlertManagerConfigEntity(io.hops.hopsworks.persistence.entity.alertmanager.AlertManagerConfigEntity) AlertManagerConfiguration(io.hops.hopsworks.alert.AlertManagerConfiguration) Before(org.junit.Before)

Example 25 with AlertManagerConfig

use of io.hops.hopsworks.alerting.config.dto.AlertManagerConfig in project hopsworks by logicalclocks.

the class TestAlertManagerConfigTimer method testGlobalFromBackup.

@Test
public void testGlobalFromBackup() throws AlertManagerConfigReadException, IOException, AlertManagerUnreachableException, AlertManagerConfigCtrlCreateException, AlertManagerConfigUpdateException, AlertManagerClientCreateException {
    AlertManagerConfig alertManagerConfig = read(new File(alertManagerConfigDBPath));
    alertManagerConfig.getGlobal().setSlackApiUrl("https://hooks.slack.com/services/1234567/ASEDWRFDE/XXXXXXXXXXXXXX");
    AlertManagerConfigEntity alertManagerConfigEntity = new AlertManagerConfigEntity();
    alertManagerConfigEntity.setId(1);
    alertManagerConfigEntity.setCreated(new Date());
    alertManagerConfigEntity.setContent(toJson(alertManagerConfig));
    Mockito.when(alertManagerConfigFacade.getLatest()).thenReturn(Optional.of(alertManagerConfigEntity));
    alertManagerConfiguration.restoreFromBackup();
    AlertManagerConfig alertManagerConfig1 = alertManagerConfigController.read();
    assert alertManagerConfig1.getGlobal().getSlackApiUrl().equals("https://hooks.slack.com/services/1234567/ASEDWRFDE/XXXXXXXXXXXXXX");
}
Also used : AlertManagerConfig(io.hops.hopsworks.alerting.config.dto.AlertManagerConfig) File(java.io.File) AlertManagerConfigEntity(io.hops.hopsworks.persistence.entity.alertmanager.AlertManagerConfigEntity) Date(java.util.Date) Test(org.junit.Test)

Aggregations

AlertManagerConfig (io.hops.hopsworks.alerting.config.dto.AlertManagerConfig)53 Receiver (io.hops.hopsworks.alerting.config.dto.Receiver)22 Test (org.junit.Test)19 EmailConfig (io.hops.hopsworks.alerting.config.dto.EmailConfig)8 ArrayList (java.util.ArrayList)8 AlertManagerDuplicateEntryException (io.hops.hopsworks.alerting.exceptions.AlertManagerDuplicateEntryException)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 Route (io.hops.hopsworks.alerting.config.dto.Route)5 SlackConfig (io.hops.hopsworks.alerting.config.dto.SlackConfig)5 PagerdutyConfig (io.hops.hopsworks.alerting.config.dto.PagerdutyConfig)4 AlertManagerConfigUpdateException (io.hops.hopsworks.alerting.exceptions.AlertManagerConfigUpdateException)4 AlertManagerConfigEntity (io.hops.hopsworks.persistence.entity.alertmanager.AlertManagerConfigEntity)4 AlertReceiver (io.hops.hopsworks.persistence.entity.alertmanager.AlertReceiver)4 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)3 AlertManagerUnreachableException (io.hops.hopsworks.alert.exception.AlertManagerUnreachableException)3 AlertManagerClientCreateException (io.hops.hopsworks.alerting.exceptions.AlertManagerClientCreateException)3 AlertManagerConfigCtrlCreateException (io.hops.hopsworks.alerting.exceptions.AlertManagerConfigCtrlCreateException)3 AlertManagerConfigReadException (io.hops.hopsworks.alerting.exceptions.AlertManagerConfigReadException)3 File (java.io.File)3 Date (java.util.Date)3