This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add PPL stats endpoint * update
- Loading branch information
Showing
6 changed files
with
207 additions
and
6 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
integ-test/src/test/java/com/amazon/opendistroforelasticsearch/sql/ppl/MetricsIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.sql.ppl; | ||
|
||
import static com.amazon.opendistroforelasticsearch.sql.legacy.TestsConstants.TEST_INDEX_BANK; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
import com.amazon.opendistroforelasticsearch.sql.legacy.metrics.MetricName; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.concurrent.TimeUnit; | ||
import org.elasticsearch.client.Request; | ||
import org.elasticsearch.client.Response; | ||
import org.json.JSONObject; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class MetricsIT extends PPLIntegTestCase { | ||
|
||
@Override | ||
protected void init() throws Exception { | ||
loadIndex(Index.BANK); | ||
} | ||
|
||
@Test | ||
public void requestCount() throws IOException, InterruptedException { | ||
int beforeQueries = pplRequestTotal(); | ||
multiQueries(3); | ||
TimeUnit.SECONDS.sleep(2L); | ||
|
||
assertThat(pplRequestTotal(), equalTo(beforeQueries + 3)); | ||
} | ||
|
||
private void multiQueries(int n) throws IOException { | ||
for (int i = 0; i < n; ++i) { | ||
executeQuery(String.format("source=%s | where age = 31 + 1 | fields age", TEST_INDEX_BANK)); | ||
} | ||
} | ||
|
||
private Request makeStatRequest() { | ||
return new Request( | ||
"GET", "/_opendistro/_ppl/stats" | ||
); | ||
} | ||
|
||
private int pplRequestTotal() throws IOException { | ||
JSONObject jsonObject = new JSONObject(executeStatRequest(makeStatRequest())); | ||
return jsonObject.getInt(MetricName.PPL_REQ_TOTAL.getName()); | ||
} | ||
|
||
private String executeStatRequest(final Request request) throws IOException { | ||
Response sqlResponse = client().performRequest(request); | ||
|
||
Assert.assertTrue(sqlResponse.getStatusLine().getStatusCode() == 200); | ||
|
||
InputStream is = sqlResponse.getEntity().getContent(); | ||
StringBuilder sb = new StringBuilder(); | ||
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) { | ||
String line = null; | ||
while ((line = br.readLine()) != null) { | ||
sb.append(line); | ||
} | ||
} | ||
|
||
return sb.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...c/main/java/com/amazon/opendistroforelasticsearch/sql/plugin/rest/RestPPLStatsAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.amazon.opendistroforelasticsearch.sql.plugin.rest; | ||
|
||
import static org.elasticsearch.rest.RestStatus.SERVICE_UNAVAILABLE; | ||
|
||
import com.amazon.opendistroforelasticsearch.sql.legacy.executor.format.ErrorMessageFactory; | ||
import com.amazon.opendistroforelasticsearch.sql.legacy.metrics.Metrics; | ||
import com.amazon.opendistroforelasticsearch.sql.legacy.utils.LogUtils; | ||
import com.google.common.collect.ImmutableList; | ||
import java.util.List; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.client.node.NodeClient; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.rest.BaseRestHandler; | ||
import org.elasticsearch.rest.BytesRestResponse; | ||
import org.elasticsearch.rest.RestController; | ||
import org.elasticsearch.rest.RestRequest; | ||
import org.elasticsearch.rest.RestStatus; | ||
|
||
/** | ||
* PPL Node level status. | ||
*/ | ||
public class RestPPLStatsAction extends BaseRestHandler { | ||
|
||
private static final Logger LOG = LogManager.getLogger(RestPPLStatsAction.class); | ||
|
||
/** | ||
* API endpoint path. | ||
*/ | ||
public static final String PPL_STATS_API_ENDPOINT = "/_opendistro/_ppl/stats"; | ||
|
||
public RestPPLStatsAction(Settings settings, RestController restController) { | ||
super(); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "ppl_stats_action"; | ||
} | ||
|
||
@Override | ||
public List<Route> routes() { | ||
return ImmutableList.of( | ||
new Route(RestRequest.Method.POST, PPL_STATS_API_ENDPOINT), | ||
new Route(RestRequest.Method.GET, PPL_STATS_API_ENDPOINT) | ||
); | ||
} | ||
|
||
@Override | ||
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) { | ||
|
||
LogUtils.addRequestId(); | ||
|
||
try { | ||
return channel -> channel.sendResponse(new BytesRestResponse(RestStatus.OK, | ||
Metrics.getInstance().collectToJSON())); | ||
} catch (Exception e) { | ||
LOG.error("Failed during Query PPL STATS Action.", e); | ||
|
||
return channel -> channel.sendResponse(new BytesRestResponse(SERVICE_UNAVAILABLE, | ||
ErrorMessageFactory.createErrorMessage(e, SERVICE_UNAVAILABLE.getStatus()).toString())); | ||
} | ||
} | ||
} |