@Test public void shouldSendMailForGaugeAlert() throws Exception { setupForGauge(500); AlertingService alertingService = new AlertingService(configRepository, incidentRepository, aggregateRepository, gaugeValueRepository, 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."); }
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 { 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 { 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,
            RollupLevelService rollupLevelService, MailService mailService, HttpClient httpClient,
            LockSet<IncidentKey> openingIncidentLockSet,
            LockSet<IncidentKey> resolvingIncidentLockSet, Clock clock); }
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,
            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); }
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,
            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);  }