Alerting : Fix IndexAction
This commit contains fixes to the IndexAlertAction and normalizes the serialization of Alerts using ToXContent interface. Original commit: elastic/x-pack-elasticsearch@47325bdd01
This commit is contained in:
parent
26e053eaed
commit
4c1c502f80
|
@ -7,6 +7,7 @@ package org.elasticsearch.alerting;
|
||||||
|
|
||||||
import org.elasticsearch.common.joda.time.DateTime;
|
import org.elasticsearch.common.joda.time.DateTime;
|
||||||
import org.elasticsearch.common.unit.TimeValue;
|
import org.elasticsearch.common.unit.TimeValue;
|
||||||
|
import org.elasticsearch.common.xcontent.ToXContent;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||||
import org.elasticsearch.common.xcontent.XContentType;
|
import org.elasticsearch.common.xcontent.XContentType;
|
||||||
|
@ -17,7 +18,7 @@ import java.util.List;
|
||||||
/**
|
/**
|
||||||
* Created by brian on 8/12/14.
|
* Created by brian on 8/12/14.
|
||||||
*/
|
*/
|
||||||
public class Alert {
|
public class Alert implements ToXContent{
|
||||||
private final String alertName;
|
private final String alertName;
|
||||||
private String queryName;
|
private String queryName;
|
||||||
private AlertTrigger trigger;
|
private AlertTrigger trigger;
|
||||||
|
@ -121,7 +122,8 @@ public class Alert {
|
||||||
this.running = running;
|
this.running = running;
|
||||||
}
|
}
|
||||||
|
|
||||||
public XContentBuilder toXContent(XContentBuilder builder) throws IOException {
|
@Override
|
||||||
|
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||||
|
|
||||||
//Note we deliberately don't serialize the version here
|
//Note we deliberately don't serialize the version here
|
||||||
builder.startObject();
|
builder.startObject();
|
||||||
|
@ -131,13 +133,13 @@ public class Alert {
|
||||||
builder.field(AlertManager.LASTRAN_FIELD.getPreferredName(), lastRan);
|
builder.field(AlertManager.LASTRAN_FIELD.getPreferredName(), lastRan);
|
||||||
builder.field(AlertManager.CURRENTLY_RUNNING.getPreferredName(), running);
|
builder.field(AlertManager.CURRENTLY_RUNNING.getPreferredName(), running);
|
||||||
builder.field(AlertManager.TRIGGER_FIELD.getPreferredName());
|
builder.field(AlertManager.TRIGGER_FIELD.getPreferredName());
|
||||||
trigger.toXContent(builder);
|
trigger.toXContent(builder, params);
|
||||||
builder.field(AlertManager.ACTION_FIELD.getPreferredName());
|
builder.field(AlertManager.ACTION_FIELD.getPreferredName());
|
||||||
|
|
||||||
builder.startObject();
|
builder.startObject();
|
||||||
for (AlertAction action : actions){
|
for (AlertAction action : actions){
|
||||||
builder.field(action.getActionName());
|
builder.field(action.getActionName());
|
||||||
action.toXContent(builder);
|
action.toXContent(builder, params);
|
||||||
}
|
}
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
|
|
||||||
|
|
|
@ -5,14 +5,14 @@
|
||||||
*/
|
*/
|
||||||
package org.elasticsearch.alerting;
|
package org.elasticsearch.alerting;
|
||||||
|
|
||||||
|
import org.elasticsearch.common.xcontent.ToXContent;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.common.xcontent.XContentType;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public interface AlertAction {
|
public interface AlertAction extends ToXContent {
|
||||||
public String getActionName();
|
public String getActionName();
|
||||||
public XContentBuilder toXContent(XContentBuilder builder) throws IOException;
|
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException;
|
||||||
|
|
||||||
public boolean doAction(String alertName, AlertResult alert);
|
public boolean doAction(String alertName, AlertResult alert);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,6 @@ package org.elasticsearch.alerting;
|
||||||
import org.elasticsearch.ElasticsearchException;
|
import org.elasticsearch.ElasticsearchException;
|
||||||
import org.elasticsearch.ElasticsearchIllegalArgumentException;
|
import org.elasticsearch.ElasticsearchIllegalArgumentException;
|
||||||
import org.elasticsearch.ElasticsearchIllegalStateException;
|
import org.elasticsearch.ElasticsearchIllegalStateException;
|
||||||
import org.elasticsearch.action.ActionListener;
|
|
||||||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
|
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
|
||||||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
|
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
|
||||||
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
|
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
|
||||||
|
@ -19,7 +18,6 @@ import org.elasticsearch.action.get.GetResponse;
|
||||||
import org.elasticsearch.action.index.IndexRequest;
|
import org.elasticsearch.action.index.IndexRequest;
|
||||||
import org.elasticsearch.action.search.SearchResponse;
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
import org.elasticsearch.action.update.UpdateRequest;
|
import org.elasticsearch.action.update.UpdateRequest;
|
||||||
import org.elasticsearch.action.update.UpdateResponse;
|
|
||||||
import org.elasticsearch.client.Client;
|
import org.elasticsearch.client.Client;
|
||||||
import org.elasticsearch.client.Requests;
|
import org.elasticsearch.client.Requests;
|
||||||
import org.elasticsearch.common.Nullable;
|
import org.elasticsearch.common.Nullable;
|
||||||
|
@ -30,12 +28,11 @@ import org.elasticsearch.common.inject.Inject;
|
||||||
import org.elasticsearch.common.joda.time.DateTime;
|
import org.elasticsearch.common.joda.time.DateTime;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.common.unit.TimeValue;
|
import org.elasticsearch.common.unit.TimeValue;
|
||||||
|
import org.elasticsearch.common.xcontent.ToXContent;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||||
import org.elasticsearch.common.xcontent.XContentHelper;
|
import org.elasticsearch.common.xcontent.XContentHelper;
|
||||||
import org.elasticsearch.index.engine.Engine;
|
|
||||||
import org.elasticsearch.search.SearchHit;
|
import org.elasticsearch.search.SearchHit;
|
||||||
import org.elasticsearch.search.SearchHitField;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -177,7 +174,7 @@ public class AlertManager extends AbstractLifecycleComponent {
|
||||||
XContentBuilder alertBuilder;
|
XContentBuilder alertBuilder;
|
||||||
try {
|
try {
|
||||||
alertBuilder = XContentFactory.jsonBuilder();
|
alertBuilder = XContentFactory.jsonBuilder();
|
||||||
alert.toXContent(alertBuilder);
|
alert.toXContent(alertBuilder, ToXContent.EMPTY_PARAMS);
|
||||||
} catch (IOException ie) {
|
} catch (IOException ie) {
|
||||||
throw new ElasticsearchException("Unable to serialize alert ["+ alertName + "]", ie);
|
throw new ElasticsearchException("Unable to serialize alert ["+ alertName + "]", ie);
|
||||||
}
|
}
|
||||||
|
@ -190,13 +187,13 @@ public class AlertManager extends AbstractLifecycleComponent {
|
||||||
logger.error("Failed to update in claim", ee);
|
logger.error("Failed to update in claim", ee);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized (alertMap) { //Update the alert map
|
synchronized (alertMap) { //Update the alert map
|
||||||
if (alertMap.containsKey(alertName)) {
|
if (alertMap.containsKey(alertName)) {
|
||||||
alertMap.get(alertName).running(scheduleRunTime);
|
alertMap.get(alertName).running(scheduleRunTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Alert getAlertFromIndex(String alertName) {
|
private Alert getAlertFromIndex(String alertName) {
|
||||||
|
@ -260,7 +257,7 @@ public class AlertManager extends AbstractLifecycleComponent {
|
||||||
Alert alert = getAlertForName(alertName);
|
Alert alert = getAlertForName(alertName);
|
||||||
alert.lastRan(fireTime);
|
alert.lastRan(fireTime);
|
||||||
XContentBuilder alertBuilder = XContentFactory.jsonBuilder().prettyPrint();
|
XContentBuilder alertBuilder = XContentFactory.jsonBuilder().prettyPrint();
|
||||||
alert.toXContent(alertBuilder);
|
alert.toXContent(alertBuilder, ToXContent.EMPTY_PARAMS);
|
||||||
UpdateRequest updateRequest = new UpdateRequest();
|
UpdateRequest updateRequest = new UpdateRequest();
|
||||||
updateRequest.id(alertName);
|
updateRequest.id(alertName);
|
||||||
updateRequest.index(ALERT_INDEX);
|
updateRequest.index(ALERT_INDEX);
|
||||||
|
@ -286,7 +283,7 @@ public class AlertManager extends AbstractLifecycleComponent {
|
||||||
historyEntry.field("triggered", triggered);
|
historyEntry.field("triggered", triggered);
|
||||||
historyEntry.field("fireTime", fireTime.toDateTimeISO());
|
historyEntry.field("fireTime", fireTime.toDateTimeISO());
|
||||||
historyEntry.field("trigger");
|
historyEntry.field("trigger");
|
||||||
trigger.toXContent(historyEntry);
|
trigger.toXContent(historyEntry, ToXContent.EMPTY_PARAMS);
|
||||||
historyEntry.field("queryRan", XContentHelper.convertToJson(triggeringQuery.bytes(),false,true));
|
historyEntry.field("queryRan", XContentHelper.convertToJson(triggeringQuery.bytes(),false,true));
|
||||||
historyEntry.field("numberOfResults", numberOfResults);
|
historyEntry.field("numberOfResults", numberOfResults);
|
||||||
if (indices != null) {
|
if (indices != null) {
|
||||||
|
@ -352,7 +349,7 @@ public class AlertManager extends AbstractLifecycleComponent {
|
||||||
XContentBuilder builder;
|
XContentBuilder builder;
|
||||||
try {
|
try {
|
||||||
builder = XContentFactory.jsonBuilder();
|
builder = XContentFactory.jsonBuilder();
|
||||||
alert.toXContent(builder);
|
alert.toXContent(builder, ToXContent.EMPTY_PARAMS);
|
||||||
IndexRequest indexRequest = new IndexRequest(ALERT_INDEX, ALERT_TYPE, alertName);
|
IndexRequest indexRequest = new IndexRequest(ALERT_INDEX, ALERT_TYPE, alertName);
|
||||||
indexRequest.listenerThreaded(false);
|
indexRequest.listenerThreaded(false);
|
||||||
indexRequest.operationThreaded(false);
|
indexRequest.operationThreaded(false);
|
||||||
|
|
|
@ -9,10 +9,7 @@ import org.elasticsearch.ElasticsearchIllegalArgumentException;
|
||||||
import org.elasticsearch.common.inject.Inject;
|
import org.elasticsearch.common.inject.Inject;
|
||||||
import org.elasticsearch.common.logging.ESLogger;
|
import org.elasticsearch.common.logging.ESLogger;
|
||||||
import org.elasticsearch.common.logging.Loggers;
|
import org.elasticsearch.common.logging.Loggers;
|
||||||
import org.elasticsearch.common.xcontent.XContent;
|
import org.elasticsearch.common.xcontent.*;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
||||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
|
||||||
import org.elasticsearch.common.xcontent.XContentHelper;
|
|
||||||
import org.elasticsearch.rest.*;
|
import org.elasticsearch.rest.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -70,7 +67,7 @@ public class AlertRestHandler implements RestHandler {
|
||||||
boolean added = alertManager.addAlert(alert.alertName(), alert, true);
|
boolean added = alertManager.addAlert(alert.alertName(), alert, true);
|
||||||
if (added) {
|
if (added) {
|
||||||
XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
|
XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
|
||||||
alert.toXContent(builder);
|
alert.toXContent(builder, ToXContent.EMPTY_PARAMS);
|
||||||
restChannel.sendResponse(new BytesRestResponse(OK, builder));
|
restChannel.sendResponse(new BytesRestResponse(OK, builder));
|
||||||
} else {
|
} else {
|
||||||
restChannel.sendResponse(new BytesRestResponse(BAD_REQUEST));
|
restChannel.sendResponse(new BytesRestResponse(BAD_REQUEST));
|
||||||
|
@ -103,7 +100,7 @@ public class AlertRestHandler implements RestHandler {
|
||||||
builder.startObject();
|
builder.startObject();
|
||||||
for (Map.Entry<String, Alert> alertEntry : alertMap.entrySet()) {
|
for (Map.Entry<String, Alert> alertEntry : alertMap.entrySet()) {
|
||||||
builder.field(alertEntry.getKey());
|
builder.field(alertEntry.getKey());
|
||||||
alertEntry.getValue().toXContent(builder);
|
alertEntry.getValue().toXContent(builder, ToXContent.EMPTY_PARAMS);
|
||||||
}
|
}
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
return builder;
|
return builder;
|
||||||
|
|
|
@ -6,16 +6,14 @@
|
||||||
package org.elasticsearch.alerting;
|
package org.elasticsearch.alerting;
|
||||||
|
|
||||||
import org.elasticsearch.ElasticsearchIllegalArgumentException;
|
import org.elasticsearch.ElasticsearchIllegalArgumentException;
|
||||||
|
import org.elasticsearch.common.xcontent.ToXContent;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||||
import org.elasticsearch.common.xcontent.XContentType;
|
import org.elasticsearch.common.xcontent.XContentType;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
public class AlertTrigger implements ToXContent {
|
||||||
* Created by brian on 8/12/14.
|
|
||||||
*/
|
|
||||||
public class AlertTrigger {
|
|
||||||
|
|
||||||
private SimpleTrigger trigger;
|
private SimpleTrigger trigger;
|
||||||
private TriggerType triggerType;
|
private TriggerType triggerType;
|
||||||
|
@ -107,7 +105,8 @@ public class AlertTrigger {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public XContentBuilder toXContent(XContentBuilder builder) throws IOException {
|
@Override
|
||||||
|
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||||
builder.startObject();
|
builder.startObject();
|
||||||
builder.field(triggerType.toString(), trigger.toString() + value);
|
builder.field(triggerType.toString(), trigger.toString() + value);
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
|
|
|
@ -57,7 +57,7 @@ public class EmailAlertAction implements AlertAction {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public XContentBuilder toXContent(XContentBuilder builder) throws IOException {
|
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||||
builder.startObject();
|
builder.startObject();
|
||||||
builder.field("addresses");
|
builder.field("addresses");
|
||||||
builder.startArray();
|
builder.startArray();
|
||||||
|
|
|
@ -9,15 +9,20 @@ import org.elasticsearch.ElasticsearchException;
|
||||||
import org.elasticsearch.action.index.IndexRequest;
|
import org.elasticsearch.action.index.IndexRequest;
|
||||||
import org.elasticsearch.client.Client;
|
import org.elasticsearch.client.Client;
|
||||||
import org.elasticsearch.common.inject.Inject;
|
import org.elasticsearch.common.inject.Inject;
|
||||||
|
import org.elasticsearch.common.logging.ESLogger;
|
||||||
|
import org.elasticsearch.common.logging.Loggers;
|
||||||
|
import org.elasticsearch.common.xcontent.ToXContent;
|
||||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||||
|
import org.elasticsearch.common.xcontent.XContentHelper;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class IndexAlertAction implements AlertAction {
|
public class IndexAlertAction implements AlertAction, ToXContent {
|
||||||
private final String index;
|
private final String index;
|
||||||
private final String type;
|
private final String type;
|
||||||
private Client client = null;
|
private Client client = null;
|
||||||
|
ESLogger logger = Loggers.getLogger(IndexAlertAction.class);
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public IndexAlertAction(String index, String type, Client client){
|
public IndexAlertAction(String index, String type, Client client){
|
||||||
|
@ -33,7 +38,7 @@ public class IndexAlertAction implements AlertAction {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public XContentBuilder toXContent(XContentBuilder builder) throws IOException {
|
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||||
builder.startObject();
|
builder.startObject();
|
||||||
builder.field("index", index);
|
builder.field("index", index);
|
||||||
builder.field("type", type);
|
builder.field("type", type);
|
||||||
|
@ -47,9 +52,11 @@ public class IndexAlertAction implements AlertAction {
|
||||||
indexRequest.index(index);
|
indexRequest.index(index);
|
||||||
indexRequest.type(type);
|
indexRequest.type(type);
|
||||||
try {
|
try {
|
||||||
XContentBuilder resultBuilder = XContentFactory.jsonBuilder();
|
XContentBuilder resultBuilder = XContentFactory.jsonBuilder().prettyPrint();
|
||||||
alertResult.searchResponse.toXContent(resultBuilder,null);
|
resultBuilder.startObject();
|
||||||
|
resultBuilder = alertResult.searchResponse.toXContent(resultBuilder, ToXContent.EMPTY_PARAMS);
|
||||||
resultBuilder.field("timestamp", alertResult.fireTime);
|
resultBuilder.field("timestamp", alertResult.fireTime);
|
||||||
|
resultBuilder.endObject();
|
||||||
indexRequest.source(resultBuilder);
|
indexRequest.source(resultBuilder);
|
||||||
} catch (IOException ie) {
|
} catch (IOException ie) {
|
||||||
throw new ElasticsearchException("Unable to create XContentBuilder",ie);
|
throw new ElasticsearchException("Unable to create XContentBuilder",ie);
|
||||||
|
|
Loading…
Reference in New Issue