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.
[APM] Inject agent config directly into APM Fleet policies (elastic#9…
- Loading branch information
Showing
12 changed files
with
521 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,8 @@ | |
"security", | ||
"ml", | ||
"home", | ||
"maps" | ||
"maps", | ||
"fleet" | ||
], | ||
"server": true, | ||
"ui": true, | ||
|
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
99 changes: 99 additions & 0 deletions
99
x-pack/plugins/apm/server/lib/alerts/register_agent_config_fleet_sync_alert_type.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,99 @@ | ||
/* | ||
* 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 { schema } from '@kbn/config-schema'; | ||
import { take } from 'rxjs/operators'; | ||
import { AlertType, ALERT_TYPES_CONFIG } from '../../../common/alert_types'; | ||
import { getApmIndices } from '../settings/apm_indices/get_apm_indices'; | ||
import { alertingEsClient } from './alerting_es_client'; | ||
import { RegisterRuleDependencies } from './register_apm_alerts'; | ||
import { createAPMLifecycleRuleType } from './create_apm_lifecycle_rule_type'; | ||
import { convertConfigSettingsToString } from '../settings/agent_configuration/convert_settings_to_string'; | ||
import { AgentConfiguration } from '../../../common/agent_configuration/configuration_types'; | ||
import { syncAgentConfigsToApmPackagePolicies } from '../fleet/sync_agent_configs_to_apm_package_policies'; | ||
|
||
const alertTypeConfig = ALERT_TYPES_CONFIG[AlertType.AgentConfigFleetSync]; | ||
|
||
export function registerAgentConfigFleetSyncAlertType({ | ||
registry, | ||
config$, | ||
getFleetPluginStart, | ||
}: RegisterRuleDependencies) { | ||
registry.registerType( | ||
createAPMLifecycleRuleType({ | ||
id: AlertType.AgentConfigFleetSync, | ||
name: alertTypeConfig.name, | ||
actionGroups: alertTypeConfig.actionGroups, | ||
defaultActionGroupId: alertTypeConfig.defaultActionGroupId, | ||
validate: { | ||
params: schema.any(), | ||
}, | ||
actionVariables: { | ||
context: [], | ||
}, | ||
producer: 'apm', | ||
minimumLicenseRequired: 'basic', | ||
executor: async ({ services, state }) => { | ||
const config = await config$.pipe(take(1)).toPromise(); | ||
const indices = await getApmIndices({ | ||
config, | ||
savedObjectsClient: services.savedObjectsClient, | ||
}); | ||
|
||
const searchParams = { | ||
index: indices['apmAgentConfigurationIndex'], | ||
body: { | ||
size: 1, | ||
query: { | ||
match_all: {}, | ||
}, | ||
sort: { '@timestamp': 'desc' as const }, | ||
}, | ||
}; | ||
|
||
const response = await alertingEsClient({ | ||
scopedClusterClient: services.scopedClusterClient, | ||
params: searchParams, | ||
}); | ||
if (response.hits.total.value === 0) { | ||
return {}; | ||
} | ||
|
||
const { ['@timestamp']: lastTimestamp } = response.hits.hits[0] | ||
._source as { '@timestamp': number }; | ||
// @ts-ignore | ||
if (lastTimestamp > state.lastTimestamp) { | ||
const fleetPluginStart = await getFleetPluginStart(); | ||
if (fleetPluginStart) { | ||
services.logger.info( | ||
`New agent configurations detected. Updating fleet policy...` | ||
); | ||
const configurationsSearchResponse = await alertingEsClient({ | ||
scopedClusterClient: services.scopedClusterClient, | ||
params: { | ||
index: indices['apmAgentConfigurationIndex'], | ||
body: { size: 200, query: { match_all: {} } }, | ||
}, | ||
}); | ||
const agentConfigurations = configurationsSearchResponse.hits.hits | ||
// @ts-ignore | ||
.map(convertConfigSettingsToString) | ||
.map((hit) => hit._source) as AgentConfiguration[]; | ||
await syncAgentConfigsToApmPackagePolicies({ | ||
fleetPluginStart, | ||
savedObjectsClient: services.savedObjectsClient, | ||
esClient: services.scopedClusterClient.asCurrentUser, | ||
agentConfigurations, | ||
}); | ||
services.logger.info(`Policy updated.`); | ||
} | ||
} | ||
return { lastTimestamp }; | ||
}, | ||
}) | ||
); | ||
} |
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
61 changes: 61 additions & 0 deletions
61
x-pack/plugins/apm/server/lib/fleet/agent_config_fleet_sync_alert.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,61 @@ | ||
/* | ||
* 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 { AlertsClient } from 'x-pack/plugins/alerting/server'; | ||
import { APMPluginStartDependencies } from '../../types'; | ||
|
||
export async function createApmAgentFleetSyncAlert({ | ||
alertsClient, | ||
}: { | ||
alertsClient: AlertsClient; | ||
}) { | ||
return alertsClient?.create({ | ||
data: { | ||
enabled: true, | ||
name: 'APM - Agent config fleet sync', | ||
tags: ['apm'], | ||
alertTypeId: 'apm.agent_config_fleet_sync', | ||
consumer: 'apm', | ||
schedule: { interval: '1m' }, | ||
actions: [], | ||
params: {}, | ||
throttle: null, | ||
notifyWhen: null, | ||
}, | ||
}); | ||
} | ||
export async function getApmAgentFleetSyncAlert({ | ||
alertsClient, | ||
}: { | ||
alertsClient: AlertsClient; | ||
}) { | ||
const { total, data } = (await alertsClient?.find({ | ||
options: { | ||
filter: `alert.attributes.alertTypeId: apm.agent_config_fleet_sync`, | ||
}, | ||
})) ?? { total: 0, data: [] }; | ||
|
||
return total === 0 ? null : data[0]; | ||
} | ||
|
||
export async function runTaskForApmAgentFleetSyncAlert({ | ||
alertsClient, | ||
taskManagerPluginStart, | ||
}: { | ||
alertsClient: AlertsClient; | ||
taskManagerPluginStart: APMPluginStartDependencies['taskManager']; | ||
}) { | ||
const alert = await getApmAgentFleetSyncAlert({ alertsClient }); | ||
if (!alert) { | ||
return; | ||
} | ||
const { scheduledTaskId } = alert; | ||
if (!scheduledTaskId) { | ||
return; | ||
} | ||
await taskManagerPluginStart?.runNow(scheduledTaskId); | ||
} |
Oops, something went wrong.