AlertingService { public void checkMetricAlert(String centralDisplay, String agentRollupId, String agentRollupDisplay, AlertConfig alertConfig, MetricCondition metricCondition, long endTime) throws Exception { long startTime = endTime - SECONDS.toMillis(metricCondition.getTimePeriodSeconds()); Number value = metricService.getMetricValue(agentRollupId, metricCondition, startTime, endTime); if (value == null) { return; } boolean currentlyTriggered; if (metricCondition.getLowerBoundThreshold()) { currentlyTriggered = value.doubleValue() <= metricCondition.getThreshold(); } else { currentlyTriggered = value.doubleValue() >= metricCondition.getThreshold(); } AlertCondition alertCondition = alertConfig.getCondition(); OpenIncident openIncident = incidentRepository.readOpenIncident(agentRollupId, alertCondition, alertConfig.getSeverity()); if (openIncident != null && !currentlyTriggered) { resolveIncident(centralDisplay, agentRollupId, agentRollupDisplay, alertConfig, metricCondition, endTime, alertCondition, openIncident); } else if (openIncident == null && currentlyTriggered) { if (hasMinTransactionCount(metricCondition.getMetric())) { long minTransactionCount = metricCondition.getMinTransactionCount(); if (minTransactionCount != 0) { long transactionCount = metricService.getTransactionCount(agentRollupId, metricCondition.getTransactionType(), Strings.emptyToNull(metricCondition.getTransactionName()), startTime, endTime); if (transactionCount < minTransactionCount) { return; } } } openIncident(centralDisplay, agentRollupId, agentRollupDisplay, alertConfig, metricCondition, endTime, alertCondition); } } AlertingService(ConfigRepository configRepository, IncidentRepository incidentRepository,
            AggregateRepository aggregateRepository, GaugeValueRepository gaugeValueRepository,
            TraceRepository traceRepository, RollupLevelService rollupLevelService,
            MailService mailService, HttpClient httpClient,
            LockSet<IncidentKey> openingIncidentLockSet,
            LockSet<IncidentKey> resolvingIncidentLockSet, Clock clock); void close(); void checkForDeletedAlerts(String agentRollupId); void checkForAllDeletedAlerts(); void checkMetricAlert(String centralDisplay, String agentRollupId,
            String agentRollupDisplay, AlertConfig alertConfig, MetricCondition metricCondition,
            long endTime); void sendNotification(String centralDisplay, String agentRollupId,
            String agentRollupDisplay, AlertConfig alertConfig, long endTime, String subject,
            String messageText, boolean ok); static void sendEmail(String centralDisplay, String agentRollupDisplay, String subject,
            List<String> emailAddresses, String messageText, SmtpConfig smtpConfig,
            @Nullable String passwordOverride, LazySecretKey lazySecretKey, MailService mailService); static String getPreUpperBoundText(boolean ok); static String getGaugeThresholdText(double threshold, String gaugeUnit); static String getOverTheLastMinutesText(int timePeriodSeconds); static String getWithUnit(double val, String unit); static boolean hasTransactionTypeAndName(String metric); static boolean hasMinTransactionCount(String metric); static boolean hasErrorMessageFilter(String metric);  }
@Test public void shouldSendMailForTransactionAlert() throws Exception { setupForTransaction(1000000); AlertingService alertingService = new AlertingService(configRepository, incidentRepository, aggregateRepository, gaugeValueRepository, traceRepository, rollupLevelService, mailService, httpClient, newLockSet(), newLockSet(), Clock.systemClock()); alertingService.checkMetricAlert("", "", "", TRANSACTION_X_PERCENTILE_ALERT_CONFIG, TRANSACTION_X_PERCENTILE_ALERT_CONFIG.getCondition().getMetricCondition(), 120000); assertThat(mailService.getMessage()).isNotNull(); assertThat(((String) mailService.getMessage().getContent()).trim()) .isEqualTo("95th percentile over the last 1 minute is greater than or equal to the" + " alert threshold of 1 millisecond."); }
@Test public void shouldNotSendMailForTransactionAlert() throws Exception { setupForTransaction(999999); AlertingService alertingService = new AlertingService(configRepository, incidentRepository, aggregateRepository, gaugeValueRepository, traceRepository, rollupLevelService, mailService, httpClient, newLockSet(), newLockSet(), Clock.systemClock()); alertingService.checkMetricAlert("", "", "", TRANSACTION_X_PERCENTILE_ALERT_CONFIG, TRANSACTION_X_PERCENTILE_ALERT_CONFIG.getCondition().getMetricCondition(), 120000); assertThat(mailService.getMessage()).isNull(); }
@Test public void shouldSendMailForGaugeAlert() throws Exception { setupForGauge(500); AlertingService alertingService = new AlertingService(configRepository, incidentRepository, aggregateRepository, gaugeValueRepository, traceRepository, rollupLevelService, mailService, httpClient, newLockSet(), newLockSet(), Clock.systemClock()); alertingService.checkMetricAlert("", "", "", UPPER_BOUND_GAUGE_ALERT_CONFIG, UPPER_BOUND_GAUGE_ALERT_CONFIG.getCondition().getMetricCondition(), 120000); assertThat(mailService.getMessage()).isNotNull(); assertThat(((String) mailService.getMessage().getContent()).trim()) .isEqualTo("Average over the last 1 minute is greater than or equal to the alert" + " threshold of 500 milliseconds per second."); }
@Test public void shouldNotSendMailForGaugeAlert() throws Exception { setupForGauge(499); AlertingService alertingService = new AlertingService(configRepository, incidentRepository, aggregateRepository, gaugeValueRepository, traceRepository, rollupLevelService, mailService, httpClient, newLockSet(), newLockSet(), Clock.systemClock()); alertingService.checkMetricAlert("", "", "", UPPER_BOUND_GAUGE_ALERT_CONFIG, UPPER_BOUND_GAUGE_ALERT_CONFIG.getCondition().getMetricCondition(), 120000); assertThat(mailService.getMessage()).isNull(); }
@Test public void shouldSendMailForLowerBoundGaugeAlert() throws Exception { setupForGauge(500); AlertingService alertingService = new AlertingService(configRepository, incidentRepository, aggregateRepository, gaugeValueRepository, traceRepository, rollupLevelService, mailService, httpClient, newLockSet(), newLockSet(), Clock.systemClock()); alertingService.checkMetricAlert("", "", "", LOWER_BOUND_GAUGE_ALERT_CONFIG, LOWER_BOUND_GAUGE_ALERT_CONFIG.getCondition().getMetricCondition(), 120000); assertThat(mailService.getMessage()).isNotNull(); assertThat(((String) mailService.getMessage().getContent()).trim()) .isEqualTo("Average over the last 1 minute is less than or equal to the alert" + " threshold of 500 milliseconds per second."); }
@Test public void shouldNotSendMailForLowerBoundGaugeAlert() throws Exception { setupForGauge(501); AlertingService alertingService = new AlertingService(configRepository, incidentRepository, aggregateRepository, gaugeValueRepository, traceRepository, rollupLevelService, mailService, httpClient, newLockSet(), newLockSet(), Clock.systemClock()); alertingService.checkMetricAlert("", "", "", LOWER_BOUND_GAUGE_ALERT_CONFIG, LOWER_BOUND_GAUGE_ALERT_CONFIG.getCondition().getMetricCondition(), 120000); assertThat(mailService.getMessage()).isNull(); }