Skip to content

Commit

Permalink
Create counter atomically
Browse files Browse the repository at this point in the history
Signed-off-by: Siddhant Deshmukh <deshsid@amazon.com>
  • Loading branch information
deshsidd committed Mar 13, 2024
1 parent b52cd83 commit dc1f4f3
Showing 1 changed file with 3 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,16 @@ public SearchQueryCounters(MetricsRegistry metricsRegistry) {
public void incrementCounter(QueryBuilder queryBuilder, int level) {
String uniqueQueryCounterName = queryBuilder.getName();

if (!nameToQueryTypeCounters.contains(uniqueQueryCounterName)) {
createQueryCounter(uniqueQueryCounterName);
}

Counter counter = nameToQueryTypeCounters.getOrDefault(uniqueQueryCounterName, otherQueryCounter);
Counter counter = nameToQueryTypeCounters.computeIfAbsent(uniqueQueryCounterName, k -> createQueryCounter(k));
counter.add(1, Tags.create().addTag(LEVEL_TAG, level));
}

private void createQueryCounter(String counterName) {
private Counter createQueryCounter(String counterName) {
Counter counter = metricsRegistry.createCounter(
"search.query.type." + counterName + ".count",
"Counter for the number of top level and nested " + counterName + " search queries",
UNIT
);
nameToQueryTypeCounters.put(counterName, counter);
return counter;
}
}

0 comments on commit dc1f4f3

Please sign in to comment.