Skip to content

Commit a76ac57

Browse files
authored
Rest HL client: Add watcher stats API (#35185)
Relates to #29827
1 parent 4b5fbad commit a76ac57

16 files changed

+1092
-5
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.elasticsearch.client.watcher.DeleteWatchResponse;
3333
import org.elasticsearch.client.watcher.PutWatchRequest;
3434
import org.elasticsearch.client.watcher.PutWatchResponse;
35+
import org.elasticsearch.client.watcher.WatcherStatsRequest;
36+
import org.elasticsearch.client.watcher.WatcherStatsResponse;
3537

3638
import java.io.IOException;
3739

@@ -237,4 +239,31 @@ public void activateWatchAsync(ActivateWatchRequest request, RequestOptions opti
237239
ActivateWatchResponse::fromXContent, listener, singleton(404));
238240
}
239241

242+
/**
243+
* Get the watcher stats
244+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-stats.html">
245+
* the docs</a> for more.
246+
* @param request the request
247+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
248+
* @return the response
249+
* @throws IOException in case there is a problem sending the request or parsing back the response
250+
*/
251+
public WatcherStatsResponse watcherStats(WatcherStatsRequest request, RequestOptions options) throws IOException {
252+
return restHighLevelClient.performRequestAndParseEntity(request, WatcherRequestConverters::watcherStats, options,
253+
WatcherStatsResponse::fromXContent, emptySet());
254+
}
255+
256+
/**
257+
* Asynchronously get the watcher stats
258+
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api-stats.html">
259+
* the docs</a> for more.
260+
* @param request the request
261+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
262+
* @param listener the listener to be notified upon request completion
263+
*/
264+
public void watcherStatsAsync(WatcherStatsRequest request, RequestOptions options, ActionListener<WatcherStatsResponse> listener) {
265+
restHighLevelClient.performRequestAsyncAndParseEntity(request, WatcherRequestConverters::watcherStats, options,
266+
WatcherStatsResponse::fromXContent, listener, emptySet());
267+
}
268+
240269
}

client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherRequestConverters.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.client;
2121

