-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Formalize usage stats for analytics (#52966)
This moves the usage statistics gathering from the `AnalyticsPlugin` into an `AnalyicsUsage`, removing the static state. It also checks the license level when parsing all analytics aggregations. This is how we were checking them before but we did it in an easy to forget way. This way is slightly simpler, I think.
- Loading branch information
Showing
7 changed files
with
188 additions
and
72 deletions.
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
59 changes: 59 additions & 0 deletions
59
x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/AnalyticsUsage.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,59 @@ | ||
/* | ||
* 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; | ||
|
||
import org.elasticsearch.cluster.node.DiscoveryNode; | ||
import org.elasticsearch.common.xcontent.ContextParser; | ||
import org.elasticsearch.xpack.core.analytics.action.AnalyticsStatsAction; | ||
|
||
import java.util.EnumMap; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
/** | ||
* Tracks usage of the Analytics aggregations. | ||
*/ | ||
public class AnalyticsUsage { | ||
/** | ||
* Items to track. | ||
*/ | ||
public enum Item { | ||
BOXPLOT, | ||
CUMULATIVE_CARDINALITY, | ||
STRING_STATS, | ||
TOP_METRICS; | ||
} | ||
|
||
private final Map<Item, AtomicLong> trackers = new EnumMap<>(Item.class); | ||
|
||
public AnalyticsUsage() { | ||
for (Item item: Item.values()) { | ||
trackers.put(item, new AtomicLong(0)); | ||
} | ||
} | ||
|
||
/** | ||
* Track successful parsing. | ||
*/ | ||
public <C, T> ContextParser<C, T> track(Item item, ContextParser<C, T> realParser) { | ||
AtomicLong usage = trackers.get(item); | ||
return (parser, context) -> { | ||
T value = realParser.parse(parser, context); | ||
// Intentionally doesn't count unless the parser returns cleanly. | ||
usage.incrementAndGet(); | ||
return value; | ||
}; | ||
} | ||
|
||
public AnalyticsStatsAction.NodeResponse stats(DiscoveryNode node) { | ||
return new AnalyticsStatsAction.NodeResponse(node, | ||
trackers.get(Item.BOXPLOT).get(), | ||
trackers.get(Item.CUMULATIVE_CARDINALITY).get(), | ||
trackers.get(Item.STRING_STATS).get(), | ||
trackers.get(Item.TOP_METRICS).get()); | ||
} | ||
} |
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
Oops, something went wrong.