|
6 | 6 |
|
7 | 7 | import { FrameworkAdapter, FrameworkRequest } from '../framework'; |
8 | 8 | import { SourceStatusAdapter } from './index'; |
| 9 | +import { buildQuery } from './query.dsl'; |
| 10 | +import { ApmServiceNameAgg } from './types'; |
| 11 | + |
| 12 | +const APM_INDEX_NAME = 'apm-*-transaction*'; |
9 | 13 |
|
10 | 14 | export class ElasticsearchSourceStatusAdapter implements SourceStatusAdapter { |
11 | 15 | constructor(private readonly framework: FrameworkAdapter) {} |
12 | 16 |
|
13 | | - public async hasIndices(request: FrameworkRequest, indexNames: string | string[]) { |
14 | | - return this.framework |
15 | | - .callWithRequest(request, 'search', { |
16 | | - index: indexNames, |
17 | | - size: 0, |
18 | | - terminate_after: 1, |
19 | | - allow_no_indices: true, |
20 | | - }) |
21 | | - .then( |
22 | | - response => response._shards.total > 0, |
23 | | - err => { |
24 | | - if (err.status === 404) { |
25 | | - return false; |
26 | | - } |
27 | | - throw err; |
28 | | - } |
| 17 | + 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 |
| 33 | + ? this.framework.callWithRequest(request, 'search', { |
| 34 | + index: nonApmIndexNameArray, |
| 35 | + size: 0, |
| 36 | + terminate_after: 1, |
| 37 | + allow_no_indices: true, |
| 38 | + }) |
| 39 | + : Promise.resolve(undefined); |
| 40 | + |
| 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 |
29 | 47 | ); |
| 48 | + } catch (err) { |
| 49 | + if (err.status === 404) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + throw err; |
| 53 | + } |
30 | 54 | } |
31 | 55 | } |
0 commit comments