24
24
import org .elasticsearch .action .support .nodes .BaseNodesRequest ;
25
25
import org .elasticsearch .common .io .stream .StreamInput ;
26
26
import org .elasticsearch .common .io .stream .StreamOutput ;
27
-
28
27
import java .io .IOException ;
29
28
import java .util .Arrays ;
30
29
import java .util .HashSet ;
31
30
import java .util .Set ;
31
+ import java .util .SortedSet ;
32
+ import java .util .TreeSet ;
32
33
import java .util .stream .Collectors ;
33
34
34
35
/**
@@ -49,18 +50,18 @@ public NodesStatsRequest(StreamInput in) throws IOException {
49
50
indices = new CommonStatsFlags (in );
50
51
requestedMetrics .clear ();
51
52
if (in .getVersion ().before (Version .V_7_7_0 )) {
52
- addOrRemoveMetric (in .readBoolean (), Metric .OS .metricName ());
53
- addOrRemoveMetric (in .readBoolean (), Metric .PROCESS .metricName ());
54
- addOrRemoveMetric (in .readBoolean (), Metric .JVM .metricName ());
55
- addOrRemoveMetric (in .readBoolean (), Metric .THREAD_POOL .metricName ());
56
- addOrRemoveMetric (in .readBoolean (), Metric .FS .metricName ());
57
- addOrRemoveMetric (in .readBoolean (), Metric .TRANSPORT .metricName ());
58
- addOrRemoveMetric (in .readBoolean (), Metric .HTTP .metricName ());
59
- addOrRemoveMetric (in .readBoolean (), Metric .BREAKER .metricName ());
60
- addOrRemoveMetric (in .readBoolean (), Metric .SCRIPT .metricName ());
61
- addOrRemoveMetric (in .readBoolean (), Metric .DISCOVERY .metricName ());
62
- addOrRemoveMetric (in .readBoolean (), Metric .INGEST .metricName ());
63
- addOrRemoveMetric (in .readBoolean (), Metric .ADAPTIVE_SELECTION .metricName ());
53
+ optionallyAddMetric (in .readBoolean (), Metric .OS .metricName ());
54
+ optionallyAddMetric (in .readBoolean (), Metric .PROCESS .metricName ());
55
+ optionallyAddMetric (in .readBoolean (), Metric .JVM .metricName ());
56
+ optionallyAddMetric (in .readBoolean (), Metric .THREAD_POOL .metricName ());
57
+ optionallyAddMetric (in .readBoolean (), Metric .FS .metricName ());
58
+ optionallyAddMetric (in .readBoolean (), Metric .TRANSPORT .metricName ());
59
+ optionallyAddMetric (in .readBoolean (), Metric .HTTP .metricName ());
60
+ optionallyAddMetric (in .readBoolean (), Metric .BREAKER .metricName ());
61
+ optionallyAddMetric (in .readBoolean (), Metric .SCRIPT .metricName ());
62
+ optionallyAddMetric (in .readBoolean (), Metric .DISCOVERY .metricName ());
63
+ optionallyAddMetric (in .readBoolean (), Metric .INGEST .metricName ());
64
+ optionallyAddMetric (in .readBoolean (), Metric .ADAPTIVE_SELECTION .metricName ());
64
65
} else {
65
66
requestedMetrics .addAll (in .readStringList ());
66
67
}
@@ -92,10 +93,21 @@ public NodesStatsRequest clear() {
92
93
return this ;
93
94
}
94
95
96
+ /**
97
+ * Get indices. Handles separately from other metrics because it may or
98
+ * may not have submetrics.
99
+ * @return flags indicating which indices stats to return
100
+ */
95
101
public CommonStatsFlags indices () {
96
102
return indices ;
97
103
}
98
104
105
+ /**
106
+ * Set indices. Handles separately from other metrics because it may or
107
+ * may not involve submetrics.
108
+ * @param indices flags indicating which indices stats to return
109
+ * @return This object, for request chaining.
110
+ */
99
111
public NodesStatsRequest indices (CommonStatsFlags indices ) {
100
112
this .indices = indices ;
101
113
return this ;
@@ -114,178 +126,58 @@ public NodesStatsRequest indices(boolean indices) {
114
126
}
115
127
116
128
/**
117
- * Should the node OS be returned.
118
- */
119
- public boolean os () {
120
- return Metric .OS .containedIn (requestedMetrics );
121
- }
122
-
123
- /**
124
- * Should the node OS be returned.
125
- */
126
- public NodesStatsRequest os (boolean os ) {
127
- addOrRemoveMetric (os , Metric .OS .metricName ());
128
- return this ;
129
- }
130
-
131
- /**
132
- * Should the node Process be returned.
133
- */
134
- public boolean process () {
135
- return Metric .PROCESS .containedIn (requestedMetrics );
136
- }
137
-
138
- /**
139
- * Should the node Process be returned.
140
- */
141
- public NodesStatsRequest process (boolean process ) {
142
- addOrRemoveMetric (process , Metric .PROCESS .metricName ());
143
- return this ;
144
- }
145
-
146
- /**
147
- * Should the node JVM be returned.
148
- */
149
- public boolean jvm () {
150
- return Metric .JVM .containedIn (requestedMetrics );
151
- }
152
-
153
- /**
154
- * Should the node JVM be returned.
155
- */
156
- public NodesStatsRequest jvm (boolean jvm ) {
157
- addOrRemoveMetric (jvm , Metric .JVM .metricName ());
158
- return this ;
159
- }
160
-
161
- /**
162
- * Should the node Thread Pool be returned.
163
- */
164
- public boolean threadPool () {
165
- return Metric .THREAD_POOL .containedIn (requestedMetrics );
166
- }
167
-
168
- /**
169
- * Should the node Thread Pool be returned.
170
- */
171
- public NodesStatsRequest threadPool (boolean threadPool ) {
172
- addOrRemoveMetric (threadPool , Metric .THREAD_POOL .metricName ());
173
- return this ;
174
- }
175
-
176
- /**
177
- * Should the node file system stats be returned.
129
+ * Get the names of requested metrics, excluding indices, which are
130
+ * handled separately.
178
131
*/
179
- public boolean fs () {
180
- return Metric . FS . containedIn (requestedMetrics );
132
+ public Set < String > requestedMetrics () {
133
+ return Set . copyOf (requestedMetrics );
181
134
}
182
135
183
136
/**
184
- * Should the node file system stats be returned.
137
+ * Add metric
185
138
*/
186
- public NodesStatsRequest fs (boolean fs ) {
187
- addOrRemoveMetric (fs , Metric .FS .metricName ());
188
- return this ;
189
- }
190
-
191
- /**
192
- * Should the node Transport be returned.
193
- */
194
- public boolean transport () {
195
- return Metric .TRANSPORT .containedIn (requestedMetrics );
196
- }
197
-
198
- /**
199
- * Should the node Transport be returned.
200
- */
201
- public NodesStatsRequest transport (boolean transport ) {
202
- addOrRemoveMetric (transport , Metric .TRANSPORT .metricName ());
203
- return this ;
204
- }
205
-
206
- /**
207
- * Should the node HTTP be returned.
208
- */
209
- public boolean http () {
210
- return Metric .HTTP .containedIn (requestedMetrics );
211
- }
212
-
213
- /**
214
- * Should the node HTTP be returned.
215
- */
216
- public NodesStatsRequest http (boolean http ) {
217
- addOrRemoveMetric (http , Metric .HTTP .metricName ());
218
- return this ;
219
- }
220
-
221
- public boolean breaker () {
222
- return Metric .BREAKER .containedIn (requestedMetrics );
223
- }
224
-
225
- /**
226
- * Should the node's circuit breaker stats be returned.
227
- */
228
- public NodesStatsRequest breaker (boolean breaker ) {
229
- addOrRemoveMetric (breaker , Metric .BREAKER .metricName ());
230
- return this ;
231
- }
232
-
233
- public boolean script () {
234
- return Metric .SCRIPT .containedIn (requestedMetrics );
235
- }
236
-
237
- public NodesStatsRequest script (boolean script ) {
238
- addOrRemoveMetric (script , Metric .SCRIPT .metricName ());
239
- return this ;
240
- }
241
-
242
-
243
- public boolean discovery () {
244
- return Metric .DISCOVERY .containedIn (requestedMetrics );
245
- }
246
-
247
- /**
248
- * Should the node's discovery stats be returned.
249
- */
250
- public NodesStatsRequest discovery (boolean discovery ) {
251
- addOrRemoveMetric (discovery , Metric .DISCOVERY .metricName ());
139
+ public NodesStatsRequest addMetric (String metric ) {
140
+ if (Metric .allMetrics ().contains (metric ) == false ) {
141
+ throw new IllegalStateException ("Used an illegal metric: " + metric );
142
+ }
143
+ requestedMetrics .add (metric );
252
144
return this ;
253
145
}
254
146
255
- public boolean ingest () {
256
- return Metric .INGEST .containedIn (requestedMetrics );
257
- }
258
-
259
147
/**
260
- * Should ingest statistics be returned.
148
+ * Add an array of metric names
261
149
*/
262
- public NodesStatsRequest ingest (boolean ingest ) {
263
- addOrRemoveMetric (ingest , Metric .INGEST .metricName ());
150
+ public NodesStatsRequest addMetrics (String ... metrics ) {
151
+ // use sorted set for reliable ordering in error messages
152
+ SortedSet <String > metricsSet = new TreeSet <>(Set .of (metrics ));
153
+ if (Metric .allMetrics ().containsAll (metricsSet ) == false ) {
154
+ metricsSet .removeAll (Metric .allMetrics ());
155
+ String plural = metricsSet .size () == 1 ? "" : "s" ;
156
+ throw new IllegalStateException ("Used illegal metric" + plural + ": " + metricsSet );
157
+ }
158
+ requestedMetrics .addAll (metricsSet );
264
159
return this ;
265
160
}
266
161
267
- public boolean adaptiveSelection () {
268
- return Metric .ADAPTIVE_SELECTION .containedIn (requestedMetrics );
269
- }
270
-
271
162
/**
272
- * Should adaptiveSelection statistics be returned.
163
+ * Remove metric
273
164
*/
274
- public NodesStatsRequest adaptiveSelection (boolean adaptiveSelection ) {
275
- addOrRemoveMetric (adaptiveSelection , Metric .ADAPTIVE_SELECTION .metricName ());
165
+ public NodesStatsRequest removeMetric (String metric ) {
166
+ if (Metric .allMetrics ().contains (metric ) == false ) {
167
+ throw new IllegalStateException ("Used an illegal metric: " + metric );
168
+ }
169
+ requestedMetrics .remove (metric );
276
170
return this ;
277
171
}
278
172
279
173
/**
280
- * Helper method for adding and removing metrics .
174
+ * Helper method for adding metrics during deserialization .
281
175
* @param includeMetric Whether or not to include a metric.
282
- * @param metricName Name of the metric to include or remove .
176
+ * @param metricName Name of the metric to add .
283
177
*/
284
- private void addOrRemoveMetric (boolean includeMetric , String metricName ) {
178
+ private void optionallyAddMetric (boolean includeMetric , String metricName ) {
285
179
if (includeMetric ) {
286
180
requestedMetrics .add (metricName );
287
- } else {
288
- requestedMetrics .remove (metricName );
289
181
}
290
182
}
291
183
@@ -315,7 +207,7 @@ public void writeTo(StreamOutput out) throws IOException {
315
207
* An enumeration of the "core" sections of metrics that may be requested
316
208
* from the nodes stats endpoint. Eventually this list will be pluggable.
317
209
*/
318
- private enum Metric {
210
+ public enum Metric {
319
211
OS ("os" ),
320
212
PROCESS ("process" ),
321
213
JVM ("jvm" ),
@@ -327,15 +219,15 @@ private enum Metric {
327
219
SCRIPT ("script" ),
328
220
DISCOVERY ("discovery" ),
329
221
INGEST ("ingest" ),
330
- ADAPTIVE_SELECTION ("adaptiveSelection " );
222
+ ADAPTIVE_SELECTION ("adaptive_selection " );
331
223
332
224
private String metricName ;
333
225
334
226
Metric (String name ) {
335
227
this .metricName = name ;
336
228
}
337
229
338
- String metricName () {
230
+ public String metricName () {
339
231
return this .metricName ;
340
232
}
341
233
0 commit comments