DefaultAlertService extends DefaultJPAService implements AlertService { @Override @Transactional public Integer executeScheduledAlerts(int alertCount, int timeout) { requireNotDisposed(); requireArgument(alertCount > 0, "Alert count must be greater than zero."); requireArgument(timeout > 0, "Timeout in milliseconds must be greater than zero."); List<History> historyList = new ArrayList<>(); List<AlertWithTimestamp> alertsWithTimestamp = _mqService.dequeue(ALERT.getQueueName(), AlertWithTimestamp.class, timeout, alertCount); List<Notification> allNotifications = new ArrayList<>(); Map<BigInteger, Alert> alertsByNotificationId = new HashMap<>(); Map<BigInteger, Long> alertEnqueueTimestampsByAlertId = new HashMap<>(); loadWhiteListRegexPatterns(); _monitorService.modifyCounter(Counter.ALERTS_EVALUATED_RAWTOTAL, alertsWithTimestamp.size(), new HashMap<>()); for (AlertWithTimestamp alertWithTimestamp : alertsWithTimestamp) { String serializedAlert = alertWithTimestamp.getSerializedAlert(); _logger.debug(MessageFormat.format("serializedAlert {0}", serializedAlert)); Alert alert; try { alert = _mapper.readValue(serializedAlert, Alert.class); } catch (Exception e) { String logMessage = MessageFormat.format("Failed to deserialize alert {0}. Full stack trace of exception {1}", serializedAlert, ExceptionUtils.getFullStackTrace(e)); _logger.warn(logMessage); logAlertStatsOnFailure(DEFAULTALERTID, DEFAULTUSER); continue; } if (!_shouldEvaluateAlert(alert, alert.getId())) { logAlertStatsOnFailure(alert.getId(), alert.getOwner().getUserName()); continue; } if (alertEnqueueTimestampsByAlertId.containsKey(alert.getId())) { String logMessage = MessageFormat.format("Found alert {0}:{1} with multiple timestamps. ExistingTime:{2} NewTime:{3}. Existing evaluation will be overwritten.", alert.getId(), alert.getName(), alertEnqueueTimestampsByAlertId.get(alert.getId()), alertWithTimestamp.getAlertEnqueueTime()); _logger.warn(logMessage); logAlertStatsOnFailure(alert.getId(), alert.getOwner().getUserName()); } alertEnqueueTimestampsByAlertId.put(alert.getId(), alertWithTimestamp.getAlertEnqueueTime()); List<Notification> notifications = new ArrayList<>(alert.getNotifications()); alert.setNotifications(null); if (notifications.size() == 0) { String logMessage = MessageFormat.format("Found alert {0}:{1} with no notification.", alert.getId(), alert.getName()); _logger.warn(logMessage); logAlertStatsOnFailure(alert.getId(), alert.getOwner().getUserName()); continue; } for (Notification n : notifications) { if (alertsByNotificationId.containsKey(n.getId())) { String logMessage = MessageFormat.format("Found alert {0}:{1} where notification {2} is present multiple times. ", alert.getId(), alert.getName(), n.getId()); _logger.warn(logMessage); } alertsByNotificationId.put(n.getId(), alert); } allNotifications.addAll(notifications); } updateNotificationsActiveStatusAndCooldown(allNotifications); for (Notification n : allNotifications) { alertsByNotificationId.get(n.getId()).addNotification(n); } Set<Alert> alerts = new HashSet<>(alertsByNotificationId.values()); long jobStartTime, evaluateEndTime; Long alertEnqueueTimestamp; String logMessage; History history; NotificationProcessor np = new NotificationProcessor(this, _logger); _monitorService.modifyCounter(Counter.ALERTS_EVALUATED_TOTAL, alerts.size(), new HashMap<>()); boolean datalagMonitorEnabled = Boolean.valueOf(_configuration.getValue(DataLagService.Property.DATA_LAG_MONITOR_ENABLED.getName(), DataLagService.Property.DATA_LAG_MONITOR_ENABLED.getDefaultValue())); AtomicInteger numberOfAlertsEvaluated = new AtomicInteger(alerts.size()); for (Alert alert : alerts) { jobStartTime = System.currentTimeMillis(); evaluateEndTime = 0; alertEnqueueTimestamp = alertEnqueueTimestampsByAlertId.get(alert.getId()); updateRequestContext(alert); updateAlertStartEvaluationStats(alertEnqueueTimestampsByAlertId, alert, jobStartTime); history = new History(History.addDateToMessage(JobStatus.STARTED.getDescription()), HOSTNAME, alert.getId(), JobStatus.STARTED); Set<Trigger> missingDataTriggers = new HashSet<Trigger>(); for (Trigger trigger : alert.getTriggers()) { if (trigger.getType().equals(TriggerType.NO_DATA)) { missingDataTriggers.add(trigger); } } boolean alertSkipped = false; boolean alertFailure = false; boolean alertEvaluationStarted = false; boolean doesDatalagExistInAnyDC = false; try { alertEnqueueTimestamp = alertEnqueueTimestampsByAlertId.get(alert.getId()); MetricQueryResult queryResult = _metricService.getMetrics(alert.getExpression(), alertEnqueueTimestamp); MonitoringUtils.updateAlertMetricQueryPerfCounters(_monitorService, queryResult, alert.getOwner().getUserName()); List<Metric> metrics = new ArrayList<>(queryResult.getMetricsList()); int initialMetricSize = metrics.size(); doesDatalagExistInAnyDC = datalagMonitorEnabled && doesDatalagExistsInAtLeastOneDC(queryResult, alert); if (false) { _logger.info("Orphan Alert detected. Disabling it and notifying user. Alert Id: {}", alert.getId()); Alert dbAlert = findAlertByPrimaryKey(alert.getId()); dbAlert.setEnabled(false); _sendOrphanAlertNotification(alert); } else { if (datalagMonitorEnabled) { if (queryResult.containsTransform() || initialMetricSize == 0) { if (doesDatalagExistInAnyDC) { logMessage = MessageFormat.format("Skipping Alert {0} Evaluation as data was lagging in at least one dc for expression: {1}", alert.getId().intValue(), alert.getExpression()); updateDatalagHistory(alert, historyList, logMessage); alertSkipped = true; continue; } } else { metrics.removeIf(m -> shouldMetricBeRemovedForDataLag(alert, m, historyList)); } if (initialMetricSize > 0 && metrics.size() == 0) { alertSkipped = true; _logger.info(MessageFormat.format("Skipping Alert {0} Evaluation as the metrics expressions evaluation were skipped due to data lag. {1}", alert.getId().intValue(), alert.getExpression())); continue; } } alertEvaluationStarted = true; evaluateEndTime = System.currentTimeMillis(); if (areDatapointsEmpty(metrics)) { _processMissingDataNotifications(np, alert, history, alertEnqueueTimestamp, missingDataTriggers); } else { Set<Trigger> triggersToEvaluate = new HashSet<>(); for (Notification notification : alert.getNotifications()) { triggersToEvaluate.addAll(notification.getTriggers()); } Map<BigInteger, Map<Metric, Long>> triggerFiredTimesAndMetricsByTrigger = _evaluateTriggers(triggersToEvaluate, metrics, alert.getExpression(), alertEnqueueTimestamp, this::noopTags); evaluateEndTime = System.currentTimeMillis(); for (Notification notification : alert.getNotifications()) { if (notification.getTriggers().isEmpty()) { _processTriggerlessNotification(np, alert, history, metrics, notification, alertEnqueueTimestamp); } else { _processNotification(np, alert, history, metrics, triggerFiredTimesAndMetricsByTrigger, notification, alertEnqueueTimestamp); if (missingDataTriggers.size() > 0) { _processMissingDataNotification(np, alert, history, missingDataTriggers, notification, false, alertEnqueueTimestamp); } } } } } history.appendMessageNUpdateHistory("Alert was evaluated successfully.", JobStatus.SUCCESS, System.currentTimeMillis() - jobStartTime); } catch (MissingDataException mde) { if (doesDatalagExistInAnyDC && !alertEvaluationStarted) { alertSkipped = true; } alertFailure = true; _handleAlertEvaluationException(np, alert, jobStartTime, alertEnqueueTimestamp, history, missingDataTriggers, mde, true); } catch (Exception ex) { if (doesDatalagExistInAnyDC && !alertEvaluationStarted) { alertSkipped = true; } alertFailure = true; _handleAlertEvaluationException(np, alert, jobStartTime, alertEnqueueTimestamp, history, missingDataTriggers, ex, false); } finally { history = _historyService.createHistory(alert, history.getMessage(), history.getJobStatus(), history.getExecutionTime()); historyList.add(history); Map<String, String> tags = new HashMap<>(); tags.put(HOSTTAG, HOSTNAME); tags.put(USERTAG, alert.getOwner().getUserName()); if (!alertSkipped) { _monitorService.modifyCounter(Counter.ALERTS_EVALUATION_LATENCY, System.currentTimeMillis() - jobStartTime, tags); if (evaluateEndTime == 0) { evaluateEndTime = System.currentTimeMillis(); } _monitorService.modifyCounter(Counter.ALERTS_EVALUATION_ONLY_LATENCY, evaluateEndTime - jobStartTime, tags); _monitorService.modifyCounter(Counter.ALERTS_EVALUATION_LATENCY_COUNT, 1, tags); } _monitorService.modifyCounter(alertSkipped ? Counter.ALERTS_SKIPPED : Counter.ALERTS_EVALUATED, 1, tags); if (alertFailure) { _monitorService.modifyCounter(Counter.ALERTS_FAILED, 1, tags); } tags.put(ALERTIDTAG, alert.getId().toString()); if(alertSkipped) { numberOfAlertsEvaluated.decrementAndGet(); publishAlertTrackingMetric(Counter.ALERTS_SKIPPED.getMetric(), 1.0, tags); } else { publishAlertTrackingMetric(Counter.ALERTS_EVALUATED.getMetric(), 1.0, tags); } if (alertFailure) { publishAlertTrackingMetric(Counter.ALERTS_FAILED.getMetric(), 1.0, tags); } } } return numberOfAlertsEvaluated.get(); } @Inject  DefaultAlertService(SystemConfiguration configuration, MQService mqService, MetricService metricService,
							   AuditService auditService, TSDBService tsdbService, MailService mailService, HistoryService historyService,
							   MonitorService monitorService, ImageDataRetriever imageDataRetriever, NotifierFactory notifierFactory, Provider<EntityManager> emProvider); @Override @Transactional Alert updateAlert(Alert alert); @Override @Transactional void deleteAlert(String name, PrincipalUser owner); @Override @Transactional void deleteAlert(Alert alert); @Override @Transactional void markAlertForDeletion(String name, PrincipalUser owner); @Override @Transactional void markAlertForDeletion(Alert alert); @Override List<Alert> findAlertsMarkedForDeletion(); @Override List<Alert> findAlertsMarkedForDeletion(final int limit); @Override List<Alert> findAlertsByOwner(PrincipalUser owner, boolean metadataOnly); @Override List<Alert> findAlertsByOwnerPaged(PrincipalUser owner, Integer limit, Integer offset, String searchText, String sortField, String sortOrder); @Override Alert findAlertByPrimaryKey(BigInteger id); @Override List<Alert> findAlertsByPrimaryKeys(List<BigInteger> ids); @Override void updateNotificationsActiveStatusAndCooldown(List<Notification> notifications); @Override @Transactional Integer executeScheduledAlerts(int alertCount, int timeout); void _processNotification(INotificationHandler notificationHandler,
									 Alert alert,
									 History history,
									 List<Metric> metrics,
									 Map<BigInteger, Map<Metric, Long>> triggerFiredTimesAndMetricsByTrigger,
									 Notification notification,
									 Long alertEnqueueTimestamp); void _processTriggerlessNotification(INotificationHandler notificationHandler,
												Alert alert,
												History history,
												List<Metric> metrics,
												Notification notification,
												Long alertEnqueueTimestamp); void _processMissingDataNotification(INotificationHandler notificationHandler,
												Alert alert,
												History history,
												Set<Trigger> triggers,
												Notification notification,
												boolean isDataMissing,
												Long alertEnqueueTimestamp); void sendNotification(Trigger trigger, Metric metric, History history, Notification notification, Alert alert,
			Long triggerFiredTime, Long alertEnqueueTime, String action); void sendClearNotification(Trigger trigger, Metric metric, History history, Notification notification, Alert alert, Long alertEnqueueTime, String action); @Override @Transactional Alert findAlertByNameAndOwner(String name, PrincipalUser owner); @Override void enqueueAlerts(List<Alert> alerts); @Override // IMPORTANT - @Transactional ?? - should not be as it shouldn't talk to the DB void testEvaluateAlert(String serializedAlert, Long when, String testUuid); @VisibleForTesting boolean testEvaluateAlert(Alert alert, Long alertEvaluationTime, AlertTestResults testResults); @Override List<Alert> findAllAlerts(boolean metadataOnly); @Override List<Alert> findAlertsByStatus(boolean enabled); @Override List<BigInteger> findAlertIdsByStatus(boolean enabled); @Override List<Alert> findAlertsByRangeAndStatus(BigInteger fromId, BigInteger toId, boolean enabled); @Override List<Alert> findAlertsModifiedAfterDate(Date modifiedDate); @Override int alertCountByStatus(boolean enabled); @Override List<Alert> findAlertsByLimitOffsetStatus(int limit, int offset,
			boolean enabled); @Override List<Alert> findAlertsByNameWithPrefix(String prefix); @Override List<String> getSupportedNotifiers(); @Override List<Alert> findSharedAlerts(boolean metadataOnly, PrincipalUser owner, Integer limit); @Override List<Alert> findSharedAlertsPaged(Integer limit, Integer offset, String searchText, String sortField, String sortOrder); @Override List<Alert> findPrivateAlertsForPrivilegedUserPaged(PrincipalUser owner, Integer limit, Integer offset, String searchText,
		String sortField, String sortOrder); @Override int countAlerts(AlertsCountContext context); @Override Notifier getNotifier(SupportedNotifier notifier); @Override void dispose(); Long getTriggerFiredDatapointTime(Trigger trigger, Metric metric, String queryExpression, Long alertEnqueueTimestamp); Long getLatestDatapointTime(Metric metric, String queryExpression, Long alertEnqueueTimestamp); @Override @Transactional void deleteTrigger(Trigger trigger); @Override @Transactional void deleteNotification(Notification notification); @Override void exportMetric(Metric metric, Double value); @Override void updateCounter(Counter counter, Double value);  }
