|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 6 | + * You may not use this file except in compliance with the License. |
| 7 | + * A copy of the License is located at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * or in the "license" file accompanying this file. This file is distributed |
| 12 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 13 | + * express or implied. See the License for the specific language governing |
| 14 | + * permissions and limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package com.amazon.opendistroforelasticsearch.sql.plugin.rest; |
| 19 | + |
| 20 | +import static org.elasticsearch.rest.RestStatus.SERVICE_UNAVAILABLE; |
| 21 | + |
| 22 | +import com.amazon.opendistroforelasticsearch.sql.legacy.executor.format.ErrorMessageFactory; |
| 23 | +import com.amazon.opendistroforelasticsearch.sql.legacy.metrics.Metrics; |
| 24 | +import com.amazon.opendistroforelasticsearch.sql.legacy.utils.LogUtils; |
| 25 | +import com.google.common.collect.ImmutableList; |
| 26 | +import java.util.List; |
| 27 | +import org.apache.logging.log4j.LogManager; |
| 28 | +import org.apache.logging.log4j.Logger; |
| 29 | +import org.elasticsearch.client.node.NodeClient; |
| 30 | +import org.elasticsearch.common.settings.Settings; |
| 31 | +import org.elasticsearch.rest.BaseRestHandler; |
| 32 | +import org.elasticsearch.rest.BytesRestResponse; |
| 33 | +import org.elasticsearch.rest.RestController; |
| 34 | +import org.elasticsearch.rest.RestRequest; |
| 35 | +import org.elasticsearch.rest.RestStatus; |
| 36 | + |
| 37 | +/** |
| 38 | + * PPL Node level status. |
| 39 | + */ |
| 40 | +public class RestPPLStatsAction extends BaseRestHandler { |
| 41 | + |
| 42 | + private static final Logger LOG = LogManager.getLogger(RestPPLStatsAction.class); |
| 43 | + |
| 44 | + /** |
| 45 | + * API endpoint path. |
| 46 | + */ |
| 47 | + public static final String PPL_STATS_API_ENDPOINT = "/_opendistro/_ppl/stats"; |
| 48 | + |
| 49 | + public RestPPLStatsAction(Settings settings, RestController restController) { |
| 50 | + super(); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public String getName() { |
| 55 | + return "ppl_stats_action"; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public List<Route> routes() { |
| 60 | + return ImmutableList.of( |
| 61 | + new Route(RestRequest.Method.POST, PPL_STATS_API_ENDPOINT), |
| 62 | + new Route(RestRequest.Method.GET, PPL_STATS_API_ENDPOINT) |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) { |
| 68 | + |
| 69 | + LogUtils.addRequestId(); |
| 70 | + |
| 71 | + try { |
| 72 | + return channel -> channel.sendResponse(new BytesRestResponse(RestStatus.OK, |
| 73 | + Metrics.getInstance().collectToJSON())); |
| 74 | + } catch (Exception e) { |
| 75 | + LOG.error("Failed during Query PPL STATS Action.", e); |
| 76 | + |
| 77 | + return channel -> channel.sendResponse(new BytesRestResponse(SERVICE_UNAVAILABLE, |
| 78 | + ErrorMessageFactory.createErrorMessage(e, SERVICE_UNAVAILABLE.getStatus()).toString())); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments