CORE : Add alerts stats.

This commit adds the alerts stats actions.

Original commit: elastic/x-pack-elasticsearch@c3c471de74
This commit is contained in:
Brian Murphy 2014-11-13 11:25:28 +00:00
parent b731f84d68
commit 8b2c6ba39d
13 changed files with 337 additions and 15 deletions

View File

@ -151,6 +151,10 @@ public class AlertManager extends AbstractComponent {
}
}
public long getNumberOfAlerts() {
return alertsStore.getAlerts().size();
}
private final class AlertsClusterStateListener implements ClusterStateListener {
@Override

View File

@ -9,6 +9,7 @@ package org.elasticsearch.alerts;
import org.elasticsearch.alerts.actions.AlertActionManager;
import org.elasticsearch.alerts.actions.AlertActionRegistry;
import org.elasticsearch.alerts.client.AlertsClient;
import org.elasticsearch.alerts.rest.RestAlertsStatsAction;
import org.elasticsearch.alerts.rest.RestDeleteAlertAction;
import org.elasticsearch.alerts.rest.RestIndexAlertAction;
import org.elasticsearch.alerts.scheduler.AlertScheduler;
@ -29,6 +30,7 @@ public class AlertingModule extends AbstractModule {
bind(AlertActionRegistry.class).asEagerSingleton();
bind(RestIndexAlertAction.class).asEagerSingleton();
bind(RestDeleteAlertAction.class).asEagerSingleton();
bind(RestAlertsStatsAction.class).asEagerSingleton();
//bind(AlertsClientInterface.class).to(AlertsClient.class).asEagerSingleton();
bind(AlertsClient.class).asEagerSingleton();
}

View File

@ -5,17 +5,18 @@
*/
package org.elasticsearch.alerts.actions;
import org.elasticsearch.alerts.Alert;
import org.elasticsearch.alerts.triggers.TriggerResult;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
/**
* Classes that implement this interface should be a POJO
* containing the data needed to do this action
*/
public interface AlertAction extends ToXContent {
/**
*
* @return
*/
public String getActionName();
}

View File

@ -268,6 +268,10 @@ public class AlertActionManager extends AbstractComponent {
entry.setVersion(response.getVersion());
}
public long getQueueSize() {
return actionsToBeProcessed.size();
}
private class AlertHistoryRunnable implements Runnable {
private final AlertActionEntry entry;

View File

@ -12,8 +12,6 @@ import org.elasticsearch.client.Client;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser;
@ -23,7 +21,7 @@ import java.util.List;
public class AlertActionRegistry extends AbstractComponent {
private static volatile ImmutableOpenMap<String, AlertActionFactory> actionImplemented;
private volatile ImmutableOpenMap<String, AlertActionFactory> actionImplemented;
@Inject
public AlertActionRegistry(Settings settings, Client client) {

View File

@ -9,9 +9,11 @@ import org.elasticsearch.action.*;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.TransportAction;
import org.elasticsearch.alerts.AlertManager;
import org.elasticsearch.alerts.actions.AlertActionManager;
import org.elasticsearch.alerts.transport.actions.index.*;
import org.elasticsearch.alerts.transport.actions.delete.*;
import org.elasticsearch.alerts.transport.actions.get.*;
import org.elasticsearch.alerts.transport.actions.stats.*;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.support.Headers;
import org.elasticsearch.cluster.ClusterService;
@ -35,7 +37,7 @@ public class AlertsClient implements AlertsClientInterface {
Headers headers,
ActionFilters filters,
TransportService transportService, ClusterService clusterService, AlertManager alertManager,
Client client) {
Client client, AlertActionManager alertActionManager) {
this.headers = headers;
internalActions = new HashMap<>();
this.threadPool = threadPool;
@ -49,6 +51,11 @@ public class AlertsClient implements AlertsClientInterface {
internalActions.put(DeleteAlertAction.INSTANCE, new TransportDeleteAlertAction(settings,
DeleteAlertAction.NAME, transportService, clusterService, threadPool, filters, alertManager));
internalActions.put(AlertsStatsAction.INSTANCE, new TransportAlertStatsAction(settings,
AlertsStatsAction.NAME, transportService, clusterService, threadPool, filters, alertManager,
alertActionManager));
}
@Override
@ -110,6 +117,21 @@ public class AlertsClient implements AlertsClientInterface {
return execute(IndexAlertAction.INSTANCE, request);
}
@Override
public ActionFuture<AlertsStatsResponse> alertsStats(AlertsStatsRequest request) {
return execute(AlertsStatsAction.INSTANCE, request);
}
@Override
public AlertsStatsRequestBuilder prepareAlertsStats() {
return new AlertsStatsRequestBuilder(this);
}
@Override
public void alertsStats(AlertsStatsRequest request, ActionListener<AlertsStatsResponse> listener) {
execute(AlertsStatsAction.INSTANCE, request, listener);
}
@SuppressWarnings("unchecked")
@Override
public <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder, AlertsClientInterface>> ActionFuture<Response> execute(Action<Request, Response, RequestBuilder, AlertsClientInterface> action, Request request) {

View File

@ -5,16 +5,20 @@
*/
package org.elasticsearch.alerts.client;
import org.elasticsearch.action.*;
import org.elasticsearch.alerts.transport.actions.index.IndexAlertRequest;
import org.elasticsearch.alerts.transport.actions.index.IndexAlertRequestBuilder;
import org.elasticsearch.alerts.transport.actions.index.IndexAlertResponse;
import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequest;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertRequestBuilder;
import org.elasticsearch.alerts.transport.actions.delete.DeleteAlertResponse;
import org.elasticsearch.alerts.transport.actions.get.GetAlertRequest;
import org.elasticsearch.alerts.transport.actions.get.GetAlertRequestBuilder;
import org.elasticsearch.alerts.transport.actions.get.GetAlertResponse;
import org.elasticsearch.alerts.transport.actions.index.IndexAlertRequest;
import org.elasticsearch.alerts.transport.actions.index.IndexAlertRequestBuilder;
import org.elasticsearch.alerts.transport.actions.index.IndexAlertResponse;
import org.elasticsearch.alerts.transport.actions.stats.AlertsStatsRequest;
import org.elasticsearch.alerts.transport.actions.stats.AlertsStatsRequestBuilder;
import org.elasticsearch.alerts.transport.actions.stats.AlertsStatsResponse;
import org.elasticsearch.client.ElasticsearchClient;
/**
@ -114,4 +118,29 @@ public interface AlertsClientInterface extends ElasticsearchClient<AlertsClientI
*/
ActionFuture<IndexAlertResponse> indexAlert(IndexAlertRequest request);
/**
* Gets the alert stats
*
* @param request The request for the alert stats
* @return The response containing the StatsResponse for this action
*/
ActionFuture<AlertsStatsResponse> alertsStats(AlertsStatsRequest request);
/**
* Creates a request builder to build a request to get the alerts stats
*
* @return The builder get the alerts stats
*/
AlertsStatsRequestBuilder prepareAlertsStats();
/**
* Gets the alert stats
*
* @param request The request for the alert stats
* @param listener The listener for the response containing the AlertsStatsResponse
*/
public void alertsStats(AlertsStatsRequest request, ActionListener<AlertsStatsResponse> listener);
}

View File

@ -0,0 +1,31 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.alerts.transport.actions.stats;
import org.elasticsearch.alerts.client.AlertsClientAction;
import org.elasticsearch.alerts.client.AlertsClientInterface;
/**
*/
public class AlertsStatsAction extends AlertsClientAction<AlertsStatsRequest, AlertsStatsResponse, AlertsStatsRequestBuilder> {
public static final AlertsStatsAction INSTANCE = new AlertsStatsAction();
public static final String NAME = "cluster/alerts/stats";
private AlertsStatsAction() {
super(NAME);
}
@Override
public AlertsStatsResponse newResponse() {
return new AlertsStatsResponse();
}
@Override
public AlertsStatsRequestBuilder newRequestBuilder(AlertsClientInterface client) {
return new AlertsStatsRequestBuilder(client);
}
}

View File

@ -0,0 +1,44 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.alerts.transport.actions.stats;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.MasterNodeOperationRequest;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import java.io.IOException;
/**
*/
public class AlertsStatsRequest extends MasterNodeOperationRequest<AlertsStatsRequest> {
public AlertsStatsRequest() {
}
@Override
public ActionRequestValidationException validate() {
return null;
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
}
@Override
public String toString() {
return "alertStats";
}
}

View File

@ -0,0 +1,28 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.alerts.transport.actions.stats;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.master.MasterNodeOperationRequestBuilder;
import org.elasticsearch.alerts.client.AlertsClientInterface;
/**
* An alert stats document action request builder.
*/
public class AlertsStatsRequestBuilder
extends MasterNodeOperationRequestBuilder<AlertsStatsRequest, AlertsStatsResponse, AlertsStatsRequestBuilder, AlertsClientInterface> {
public AlertsStatsRequestBuilder(AlertsClientInterface client) {
super(client, new AlertsStatsRequest());
}
@Override
protected void doExecute(final ActionListener<AlertsStatsResponse> listener) {
client.alertsStats(request, listener);
}
}

View File

@ -0,0 +1,78 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.alerts.transport.actions.stats;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import java.io.IOException;
/**
*/
public class AlertsStatsResponse extends ActionResponse {
private long numberOfRegisteredAlerts;
private boolean alertManagerStarted;
private boolean alertActionManagerStarted;
private long alertActionManagerQueueSize;
public AlertsStatsResponse() {
}
public long getAlertActionManagerQueueSize() {
return alertActionManagerQueueSize;
}
public void setAlertActionManagerQueueSize(long alertActionManagerQueueSize) {
this.alertActionManagerQueueSize = alertActionManagerQueueSize;
}
public long getNumberOfRegisteredAlerts() {
return numberOfRegisteredAlerts;
}
public void setNumberOfRegisteredAlerts(long numberOfRegisteredAlerts) {
this.numberOfRegisteredAlerts = numberOfRegisteredAlerts;
}
public boolean isAlertManagerStarted() {
return alertManagerStarted;
}
public void setAlertManagerStarted(boolean alertManagerStarted) {
this.alertManagerStarted = alertManagerStarted;
}
public boolean isAlertActionManagerStarted() {
return alertActionManagerStarted;
}
public void setAlertActionManagerStarted(boolean alertActionManagerStarted) {
this.alertActionManagerStarted = alertActionManagerStarted;
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
numberOfRegisteredAlerts = in.readLong();
alertActionManagerQueueSize = in.readLong();
alertManagerStarted = in.readBoolean();
alertActionManagerStarted = in.readBoolean();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeLong(numberOfRegisteredAlerts);
out.writeLong(alertActionManagerQueueSize);
out.writeBoolean(alertManagerStarted);
out.writeBoolean(alertActionManagerStarted);
}
}

View File

@ -0,0 +1,71 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.alerts.transport.actions.stats;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction;
import org.elasticsearch.alerts.AlertManager;
import org.elasticsearch.alerts.AlertsStore;
import org.elasticsearch.alerts.actions.AlertActionManager;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
/**
* Performs the delete operation.
*/
public class TransportAlertStatsAction extends TransportMasterNodeOperationAction<AlertsStatsRequest, AlertsStatsResponse> {
private final AlertManager alertManager;
private final AlertActionManager alertActionManager;
@Inject
public TransportAlertStatsAction(Settings settings, String actionName, TransportService transportService,
ClusterService clusterService, ThreadPool threadPool, ActionFilters actionFilters,
AlertManager alertManager, AlertActionManager alertActionManager) {
super(settings, actionName, transportService, clusterService, threadPool, actionFilters);
this.alertManager = alertManager;
this.alertActionManager = alertActionManager;
}
@Override
protected String executor() {
return ThreadPool.Names.MANAGEMENT;
}
@Override
protected AlertsStatsRequest newRequest() {
return new AlertsStatsRequest();
}
@Override
protected AlertsStatsResponse newResponse() {
return new AlertsStatsResponse();
}
@Override
protected void masterOperation(AlertsStatsRequest request, ClusterState state, ActionListener<AlertsStatsResponse> listener) throws ElasticsearchException {
AlertsStatsResponse statsResponse = new AlertsStatsResponse();
statsResponse.setAlertManagerStarted(alertManager.isStarted());
statsResponse.setAlertActionManagerStarted(alertActionManager.started());
statsResponse.setAlertActionManagerQueueSize(alertActionManager.getQueueSize());
statsResponse.setNumberOfRegisteredAlerts(alertManager.getNumberOfAlerts());
}
@Override
protected ClusterBlockException checkBlock(AlertsStatsRequest request, ClusterState state) {
return state.blocks().indicesBlockedException(ClusterBlockLevel.WRITE, new String[]{AlertsStore.ALERT_INDEX, AlertActionManager.ALERT_HISTORY_INDEX});
}
}

View File

@ -0,0 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
/**
* Delete action.
*/
package org.elasticsearch.alerts.transport.actions.stats;