Skip to content
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

Merged
merged 4 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions x-pack/plugins/fleet/scripts/create_agents/create_agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ async function createAgentPolicy(id: string) {
namespace: 'default',
description: '',
monitoring_enabled: ['logs'],
data_output_id: 'fleet-default-output',
monitoring_output_id: 'fleet-default-output',
}),
headers: {
Authorization: auth,
Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/fleet/server/collectors/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ export const fetchUsage = async (core: CoreSetup, config: FleetConfigType) => {
return usage;
};

export const fetchAgentsUsage = async (core: CoreSetup, config: FleetConfigType) => {
const [soClient, esClient] = await getInternalClients(core);
const usage = {
agents_enabled: getIsAgentsEnabled(config),
agents: await getAgentUsage(config, soClient, esClient),
fleet_server: await getFleetServerUsage(soClient, esClient),
};
return usage;
};

export function registerFleetUsageCollector(
core: CoreSetup,
config: FleetConfigType,
Expand Down
5 changes: 4 additions & 1 deletion x-pack/plugins/fleet/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ import {
AgentServiceImpl,
PackageServiceImpl,
} from './services';
import { registerFleetUsageCollector, fetchUsage } from './collectors/register';
import { registerFleetUsageCollector, fetchUsage, fetchAgentsUsage } from './collectors/register';
import { getAuthzFromRequest, makeRouterWithFleetAuthz } from './routes/security';
import { FleetArtifactsClient } from './services/artifacts';
import type { FleetRouter } from './types/request_context';
Expand All @@ -110,6 +110,7 @@ import { setupFleet } from './services/setup';
import { BulkActionsResolver } from './services/agents';
import type { PackagePolicyService } from './services/package_policy_service';
import { PackagePolicyServiceImpl } from './services/package_policy';
import { registerFleetUsageLogger, startFleetUsageLogger } from './services/fleet_usage_logger';

export interface FleetSetupDeps {
security: SecurityPluginSetup;
Expand Down Expand Up @@ -388,6 +389,7 @@ export class FleetPlugin
this.kibanaVersion,
this.isProductionMode
);
registerFleetUsageLogger(deps.taskManager, async () => fetchAgentsUsage(core, config));

const router: FleetRouter = core.http.createRouter<FleetRequestHandlerContext>();
// Allow read-only users access to endpoints necessary for Integrations UI
Expand Down Expand Up @@ -455,6 +457,7 @@ export class FleetPlugin
this.telemetryEventsSender.start(plugins.telemetry, core);
this.bulkActionsResolver?.start(plugins.taskManager);
this.fleetUsageSender?.start(plugins.taskManager);
startFleetUsageLogger(plugins.taskManager);

const logger = appContextService.getLogger();

Expand Down
71 changes: 71 additions & 0 deletions x-pack/plugins/fleet/server/services/fleet_usage_logger.ts
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)}`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

} else {
appContextService.getLogger().info(`Feet Usage: ${JSON.stringify(usageData)}`);

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This todo has been addressed now, right?

Copy link
Member Author

Choose a reason for hiding this comment

The 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: {},
});
}