-
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
Add analytics plugin usage stats to _xpack/usage #54911
Merged
imotov
merged 8 commits into
elastic:master
from
imotov:issue-54847-analytics-xpack-usage
Apr 13, 2020
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b86bd57
Add analytics plugin usage stats to _xpack/usage
imotov 0e1bb69
Address review comments by replacing String based counters with enum …
imotov c6818b9
Fix usage docs
imotov c22680e
Add comment about underlying EnumCounters assumptions
imotov 67eb9fa
Merge remote-tracking branch 'elastic/master' into issue-54847-analyt…
imotov 684cbc3
Fix checkstyle
imotov 7f5e89e
Fix boxplot stats serialization
imotov 950be0f
Add node response tests
imotov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.support.ActionFilters; | ||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; | ||
import org.elasticsearch.cluster.service.ClusterService; | ||
|
@@ -20,26 +21,49 @@ | |
import org.elasticsearch.xpack.core.action.XPackUsageFeatureResponse; | ||
import org.elasticsearch.xpack.core.action.XPackUsageFeatureTransportAction; | ||
import org.elasticsearch.xpack.core.analytics.AnalyticsFeatureSetUsage; | ||
import org.elasticsearch.xpack.core.analytics.EnumCounters; | ||
import org.elasticsearch.xpack.core.analytics.action.AnalyticsStatsAction; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class AnalyticsUsageTransportAction extends XPackUsageFeatureTransportAction { | ||
private final XPackLicenseState licenseState; | ||
private final Client client; | ||
|
||
@Inject | ||
public AnalyticsUsageTransportAction(TransportService transportService, ClusterService clusterService, ThreadPool threadPool, | ||
ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver, | ||
XPackLicenseState licenseState) { | ||
XPackLicenseState licenseState, Client client) { | ||
super(XPackUsageFeatureAction.ANALYTICS.name(), transportService, clusterService, | ||
threadPool, actionFilters, indexNameExpressionResolver); | ||
this.licenseState = licenseState; | ||
this.client = client; | ||
} | ||
|
||
@Override | ||
protected void masterOperation(Task task, XPackUsageRequest request, ClusterState state, | ||
ActionListener<XPackUsageFeatureResponse> listener) { | ||
boolean available = licenseState.isDataScienceAllowed(); | ||
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. Oof, just noticed "DataScience"... we should probably change that at some point 😓 Not for this PR though :) |
||
if (available) { | ||
AnalyticsStatsAction.Request statsRequest = new AnalyticsStatsAction.Request(); | ||
statsRequest.setParentTask(clusterService.localNode().getId(), task.getId()); | ||
client.execute(AnalyticsStatsAction.INSTANCE, statsRequest, ActionListener.wrap(r -> | ||
listener.onResponse(new XPackUsageFeatureResponse(usageFeatureResponse(true, true, r))), | ||
listener::onFailure)); | ||
} else { | ||
AnalyticsFeatureSetUsage usage = new AnalyticsFeatureSetUsage(false, true, Collections.emptyMap()); | ||
listener.onResponse(new XPackUsageFeatureResponse(usage)); | ||
} | ||
} | ||
|
||
AnalyticsFeatureSetUsage usage = | ||
new AnalyticsFeatureSetUsage(available, true); | ||
listener.onResponse(new XPackUsageFeatureResponse(usage)); | ||
static AnalyticsFeatureSetUsage usageFeatureResponse(boolean available, boolean enabled, AnalyticsStatsAction.Response r) { | ||
List<EnumCounters<AnalyticsStatsAction.Item>> countersPerNode = r.getNodes() | ||
.stream() | ||
.map(AnalyticsStatsAction.NodeResponse::getStats) | ||
.collect(Collectors.toList()); | ||
EnumCounters<AnalyticsStatsAction.Item> mergedCounters = EnumCounters.merge(AnalyticsStatsAction.Item.class, countersPerNode); | ||
return new AnalyticsFeatureSetUsage(available, enabled, mergedCounters.toMap()); | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
.../java/org/elasticsearch/xpack/analytics/action/AnalyticsStatsActionNodeResponseTests.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,49 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.analytics.action; | ||
|
||
import org.elasticsearch.Version; | ||
import org.elasticsearch.cluster.node.DiscoveryNode; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.test.AbstractWireSerializingTestCase; | ||
import org.elasticsearch.xpack.core.analytics.EnumCounters; | ||
import org.elasticsearch.xpack.core.analytics.action.AnalyticsStatsAction; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class AnalyticsStatsActionNodeResponseTests extends AbstractWireSerializingTestCase<AnalyticsStatsAction.NodeResponse> { | ||
|
||
@Override | ||
protected Writeable.Reader<AnalyticsStatsAction.NodeResponse> instanceReader() { | ||
return AnalyticsStatsAction.NodeResponse::new; | ||
} | ||
|
||
@Override | ||
protected AnalyticsStatsAction.NodeResponse createTestInstance() { | ||
String nodeName = randomAlphaOfLength(10); | ||
DiscoveryNode node = new DiscoveryNode(nodeName, buildNewFakeTransportAddress(), Version.CURRENT); | ||
EnumCounters<AnalyticsStatsAction.Item> counters = new EnumCounters<>(AnalyticsStatsAction.Item.class); | ||
for (AnalyticsStatsAction.Item item : AnalyticsStatsAction.Item.values()) { | ||
if (randomBoolean()) { | ||
counters.inc(item, randomLongBetween(0, 1000)); | ||
} | ||
} | ||
return new AnalyticsStatsAction.NodeResponse(node, counters); | ||
} | ||
|
||
public void testItemEnum() { | ||
int i = 0; | ||
// We rely on the ordinals for serialization, so they shouldn't change between version | ||
assertThat(AnalyticsStatsAction.Item.BOXPLOT.ordinal(), equalTo(i++)); | ||
assertThat(AnalyticsStatsAction.Item.CUMULATIVE_CARDINALITY.ordinal(), equalTo(i++)); | ||
assertThat(AnalyticsStatsAction.Item.STRING_STATS.ordinal(), equalTo(i++)); | ||
assertThat(AnalyticsStatsAction.Item.TOP_METRICS.ordinal(), equalTo(i++)); | ||
assertThat(AnalyticsStatsAction.Item.T_TEST.ordinal(), equalTo(i++)); | ||
// Please add tests for newly added items here | ||
assertThat(AnalyticsStatsAction.Item.values().length, equalTo(i)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Ouch.