diff --git a/server/src/main/java/org/opensearch/action/admin/indices/stats/CommonStatsFlags.java b/server/src/main/java/org/opensearch/action/admin/indices/stats/CommonStatsFlags.java index 03fb55323feec..1e1d213e7400f 100644 --- a/server/src/main/java/org/opensearch/action/admin/indices/stats/CommonStatsFlags.java +++ b/server/src/main/java/org/opensearch/action/admin/indices/stats/CommonStatsFlags.java @@ -332,7 +332,8 @@ public enum Flag { Translog("translog", 13), // 14 was previously used for Suggest RequestCache("request_cache", 15), - Recovery("recovery", 16); + Recovery("recovery", 16), + StatusCounter("status_counter", 17); private final String restName; private final int index; diff --git a/server/src/main/java/org/opensearch/indices/IndicesService.java b/server/src/main/java/org/opensearch/indices/IndicesService.java index 4d11c8460eb7b..4a67130165d42 100644 --- a/server/src/main/java/org/opensearch/indices/IndicesService.java +++ b/server/src/main/java/org/opensearch/indices/IndicesService.java @@ -927,11 +927,18 @@ public NodeIndicesStats stats(CommonStatsFlags flags) { break; } } + // Only include status_counter when it is explicitly requested (or when all flags are set). + // Previously it was always included regardless of the requested index metrics, causing it + // to appear even for targeted requests like GET _nodes/stats/indices/request_cache. + // See https://github.com/opensearch-project/OpenSearch/issues/22383 + final StatusCounterStats resolvedStatusCounterStats = flags.isSet(CommonStatsFlags.Flag.StatusCounter) + ? statusCounterStats + : null; if (flags.getIncludeIndicesStatsByLevel()) { NodeIndicesStats.StatsLevel statsLevel = NodeIndicesStats.getAcceptedLevel(flags.getLevels()); - return new NodeIndicesStats(commonStats, statsByShard(this, flags), searchRequestStats, statusCounterStats, statsLevel); + return new NodeIndicesStats(commonStats, statsByShard(this, flags), searchRequestStats, resolvedStatusCounterStats, statsLevel); } else { - return new NodeIndicesStats(commonStats, statsByShard(this, flags), searchRequestStats, statusCounterStats); + return new NodeIndicesStats(commonStats, statsByShard(this, flags), searchRequestStats, resolvedStatusCounterStats); } }