2222
import org.apache.http.client.methods.HttpDelete;
23+
import org.apache.http.client.methods.HttpGet;
2324
import org.apache.http.client.methods.HttpPost;
2425
import org.apache.http.client.methods.HttpPut;
2526
import org.apache.http.entity.ByteArrayEntity;
@@ -29,6 +30,7 @@
2930
import org.elasticsearch.client.watcher.AckWatchRequest;
3031
import org.elasticsearch.client.watcher.StartWatchServiceRequest;
3132
import org.elasticsearch.client.watcher.StopWatchServiceRequest;
33+
import org.elasticsearch.client.watcher.WatcherStatsRequest;
3234
import org.elasticsearch.common.bytes.BytesReference;
3335
import org.elasticsearch.client.watcher.DeleteWatchRequest;
3436
import org.elasticsearch.client.watcher.PutWatchRequest;
@@ -115,4 +117,25 @@ static Request activateWatch(ActivateWatchRequest activateWatchRequest) {
115117
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
116118
return request;
117119
}
120+
121+
static Request watcherStats(WatcherStatsRequest watcherStatsRequest) {
122+
RequestConverters.EndpointBuilder builder = new RequestConverters.EndpointBuilder().addPathPartAsIs("_xpack", "watcher", "stats");
123+
String endpoint = builder.build();
124+
Request request = new Request(HttpGet.METHOD_NAME, endpoint);
125+
RequestConverters.Params parameters = new RequestConverters.Params(request);
126+
StringBuilder metric = new StringBuilder();
127+
if (watcherStatsRequest.includeCurrentWatches()) {
128+
metric.append("current_watches");
129+
}
130+
if (watcherStatsRequest.includeQueuedWatches()) {
131+
if (metric.length() > 0) {
132+
metric.append(",");
133+
}
134+
metric.append("queued_watches");
135+
}
136+
if (metric.length() > 0) {
137+
parameters.putParam("metric", metric.toString());
138+
}
139+
return request;
140+
}
118141
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.watcher;
21+
22+
public enum ExecutionPhase {
23+
24+
// awaiting execution of the watch
25+
AWAITS_EXECUTION(false),
26+
// initial phase, watch execution has started, but the input is not yet processed
27+
STARTED(false),
28+
// input is being executed
29+
INPUT(false),
30+
// condition phase is being executed
31+
CONDITION(false),
32+
// transform phase (optional, depends if a global transform was configured in the watch)
33+
WATCH_TRANSFORM(false),
34+
// actions phase, all actions, including specific action transforms
35+
ACTIONS(false),
36+
// missing watch, failed execution of input/condition/transform,
37+
ABORTED(true),
38+
// successful run
39+
FINISHED(true);
40+
41+
private final boolean sealed;
42+
43+
ExecutionPhase(boolean sealed) {
44+
this.sealed = sealed;
45+
}
46+
47+
public boolean sealed() {
48+
return sealed;
49+
}
50+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.watcher;
21+
22+
import org.elasticsearch.common.ParseField;
23+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
24+
import org.joda.time.DateTime;
25+
26+
import java.util.Objects;
27+
28+
public class QueuedWatch {
29+
30+
@SuppressWarnings("unchecked")
31+
public static final ConstructingObjectParser<QueuedWatch, Void> PARSER =
32+
new ConstructingObjectParser<>("watcher_stats_node", true, (args, c) -> new QueuedWatch(
33+
(String) args[0],
34+
(String) args[1],
35+
DateTime.parse((String) args[2]),
36+
DateTime.parse((String) args[3])
37+
));
38+
39+
static {
40+
PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("watch_id"));
41+
PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("watch_record_id"));
42+
PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("triggered_time"));
43+
PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("execution_time"));
44+
}
45+
46+
47+
private final String watchId;
48+
private final String watchRecordId;
49+
private final DateTime triggeredTime;
50+
private final DateTime executionTime;
51+
52+
public QueuedWatch(String watchId, String watchRecordId, DateTime triggeredTime, DateTime executionTime) {
53+
this.watchId = watchId;
54+
this.watchRecordId = watchRecordId;
55+
this.triggeredTime = triggeredTime;
56+
this.executionTime = executionTime;
57+
}
58+
59+
public String getWatchId() {
60+
return watchId;
61+
}
62+
63+
public String getWatchRecordId() {
64+
return watchRecordId;
65+
}
66+
67+
public DateTime getTriggeredTime() {
68+
return triggeredTime;
69+
}
70+
71+
public DateTime getExecutionTime() {
72+
return executionTime;
73+
}
74+
75+
@Override
76+
public boolean equals(Object o) {
77+
if (this == o) return true;
78+
if (o == null || getClass() != o.getClass()) return false;
79+
QueuedWatch that = (QueuedWatch) o;
80+
return Objects.equals(watchId, that.watchId) &&
81+
Objects.equals(watchRecordId, that.watchRecordId) &&
82+
Objects.equals(triggeredTime, that.triggeredTime) &&
83+
Objects.equals(executionTime, that.executionTime);
84+
}
85+
86+
@Override
87+
public int hashCode() {
88+
89+
return Objects.hash(watchId, watchRecordId, triggeredTime, executionTime);
90+
}
91+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.watcher;
21+
22+
import org.elasticsearch.common.ParseField;
23+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
24+
import org.joda.time.DateTime;
25+
26+
import java.util.Arrays;
27+
import java.util.List;
28+
import java.util.Locale;
29+
import java.util.Objects;
30+
31+
public class WatchExecutionSnapshot {
32+
@SuppressWarnings("unchecked")
33+
public static final ConstructingObjectParser<WatchExecutionSnapshot, Void> PARSER =
34+
new ConstructingObjectParser<>("watcher_stats_node", true, (args, c) -> new WatchExecutionSnapshot(
35+
(String) args[0],
36+
(String) args[1],
37+
DateTime.parse((String) args[2]),
38+
DateTime.parse((String) args[3]),
39+
ExecutionPhase.valueOf(((String) args[4]).toUpperCase(Locale.ROOT)),
40+
args[5] == null ? null : ((List<String>) args[5]).toArray(new String[0]),
41+
args[6] == null ? null : ((List<String>) args[6]).toArray(new String[0])
42+
));
43+
44+
static {
45+
PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("watch_id"));
46+
PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("watch_record_id"));
47+
PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("triggered_time"));
48+
PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("execution_time"));
49+
PARSER.declareString(ConstructingObjectParser.constructorArg(), new ParseField("execution_phase"));
50+
PARSER.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), new ParseField("executed_actions"));
51+
PARSER.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), new ParseField("stack_trace"));
52+
}
53+
54+
private final String watchId;
55+
private final String watchRecordId;
56+
private final DateTime triggeredTime;
57+
private final DateTime executionTime;
58+
private final ExecutionPhase phase;
59+
private final String[] executedActions;
60+
private final String[] executionStackTrace;
61+
62+
public WatchExecutionSnapshot(String watchId, String watchRecordId, DateTime triggeredTime, DateTime executionTime,
63+
ExecutionPhase phase, String[] executedActions, String[] executionStackTrace) {
64+
this.watchId = watchId;
65+
this.watchRecordId = watchRecordId;
66+
this.triggeredTime = triggeredTime;
67+
this.executionTime = executionTime;
68+
this.phase = phase;
69+
this.executedActions = executedActions;
70+
this.executionStackTrace = executionStackTrace;
71+
}
72+
73+
public String getWatchId() {
74+
return watchId;
75+
}
76+
77+
public String getWatchRecordId() {
78+
return watchRecordId;
79+
}
80+
81+
public DateTime getTriggeredTime() {
82+
return triggeredTime;
83+
}
84+
85+
public DateTime getExecutionTime() {
86+
return executionTime;
87+
}
88+
89+
public ExecutionPhase getPhase() {
90+
return phase;
91+
}
92+
93+
public String[] getExecutedActions() {
94+
return executedActions;
95+
}
96+
97+
public String[] getExecutionStackTrace() {
98+
return executionStackTrace;
99+
}
100+
101+
@Override
102+
public boolean equals(Object o) {
103+
if (this == o) return true;
104+
if (o == null || getClass() != o.getClass()) return false;
105+
WatchExecutionSnapshot that = (WatchExecutionSnapshot) o;
106+
return Objects.equals(watchId, that.watchId) &&
107+
Objects.equals(watchRecordId, that.watchRecordId) &&
108+
Objects.equals(triggeredTime, that.triggeredTime) &&
109+
Objects.equals(executionTime, that.executionTime) &&
110+
phase == that.phase &&
111+
Arrays.equals(executedActions, that.executedActions) &&
112+
Arrays.equals(executionStackTrace, that.executionStackTrace);
113+
}
114+
115+
@Override
116+
public int hashCode() {
117+
118+
int result = Objects.hash(watchId, watchRecordId, triggeredTime, executionTime, phase);
119+
result = 31 * result + Arrays.hashCode(executedActions);
120+
result = 31 * result + Arrays.hashCode(executionStackTrace);
121+
return result;
122+
}
123+
}

0 commit comments

Comments
 (0)