diff --git a/server/src/main/java/org/opensearch/action/search/SearchQueryCounters.java b/server/src/main/java/org/opensearch/action/search/SearchQueryCounters.java index 27270f8465ef5..a8a7e352b89dc 100644 --- a/server/src/main/java/org/opensearch/action/search/SearchQueryCounters.java +++ b/server/src/main/java/org/opensearch/action/search/SearchQueryCounters.java @@ -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; } }