33
33
34
34
import java .io .IOException ;
35
35
import java .util .Collections ;
36
+ import java .util .HashMap ;
37
+ import java .util .HashSet ;
38
+ import java .util .Locale ;
39
+ import java .util .Map ;
36
40
import java .util .Set ;
41
+ import java .util .TreeSet ;
42
+ import java .util .function .Consumer ;
37
43
38
44
import static org .elasticsearch .rest .RestRequest .Method .GET ;
39
45
@@ -48,9 +54,38 @@ public RestNodesStatsAction(Settings settings, RestController controller) {
48
54
controller .registerHandler (GET , "/_nodes/stats/{metric}" , this );
49
55
controller .registerHandler (GET , "/_nodes/{nodeId}/stats/{metric}" , this );
50
56
51
- controller .registerHandler (GET , "/_nodes/stats/{metric}/{indexMetric }" , this );
57
+ controller .registerHandler (GET , "/_nodes/stats/{metric}/{index_metric }" , this );
52
58
53
- controller .registerHandler (GET , "/_nodes/{nodeId}/stats/{metric}/{indexMetric}" , this );
59
+ controller .registerHandler (GET , "/_nodes/{nodeId}/stats/{metric}/{index_metric}" , this );
60
+ }
61
+
62
+ static final Map <String , Consumer <NodesStatsRequest >> METRICS ;
63
+
64
+ static {
65
+ final Map <String , Consumer <NodesStatsRequest >> metrics = new HashMap <>();
66
+ metrics .put ("os" , r -> r .os (true ));
67
+ metrics .put ("jvm" , r -> r .jvm (true ));
68
+ metrics .put ("thread_pool" , r -> r .threadPool (true ));
69
+ metrics .put ("fs" , r -> r .fs (true ));
70
+ metrics .put ("transport" , r -> r .transport (true ));
71
+ metrics .put ("http" , r -> r .http (true ));
72
+ metrics .put ("indices" , r -> r .indices (true ));
73
+ metrics .put ("process" , r -> r .process (true ));
74
+ metrics .put ("breaker" , r -> r .breaker (true ));
75
+ metrics .put ("script" , r -> r .script (true ));
76
+ metrics .put ("discovery" , r -> r .discovery (true ));
77
+ metrics .put ("ingest" , r -> r .ingest (true ));
78
+ METRICS = Collections .unmodifiableMap (metrics );
79
+ }
80
+
81
+ static final Map <String , Consumer <CommonStatsFlags >> FLAGS ;
82
+
83
+ static {
84
+ final Map <String , Consumer <CommonStatsFlags >> flags = new HashMap <>();
85
+ for (final Flag flag : CommonStatsFlags .Flag .values ()) {
86
+ flags .put (flag .getRestName (), f -> f .set (flag , true ));
87
+ }
88
+ FLAGS = Collections .unmodifiableMap (flags );
54
89
}
55
90
56
91
@ Override
@@ -62,35 +97,72 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC
62
97
nodesStatsRequest .timeout (request .param ("timeout" ));
63
98
64
99
if (metrics .size () == 1 && metrics .contains ("_all" )) {
100
+ if (request .hasParam ("index_metric" )) {
101
+ throw new IllegalArgumentException (
102
+ String .format (
103
+ Locale .ROOT ,
104
+ "request [%s] contains index metrics [%s] but all stats requested" ,
105
+ request .path (),
106
+ request .param ("index_metric" )));
107
+ }
65
108
nodesStatsRequest .all ();
66
109
nodesStatsRequest .indices (CommonStatsFlags .ALL );
110
+ } else if (metrics .contains ("_all" )) {
111
+ throw new IllegalArgumentException (
112
+ String .format (Locale .ROOT ,
113
+ "request [%s] contains _all and individual metrics [%s]" ,
114
+ request .path (),
115
+ request .param ("metric" )));
67
116
} else {
68
117
nodesStatsRequest .clear ();
69
- nodesStatsRequest .os (metrics .contains ("os" ));
70
- nodesStatsRequest .jvm (metrics .contains ("jvm" ));
71
- nodesStatsRequest .threadPool (metrics .contains ("thread_pool" ));
72
- nodesStatsRequest .fs (metrics .contains ("fs" ));
73
- nodesStatsRequest .transport (metrics .contains ("transport" ));
74
- nodesStatsRequest .http (metrics .contains ("http" ));
75
- nodesStatsRequest .indices (metrics .contains ("indices" ));
76
- nodesStatsRequest .process (metrics .contains ("process" ));
77
- nodesStatsRequest .breaker (metrics .contains ("breaker" ));
78
- nodesStatsRequest .script (metrics .contains ("script" ));
79
- nodesStatsRequest .discovery (metrics .contains ("discovery" ));
80
- nodesStatsRequest .ingest (metrics .contains ("ingest" ));
118
+
119
+ // use a sorted set so the unrecognized parameters appear in a reliable sorted order
120
+ final Set <String > invalidMetrics = new TreeSet <>();
121
+ for (final String metric : metrics ) {
122
+ final Consumer <NodesStatsRequest > handler = METRICS .get (metric );
123
+ if (handler != null ) {
124
+ handler .accept (nodesStatsRequest );
125
+ } else {
126
+ invalidMetrics .add (metric );
127
+ }
128
+ }
129
+
130
+ if (!invalidMetrics .isEmpty ()) {
131
+ throw new IllegalArgumentException (unrecognized (request , invalidMetrics , METRICS .keySet (), "metric" ));
132
+ }
81
133
82
134
// check for index specific metrics
83
135
if (metrics .contains ("indices" )) {
84
- Set <String > indexMetrics = Strings .splitStringByCommaToSet (request .param ("indexMetric " , "_all" ));
136
+ Set <String > indexMetrics = Strings .splitStringByCommaToSet (request .param ("index_metric " , "_all" ));
85
137
if (indexMetrics .size () == 1 && indexMetrics .contains ("_all" )) {
86
138
nodesStatsRequest .indices (CommonStatsFlags .ALL );
87
139
} else {
88
140
CommonStatsFlags flags = new CommonStatsFlags ();
89
- for (Flag flag : CommonStatsFlags .Flag .values ()) {
90
- flags .set (flag , indexMetrics .contains (flag .getRestName ()));
141
+ flags .clear ();
142
+ // use a sorted set so the unrecognized parameters appear in a reliable sorted order
143
+ final Set <String > invalidIndexMetrics = new TreeSet <>();
144
+ for (final String indexMetric : indexMetrics ) {
145
+ final Consumer <CommonStatsFlags > handler = FLAGS .get (indexMetric );
146
+ if (handler != null ) {
147
+ handler .accept (flags );
148
+ } else {
149
+ invalidIndexMetrics .add (indexMetric );
150
+ }
151
+ }
152
+
153
+ if (!invalidIndexMetrics .isEmpty ()) {
154
+ throw new IllegalArgumentException (unrecognized (request , invalidIndexMetrics , FLAGS .keySet (), "index metric" ));
91
155
}
156
+
92
157
nodesStatsRequest .indices (flags );
93
158
}
159
+ } else if (request .hasParam ("index_metric" )) {
160
+ throw new IllegalArgumentException (
161
+ String .format (
162
+ Locale .ROOT ,
163
+ "request [%s] contains index metrics [%s] but indices stats not requested" ,
164
+ request .path (),
165
+ request .param ("index_metric" )));
94
166
}
95
167
}
96
168
0 commit comments