Skip to content

Commit 34dfdf0

Browse files
committed
Optimizing index check for normal app usage
1 parent 287416e commit 34dfdf0

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

x-pack/legacy/plugins/siem/server/lib/source_status/elasticsearch_adapter.ts

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,45 @@ export class ElasticsearchSourceStatusAdapter implements SourceStatusAdapter {
1515
constructor(private readonly framework: FrameworkAdapter) {}
1616

1717
public async hasIndices(request: FrameworkRequest, indexNames: string[]) {
18-
// Note: Additional check necessary for APM-specific index. For details see: https://github.com/elastic/kibana/issues/56363
19-
// Only verify if APM data exists if indexNames includes `apm-*-transaction*` (default included apm index)
20-
const includesApmIndex = indexNames.includes(APM_INDEX_NAME);
21-
const hasApmDataReq = includesApmIndex
22-
? this.framework.callWithRequest<{}, ApmServiceNameAgg>(
23-
request,
24-
'search',
25-
buildQuery({ defaultIndex: [APM_INDEX_NAME] })
26-
)
27-
: Promise.resolve(undefined);
28-
29-
// Remove APM index if exists, and only query if length > 0 in case it's the only index provided
30-
const nonApmIndexNameArray = indexNames.filter(name => name !== APM_INDEX_NAME);
31-
const indexCheckReq =
32-
nonApmIndexNameArray.length > 0
18+
// Intended flow to determine app-empty state is to first check siem indices (as this is a quick shard count), and
19+
// if no shards exist only then perform the heavier APM query. This optimizes for normal use when siem data exists
20+
try {
21+
// Remove APM index if exists, and only query if length > 0 in case it's the only index provided
22+
const nonApmIndexNames = indexNames.filter(name => name !== APM_INDEX_NAME);
23+
const indexCheckResponse = await (nonApmIndexNames.length > 0
3324
? this.framework.callWithRequest(request, 'search', {
34-
index: nonApmIndexNameArray,
25+
index: nonApmIndexNames,
3526
size: 0,
3627
terminate_after: 1,
3728
allow_no_indices: true,
3829
})
39-
: Promise.resolve(undefined);
30+
: Promise.resolve(undefined));
4031

41-
try {
42-
const [apmResponse, indexCheckResponse] = await Promise.all([hasApmDataReq, indexCheckReq]);
43-
44-
return (
45-
(apmResponse?.aggregations?.total_service_names?.value ?? -1) > 0 ||
46-
(indexCheckResponse?._shards.total ?? -1) > 0
47-
);
48-
} catch (err) {
49-
if (err.status === 404) {
32+
if ((indexCheckResponse?._shards.total ?? -1) > 0) {
33+
return true;
34+
}
35+
36+
// Note: Additional check necessary for APM-specific index. For details see: https://github.com/elastic/kibana/issues/56363
37+
// Only verify if APM data exists if indexNames includes `apm-*-transaction*` (default included apm index)
38+
const includesApmIndex = indexNames.includes(APM_INDEX_NAME);
39+
const hasApmDataResponse = await (includesApmIndex
40+
? this.framework.callWithRequest<{}, ApmServiceNameAgg>(
41+
request,
42+
'search',
43+
buildQuery({ defaultIndex: [APM_INDEX_NAME] })
44+
)
45+
: Promise.resolve(undefined));
46+
47+
if ((hasApmDataResponse?.aggregations?.total_service_names?.value ?? -1) > 0) {
48+
return true;
49+
}
50+
} catch (e) {
51+
if (e.status === 404) {
5052
return false;
5153
}
52-
throw err;
54+
throw e;
5355
}
56+
57+
return false;
5458
}
5559
}

0 commit comments

Comments
 (0)