-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Fleet] Log agent usage every n minutes #144037
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* 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 { | ||
ConcreteTaskInstance, | ||
TaskManagerStartContract, | ||
TaskManagerSetupContract, | ||
} from '@kbn/task-manager-plugin/server'; | ||
|
||
import type { fetchAgentsUsage } from '../collectors/register'; | ||
|
||
import { appContextService } from './app_context'; | ||
|
||
const TASK_ID = 'Fleet-Usage-Logger-Task'; | ||
const TASK_TYPE = 'Fleet-Usage-Logger'; | ||
|
||
export async function registerFleetUsageLogger( | ||
taskManager: TaskManagerSetupContract, | ||
fetchUsage: () => ReturnType<typeof fetchAgentsUsage> | ||
) { | ||
taskManager.registerTaskDefinitions({ | ||
[TASK_TYPE]: { | ||
title: 'Fleet Usage Logger', | ||
timeout: '1m', | ||
maxAttempts: 1, | ||
createTaskRunner: ({ taskInstance }: { taskInstance: ConcreteTaskInstance }) => { | ||
return { | ||
async run() { | ||
try { | ||
const usageData = await fetchUsage(); | ||
if (appContextService.getLogger().isLevelEnabled('debug')) { | ||
appContextService.getLogger().debug(`Feet Usage: ${JSON.stringify(usageData)}`); | ||
} else { | ||
appContextService.getLogger().info(`Feet Usage: ${JSON.stringify(usageData)}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo |
||
} | ||
} catch (error) { | ||
appContextService | ||
.getLogger() | ||
.error('Error occurred while fetching fleet usage: ' + error); | ||
} | ||
}, | ||
|
||
async cancel() {}, | ||
}; | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
export async function startFleetUsageLogger(taskManager: TaskManagerStartContract) { | ||
const isDebugLogLevelEnabled = appContextService.getLogger().isLevelEnabled('debug'); | ||
const isInfoLogLevelEnabled = appContextService.getLogger().isLevelEnabled('info'); | ||
if (!isInfoLogLevelEnabled) { | ||
return; | ||
} | ||
// TODO get log level and schedule interval based on that | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This todo has been addressed now, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it has been |
||
appContextService.getLogger().info(`Task ${TASK_ID} scheduled with interval 5m`); | ||
await taskManager?.ensureScheduled({ | ||
id: TASK_ID, | ||
taskType: TASK_TYPE, | ||
schedule: { | ||
interval: isDebugLogLevelEnabled ? '5m' : '15m', | ||
}, | ||
scope: ['fleet'], | ||
state: {}, | ||
params: {}, | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo