From 4544586d4781e6a8cedb15bd28d667cf3e6e9419 Mon Sep 17 00:00:00 2001 From: Daniel Mitterdorfer Date: Mon, 12 Feb 2024 10:37:20 +0100 Subject: [PATCH] [Profiling] Return early when no data are found With this commit we check whether any documents match when custom indices are provided and terminate early if the response is empty. --- .../profiling/GetStackTracesActionIT.java | 21 +++++++++++++++++++ .../TransportGetStackTracesAction.java | 6 +++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/x-pack/plugin/profiling/src/internalClusterTest/java/org/elasticsearch/xpack/profiling/GetStackTracesActionIT.java b/x-pack/plugin/profiling/src/internalClusterTest/java/org/elasticsearch/xpack/profiling/GetStackTracesActionIT.java index d42bc59e7dbb5..27fd5f58d755e 100644 --- a/x-pack/plugin/profiling/src/internalClusterTest/java/org/elasticsearch/xpack/profiling/GetStackTracesActionIT.java +++ b/x-pack/plugin/profiling/src/internalClusterTest/java/org/elasticsearch/xpack/profiling/GetStackTracesActionIT.java @@ -176,4 +176,25 @@ public void testGetStackTracesFromAPMNoMatch() throws Exception { GetStackTracesResponse response = client().execute(GetStackTracesAction.INSTANCE, request).get(); assertEquals(0, response.getTotalFrames()); } + + public void testGetStackTracesFromAPMIndexNotAvailable() throws Exception { + TermQueryBuilder query = QueryBuilders.termQuery("transaction.name", "nonExistingTransaction"); + + GetStackTracesRequest request = new GetStackTracesRequest( + null, + 1.0d, + 1.0d, + 1.0d, + query, + new String[] { "non-existing-apm-index-*" }, + "transaction.profiler_stack_trace_ids", + null, + null, + null, + null, + null + ); + GetStackTracesResponse response = client().execute(GetStackTracesAction.INSTANCE, request).get(); + assertEquals(0, response.getTotalFrames()); + } } diff --git a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/TransportGetStackTracesAction.java b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/TransportGetStackTracesAction.java index d49f42ce69737..a0547b39388d8 100644 --- a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/TransportGetStackTracesAction.java +++ b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/TransportGetStackTracesAction.java @@ -242,7 +242,11 @@ private void searchGenericEvents( request.getIndices(), responseBuilder.getSamplingRate() ); - searchGenericEventGroupedByStackTrace(submitTask, client, request, submitListener, responseBuilder); + if (sampleCount > 0) { + searchGenericEventGroupedByStackTrace(submitTask, client, request, submitListener, responseBuilder); + } else { + submitListener.onResponse(responseBuilder.build()); + } }, submitListener::onFailure)); }