Skip to content

Commit 017ae17

Browse files
authored
Add search request attributes to the response count metric (#135707)
This adds the search request attributes to the es.search_response.response_count.total metric.
1 parent 62b0dd3 commit 017ae17

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

server/src/main/java/org/elasticsearch/action/search/TransportSearchAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,7 +2099,7 @@ public void onResponse(SearchResponse searchResponse) {
20992099
}
21002100
}
21012101
}
2102-
searchResponseMetrics.incrementResponseCount(responseCountTotalStatus);
2102+
searchResponseMetrics.incrementResponseCount(responseCountTotalStatus, searchRequestAttributes);
21032103

21042104
if (collectCCSTelemetry) {
21052105
extractCCSTelemetry(searchResponse);
@@ -2115,7 +2115,7 @@ public void onResponse(SearchResponse searchResponse) {
21152115

21162116
@Override
21172117
public void onFailure(Exception e) {
2118-
searchResponseMetrics.incrementResponseCount(SearchResponseMetrics.ResponseCountTotalStatus.FAILURE);
2118+
searchResponseMetrics.incrementResponseCount(SearchResponseMetrics.ResponseCountTotalStatus.FAILURE, searchRequestAttributes);
21192119
if (collectCCSTelemetry) {
21202120
usageBuilder.setFailure(e);
21212121
recordTelemetry();

server/src/main/java/org/elasticsearch/rest/action/search/SearchResponseMetrics.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.telemetry.metric.LongHistogram;
1515
import org.elasticsearch.telemetry.metric.MeterRegistry;
1616

17+
import java.util.HashMap;
1718
import java.util.Map;
1819

1920
/**
@@ -83,4 +84,10 @@ public void incrementResponseCount(ResponseCountTotalStatus responseCountTotalSt
8384
Map.of(RESPONSE_COUNT_TOTAL_STATUS_ATTRIBUTE_NAME, responseCountTotalStatus.getDisplayName())
8485
);
8586
}
87+
88+
public void incrementResponseCount(ResponseCountTotalStatus responseCountTotalStatus, Map<String, Object> attributes) {
89+
Map<String, Object> attributesWithStatus = new HashMap<>(attributes);
90+
attributesWithStatus.put(RESPONSE_COUNT_TOTAL_STATUS_ATTRIBUTE_NAME, responseCountTotalStatus.getDisplayName());
91+
responseCountTotalCounter.incrementBy(1L, attributesWithStatus);
92+
}
8693
}

server/src/test/java/org/elasticsearch/search/TelemetryMetrics/SearchResponseCountTelemetryTests.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@
3131
import java.util.Arrays;
3232
import java.util.Collection;
3333
import java.util.List;
34+
import java.util.Map;
3435

3536
import static org.elasticsearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE;
3637
import static org.elasticsearch.index.query.QueryBuilders.simpleQueryStringQuery;
3738
import static org.elasticsearch.rest.action.search.SearchResponseMetrics.RESPONSE_COUNT_TOTAL_COUNTER_NAME;
3839
import static org.elasticsearch.rest.action.search.SearchResponseMetrics.RESPONSE_COUNT_TOTAL_STATUS_ATTRIBUTE_NAME;
40+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
3941
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertScrollResponsesAndHitCount;
42+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits;
4043
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHitsWithoutFailures;
4144
import static org.hamcrest.Matchers.containsString;
4245
import static org.hamcrest.Matchers.equalTo;
@@ -239,4 +242,48 @@ public void testScrollWithAllShardsFail() throws Exception {
239242
);
240243
});
241244
}
245+
246+
public void testAdditionalSearchAttributes() {
247+
// target index attribute contains the system index name
248+
createIndex(".kibana");
249+
{
250+
SearchResponse searchResponse = client().prepareSearch(".kibana").setQuery(simpleQueryStringQuery("foo")).get();
251+
try {
252+
assertNoFailures(searchResponse);
253+
assertSearchHits(searchResponse);
254+
} finally {
255+
searchResponse.decRef();
256+
}
257+
List<Measurement> measurements = getTestTelemetryPlugin().getLongCounterMeasurement(RESPONSE_COUNT_TOTAL_COUNTER_NAME);
258+
assertEquals(1, measurements.size());
259+
Measurement measurement = measurements.getFirst();
260+
assertEquals(1, measurement.getLong());
261+
Map<String, Object> attributes = measurement.attributes();
262+
assertEquals(4, attributes.size());
263+
assertEquals(".kibana", attributes.get("target"));
264+
assertEquals("hits_only", attributes.get("query_type"));
265+
assertEquals("_score", attributes.get("sort"));
266+
assertEquals("success", attributes.get(RESPONSE_COUNT_TOTAL_STATUS_ATTRIBUTE_NAME));
267+
}
268+
// target index attribute should be "user"
269+
createIndex("custom-index");
270+
{
271+
SearchResponse searchResponse = client().prepareSearch("custom*").setQuery(simpleQueryStringQuery("foo")).get();
272+
try {
273+
assertNoFailures(searchResponse);
274+
assertSearchHits(searchResponse);
275+
} finally {
276+
searchResponse.decRef();
277+
}
278+
List<Measurement> measurements = getTestTelemetryPlugin().getLongCounterMeasurement(RESPONSE_COUNT_TOTAL_COUNTER_NAME);
279+
assertEquals(2, measurements.size());
280+
Measurement measurement = measurements.getLast();
281+
Map<String, Object> attributes = measurement.attributes();
282+
assertEquals(4, attributes.size());
283+
assertEquals("user", attributes.get("target"));
284+
assertEquals("hits_only", attributes.get("query_type"));
285+
assertEquals("_score", attributes.get("sort"));
286+
assertEquals("success", attributes.get(RESPONSE_COUNT_TOTAL_STATUS_ATTRIBUTE_NAME));
287+
}
288+
}
242289
}

0 commit comments

Comments
 (0)