-
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.
[Fleet] Define new telemetry hourly job for reusable policies (#188536)
Part of #75867 Depends on merging first elastic/telemetry#3759 ## Summary Define new telemetry hourly job for reusable policies; Example of data: ``` { total_integration_policies: 3, shared_integration_policies: 2, shared_integrations: { agents: 10, name: 'aws-1', pkg_name: 'aws', pkg_version: '1.0.0', shared_by_agent_policies: 3, } ``` ### Checklist - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --------- Co-authored-by: Elastic Machine <[email protected]>
- Loading branch information
1 parent
39a5515
commit 76c19c6
Showing
6 changed files
with
266 additions
and
1 deletion.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
x-pack/plugins/fleet/server/collectors/integrations_collector.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,111 @@ | ||
/* | ||
* 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 { savedObjectsClientMock } from '@kbn/core/server/mocks'; | ||
|
||
import { packagePolicyService } from '../services'; | ||
|
||
import { getIntegrationsDetails } from './integrations_collector'; | ||
|
||
describe('getIntegrationsDetails', () => { | ||
const soClientMock = savedObjectsClientMock.create(); | ||
|
||
it('should return empty array if there are no package policies', async () => { | ||
packagePolicyService.list = jest.fn().mockResolvedValue({ | ||
items: [], | ||
}); | ||
expect(await getIntegrationsDetails(soClientMock)).toEqual([]); | ||
}); | ||
|
||
it('should return data about shared integration policies', async () => { | ||
packagePolicyService.list = jest.fn().mockResolvedValue({ | ||
items: [ | ||
{ | ||
name: 'apache-1', | ||
package: { name: 'apache', version: '1.0.0' }, | ||
policy_ids: ['agent-1', 'agent-2'], | ||
}, | ||
{ | ||
name: 'aws-11', | ||
package: { name: 'aws', version: '1.0.0' }, | ||
policy_ids: ['agent-1'], | ||
agents: 10, | ||
}, | ||
{ name: 'nginx-1', package: { name: 'aws', version: '1.0.0' } }, | ||
], | ||
}); | ||
expect(await getIntegrationsDetails(soClientMock)).toEqual([ | ||
{ | ||
total_integration_policies: 3, | ||
shared_integration_policies: 1, | ||
shared_integrations: { | ||
agents: undefined, | ||
name: 'apache-1', | ||
pkg_name: 'apache', | ||
pkg_version: '1.0.0', | ||
shared_by_agent_policies: 2, | ||
}, | ||
}, | ||
]); | ||
}); | ||
|
||
it('should return data about shared integration policies when there are multiple of them', async () => { | ||
packagePolicyService.list = jest.fn().mockResolvedValue({ | ||
items: [ | ||
{ | ||
name: 'apache-1', | ||
package: { name: 'apache', version: '1.0.0' }, | ||
policy_ids: ['agent-1', 'agent-2'], | ||
}, | ||
{ | ||
name: 'aws-1', | ||
package: { name: 'aws', version: '1.0.0' }, | ||
policy_ids: ['agent-1', 'agent-3', 'agent-4'], | ||
agents: 10, | ||
}, | ||
{ name: 'nginx-1', package: { name: 'aws', version: '1.0.0' } }, | ||
], | ||
}); | ||
expect(await getIntegrationsDetails(soClientMock)).toEqual([ | ||
{ | ||
total_integration_policies: 3, | ||
shared_integration_policies: 2, | ||
shared_integrations: { | ||
agents: undefined, | ||
name: 'apache-1', | ||
pkg_name: 'apache', | ||
pkg_version: '1.0.0', | ||
shared_by_agent_policies: 2, | ||
}, | ||
}, | ||
{ | ||
total_integration_policies: 3, | ||
shared_integration_policies: 2, | ||
shared_integrations: { | ||
agents: 10, | ||
name: 'aws-1', | ||
pkg_name: 'aws', | ||
pkg_version: '1.0.0', | ||
shared_by_agent_policies: 3, | ||
}, | ||
}, | ||
]); | ||
}); | ||
|
||
it('should return empty array if there are no shared integrations', async () => { | ||
packagePolicyService.list = jest.fn().mockResolvedValue({ | ||
items: [ | ||
{ | ||
name: 'apache-1', | ||
package: { name: 'apache', version: '1.0.0' }, | ||
}, | ||
{ name: 'nginx-1', package: { name: 'aws', version: '1.0.0' } }, | ||
], | ||
}); | ||
expect(await getIntegrationsDetails(soClientMock)).toEqual([]); | ||
}); | ||
}); |
56 changes: 56 additions & 0 deletions
56
x-pack/plugins/fleet/server/collectors/integrations_collector.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,56 @@ | ||
/* | ||
* 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 type { SavedObjectsClientContract } from '@kbn/core/server'; | ||
|
||
import { SO_SEARCH_LIMIT } from '../constants'; | ||
import { packagePolicyService } from '../services'; | ||
|
||
export interface IntegrationsDetails { | ||
total_integration_policies: number; | ||
shared_integration_policies: number; | ||
shared_integrations: SharedIntegration; | ||
} | ||
|
||
interface SharedIntegration { | ||
name: string; | ||
shared_by_agent_policies: number; | ||
pkg_name?: string; | ||
pkg_version?: string; | ||
agents?: number; | ||
} | ||
|
||
export const getIntegrationsDetails = async ( | ||
soClient?: SavedObjectsClientContract | ||
): Promise<IntegrationsDetails[]> => { | ||
if (!soClient) { | ||
return []; | ||
} | ||
const allPackagePolicies = await packagePolicyService.list(soClient, { | ||
perPage: SO_SEARCH_LIMIT, | ||
}); | ||
const sharedPackagePolicies = allPackagePolicies.items.filter((packagePolicy) => { | ||
if (packagePolicy?.policy_ids?.length > 1) return packagePolicy; | ||
}); | ||
|
||
const integrationsDetails: IntegrationsDetails[] = (sharedPackagePolicies || []).map( | ||
(packagePolicy) => { | ||
return { | ||
total_integration_policies: allPackagePolicies.items.length, | ||
shared_integration_policies: sharedPackagePolicies.length, | ||
shared_integrations: { | ||
name: packagePolicy.name, | ||
pkg_name: packagePolicy.package?.name, | ||
pkg_version: packagePolicy.package?.version, | ||
shared_by_agent_policies: packagePolicy?.policy_ids.length, | ||
agents: packagePolicy?.agents, | ||
}, | ||
}; | ||
} | ||
); | ||
return integrationsDetails; | ||
}; |
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