-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HLRC: Add xpack usage api #31975
HLRC: Add xpack usage api #31975
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,6 +106,7 @@ | |
import org.elasticsearch.index.VersionType; | ||
import org.elasticsearch.index.rankeval.RankEvalRequest; | ||
import org.elasticsearch.protocol.xpack.XPackInfoRequest; | ||
import org.elasticsearch.protocol.xpack.XPackUsageRequest; | ||
import org.elasticsearch.rest.action.search.RestSearchAction; | ||
import org.elasticsearch.script.mustache.MultiSearchTemplateRequest; | ||
import org.elasticsearch.script.mustache.SearchTemplateRequest; | ||
|
@@ -1096,6 +1097,11 @@ static Request xPackInfo(XPackInfoRequest infoRequest) { | |
return request; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
static Request xpackUsage(XPackUsageRequest request) { | ||
return new Request(HttpGet.METHOD_NAME, "/_xpack/usage"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add the master timeout? |
||
} | ||
|
||
private static HttpEntity createEntity(ToXContent toXContent, XContentType xContentType) throws IOException { | ||
BytesRef source = XContentHelper.toXContent(toXContent, xContentType, false).toBytesRef(); | ||
return new ByteArrayEntity(source.bytes, source.offset, source.length, createContentType(xContentType)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,12 +35,17 @@ | |
import org.elasticsearch.protocol.xpack.XPackInfoResponse.BuildInfo; | ||
import org.elasticsearch.protocol.xpack.XPackInfoResponse.FeatureSetsInfo; | ||
import org.elasticsearch.protocol.xpack.XPackInfoResponse.LicenseInfo; | ||
import org.elasticsearch.protocol.xpack.XPackUsageRequest; | ||
import org.elasticsearch.protocol.xpack.XPackUsageResponse; | ||
|
||
import java.io.IOException; | ||
import java.util.EnumSet; | ||
import java.util.Map; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
|
||
/** | ||
* Documentation for miscellaneous APIs in the high level java client. | ||
* Code wrapped in {@code tag} and {@code end} tags is included in the docs. | ||
|
@@ -129,6 +134,47 @@ public void onFailure(Exception e) { | |
} | ||
} | ||
|
||
public void testXPackUsage() throws Exception { | ||
RestHighLevelClient client = highLevelClient(); | ||
{ | ||
//tag::x-pack-usage-execute | ||
XPackUsageResponse response = client.xpack().usage(RequestOptions.DEFAULT); | ||
//end::x-pack-info-execute | ||
|
||
//tag::x-pack-usage-response | ||
Map<String, Map<String, Object>> usages = response.getUsages(); | ||
Map<String, Object> monitoringUsage = usages.get("monitoring"); | ||
assertThat(monitoringUsage.get("available"), is(true)); | ||
assertThat(monitoringUsage.get("enabled"), is(true)); | ||
assertThat(monitoringUsage.get("collection_enabled"), is(false)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing and |
||
} | ||
{ | ||
// tag::x-pack-usage-execute-listener | ||
ActionListener<XPackUsageResponse> listener = new ActionListener<XPackUsageResponse>() { | ||
@Override | ||
public void onResponse(XPackUsageResponse response) { | ||
// <1> | ||
} | ||
|
||
@Override | ||
public void onFailure(Exception e) { | ||
// <2> | ||
} | ||
}; | ||
// end::x-pack-usage-execute-listener | ||
|
||
// Replace the empty listener by a blocking listener in test | ||
final CountDownLatch latch = new CountDownLatch(1); | ||
listener = new LatchedActionListener<>(listener, latch); | ||
|
||
// tag::x-pack-usage-execute-async | ||
client.xpack().usageAsync(RequestOptions.DEFAULT, listener); // <1> | ||
// end::x-pack-usage-execute-async | ||
|
||
assertTrue(latch.await(30L, TimeUnit.SECONDS)); | ||
} | ||
} | ||
|
||
public void testInitializationFromClientBuilder() throws IOException { | ||
//tag::rest-high-level-client-init | ||
RestHighLevelClient client = new RestHighLevelClient( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
[[java-rest-high-x-pack-usage]] | ||
=== X-Pack Usage API | ||
|
||
[[java-rest-high-x-pack-usage-execution]] | ||
==== Execution | ||
|
||
Detailed information about the usage of features from {xpack} can be | ||
retrieved using the `xpackUsage()` method: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name isn't quite right here. I suspect the name in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the name is correct. What do you think it should be? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah ic, xpackUsage is the helper method in RequestConverters. |
||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/MiscellaneousDocumentationIT.java[x-pack-usage-execute] | ||
-------------------------------------------------- | ||
|
||
[[java-rest-high-x-pack-info-response]] | ||
==== Response | ||
|
||
The returned `XPackUsageResponse` contains a `Map` keyed by feature name. | ||
Every feature map has an `available` key, indicating whether that | ||
feature is available given the current license, and an `enabled` key, | ||
indicating whether that feature is currently enabled. Other keys | ||
are specific to each feature. | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/MiscellaneousDocumentationIT.java[x-pack-usage-response] | ||
-------------------------------------------------- | ||
|
||
[[java-rest-high-x-pack-usage-async]] | ||
==== Asynchronous Execution | ||
|
||
This request can be executed asynchronously: | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/MiscellaneousDocumentationIT.java[x-pack-usage-execute-async] | ||
-------------------------------------------------- | ||
<1> The call to execute the usage api and the `ActionListener` to use when | ||
the execution completes | ||
|
||
The asynchronous method does not block and returns immediately. Once it is | ||
completed the `ActionListener` is called back using the `onResponse` method | ||
if the execution successfully completed or using the `onFailure` method if | ||
it failed. | ||
|
||
A typical listener for `XPackUsageResponse` looks like: | ||
|
||
["source","java",subs="attributes,callouts,macros"] | ||
-------------------------------------------------- | ||
include-tagged::{doc-tests}/MiscellaneousDocumentationIT.java[x-pack-usage-execute-listener] | ||
-------------------------------------------------- | ||
<1> Called when the execution is successfully completed. The response is | ||
provided as an argument | ||
<2> Called in case of failure. The raised exception is provided as an argument |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
* Licensed to Elasticsearch under one or more contributor | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this'll cause the build to fail. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Silly IntelliJ :/ |
||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License 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 org.elasticsearch.xpack.core.action; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License 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 org.elasticsearch.protocol.xpack; | ||
|
||
import org.elasticsearch.action.ActionRequest; | ||
import org.elasticsearch.action.ActionRequestValidationException; | ||
|
||
/** A dummy request object solely for the purpose of boilerplate. */ | ||
public class XPackUsageRequest extends ActionRequest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks to me like there is a |
||
@Override | ||
public ActionRequestValidationException validate() { | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License 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 org.elasticsearch.protocol.xpack; | ||
|
||
import org.elasticsearch.common.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class XPackUsageResponse { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May as well stick javadoc on it. It'll be mostly ceremonial but it might help someone who hasn't figured out the naming convention yet. |
||
|
||
private final Map<String, Map<String, Object>> usages; | ||
|
||
private XPackUsageResponse(Map<String, Map<String, Object>> usages) throws IOException { | ||
this.usages = usages; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static Map<String, Object> castMap(Object value) { | ||
return (Map<String, Object>)value; | ||
} | ||
|
||
/** Return a map from feature name to usage information for that feature. */ | ||
public Map<String, Map<String, Object>> getUsages() { | ||
return usages; | ||
} | ||
|
||
public static XPackUsageResponse fromXContent(XContentParser parser) throws IOException { | ||
Map<String, Object> rawMap = parser.map(); | ||
Map<String, Map<String, Object>> usages = rawMap.entrySet().stream().collect( | ||
Collectors.toMap(Map.Entry::getKey, e -> castMap(e.getValue()))); | ||
return new XPackUsageResponse(usages); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is used.