@Test public void testConstruction2() { Alert alert = new Alert(alertingDefinition, 1L, 2L); assertThat(alert.getId(), notNullValue()); assertThat(alert.getAlertingDefinition(), is(alertingDefinition)); assertThat(alert.getStartTimestamp(), is(1L)); assertThat(alert.getStopTimestamp(), is(2L)); } }
/** * Returns string representation of an alert. * * @param alert * {@link Alert} instance to create string for. * @return Returns string representation of an alert. */ public static String getAlertDescription(Alert alert) { String closing = alert.isOpen() ? "Open" : DateFormat.getDateTimeInstance().format(new Date(alert.getStopTimestamp())); return alert.getAlertingDefinition().getName() + " (" + DateFormat.getDateTimeInstance().format(new Date(alert.getStartTimestamp())) + " - " + closing + ")"; }
/** * Registers the given alert. If the passed {@link Alert} does not refer to the business * transaction metric, then the registration fails. * * @param alert * the {@link Alert} to register */ public void registerAlert(Alert alert) { if (alert == null) { throw new IllegalArgumentException("The given alert may not be null."); } else if (alert.getId() == null) { throw new IllegalArgumentException("The alert must have an ID."); } else if (alert.getAlertingDefinition() == null) { throw new IllegalArgumentException("The alert must have an alerting definition."); } registry.put(alert.getId(), alert); }
/** * Creates a influxDB query for the given alert id. The query retrieves the ids of the * invocation sequences that constitute the alert. * * @param alert * the alert to retrieve the invocation IDs for. * @return Returns the query string. */ public static String buildTraceIdForAlertQuery(Alert alert) { StringBuilder builder = new StringBuilder(); builder.append("SELECT \"").append(Series.BusinessTransaction.FIELD_TRACE_ID).append("\" FROM \"").append(Series.BusinessTransaction.NAME); builder.append("\" WHERE time >= ").append(alert.getStartTimestamp()).append("ms"); if (alert.getStopTimestamp() >= 0) { builder.append(" AND time < ").append(alert.getStopTimestamp()).append("ms"); } for (Entry<String, String> entry : alert.getAlertingDefinition().getTags().entrySet()) { builder.append(" AND \"").append(entry.getKey()).append("\" = '").append(entry.getValue()).append('\''); } builder.append(" AND \"").append(Series.BusinessTransaction.FIELD_DURATION).append("\" >= ").append(alert.getAlertingDefinition().getThreshold()); return builder.toString(); } }
when(alertingDefinition.getTags()).thenReturn(tags); when(alertingDefinition.getNotificationEmailAddresses()).thenReturn(Arrays.asList("test@example.com")); when(alert.getId()).thenReturn("id"); when(alert.getStartTimestamp()).thenReturn(10L); when(templateManager.resolveTemplate(eq(AlertEMailTemplateType.HTML_ALERT_OPEN), any(Map.class))).thenReturn("tplt"); verify(alertingDefinition).getTags(); verifyNoMoreInteractions(alertingDefinition); verify(alert).getStartTimestamp(); verify(alert).getStopTimestamp(); verify(alert).getId(); verifyNoMoreInteractions(alert);
properties.put(Placeholders.THRESHOLD, numberFormat.format(definition.getThreshold())); properties.put(Placeholders.THRESHOLD_TYPE, definition.getThresholdType().getName()); properties.put(Placeholders.START_TIME, String.valueOf(dateFormat.format(new Date(alert.getStartTimestamp())))); properties.put(Placeholders.VIOLATION_VALUE, numberFormat.format(alertingState.getExtremeValue())); properties.put(Placeholders.CURRENT_TIME, String.valueOf(new Date(System.currentTimeMillis()))); properties.put(Placeholders.ALERT_ID, alert.getId()); properties.put(Placeholders.EXTREME_VALUE, numberFormat.format(alertingState.getExtremeValue())); if (alert.getStopTimestamp() > 0) { properties.put(AlertEMailTemplateType.Placeholders.END_TIME, dateFormat.format(new Date(alert.getStopTimestamp()))); properties.put(AlertEMailTemplateType.Placeholders.CLOSING_REASON, alert.getClosingReason().toString());
@Test @SuppressWarnings("unchecked") public void startAlert() { IAlertAction alertAction = Mockito.mock(IAlertAction.class); Iterator<IAlertAction> iterator = Mockito.mock(Iterator.class); when(iterator.hasNext()).thenReturn(true, false); when(iterator.next()).thenReturn(alertAction); when(alertActions.iterator()).thenReturn(iterator); AlertingState alertingState = Mockito.mock(AlertingState.class); AlertingDefinition alertingDefinition = Mockito.mock(AlertingDefinition.class); when(alertingState.getAlertingDefinition()).thenReturn(alertingDefinition); when(alertingState.getLastCheckTime()).thenReturn(1234L); alertingService.alertStarting(alertingState, 1.0D); ArgumentCaptor<Alert> captor = ArgumentCaptor.forClass(Alert.class); verify(alertRegistry).registerAlert(captor.capture()); verifyNoMoreInteractions(alertRegistry); assertThat(captor.getValue().getAlertingDefinition(), is(alertingDefinition)); assertThat(captor.getValue().getStartTimestamp(), is(1234L)); verify(alertAction).onStarting(alertingState); verify(alertActions).iterator(); verifyNoMoreInteractions(alertActions); verify(alertingState).getAlertingDefinition(); verify(alertingState).getLastCheckTime(); verify(alertingState).setAlert(any(Alert.class)); verify(alertingState).setExtremeValue(1.0D); verifyNoMoreInteractions(alertingState); }
/** * Retrieves the status of the alert. * * @return True, if alerts is open, otherwise false. */ public boolean isOpen() { return getStopTimestamp() < 0; }
/** * Updates the mapping of known alerts. */ private void updateKnownAlerts() { BlockingJob<List<Alert>> blockingJob = new BlockingJob<>("Loading alerts..", new Callable<List<Alert>>() { @Override public List<Alert> call() throws Exception { return cmrRepositoryDefinition.getAlertAccessService().getAlerts(); } }); List<Alert> alertList = blockingJob.scheduleAndJoin(); for (Alert knownAlert : alertList) { availableAlerts.put(knownAlert.getId(), knownAlert); } }
@Test public void queryForFinalized() { String query = InfluxQueryFactory.buildTraceIdForAlertQuery(new Alert(alertingDefinition, START_TIME, STOP_TIME)); assertThat(query, is("SELECT \"" + Series.BusinessTransaction.FIELD_TRACE_ID + "\" FROM \"" + Series.BusinessTransaction.NAME + "\" WHERE time >= " + START_TIME + "ms AND time < " + STOP_TIME + "ms AND \"duration\" >= " + THRESHOLD)); }
/** * Returns all business transaction alerts in the registry. * * @return Returns all business transaction alerts in the registry. */ public List<Alert> getBusinessTransactionAlerts() { List<Alert> resultList = new ArrayList<>(); for (Alert alert : getAlerts()) { if (AlertingUtils.isBusinessTransactionAlert(alert.getAlertingDefinition())) { resultList.add(alert); } } return resultList; } }
/** * This method is called when an active alert is ending. * * @param alertingState * the ending alert */ public void alertEnding(AlertingState alertingState) { if (alertingState == null) { throw new IllegalArgumentException("The given alerting state may not be null."); } if (log.isDebugEnabled()) { log.debug("Alert definition '{}' is ending.", alertingState.getAlertingDefinition().getName()); } alertingState.getAlert().close(System.currentTimeMillis(), AlertClosingReason.ALERT_RESOLVED); for (IAlertAction alertAction : alertActions) { alertAction.onEnding(alertingState); } alertingState.setAlert(null); } }
/** * Handle the {@link AbstractAlertingDefinitionEvent}. * * @param event * the received {@link AbstractAlertingDefinitionEvent} */ private void updatedAlertingDefinition(AbstractAlertingDefinitionEvent event) { Iterator<AlertingState> iterator = alertingStates.iterator(); while (iterator.hasNext()) { AlertingState state = iterator.next(); if (Objects.equals(state.getAlertingDefinition().getId(), event.getFirst().getId())) { state.setAlertingDefinition(event.getFirst()); if (null != state.getAlert()) { state.getAlert().setAlertingDefinition(event.getFirst()); } break; } } } }
/** * Filters open / closed alerts depending on the given boolean indicator. * * @param alerts * Alerts to filter. * @param open * Indicator whether open alerts should stay or closed. If true, the results will * contain only open alerts, otherwise only closed alerts. * @return Filtered list of alerts. */ private List<Alert> filterAlerts(List<Alert> alerts, boolean open) { List<Alert> filteredAlerts = new ArrayList<>(); for (Alert alert : alerts) { if (alert.isOpen() == open) { filteredAlerts.add(alert); } } return filteredAlerts; } }
when(alertingDefinition.getTags()).thenReturn(tags); when(alertingDefinition.getNotificationEmailAddresses()).thenReturn(Arrays.asList("test@example.com")); when(alert.getId()).thenReturn("id"); when(alert.getStartTimestamp()).thenReturn(10L); when(templateManager.resolveTemplate(eq(AlertEMailTemplateType.HTML_ALERT_OPEN), any(Map.class))).thenReturn("tplt"); verify(alertingDefinition, times(3)).getTags(); verifyNoMoreInteractions(alertingDefinition); verify(alert).getStartTimestamp(); verify(alert).getStopTimestamp(); verify(alert).getId(); verifyNoMoreInteractions(alert);
when(alertingDefinition.getTags()).thenReturn(tags); when(alertingDefinition.getNotificationEmailAddresses()).thenReturn(Arrays.asList("test@example.com")); when(alert.getId()).thenReturn("id"); when(alert.getStartTimestamp()).thenReturn(10L); when(alert.getStopTimestamp()).thenReturn(20L); when(alert.getClosingReason()).thenReturn(AlertClosingReason.ALERT_RESOLVED); when(templateManager.resolveTemplate(eq(AlertEMailTemplateType.TXT_ALERT_CLOSED), any(Map.class))).thenReturn("tplt"); verify(alertingDefinition).getTags(); verifyNoMoreInteractions(alertingDefinition); verify(alert).getStartTimestamp(); verify(alert, times(2)).getStopTimestamp(); verify(alert).getId(); verify(alert).getClosingReason(); verifyNoMoreInteractions(alert);
@Test public void getAlert() { Alert testAlert = Mockito.mock(Alert.class, Mockito.RETURNS_MOCKS); when(testAlert.getId()).thenReturn("id"); alertRegistry.registerAlert(testAlert); Alert alert = alertRegistry.getAlert("id"); verify(testAlert, times(2)).getId(); verify(testAlert).getAlertingDefinition(); verifyNoMoreInteractions(testAlert); assertThat(alert, equalTo(testAlert)); }
@Test(expectedExceptions = { IllegalArgumentException.class }) public void alertAlertingDefinitionIsNull() throws Exception { Alert testAlert = Mockito.mock(Alert.class); when(testAlert.getId()).thenReturn("id"); alertRegistry.registerAlert(testAlert); }
@Test public void queryForNotFinalized() { String query = InfluxQueryFactory.buildTraceIdForAlertQuery(new Alert(alertingDefinition, START_TIME)); assertThat(query, is("SELECT \"" + Series.BusinessTransaction.FIELD_TRACE_ID + "\" FROM \"" + Series.BusinessTransaction.NAME + "\" WHERE time >= " + START_TIME + "ms AND \"duration\" >= " + THRESHOLD)); }
throw new BusinessException("Retrieving invocation sequences for alert with id '" + alertId + "'", AlertErrorCodeEnum.UNKNOWN_ALERT_ID); if (!AlertingUtils.isBusinessTransactionAlert(alert.getAlertingDefinition())) { throw new BusinessException("The given alert '" + alertId + "' is not related to a buisness transaction.", AlertErrorCodeEnum.NO_BTX_ALERT);