Throw 400 on unknown metrics in Job Stats (elastic/elasticsearch#446)

Closes elastic/elasticsearch#426

Original commit: elastic/x-pack-elasticsearch@86f136f5c0
This commit is contained in:
Zachary Tong 2016-12-02 08:26:32 -05:00 committed by GitHub
parent b6bdef474d
commit 04445ce95f
2 changed files with 23 additions and 1 deletions

View File

@ -5,6 +5,7 @@
*/
package org.elasticsearch.xpack.prelert.action;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.Action;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionRequestValidationException;
@ -50,6 +51,7 @@ import org.elasticsearch.xpack.prelert.utils.ExceptionsHelper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
@ -62,12 +64,16 @@ public class GetJobsAction extends Action<GetJobsAction.Request, GetJobsAction.R
public static final GetJobsAction INSTANCE = new GetJobsAction();
public static final String NAME = "cluster:admin/prelert/jobs/get";
private static final String ALL = "_all";
private static final String CONFIG = "config";
private static final String DATA_COUNTS = "data_counts";
private static final String MODEL_SIZE_STATS = "model_size_stats";
private static final String SCHEDULER_STATE = "scheduler_state";
private static final String STATUS = "status";
private static final List<String> METRIC_WHITELIST = Arrays.asList(ALL, CONFIG, DATA_COUNTS,
MODEL_SIZE_STATS, SCHEDULER_STATE, STATUS);
private GetJobsAction() {
super(NAME);
}
@ -87,6 +93,8 @@ public class GetJobsAction extends Action<GetJobsAction.Request, GetJobsAction.R
public static final ObjectParser<Request, ParseFieldMatcherSupplier> PARSER = new ObjectParser<>(NAME, Request::new);
public static final ParseField METRIC = new ParseField("metric");
static {
PARSER.declareString(Request::setJobId, Job.ID);
PARSER.declareObject(Request::setPageParams, PageParams.PARSER, PageParams.PAGE);
@ -171,7 +179,13 @@ public class GetJobsAction extends Action<GetJobsAction.Request, GetJobsAction.R
}
public void setStats(Set<String> stats) {
if (stats.contains("_all")) {
for (String s : stats) {
if (!METRIC_WHITELIST.contains(s)) {
throw new ElasticsearchStatusException("Metric [" + s + "] is not a valid metric. "
+ "Accepted metrics are: [" + METRIC_WHITELIST + "]", RestStatus.BAD_REQUEST);
}
}
if (stats.contains(ALL)) {
all();
} else {
config(stats.contains(CONFIG));

View File

@ -129,3 +129,11 @@ setup:
- is_false: jobs.0.model_size_stats
- match: { jobs.0.scheduler_state.status: STOPPED }
---
"Test bad metric":
- do:
catch: request
xpack.prelert.get_jobs:
job_id: "job-stats-test"
metric: "foobar"