@Test public void testExecuteScheduledAlerts_ForOneTimeSeries() { ServiceFactory sFactory = system.getServiceFactory(); UserService userService = sFactory.getUserService(); int triggerMinValue = 50, inertiaPeriod = 1000 * 60 * 5; int cooldownPeriod = 1000 * 5; final AtomicInteger notificationCount = new AtomicInteger(0); final AtomicInteger clearCount = new AtomicInteger(0); Metric metric = _createMetric(TestUtils.createRandomName(), TestUtils.createRandomName(), triggerMinValue, inertiaPeriod); Alert alert = new Alert(userService.findAdminUser(), userService.findAdminUser(), "testAlert", "-1h:scope:metric:avg", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.GREATER_THAN_OR_EQ, "testTrigger", triggerMinValue, inertiaPeriod); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), cooldownPeriod); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(trigger)); alert.setNotifications(Arrays.asList(notification)); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric), alert, notification, false); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(1, 1000)); assertEquals(1, notificationCount.get()); }
@Test public void testExecuteScheduledAlerts_ForOneTimeSeriesMultipleTriggers() { ServiceFactory sFactory = system.getServiceFactory(); UserService userService = sFactory.getUserService(); final AtomicInteger notificationCount = new AtomicInteger(0); final AtomicInteger clearCount = new AtomicInteger(0); Metric metric = new Metric("scope", "metric"); Map<Long, String> dps = new HashMap<Long, String>(); dps.put(1000L, "11"); dps.put(2000L, "21"); dps.put(3000L, "31"); metric.setDatapoints(_convertDatapoints(dps)); Alert alert = new Alert(userService.findAdminUser(), userService.findAdminUser(), "testAlert", "-1h:scope:metric:avg", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger1 = new Trigger(alert, TriggerType.GREATER_THAN_OR_EQ, "testTrigger1", 10, 0); _setTriggerId(trigger1, "100002"); Trigger trigger2 = new Trigger(alert, TriggerType.GREATER_THAN_OR_EQ, "testTrigger2", 5, 0); _setTriggerId(trigger2, "100003"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), 0); _setNotificationId(notification, "100004"); alert.setTriggers(Arrays.asList(trigger1, trigger2)); alert.setNotifications(Arrays.asList(notification)); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric), alert, notification, false); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(1, 1000)); assertEquals(2, notificationCount.get()); }
@Test public void testExecuteScheduledAlerts_ForNoDataTrigger() { ServiceFactory sFactory = system.getServiceFactory(); UserService userService = sFactory.getUserService(); final AtomicInteger notificationCount = new AtomicInteger(0); final AtomicInteger clearCount = new AtomicInteger(0); Alert alert = new Alert(userService.findAdminUser(), userService.findAdminUser(), "testAlert", "-1h:scope:metric:avg", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger1 = new Trigger(alert, TriggerType.NO_DATA, "testTrigger1", 10, 1000*60); _setTriggerId(trigger1, "100002"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), 0); _setNotificationId(notification, "100004"); alert.setTriggers(Arrays.asList(trigger1)); alert.setNotifications(Arrays.asList(notification)); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(), alert, notification, false); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(1, 1000)); assertEquals(1, notificationCount.get()); }
@Test public void testExecuteScheduledAlerts_OnCooldown() { ServiceFactory sFactory = system.getServiceFactory(); UserService userService = sFactory.getUserService(); final AtomicInteger notificationCount = new AtomicInteger(0); final AtomicInteger clearCount = new AtomicInteger(0); Metric metric = new Metric("scope", "metric"); Map<Long, String> dps = new HashMap<Long, String>(); dps.put(1000L, "11"); dps.put(2000L, "21"); dps.put(3000L, "31"); metric.setDatapoints(_convertDatapoints(dps)); Alert alert = new Alert(userService.findAdminUser(), userService.findAdminUser(), "testAlert", "-1h:scope:metric:avg", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.GREATER_THAN_OR_EQ, "testTrigger", 10, 0); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), 600000); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(new Trigger[] { trigger })); alert.setNotifications(Arrays.asList(new Notification[] { notification })); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric), alert, notification, false); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(1, 1000)); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(1, 1000)); assertEquals(1, notificationCount.get()); assertEquals(1, notification.getCooldownExpirationMap().size()); }
@Test public void testExecuteScheduledAlerts_ForMoreThanOneTimeSeries() { UserService userService = system.getServiceFactory().getUserService(); final AtomicInteger notificationCount = new AtomicInteger(0); final AtomicInteger clearCount = new AtomicInteger(0); Metric metric1 = new Metric("scope1", "metric"); Map<Long, String> dps1 = new HashMap<Long, String>(); dps1.put(1000L, "1"); dps1.put(2000L, "2"); dps1.put(3000L, "3"); metric1.setDatapoints(_convertDatapoints(dps1)); Metric metric2 = new Metric("scope2", "metric"); Map<Long, String> dps2 = new HashMap<Long, String>(); dps2.put(4000L, "11"); dps2.put(5000L, "20"); dps2.put(6000L, "30"); metric2.setDatapoints(_convertDatapoints(dps2)); Alert alert = new Alert(userService.findAdminUser(), userService.findAdminUser(), "testAlert", "-1h:scope:metric:avg", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.GREATER_THAN_OR_EQ, "testTrigger", 10, 0); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), 0); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(new Trigger[] { trigger })); alert.setNotifications(Arrays.asList(new Notification[] { notification })); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric1, metric2), alert, notification, false); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(10, 1000)); assertEquals(1, notificationCount.get()); }
@Test public void testExecuteScheduledAlerts_ClearNotification() { UserService userService = system.getServiceFactory().getUserService(); final AtomicInteger notificationCount = new AtomicInteger(0); final AtomicInteger clearCount = new AtomicInteger(0); Metric metric = new Metric("scope", "metric"); Map<Long, String> dps = new HashMap<Long, String>(); dps.put(4000L, "11"); dps.put(5000L, "20"); dps.put(6000L, "30"); metric.setDatapoints(_convertDatapoints(dps)); Alert alert = new Alert(userService.findAdminUser(), userService.findAdminUser(), "testAlert", "-1h:scope:metric:avg", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.GREATER_THAN_OR_EQ, "testTrigger", 10, 0); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), 0); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(new Trigger[] { trigger })); alert.setNotifications(Arrays.asList(new Notification[] { notification })); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric), alert, notification, false); spyAlertService.executeScheduledAlerts(10, 1000); assertEquals(1, notificationCount.get()); notificationCount.set(0); clearCount.set(0); dps = new HashMap<Long, String>(); dps.put(4000L, "1"); dps.put(5000L, "2"); dps.put(6000L, "3"); metric.setDatapoints(_convertDatapoints(dps)); spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric), alert, notification, false); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(10, 1000)); assertEquals(0, notificationCount.get()); assertEquals(1, clearCount.get()); }
@Test public void testExecuteScheduledAlerts_OnCooldownWithRefocusNotifier() { ServiceFactory sFactory = system.getServiceFactory(); UserService userService = sFactory.getUserService(); final AtomicInteger notificationCount = new AtomicInteger(0); final AtomicInteger clearCount = new AtomicInteger(0); Metric metric = new Metric("scope", "metric"); Map<Long, String> dps = new HashMap<Long, String>(); dps.put(1000L, "11"); dps.put(2000L, "21"); dps.put(3000L, "31"); metric.setDatapoints(_convertDatapoints(dps)); Alert alert = new Alert(userService.findAdminUser(), userService.findAdminUser(), "testAlert", "-1h:scope:metric:avg", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.GREATER_THAN_OR_EQ, "testTrigger", 10, 0); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, RefocusNotifier.class.getName(), new ArrayList<String>(), 600000); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(new Trigger[] { trigger })); alert.setNotifications(Arrays.asList(new Notification[] { notification })); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric), alert, notification, false); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(1, 1000)); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(1, 1000)); assertEquals(false, notification.isActiveForTriggerAndMetric(trigger, metric)); assertEquals(2, notificationCount.get()); assertEquals(0, notification.getCooldownExpirationMap().size()); }
@Test public void testExecuteScheduledAlerts_ClearNotificationWithRefocusNotifier() { UserService userService = system.getServiceFactory().getUserService(); final AtomicInteger notificationCount = new AtomicInteger(0); final AtomicInteger clearCount = new AtomicInteger(0); Metric metric = new Metric("scope", "metric"); Map<Long, String> dps = new HashMap<Long, String>(); dps.put(4000L, "11"); dps.put(5000L, "20"); dps.put(6000L, "30"); metric.setDatapoints(_convertDatapoints(dps)); Alert alert = new Alert(userService.findAdminUser(), userService.findAdminUser(), "testAlert", "-1h:scope:metric:avg", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.GREATER_THAN_OR_EQ, "testTrigger", 10, 0); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, RefocusNotifier.class.getName(), new ArrayList<String>(), 0); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(new Trigger[] { trigger })); alert.setNotifications(Arrays.asList(new Notification[] { notification })); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric), alert, notification, false); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(10, 1000)); assertEquals(1, notificationCount.get()); assertEquals(false, notification.isActiveForTriggerAndMetric(trigger, metric)); notificationCount.set(0); clearCount.set(0); dps = new HashMap<Long, String>(); dps.put(4000L, "1"); dps.put(5000L, "2"); dps.put(6000L, "3"); metric.setDatapoints(_convertDatapoints(dps)); spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric), alert, notification, false); spyAlertService.executeScheduledAlerts(10, 1000); assertEquals(0, notificationCount.get()); assertEquals(1, clearCount.get()); assertEquals(false, notification.isActiveForTriggerAndMetric(trigger, metric)); }
@Test public void testExecuteScheduledAlerts_AlertWithMultipleMetricsNotificationSentForEach() { UserService userService = system.getServiceFactory().getUserService(); final AtomicInteger notificationCount = new AtomicInteger(0); final AtomicInteger clearCount = new AtomicInteger(0); Metric metric1 = new Metric("scope1", "metric"); Map<Long, String> dps1 = new HashMap<Long, String>(); dps1.put(1000L, "11"); dps1.put(2000L, "20"); dps1.put(3000L, "30"); metric1.setDatapoints(_convertDatapoints(dps1)); Metric metric2 = new Metric("scope2", "metric"); Map<Long, String> dps2 = new HashMap<Long, String>(); dps2.put(4000L, "11"); dps2.put(5000L, "20"); dps2.put(6000L, "30"); metric2.setDatapoints(_convertDatapoints(dps2)); Alert alert = new Alert(userService.findAdminUser(), userService.findAdminUser(), "testAlert", "-1h:scope:metric:avg", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.GREATER_THAN_OR_EQ, "testTrigger", 10, 0); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), 300000); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(new Trigger[] { trigger })); alert.setNotifications(Arrays.asList(new Notification[] { notification })); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric1, metric2), alert, notification, false); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(10, 1000)); assertEquals(2, notificationCount.get()); assertEquals(2, notification.getCooldownExpirationMap().size()); assertEquals(2, notification.getActiveStatusMap().size()); }
@Test public void testExecuteScheduledAlerts_Scenario1() { UserService userService = system.getServiceFactory().getUserService(); final AtomicInteger notificationCount = new AtomicInteger(0); final AtomicInteger clearCount = new AtomicInteger(0); Metric metric1 = new Metric("scope1", "metric"); Map<Long, String> dps1 = new HashMap<Long, String>(); dps1.put(1000L, "11"); dps1.put(2000L, "20"); dps1.put(3000L, "30"); metric1.setDatapoints(_convertDatapoints(dps1)); Metric metric2 = new Metric("scope2", "metric"); Map<Long, String> dps2 = new HashMap<Long, String>(); dps2.put(4000L, "1"); dps2.put(5000L, "2"); dps2.put(6000L, "3"); metric2.setDatapoints(_convertDatapoints(dps2)); Alert alert = new Alert(userService.findAdminUser(), userService.findAdminUser(), "testAlert", "-1h:scope:metric:avg", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.GREATER_THAN_OR_EQ, "testTrigger", 10, 0); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), 300000); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(new Trigger[] { trigger })); alert.setNotifications(Arrays.asList(new Notification[] { notification })); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric1, metric2), alert, notification, false); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(10, 1000)); assertEquals(1, notificationCount.get()); assertEquals(1, notification.getCooldownExpirationMap().size()); assertEquals(1, notification.getActiveStatusMap().size()); notificationCount.set(0); clearCount.set(0); metric1 = new Metric("scope1", "metric"); dps1 = new HashMap<Long, String>(); dps1.put(1000L, "1"); dps1.put(2000L, "2"); dps1.put(3000L, "3"); metric1.setDatapoints(_convertDatapoints(dps1)); metric2 = new Metric("scope2", "metric"); dps2 = new HashMap<Long, String>(); dps2.put(4000L, "11"); dps2.put(5000L, "21"); dps2.put(6000L, "31"); metric2.setDatapoints(_convertDatapoints(dps2)); spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric1, metric2), alert, notification, false); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(10, 1000)); assertEquals(1, notificationCount.get()); assertEquals(1, clearCount.get()); assertEquals(2, notification.getCooldownExpirationMap().size()); assertEquals(1, _getActiveSize(notification.getActiveStatusMap())); }
@Test public void testExecuteScheduledAlerts_DuringDatalagPresentWithNoDataTriggerWithSkippedEvaluation() { ServiceFactory sFactory = system.getServiceFactory(); UserService userService = sFactory.getUserService(); int triggerMinValue = 50, inertiaPeriod = 1000; int cooldownPeriod = 1000 * 5; final AtomicInteger notificationCount = new AtomicInteger(0); final AtomicInteger clearCount = new AtomicInteger(0); Metric metric = new Metric("scope", "metric"); Map<Long, Double> datapoints = new HashMap<>(); metric.setDatapoints(datapoints); Alert alert = new Alert(userService.findAdminUser(), userService.findAdminUser(), "testAlert", "-1h:scope:metric:avg", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.NO_DATA, "testTrigger", triggerMinValue, inertiaPeriod); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), cooldownPeriod); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(trigger)); alert.setNotifications(Arrays.asList(notification)); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(), alert, notification, true); MetricQueryResult queryResult = new MetricQueryResult(); queryResult.addInboundMetricQuery(new MetricQuery("scope", "metric", null, 0L, 5000L)); when(_metricServiceMock.extractDCFromMetricQuery(anyList())).thenReturn(new ArrayList<>(Arrays.asList("DC1"))); when(_metricServiceMock.getMetrics(anyString(), anyLong())).thenReturn(queryResult); assertEquals(new Integer(0), spyAlertService.executeScheduledAlerts(1, 1000)); assertEquals(0, notificationCount.get()); enableDatalagMonitoring(false); }
@Test public void testExecuteScheduledAlerts_DuringDatalagPresentWithSuccessfulEvaluation() { ServiceFactory sFactory = system.getServiceFactory(); UserService userService = sFactory.getUserService(); int triggerMinValue = 10, inertiaPeriod = 1; int cooldownPeriod = 1000 * 5; final AtomicInteger notificationCount = new AtomicInteger(0); final AtomicInteger clearCount = new AtomicInteger(0); final AtomicInteger refocusCount = new AtomicInteger(0); Metric metric = new Metric("scope", "metric"); Map<Long, String> dps1 = new HashMap<Long, String>(); dps1.put(1000L, "11"); dps1.put(2000L, "20"); dps1.put(3000L, "30"); metric.setDatapoints(_convertDatapoints(dps1)); Alert alert = new Alert(userService.findAdminUser(), userService.findAdminUser(), "testAlert", "-1h:scope:metric:avg", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.GREATER_THAN_OR_EQ, "testTrigger", triggerMinValue, inertiaPeriod); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), cooldownPeriod); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(trigger)); alert.setNotifications(Arrays.asList(notification)); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric), alert, notification, false); enableDatalagMonitoring(true); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(1, 1000)); assertEquals(1, notificationCount.get()); enableDatalagMonitoring(false); }
@Test public void testExecuteScheduledAlerts_DuringDatalagPresentWithSkippedEvaluation() { ServiceFactory sFactory = system.getServiceFactory(); UserService userService = sFactory.getUserService(); int triggerMinValue = 10, inertiaPeriod = 1; int cooldownPeriod = 1000 * 5; final AtomicInteger clearCount = new AtomicInteger(0); final AtomicInteger notificationCount = new AtomicInteger(0); Metric metric = new Metric("scope", "metric"); Map<Long, String> dps1 = new HashMap<Long, String>(); dps1.put(1000L, "11"); dps1.put(2000L, "20"); dps1.put(3000L, "30"); metric.setDatapoints(_convertDatapoints(dps1)); Alert alert = new Alert(userService.findAdminUser(), userService.findAdminUser(), "testAlert", "-1h:scope:metric:avg", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.GREATER_THAN_OR_EQ, "testTrigger", triggerMinValue, inertiaPeriod); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), cooldownPeriod); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(trigger)); alert.setNotifications(Arrays.asList(notification)); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric), alert, notification, true); assertEquals(new Integer(0), spyAlertService.executeScheduledAlerts(1, 1000)); assertEquals(0, notificationCount.get()); enableDatalagMonitoring(false); }
@Test public void testExecuteScheduledAlerts_DuringDatalagPresentWithDcDetectionFailedWithSkippedEvaluation() { ServiceFactory sFactory = system.getServiceFactory(); UserService userService = sFactory.getUserService(); int triggerMinValue = 10, inertiaPeriod = 1; int cooldownPeriod = 1000 * 5; final AtomicInteger clearCount = new AtomicInteger(0); final AtomicInteger notificationCount = new AtomicInteger(0); Metric metric = new Metric("scope", "metric"); Map<Long, String> dps1 = new HashMap<Long, String>(); dps1.put(1000L, "11"); dps1.put(2000L, "20"); dps1.put(3000L, "30"); metric.setDatapoints(_convertDatapoints(dps1)); Alert alert = new Alert(userService.findAdminUser(), userService.findAdminUser(), "testAlert", "-1h:scope:metric:avg", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.GREATER_THAN_OR_EQ, "testTrigger", triggerMinValue, inertiaPeriod); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), cooldownPeriod); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(trigger)); alert.setNotifications(Arrays.asList(notification)); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric), alert, notification, true); when(_metricServiceMock.extractDCFromMetricQuery(anyList())).thenReturn(new ArrayList<>()); assertEquals(new Integer(0), spyAlertService.executeScheduledAlerts(1, 1000)); assertEquals(0, notificationCount.get()); enableDatalagMonitoring(false); }
@Test public void testExecuteScheduledAlerts_DuringDatalagPresentWithTransformsWithSkippedEvaluation() { ServiceFactory sFactory = system.getServiceFactory(); UserService userService = sFactory.getUserService(); int triggerMinValue = 10, inertiaPeriod = 1; int cooldownPeriod = 1000 * 5; final AtomicInteger clearCount = new AtomicInteger(0); final AtomicInteger notificationCount = new AtomicInteger(0); Metric metric = new Metric("scope", "metric"); Map<Long, String> dps1 = new HashMap<Long, String>(); dps1.put(1000L, "11"); dps1.put(2000L, "20"); dps1.put(3000L, "30"); metric.setDatapoints(_convertDatapoints(dps1)); Alert alert = new Alert(userService.findAdminUser(), userService.findAdminUser(), "testAlert", "COUNT(-1h:scope:metric:avg, -1h:scope:metric:avg, -1h:scope:metric:avg)", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.GREATER_THAN_OR_EQ, "testTrigger", triggerMinValue, inertiaPeriod); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), cooldownPeriod); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(trigger)); alert.setNotifications(Arrays.asList(notification)); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric), alert, notification, true); MetricQueryResult queryResult = new MetricQueryResult(); queryResult.setMetricsList(new ArrayList<Metric>(Arrays.asList(metric))); queryResult.addTransform(TransformFactory.Function.COUNT); queryResult.addInboundMetricQuery(new MetricQuery("COUNT", "metric", null, 0L, 5000L)); when(_metricServiceMock.getMetrics(anyString(), anyLong())).thenReturn(queryResult); when(_metricServiceMock.extractDCFromMetricQuery(anyList())).thenReturn(new ArrayList<>(Arrays.asList("DC1", "DC2", "DC3"))); enableDatalagMonitoring(true); assertEquals(new Integer(0), spyAlertService.executeScheduledAlerts(1, 1000)); assertEquals(0, notificationCount.get()); enableDatalagMonitoring(false); }
@Test public void testExecuteScheduledAlerts_DuringDatalagPresentWithTransformsWithSuccessfulEvaluation() { ServiceFactory sFactory = system.getServiceFactory(); UserService userService = sFactory.getUserService(); int triggerMinValue = 10, inertiaPeriod = 1; int cooldownPeriod = 1000 * 5; final AtomicInteger clearCount = new AtomicInteger(0); final AtomicInteger notificationCount = new AtomicInteger(0); Metric metric = new Metric("scope", "metric"); Map<Long, String> dps1 = new HashMap<Long, String>(); dps1.put(1000L, "11"); dps1.put(2000L, "20"); dps1.put(3000L, "30"); metric.setDatapoints(_convertDatapoints(dps1)); Alert alert = new Alert(userService.findAdminUser(), userService.findAdminUser(), "testAlert", "COUNT(-1h:scope:metric:avg, -1h:scope:metric:avg, -1h:scope:metric:avg)", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.GREATER_THAN_OR_EQ, "testTrigger", triggerMinValue, inertiaPeriod); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), cooldownPeriod); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(trigger)); alert.setNotifications(Arrays.asList(notification)); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric), alert, notification, false); MetricQueryResult queryResult = new MetricQueryResult(); queryResult.setMetricsList(new ArrayList<Metric>(Arrays.asList(metric))); queryResult.addTransform(TransformFactory.Function.COUNT); queryResult.addInboundMetricQuery(new MetricQuery("COUNT", "metric", null, 0L, 5000L)); when(_metricServiceMock.getMetrics(anyString(), anyLong())).thenReturn(queryResult); when(_metricServiceMock.extractDCFromMetricQuery(anyList())).thenReturn(new ArrayList<>(Arrays.asList("DC1", "DC2", "DC3"))); enableDatalagMonitoring(true); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(1, 1000)); assertEquals(1, notificationCount.get()); enableDatalagMonitoring(false); }
@Test public void testExecuteScheduledAlerts_DuringDatalagPresentWithWhiteListedScopeWithNoDataWithSuccessfulEvaluation() { ServiceFactory sFactory = system.getServiceFactory(); UserService userService = sFactory.getUserService(); int triggerMinValue = 50, inertiaPeriod = 1000; int cooldownPeriod = 1000 * 5; final AtomicInteger notificationCount = new AtomicInteger(0); final AtomicInteger clearCount = new AtomicInteger(0); Alert alert = new Alert(userService.findAdminUser(), userService.findAdminUser(), "testAlert", "-1h:whiteListedScope:metric:avg", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.NO_DATA, "testTrigger", triggerMinValue, inertiaPeriod); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), cooldownPeriod); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(trigger)); alert.setNotifications(Arrays.asList(notification)); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(), alert, notification, true); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(1, 1000)); assertEquals(1, notificationCount.get()); enableDatalagMonitoring(false); }
@Test public void testExecuteScheduledAlerts_DuringDatalagPresentWithWhiteListedUserWithNoDataWithSuccessfulEvaluation() { ServiceFactory sFactory = system.getServiceFactory(); UserService userService = sFactory.getUserService(); int triggerMinValue = 50, inertiaPeriod = 1000; int cooldownPeriod = 1000 * 5; final AtomicInteger notificationCount = new AtomicInteger(0); final AtomicInteger clearCount = new AtomicInteger(0); Alert alert = new Alert(userService.findDefaultUser(), userService.findDefaultUser(), "testAlert", "-1h:scope:metric:avg", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.NO_DATA, "testTrigger", triggerMinValue, inertiaPeriod); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), cooldownPeriod); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(trigger)); alert.setNotifications(Arrays.asList(notification)); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(), alert, notification, true); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(1, 1000)); assertEquals(1, notificationCount.get()); enableDatalagMonitoring(false); }
@Test public void testExecuteScheduledAlerts_DuringDatalagPresentWithWhiteListedScopeWithMetricDataWithSuccessfulEvaluation() { ServiceFactory sFactory = system.getServiceFactory(); UserService userService = sFactory.getUserService(); int triggerMinValue = 10, inertiaPeriod = 1; int cooldownPeriod = 1000 * 5; final AtomicInteger clearCount = new AtomicInteger(0); final AtomicInteger notificationCount = new AtomicInteger(0); Metric metric = new Metric("scope", "metric"); Map<Long, String> dps1 = new HashMap<Long, String>(); dps1.put(1000L, "11"); dps1.put(2000L, "20"); dps1.put(3000L, "30"); metric.setDatapoints(_convertDatapoints(dps1)); Alert alert = new Alert(userService.findAdminUser(), userService.findAdminUser(), "testAlert", "-1h:whiteListedScope:metric:avg", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.GREATER_THAN_OR_EQ, "testTrigger", triggerMinValue, inertiaPeriod); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), cooldownPeriod); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(trigger)); alert.setNotifications(Arrays.asList(notification)); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric), alert, notification, true); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(1, 1000)); assertEquals(1, notificationCount.get()); enableDatalagMonitoring(false); }
@Test public void testExecuteScheduledAlerts_DuringDatalagPresentWithWhiteListedUserWithMetricDataWithSuccessfulEvaluation() { ServiceFactory sFactory = system.getServiceFactory(); UserService userService = sFactory.getUserService(); int triggerMinValue = 10, inertiaPeriod = 1; int cooldownPeriod = 1000 * 5; final AtomicInteger clearCount = new AtomicInteger(0); final AtomicInteger notificationCount = new AtomicInteger(0); Metric metric = new Metric("scope", "metric"); Map<Long, String> dps1 = new HashMap<Long, String>(); dps1.put(1000L, "11"); dps1.put(2000L, "20"); dps1.put(3000L, "30"); metric.setDatapoints(_convertDatapoints(dps1)); Alert alert = new Alert(userService.findDefaultUser(), userService.findDefaultUser(), "testAlert", "-1h:scope:metric:avg", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.NO_DATA, "testTrigger", triggerMinValue, inertiaPeriod); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), cooldownPeriod); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(trigger)); alert.setNotifications(Arrays.asList(notification)); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric), alert, notification, true); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(1, 1000)); assertEquals(1, notificationCount.get()); enableDatalagMonitoring(false); }
@Test public void testExecuteScheduledAlerts_DuringDatalagPresentWithWhiteListedScopeWithTransformSuccessfulEvaluation() { ServiceFactory sFactory = system.getServiceFactory(); UserService userService = sFactory.getUserService(); int triggerMinValue = 10, inertiaPeriod = 1; int cooldownPeriod = 1000 * 5; final AtomicInteger clearCount = new AtomicInteger(0); final AtomicInteger notificationCount = new AtomicInteger(0); Metric metric = new Metric("scope", "metric"); Map<Long, String> dps1 = new HashMap<Long, String>(); dps1.put(1000L, "11"); dps1.put(2000L, "20"); dps1.put(3000L, "30"); metric.setDatapoints(_convertDatapoints(dps1)); Alert alert = new Alert(userService.findDefaultUser(), userService.findDefaultUser(), "testAlert", "COUNT(-1h:whitelistedScope:metric1:avg,-1h:scope:metric2:avg,-1h:scope:metric3:avg)", "* * * * *"); _setAlertId(alert, "100001"); Trigger trigger = new Trigger(alert, TriggerType.GREATER_THAN_OR_EQ, "testTrigger", triggerMinValue, inertiaPeriod); _setTriggerId(trigger, "100002"); Notification notification = new Notification("testNotification", alert, AuditNotifier.class.getName(), new ArrayList<String>(), cooldownPeriod); _setNotificationId(notification, "100003"); alert.setTriggers(Arrays.asList(trigger)); alert.setNotifications(Arrays.asList(notification)); notification.setTriggers(alert.getTriggers()); alert.setEnabled(true); DefaultAlertService spyAlertService = _initializeSpyAlertServiceWithStubs(notificationCount, clearCount, Arrays.asList(metric), alert, notification, true); MetricQueryResult queryResult = new MetricQueryResult(); queryResult.setMetricsList(new ArrayList<Metric>(Arrays.asList(metric))); queryResult.addTransform(TransformFactory.Function.COUNT); queryResult.addInboundMetricQuery(new MetricQuery("COUNT", "metric", null, 0L, 5000L)); when(_metricServiceMock.getMetrics(anyString(), anyLong())).thenReturn(queryResult); when(_metricServiceMock.extractDCFromMetricQuery(anyList())).thenReturn(new ArrayList<>(Arrays.asList("DC1", "DC2", "DC3"))); assertEquals(new Integer(1), spyAlertService.executeScheduledAlerts(1, 1000)); assertEquals(1, notificationCount.get()); enableDatalagMonitoring(false); }