/** * Retrieves the status of the alert. * * @return True, if alerts is open, otherwise false. */ public boolean isOpen() { return getStopTimestamp() < 0; }
/** * 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); }
/** * 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 + ")"; }
@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)); } }
/** * 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(); } }
@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)); }
/** * 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); } }
/** * {@inheritDoc} */ @MethodLog @Override public RecordingData getRecordingData() { if ((storageManager.getRecordingState() == RecordingState.ON) || (storageManager.getRecordingState() == RecordingState.SCHEDULED)) { RecordingData recordingData = new RecordingData(); RecordingProperties recordingProperties = storageManager.getRecordingProperties(); if (null != recordingProperties) { recordingData.setRecordStartDate(recordingProperties.getRecordStartDate()); recordingData.setRecordEndDate(recordingProperties.getRecordEndDate()); } recordingData.setRecordingStorage(storageManager.getRecordingStorage()); recordingData.setRecordingWritingStatus(storageManager.getRecordingStatus()); return recordingData; } else { return null; } }
/** * 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; } }
/** * 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; } }
/** * {@inheritDoc} */ @Override public int hashCode() { final int prime = 31; int result = 1; result = (prime * result) + ((this.alertingDefinition == null) ? 0 : this.alertingDefinition.hashCode()); result = (prime * result) + ((this.closingReason == null) ? 0 : this.closingReason.hashCode()); result = (prime * result) + ((this.id == null) ? 0 : this.id.hashCode()); result = (prime * result) + (int) (this.startTimestamp ^ (this.startTimestamp >>> 32)); result = (prime * result) + (int) (this.stopTimestamp ^ (this.stopTimestamp >>> 32)); return result; }
/** * {@inheritDoc} */ @Override public int hashCode() { final int prime = 31; int result = 1; result = (prime * result) + ((alert == null) ? 0 : alert.hashCode()); result = (prime * result) + ((alertingDefinition == null) ? 0 : alertingDefinition.hashCode()); long temp; temp = Double.doubleToLongBits(extremeValue); result = (prime * result) + (int) (temp ^ (temp >>> 32)); result = (prime * result) + (int) (lastCheckTime ^ (lastCheckTime >>> 32)); result = (prime * result) + validCount; return result; }
/** * Updates the write status. */ private void checkWritingStatus() { if (null != writingExecutorService) { long completedTasks = writingExecutorService.getCompletedTaskCount(); long queuedTasks = writingExecutorService.getTaskCount() - completedTasks; long arrivedTasksForPeriod = (queuedTasks + completedTasks) - totalTasks; long finishedTasksForPeriod = completedTasks - finishedTasks; writingStatus = WritingStatus.getWritingStatus(arrivedTasksForPeriod, finishedTasksForPeriod); finishedTasks = completedTasks; totalTasks = completedTasks + queuedTasks; } else { writingStatus = WritingStatus.GOOD; } }
@Test public void testConstruction1() { Alert alert = new Alert(alertingDefinition, 1L); assertThat(alert.getId(), notNullValue()); assertThat(alert.getAlertingDefinition(), is(alertingDefinition)); assertThat(alert.getStartTimestamp(), is(1L)); }
@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 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)); }
@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 registerSuccessful() { Alert testAlert = Mockito.mock(Alert.class, Mockito.RETURNS_MOCKS); when(testAlert.getId()).thenReturn("id"); alertRegistry.registerAlert(testAlert); verify(testAlert, times(2)).getId(); verify(testAlert).getAlertingDefinition(); verifyNoMoreInteractions(testAlert); assertThat(alertRegistry.getAlert("id"), equalTo(testAlert)); }
@Test public void queryWithTags() throws BusinessException { String key1 = "keyOne"; String key2 = "keyTwo"; String value1 = "valueOne"; String value2 = "valueTwo"; alertingDefinition.putTag(key1, value1); alertingDefinition.putTag(key2, value2); String query = InfluxQueryFactory.buildTraceIdForAlertQuery(new Alert(alertingDefinition, START_TIME)); assertThat(query, containsString("SELECT \"" + Series.BusinessTransaction.FIELD_TRACE_ID + "\" FROM \"" + Series.BusinessTransaction.NAME + "\"")); assertThat(query, containsString("time >= " + START_TIME)); assertThat(query, not(containsString("time < " + STOP_TIME))); assertThat(query, containsString("\"duration\" >= " + THRESHOLD)); assertThat(query, containsString("\"" + key1 + "\" = '" + value1 + "'")); assertThat(query, containsString("\"" + key2 + "\" = '" + value2 + "'")); }
/** * This method is called when a new alert is started. * * @param alertingState * the started alert * @param violationValue * the value has violated the threshold */ public void alertStarting(AlertingState alertingState, double violationValue) { if (alertingState == null) { throw new IllegalArgumentException("The given alerting state may not be null."); } Alert alert = new Alert(alertingState.getAlertingDefinition(), alertingState.getLastCheckTime()); alertRegistry.registerAlert(alert); alertingState.setAlert(alert); alertingState.setExtremeValue(violationValue); for (IAlertAction alertAction : alertActions) { alertAction.onStarting(alertingState); } }