-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[asset manager] merge obsasset signals collection (#162222)
## Summary Closes #161887 Merges most of the functionality from `feat/obs-asset-manager-demo` branch. We remove implicit collection code while including: - asset collectors from signals (also include pods and containers but we don't use then directly) - source configuration code (assets or signals) - `assetAccessor` logic that determines which indices to query The change also enables ftr test suite. We'll also merge the services endpoint #160294 when approved. --------- Co-authored-by: kibanamachine <[email protected]> Co-authored-by: Jason Rhodes <[email protected]>
- Loading branch information
1 parent
b3122cb
commit a67f7f5
Showing
44 changed files
with
1,235 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
x-pack/plugins/asset_manager/server/lib/accessors/hosts/get_hosts_by_assets.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { Asset } from '../../../../common/types_api'; | ||
import { GetHostsOptionsInjected } from '.'; | ||
import { getAssets } from '../../get_assets'; | ||
|
||
export async function getHostsByAssets( | ||
options: GetHostsOptionsInjected | ||
): Promise<{ hosts: Asset[] }> { | ||
const hosts = await getAssets({ | ||
esClient: options.esClient, | ||
filters: { | ||
kind: 'host', | ||
from: options.from, | ||
to: options.to, | ||
}, | ||
}); | ||
|
||
return { | ||
hosts, | ||
}; | ||
} |
24 changes: 24 additions & 0 deletions
24
x-pack/plugins/asset_manager/server/lib/accessors/hosts/get_hosts_by_signals.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { Asset } from '../../../../common/types_api'; | ||
import { GetHostsOptionsInjected } from '.'; | ||
import { collectHosts } from '../../collectors/hosts'; | ||
|
||
export async function getHostsBySignals( | ||
options: GetHostsOptionsInjected | ||
): Promise<{ hosts: Asset[] }> { | ||
const { assets } = await collectHosts({ | ||
client: options.esClient, | ||
from: options.from, | ||
to: options.to, | ||
sourceIndices: options.sourceIndices, | ||
}); | ||
return { | ||
hosts: assets, | ||
}; | ||
} |
20 changes: 20 additions & 0 deletions
20
x-pack/plugins/asset_manager/server/lib/accessors/hosts/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { AccessorOptions, OptionsWithInjectedValues } from '..'; | ||
|
||
export interface GetHostsOptions extends AccessorOptions { | ||
from: number; | ||
to: number; | ||
} | ||
export type GetHostsOptionsInjected = OptionsWithInjectedValues<GetHostsOptions>; | ||
|
||
export interface HostIdentifier { | ||
'asset.ean': string; | ||
'asset.id': string; | ||
'asset.name'?: string; | ||
} |
19 changes: 19 additions & 0 deletions
19
x-pack/plugins/asset_manager/server/lib/accessors/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { ElasticsearchClient } from '@kbn/core-elasticsearch-server'; | ||
import { AssetManagerConfig } from '../../types'; | ||
|
||
export interface InjectedValues { | ||
sourceIndices: AssetManagerConfig['sourceIndices']; | ||
} | ||
|
||
export type OptionsWithInjectedValues<T extends object> = T & InjectedValues; | ||
|
||
export interface AccessorOptions { | ||
esClient: ElasticsearchClient; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { Asset } from '../../common/types_api'; | ||
import { AssetManagerConfig } from '../types'; | ||
import { OptionsWithInjectedValues } from './accessors'; | ||
import { GetHostsOptions } from './accessors/hosts'; | ||
import { getHostsByAssets } from './accessors/hosts/get_hosts_by_assets'; | ||
import { getHostsBySignals } from './accessors/hosts/get_hosts_by_signals'; | ||
|
||
interface AssetAccessorClassOptions { | ||
sourceIndices: AssetManagerConfig['sourceIndices']; | ||
source: AssetManagerConfig['lockedSource']; | ||
} | ||
|
||
export class AssetAccessor { | ||
constructor(private options: AssetAccessorClassOptions) {} | ||
|
||
injectOptions<T extends object = {}>(options: T): OptionsWithInjectedValues<T> { | ||
return { | ||
...options, | ||
sourceIndices: this.options.sourceIndices, | ||
}; | ||
} | ||
|
||
async getHosts(options: GetHostsOptions): Promise<{ hosts: Asset[] }> { | ||
const withInjected = this.injectOptions(options); | ||
if (this.options.source === 'assets') { | ||
return await getHostsByAssets(withInjected); | ||
} else { | ||
return await getHostsBySignals(withInjected); | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
x-pack/plugins/asset_manager/server/lib/collectors/containers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { estypes } from '@elastic/elasticsearch'; | ||
import { Asset } from '../../../common/types_api'; | ||
import { CollectorOptions, QUERY_MAX_SIZE } from '.'; | ||
|
||
export async function collectContainers({ | ||
client, | ||
from, | ||
to, | ||
sourceIndices, | ||
afterKey, | ||
}: CollectorOptions) { | ||
const { metrics, logs, traces } = sourceIndices; | ||
const dsl: estypes.SearchRequest = { | ||
index: [traces, logs, metrics], | ||
size: QUERY_MAX_SIZE, | ||
collapse: { | ||
field: 'container.id', | ||
}, | ||
sort: [{ 'container.id': 'asc' }], | ||
_source: false, | ||
fields: [ | ||
'kubernetes.*', | ||
'cloud.provider', | ||
'orchestrator.cluster.name', | ||
'host.name', | ||
'host.hostname', | ||
], | ||
query: { | ||
bool: { | ||
filter: [ | ||
{ | ||
range: { | ||
'@timestamp': { | ||
gte: from, | ||
lte: to, | ||
}, | ||
}, | ||
}, | ||
], | ||
should: [ | ||
{ exists: { field: 'kubernetes.container.id' } }, | ||
{ exists: { field: 'kubernetes.pod.uid' } }, | ||
{ exists: { field: 'kubernetes.node.name' } }, | ||
{ exists: { field: 'host.hostname' } }, | ||
], | ||
}, | ||
}, | ||
}; | ||
|
||
if (afterKey) { | ||
dsl.search_after = afterKey; | ||
} | ||
|
||
const esResponse = await client.search(dsl); | ||
|
||
const assets = esResponse.hits.hits.reduce<Asset[]>((acc: Asset[], hit: any) => { | ||
const { fields = {} } = hit; | ||
const containerId = fields['container.id']; | ||
const podUid = fields['kubernetes.pod.uid']; | ||
const nodeName = fields['kubernetes.node.name']; | ||
|
||
const parentEan = podUid ? `pod:${podUid}` : `host:${fields['host.hostname']}`; | ||
|
||
const container: Asset = { | ||
'@timestamp': new Date().toISOString(), | ||
'asset.kind': 'container', | ||
'asset.id': containerId, | ||
'asset.ean': `container:${containerId}`, | ||
'asset.parents': [parentEan], | ||
}; | ||
|
||
if (nodeName) { | ||
container['asset.references'] = [`host:${nodeName}`]; | ||
} | ||
|
||
acc.push(container); | ||
|
||
return acc; | ||
}, []); | ||
|
||
const hitsLen = esResponse.hits.hits.length; | ||
const next = hitsLen === QUERY_MAX_SIZE ? esResponse.hits.hits[hitsLen - 1].sort : undefined; | ||
return { assets, afterKey: next }; | ||
} |
Oops, something went wrong.