From bd3491cbd95cab4cde3922a35b832d557f7f9b89 Mon Sep 17 00:00:00 2001 From: Garrett Spong Date: Mon, 3 Feb 2020 17:03:34 -0700 Subject: [PATCH] [SIEM] Fixes FTUE when APM node is present (#56574) (#56698) ## Summary This PR resolves https://github.com/elastic/kibana/issues/56363 found in `7.6 BC4`, where configuring a cloud instance with an APM node would circumvent our FTUE checks and would show the Overview Page when no data is present. As detailed in the above issue, this resolves the problem by performing a similar check as to what APM performs to determine their `empty data` state. As can be observed in the gifs below, this check adds additional latency to the full-app flicker, which is not ideal... Perhaps we can tweak the query further to improve this? #### Deployment w/o APM node: ![ftue_no_apm](https://user-images.githubusercontent.com/2946766/73584819-a7212700-4458-11ea-9df8-f600fd6b1434.gif) #### Deployment w/ APM node: ![ftue_with_apm](https://user-images.githubusercontent.com/2946766/73584817-a38da000-4458-11ea-824c-c42924c58fa0.gif) ### Checklist Use ~~strikethroughs~~ to remove checklist items you don't feel are applicable to this PR. - [ ] ~This was checked for cross-browser compatibility, [including a check against IE11](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility)~ - [ ] ~Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/master/packages/kbn-i18n/README.md)~ - [ ] ~[Documentation](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#writing-documentation) was added for features that require explanation or tutorials~ - [ ] ~[Unit or functional tests](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility) were updated or added to match the most common scenarios~ - [ ] ~This was checked for [keyboard-only and screenreader accessibility](https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Cross_browser_testing/Accessibility#Accessibility_testing_checklist)~ ### For maintainers - [ ] This was checked for breaking API changes and was [labeled appropriately](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#release-notes-process) - [ ] This includes a feature addition or change that requires a release note and was [labeled appropriately](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#release-notes-process) --- .../source_status/elasticsearch_adapter.ts | 62 ++++++++++++++----- .../server/lib/source_status/query.dsl.ts | 27 ++++++++ .../siem/server/lib/source_status/types.ts | 11 ++++ 3 files changed, 83 insertions(+), 17 deletions(-) create mode 100644 x-pack/legacy/plugins/siem/server/lib/source_status/query.dsl.ts create mode 100644 x-pack/legacy/plugins/siem/server/lib/source_status/types.ts diff --git a/x-pack/legacy/plugins/siem/server/lib/source_status/elasticsearch_adapter.ts b/x-pack/legacy/plugins/siem/server/lib/source_status/elasticsearch_adapter.ts index 17d203918d825..bb4693a7749e7 100644 --- a/x-pack/legacy/plugins/siem/server/lib/source_status/elasticsearch_adapter.ts +++ b/x-pack/legacy/plugins/siem/server/lib/source_status/elasticsearch_adapter.ts @@ -6,26 +6,54 @@ import { FrameworkAdapter, FrameworkRequest } from '../framework'; import { SourceStatusAdapter } from './index'; +import { buildQuery } from './query.dsl'; +import { ApmServiceNameAgg } from './types'; + +const APM_INDEX_NAME = 'apm-*-transaction*'; export class ElasticsearchSourceStatusAdapter implements SourceStatusAdapter { constructor(private readonly framework: FrameworkAdapter) {} - public async hasIndices(request: FrameworkRequest, indexNames: string | string[]) { - return this.framework - .callWithRequest(request, 'search', { - index: indexNames, - size: 0, - terminate_after: 1, - allow_no_indices: true, - }) - .then( - response => response._shards.total > 0, - err => { - if (err.status === 404) { - return false; - } - throw err; - } - ); + public async hasIndices(request: FrameworkRequest, indexNames: string[]) { + // Intended flow to determine app-empty state is to first check siem indices (as this is a quick shard count), and + // if no shards exist only then perform the heavier APM query. This optimizes for normal use when siem data exists + try { + // Remove APM index if exists, and only query if length > 0 in case it's the only index provided + const nonApmIndexNames = indexNames.filter(name => name !== APM_INDEX_NAME); + const indexCheckResponse = await (nonApmIndexNames.length > 0 + ? this.framework.callWithRequest(request, 'search', { + index: nonApmIndexNames, + size: 0, + terminate_after: 1, + allow_no_indices: true, + }) + : Promise.resolve(undefined)); + + if ((indexCheckResponse?._shards.total ?? -1) > 0) { + return true; + } + + // Note: Additional check necessary for APM-specific index. For details see: https://github.com/elastic/kibana/issues/56363 + // Only verify if APM data exists if indexNames includes `apm-*-transaction*` (default included apm index) + const includesApmIndex = indexNames.includes(APM_INDEX_NAME); + const hasApmDataResponse = await (includesApmIndex + ? this.framework.callWithRequest<{}, ApmServiceNameAgg>( + request, + 'search', + buildQuery({ defaultIndex: [APM_INDEX_NAME] }) + ) + : Promise.resolve(undefined)); + + if ((hasApmDataResponse?.aggregations?.total_service_names?.value ?? -1) > 0) { + return true; + } + } catch (e) { + if (e.status === 404) { + return false; + } + throw e; + } + + return false; } } diff --git a/x-pack/legacy/plugins/siem/server/lib/source_status/query.dsl.ts b/x-pack/legacy/plugins/siem/server/lib/source_status/query.dsl.ts new file mode 100644 index 0000000000000..ead08fff474c7 --- /dev/null +++ b/x-pack/legacy/plugins/siem/server/lib/source_status/query.dsl.ts @@ -0,0 +1,27 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +const SERVICE_NAME = 'service.name'; + +export const buildQuery = ({ defaultIndex }: { defaultIndex: string[] }) => { + return { + allowNoIndices: true, + index: defaultIndex, + ignoreUnavailable: true, + terminate_after: 1, + body: { + size: 0, + aggs: { + total_service_names: { + cardinality: { + field: SERVICE_NAME, + }, + }, + }, + }, + track_total_hits: false, + }; +}; diff --git a/x-pack/legacy/plugins/siem/server/lib/source_status/types.ts b/x-pack/legacy/plugins/siem/server/lib/source_status/types.ts new file mode 100644 index 0000000000000..247495ffe4b23 --- /dev/null +++ b/x-pack/legacy/plugins/siem/server/lib/source_status/types.ts @@ -0,0 +1,11 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +export interface ApmServiceNameAgg { + total_service_names: { + value: number; + }; +}