forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Asset Manager] run implicit collection periodically (elastic#156830)
Closes elastic#156757 Introduce background implicit collection in the asset_manager plugin. The process can be configured with the following kibana settings: ``` xpack.assetManager: implicitCollection: enabled: true interval: 30s # elasticsearch cluster we should extract signals from input: hosts: http://input:9200 username: ... password: ... # elasticsearch cluster we should write assets to output: hosts: http://output:9200 username: ... password: ... ``` --------- Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
e98abd0
commit 0b23894
Showing
17 changed files
with
721 additions
and
14 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
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
102 changes: 102 additions & 0 deletions
102
x-pack/plugins/asset_manager/server/lib/implicit_collection/collector_runner.test.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,102 @@ | ||
/* | ||
* 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 expect from '@kbn/expect'; | ||
import { ElasticsearchClient, Logger } from '@kbn/core/server'; | ||
import { CollectorRunner } from './collector_runner'; | ||
import { CollectorOptions } from './collectors'; | ||
import { Asset } from '../../../common/types_api'; | ||
|
||
const getMockClient = () => ({ | ||
bulk: jest.fn().mockResolvedValue({ errors: false }), | ||
}); | ||
|
||
const getMockLogger = () => | ||
({ | ||
info: jest.fn(), | ||
error: jest.fn(), | ||
} as unknown as Logger); | ||
|
||
describe(__filename, () => { | ||
it('runs registered collectors', async () => { | ||
const runner = new CollectorRunner({ | ||
inputClient: getMockClient() as unknown as ElasticsearchClient, | ||
outputClient: getMockClient() as unknown as ElasticsearchClient, | ||
logger: getMockLogger(), | ||
intervalMs: 1, | ||
}); | ||
|
||
const collector1 = jest.fn(async (opts: CollectorOptions) => { | ||
return []; | ||
}); | ||
const collector2 = jest.fn(async (opts: CollectorOptions) => { | ||
return []; | ||
}); | ||
|
||
runner.registerCollector('foo', collector1); | ||
runner.registerCollector('foo', collector2); | ||
|
||
await runner.run(); | ||
|
||
expect(collector1.mock.calls).to.have.length(1); | ||
expect(collector2.mock.calls).to.have.length(1); | ||
}); | ||
|
||
it('is resilient to failing collectors', async () => { | ||
const runner = new CollectorRunner({ | ||
outputClient: getMockClient() as unknown as ElasticsearchClient, | ||
inputClient: getMockClient() as unknown as ElasticsearchClient, | ||
logger: getMockLogger(), | ||
intervalMs: 1, | ||
}); | ||
|
||
const collector1 = jest.fn(async (opts: CollectorOptions) => { | ||
throw new Error('no'); | ||
}); | ||
const collector2 = jest.fn(async (opts: CollectorOptions) => { | ||
return []; | ||
}); | ||
|
||
runner.registerCollector('foo', collector1); | ||
runner.registerCollector('foo', collector2); | ||
|
||
await runner.run(); | ||
|
||
expect(collector1.mock.calls).to.have.length(1); | ||
expect(collector2.mock.calls).to.have.length(1); | ||
}); | ||
|
||
it('stores collectors results in elasticsearch', async () => { | ||
const outputClient = getMockClient(); | ||
const runner = new CollectorRunner({ | ||
outputClient: outputClient as unknown as ElasticsearchClient, | ||
inputClient: getMockClient() as unknown as ElasticsearchClient, | ||
logger: getMockLogger(), | ||
intervalMs: 1, | ||
}); | ||
|
||
const collector = jest.fn(async (opts: CollectorOptions) => { | ||
return [ | ||
{ 'asset.kind': 'container', 'asset.ean': 'foo' }, | ||
{ 'asset.kind': 'pod', 'asset.ean': 'bar' }, | ||
] as Asset[]; | ||
}); | ||
|
||
runner.registerCollector('foo', collector); | ||
|
||
await runner.run(); | ||
|
||
expect(outputClient.bulk.mock.calls[0][0]).to.eql({ | ||
body: [ | ||
{ create: { _index: 'assets-container-default' } }, | ||
{ 'asset.kind': 'container', 'asset.ean': 'foo' }, | ||
{ create: { _index: 'assets-pod-default' } }, | ||
{ 'asset.kind': 'pod', 'asset.ean': 'bar' }, | ||
], | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.