Skip to content

Commit

Permalink
Merge pull request #76 from jshaughn/hwkalerts-73
Browse files Browse the repository at this point in the history
HWKALERTS-73 Add context data to Triggers
  • Loading branch information
lucasponce committed Jul 31, 2015
2 parents e7d847f + e1c3c81 commit 73236ca
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 24 deletions.
Expand Up @@ -20,7 +20,9 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hawkular.alerts.api.model.Severity;
Expand Down Expand Up @@ -92,8 +94,8 @@ public enum Status {
private String resolvedNotes;

/*
* This is the trigger defined when the alert was fired.
* A trigger definition can change during time, but an alert should be attached with a specific instance.
* If set this should be the trigger as defined when the alert was fired. A trigger definition can change
* over time, but an alert should be attached with the relevant instance.
*/
@JsonInclude(Include.NON_EMPTY)
@Thin
Expand All @@ -111,6 +113,13 @@ public enum Status {
@Thin
private List<Set<ConditionEval>> resolvedEvalSets;

/*
* This should be initialized to the owning trigger's context. It is not set automatically so as to allow
* for flexibility. Note, this is not marked as Thin, whereas the trigger is Thin.
*/
@JsonInclude(Include.NON_EMPTY)
private Map<String, String> context;

public Alert() {
// for json assembly
}
Expand All @@ -120,6 +129,7 @@ public Alert(String tenantId, String triggerId, Severity severity, List<Set<Cond
this.triggerId = triggerId;
this.severity = (null == severity) ? Severity.MEDIUM : severity;
this.evalSets = evalSets;

this.ctime = System.currentTimeMillis();
this.status = Status.OPEN;

Expand Down Expand Up @@ -254,6 +264,29 @@ public void setDampening(Dampening dampening) {
this.dampening = dampening;
}

public Map<String, String> getContext() {
return context;
}

public void setContext(Map<String, String> context) {
this.context = context;
}

/**
* Add context information.
* @param name context key.
* @param value context value.
*/
public void addProperty(String name, String value) {
if (null == name || null == value) {
throw new IllegalArgumentException("Propety must have non-null name and value");
}
if (null == context) {
context = new HashMap<>();
}
context.put(name, value);
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down Expand Up @@ -282,7 +315,8 @@ public boolean equals(Object obj) {
@Override
public String toString() {
return "Alert [alertId=" + alertId + ", status=" + status + ", ackTime=" + ackTime
+ ", ackBy=" + ackBy + ", resolvedTime=" + resolvedTime + ", resolvedBy=" + resolvedBy + "]";
+ ", ackBy=" + ackBy + ", resolvedTime=" + resolvedTime + ", resolvedBy=" + resolvedBy + ", context="
+ context + "]";
}

}
Expand Up @@ -16,6 +16,7 @@
*/
package org.hawkular.alerts.api.model.trigger;

import java.util.Map;
import java.util.UUID;

import com.fasterxml.jackson.annotation.JsonIgnore;
Expand Down Expand Up @@ -56,15 +57,23 @@ public Trigger() {
}

public Trigger(String name) {
this(generateId(), name);
this(generateId(), name, null);
}

public Trigger(String name, Map<String, String> context) {
this(generateId(), name, context);
}

public static String generateId() {
return UUID.randomUUID().toString();
}

public Trigger(String id, String name) {
super(name);
this(id, name, null);
}

public Trigger(String id, String name, Map<String, String> context) {
super(name, context);

if (id == null || id.isEmpty()) {
throw new IllegalArgumentException("Trigger id must be non-empty");
Expand Down Expand Up @@ -157,7 +166,7 @@ public int hashCode() {
public String toString() {
return "Trigger [tenantId=" + tenantId + " id=" + id + ", enabled=" + enabled + ", mode=" + mode +
", getName()=" + getName() + ", isAutoDisable()=" + isAutoDisable() + ", isAutoEnable()="
+ isAutoEnable() + ", isAutoResolve()=" + isAutoResolve() + "]";
+ isAutoEnable() + ", isAutoResolve()=" + isAutoResolve() + ", context=" + context + "]";
}

}
Expand Up @@ -69,8 +69,16 @@ public enum Match {
@JsonInclude
private Match autoResolveMatch;

@JsonInclude(Include.NON_EMPTY)
protected Map<String, String> context;

public TriggerTemplate(String name) {
this(name, null);
}

public TriggerTemplate(String name, Map<String, String> context) {
this.name = name;
this.context = context;

this.autoDisable = false;
this.autoEnable = false;
Expand Down Expand Up @@ -203,12 +211,36 @@ public void removeAction(String actionPlugin, String actionId) {
}
}

public Map<String, String> getContext() {
return context;
}

public void setContext(Map<String, String> context) {
this.context = context;
}

/**
* Add context information.
* @param name context key.
* @param value context value.
*/
public void addProperty(String name, String value) {
if (null == name || null == value) {
throw new IllegalArgumentException("Propety must have non-null name and value");
}
if (null == context) {
context = new HashMap<>();
}
context.put(name, value);
}

@Override
public String toString() {
return "TriggerTemplate [name=" + name + ", " +
"description=" + description + ", " +
"firingMatch=" + firingMatch + ", " +
"safetyMatch=" + autoResolveMatch + "]";
"safetyMatch=" + autoResolveMatch + ", " +
"context=" + context + "]";
}

}
Expand Up @@ -173,15 +173,19 @@ public void jsonToAlertTest() throws Exception {
"\"ackNotes\":null," +
"\"resolvedTime\":0," +
"\"resolvedBy\":null," +
"\"resolvedNotes\":null" +
"}";
"\"resolvedNotes\":null," +
"\"context\":{\"n1\":\"v1\",\"n2\":\"v2\"}}";

ObjectMapper mapper = new ObjectMapper();
Alert alert = mapper.readValue(jsonAlert, Alert.class);
assertNotNull(alert);
assertNotNull(alert.getEvalSets());
assertEquals(1, alert.getEvalSets().size());
assertEquals(2, alert.getEvalSets().get(0).size());
assertTrue(alert.getContext() != null);
assertTrue(alert.getContext().size() == 2);
assertTrue(alert.getContext().get("n1").equals("v1"));
assertTrue(alert.getContext().get("n2").equals("v2"));

/*
Testing thin deserializer
Expand Down Expand Up @@ -883,7 +887,8 @@ public void jsonTriggerTest() throws Exception {
"\"autoEnable\":true," +
"\"autoResolve\":true," +
"\"autoResolveAlerts\":true," +
"\"severity\":\"HIGH\"}";
"\"severity\":\"HIGH\"," +
"\"context\":{\"n1\":\"v1\",\"n2\":\"v2\"}}";
Trigger trigger = objectMapper.readValue(str, Trigger.class);

assertTrue(trigger.getName().equals("test-name"));
Expand All @@ -899,6 +904,10 @@ public void jsonTriggerTest() throws Exception {
assertTrue(trigger.isAutoResolve());
assertTrue(trigger.isAutoResolveAlerts());
assertTrue(trigger.getSeverity() == Severity.HIGH);
assertTrue(trigger.getContext() != null);
assertTrue(trigger.getContext().size() == 2);
assertTrue(trigger.getContext().get("n1").equals("v1"));
assertTrue(trigger.getContext().get("n2").equals("v2"));

String output = objectMapper.writeValueAsString(trigger);

Expand Down
Expand Up @@ -187,6 +187,7 @@ private void initTriggers(File fFolder) throws Exception {
TriggerTemplate.Match autoResolveMatch = TriggerTemplate.Match
.valueOf((String)t.get("autoResolveMatch"));
List<Map<String, String>> actions = (List<Map<String, String>>)t.get("actions");
Map<String, String> context = (Map<String, String>)t.get("context");

Trigger trigger = new Trigger(triggerId, name);
trigger.setEnabled(enabled);
Expand All @@ -202,6 +203,7 @@ private void initTriggers(File fFolder) throws Exception {
for (Map<String, String> action : actions) {
trigger.addAction(action.get("actionPlugin"), action.get("actionId"));
}
trigger.setContext(context);
addTrigger(tenantId, trigger);
log.debugf("Init file - Inserting [%s]", trigger);
}
Expand Down Expand Up @@ -447,7 +449,7 @@ public void addTrigger(String tenantId, Trigger trigger) throws Exception {
trigger.isAutoDisable(), trigger.isAutoEnable(), trigger.isAutoResolve(),
trigger.isAutoResolveAlerts(), trigger.getSeverity().name(),
trigger.getFiringMatch().name(), trigger.getAutoResolveMatch().name(),
trigger.getId(), trigger.isEnabled(), trigger.getTenantId()));
trigger.getId(), trigger.isEnabled(), trigger.getTenantId(), trigger.getContext()));

insertTriggerActions(trigger);
} catch (Exception e) {
Expand Down Expand Up @@ -773,6 +775,7 @@ private Trigger mapTrigger(Row row) {
trigger.setId(row.getString("id"));
trigger.setEnabled(row.getBool("enabled"));
trigger.setTenantId(row.getString("tenantId"));
trigger.setContext(row.getMap("context", String.class, String.class));

return trigger;
}
Expand Down
Expand Up @@ -224,7 +224,7 @@ public class CassStatement {

INSERT_TRIGGER = "INSERT INTO " + keyspace + ".triggers " +
"(name, description, autoDisable, autoEnable, autoResolve, autoResolveAlerts, severity, firingMatch, "
+ "autoResolveMatch, id, enabled, tenantId) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
+ "autoResolveMatch, id, enabled, tenantId, context) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";

INSERT_TRIGGER_ACTIONS = "INSERT INTO " + keyspace + ".triggers_actions "
+ "(tenantId, triggerId, actionPlugin, actions) VALUES (?, ?, ?, ?) ";
Expand Down Expand Up @@ -336,7 +336,7 @@ public class CassStatement {
+ "WHERE tenantId = ? AND name = ? ";

SELECT_TRIGGER = "SELECT name, description, autoDisable, autoEnable, autoResolve, "
+ "autoResolveAlerts, severity, firingMatch, autoResolveMatch, id, enabled, tenantId "
+ "autoResolveAlerts, severity, firingMatch, autoResolveMatch, id, enabled, tenantId, context "
+ "FROM " + keyspace + ".triggers "
+ "WHERE tenantId = ? AND id = ? ";

Expand Down Expand Up @@ -368,11 +368,11 @@ public class CassStatement {
+ "WHERE tenantId = ? AND triggerId = ? and triggerMode = ? ";

SELECT_TRIGGERS_ALL = "SELECT name, description, autoDisable, autoEnable, autoResolve, "
+ "autoResolveAlerts, severity, firingMatch, autoResolveMatch, id, enabled, tenantId "
+ "autoResolveAlerts, severity, firingMatch, autoResolveMatch, id, enabled, tenantId, context "
+ "FROM " + keyspace + ".triggers ";

SELECT_TRIGGERS_TENANT = "SELECT name, description, autoDisable, autoEnable, autoResolve, "
+ "autoResolveAlerts, severity, firingMatch, autoResolveMatch, id, enabled, tenantId "
+ "autoResolveAlerts, severity, firingMatch, autoResolveMatch, id, enabled, tenantId, context "
+ "FROM " + keyspace + ".triggers WHERE tenantId = ? ";

UPDATE_ACTION = "UPDATE " + keyspace + ".actions SET properties = ? "
Expand Down
Expand Up @@ -41,6 +41,7 @@ CREATE TABLE ${keyspace}.triggers (
autoResolveMatch text,
id text,
enabled boolean,
context map<text,text>,
PRIMARY KEY (tenantId, id)
);

Expand Down
Expand Up @@ -430,6 +430,7 @@ rule AlertOnSatisfiedDampening
Alert newAlert = new Alert( $t.getTenantId(), $tid, $t.getSeverity(), $d.getSatisfyingEvals() );
newAlert.setTrigger($t);
newAlert.setDampening($d);
newAlert.setContext($t.getContext());
alerts.add(newAlert);
if (actions != null) {
for (String actionPlugin : $t.getActions().keySet()) {
Expand Down

0 comments on commit 73236ca

Please sign in to comment.