Search in sources :

Example 1 with NodesUsageRequest

use of org.opensearch.action.admin.cluster.node.usage.NodesUsageRequest in project OpenSearch by opensearch-project.

the class RestNodesUsageAction method prepareRequest.

@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    String[] nodesIds = Strings.splitStringByCommaToArray(request.param("nodeId"));
    Set<String> metrics = Strings.tokenizeByCommaToSet(request.param("metric", "_all"));
    NodesUsageRequest nodesUsageRequest = new NodesUsageRequest(nodesIds);
    nodesUsageRequest.timeout(request.param("timeout"));
    if (metrics.size() == 1 && metrics.contains("_all")) {
        nodesUsageRequest.all();
    } else if (metrics.contains("_all")) {
        throw new IllegalArgumentException(String.format(Locale.ROOT, "request [%s] contains _all and individual metrics [%s]", request.path(), request.param("metric")));
    } else {
        nodesUsageRequest.clear();
        nodesUsageRequest.restActions(metrics.contains("rest_actions"));
        nodesUsageRequest.aggregations(metrics.contains("aggregations"));
    }
    return channel -> client.admin().cluster().nodesUsage(nodesUsageRequest, new RestBuilderListener<NodesUsageResponse>(channel) {

        @Override
        public RestResponse buildResponse(NodesUsageResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            RestActions.buildNodesHeader(builder, channel.request(), response);
            builder.field("cluster_name", response.getClusterName().value());
            response.toXContent(builder, channel.request());
            builder.endObject();
            return new BytesRestResponse(RestStatus.OK, builder);
        }
    });
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) Collections.unmodifiableList(java.util.Collections.unmodifiableList) GET(org.opensearch.rest.RestRequest.Method.GET) RestRequest(org.opensearch.rest.RestRequest) NodesUsageResponse(org.opensearch.action.admin.cluster.node.usage.NodesUsageResponse) Set(java.util.Set) IOException(java.io.IOException) RestStatus(org.opensearch.rest.RestStatus) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) Strings(org.opensearch.common.Strings) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) NodesUsageRequest(org.opensearch.action.admin.cluster.node.usage.NodesUsageRequest) RestBuilderListener(org.opensearch.rest.action.RestBuilderListener) List(java.util.List) RestActions(org.opensearch.rest.action.RestActions) Locale(java.util.Locale) Arrays.asList(java.util.Arrays.asList) BaseRestHandler(org.opensearch.rest.BaseRestHandler) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) NodesUsageResponse(org.opensearch.action.admin.cluster.node.usage.NodesUsageResponse) IOException(java.io.IOException) BytesRestResponse(org.opensearch.rest.BytesRestResponse) NodesUsageRequest(org.opensearch.action.admin.cluster.node.usage.NodesUsageRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

IOException (java.io.IOException)1 Arrays.asList (java.util.Arrays.asList)1 Collections.unmodifiableList (java.util.Collections.unmodifiableList)1 List (java.util.List)1 Locale (java.util.Locale)1 Set (java.util.Set)1 NodesUsageRequest (org.opensearch.action.admin.cluster.node.usage.NodesUsageRequest)1 NodesUsageResponse (org.opensearch.action.admin.cluster.node.usage.NodesUsageResponse)1 NodeClient (org.opensearch.client.node.NodeClient)1 Strings (org.opensearch.common.Strings)1 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)1 BaseRestHandler (org.opensearch.rest.BaseRestHandler)1 BytesRestResponse (org.opensearch.rest.BytesRestResponse)1 RestRequest (org.opensearch.rest.RestRequest)1 GET (org.opensearch.rest.RestRequest.Method.GET)1 RestResponse (org.opensearch.rest.RestResponse)1 RestStatus (org.opensearch.rest.RestStatus)1 RestActions (org.opensearch.rest.action.RestActions)1 RestBuilderListener (org.opensearch.rest.action.RestBuilderListener)1