public String getAlertId() { ensureAlertId(); return alertId; }
private void notifyAlert(PublishPartition partition, AlertStreamEvent event) { // remove the column values for publish plugin match partition.getColumnValues().clear(); if (!publishPluginMapping.containsKey(partition.getPublishId())) { LOG.warn("PublishPartition {} is not found in publish plugin map", partition); return; } AlertPublishPlugin plugin = publishPluginMapping.get(partition.getPublishId()); if (plugin == null) { LOG.warn("PublishPartition {} has problems while initializing publish plugin", partition); return; } event.ensureAlertId(); try { LOG.debug("Execute alert publisher {}", plugin.getClass().getCanonicalName()); plugin.onAlert(event); } catch (Exception ex) { LOG.error("Fail invoking publisher's onAlert, continue ", ex); } }
/** * TODO: Refactor wrapAlertPublishEvent into alertTemplateEngine and remove extraData from AlertStreamEvent. */ @Override public AlertStreamEvent filter(AlertStreamEvent event) { event.ensureAlertId(); Map<String, Object> extraData = new HashMap<>(); List<String> appIds = new ArrayList<>(); if (alertPublisherBolt.policyDefinitionMap == null || alertPublisherBolt.streamDefinitionMap == null) { LOG.warn("policyDefinitions or streamDefinitions in publisher bolt have not been initialized"); } else { PolicyDefinition policyDefinition = alertPublisherBolt.policyDefinitionMap.get(event.getPolicyId()); if (alertPublisherBolt.policyDefinitionMap != null && policyDefinition != null) { for (String inputStreamId : policyDefinition.getInputStreams()) { StreamDefinition sd = alertPublisherBolt.streamDefinitionMap.get(inputStreamId); if (sd != null) { extraData.put(AlertPublishEvent.SITE_ID_KEY, sd.getSiteId()); appIds.add(sd.getStreamSource()); } } extraData.put(AlertPublishEvent.APP_IDS_KEY, appIds); extraData.put(AlertPublishEvent.POLICY_VALUE_KEY, policyDefinition.getDefinition().getValue()); event.setSeverity(policyDefinition.getAlertSeverity()); event.setCategory(policyDefinition.getAlertCategory()); } event.setContext(extraData); } return event; } }
@Override public void emit(AlertStreamEvent event) { if (event == null) { return; } event.ensureAlertId(); Set<PublishPartition> clonedPublishPartitions = new HashSet<>(publishPartitions); for (PublishPartition publishPartition : clonedPublishPartitions) { // skip the publish partition which is not belong to this policy and also check streamId PublishPartition cloned = publishPartition.clone(); Optional.ofNullable(event) .filter(x -> x != null && x.getSchema() != null && cloned.getPolicyId().equalsIgnoreCase(x.getPolicyId()) && (cloned.getStreamId().equalsIgnoreCase(x.getSchema().getStreamId()) || cloned.getStreamId().equalsIgnoreCase(Publishment.STREAM_NAME_DEFAULT))) .ifPresent(x -> { cloned.getColumns().stream() .filter(y -> event.getSchema().getColumnIndex(y) >= 0 && event.getSchema().getColumnIndex(y) < event.getSchema().getColumns().size()) .map(y -> event.getData()[event.getSchema().getColumnIndex(y)]) .filter(y -> y != null) .forEach(y -> cloned.getColumnValues().add(y)); synchronized (outputLock) { streamContext.counter().incr("alert_count"); delegate.emit(Arrays.asList(cloned, event)); } }); } }
public static AlertStreamEvent createEvent(StreamDefinition stream, PolicyDefinition policy, Object[] data) { AlertStreamEvent event = new AlertStreamEvent(); event.setPolicyId(policy.getName()); event.setSchema(stream); event.setStreamId(stream.getStreamId()); event.setTimestamp(System.currentTimeMillis()); event.setCreatedTime(System.currentTimeMillis()); event.setSubject("Namenode Disk Used 98%"); event.setBody("Disk Usage of Test cluster's name node (<a href=\"#\">namenode.hostname.domain</a>) is <strong style=\"color: red\">98%</strong> at <strong>2016-11-30 12:30:45</strong>, exceeding alert threshold <strong>90</strong>%"); event.setData(data); event.ensureAlertId(); event.setSeverity(AlertSeverity.CRITICAL); event.setCategory("HDFS"); event.setContext(new HashMap<String,Object>(){{ put(AlertPublishEvent.SITE_ID_KEY,"TestCluster"); }}); Assert.assertNotNull(event.getAlertId()); return event; }
private AlertStreamEvent mockAlertEvent (String policyId) { AlertStreamEvent event = new AlertStreamEvent(); event.setSiteId("test"); event.setCreatedBy("junit"); event.setCreatedTime(1480491075923L); event.setPolicyId(policyId); event.setStreamId("ALERT_STREAM"); event.setSchema(mockAlertStreamDefinition("ALERT_STREAM")); event.setMetaVersion("SAMPLE_META_VERSION"); event.setTimestamp(1480491075923L); event.setData(new Object[]{"test_cluster", "cpu.usage", "localhost", "hadoop", 0.98}); event.ensureAlertId(); return event; }
alertStreamEvent.setPolicyId("setPolicyId"); alertStreamEvent.setCreatedTime(1234); alertStreamEvent.ensureAlertId(); AlertPublishEvent alertPublishEvent = AlertPublishEvent.createAlertPublishEvent(alertStreamEvent); Assert.assertEquals(null, alertPublishEvent.getSiteId());