From 5c1a6daaa32a1cedc8019bd5a457c116617823b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Zaffarano?= Date: Thu, 26 Sep 2024 16:57:47 +0200 Subject: [PATCH] Add testing route to trigger new task --- .../server/lib/telemetry/routes/index.ts | 50 +++++++++++++++++++ .../lib/telemetry/tasks/indices.metadata.ts | 14 +++--- .../security_solution/server/routes/index.ts | 4 ++ 3 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 x-pack/plugins/security_solution/server/lib/telemetry/routes/index.ts diff --git a/x-pack/plugins/security_solution/server/lib/telemetry/routes/index.ts b/x-pack/plugins/security_solution/server/lib/telemetry/routes/index.ts new file mode 100644 index 0000000000000..af29648227afc --- /dev/null +++ b/x-pack/plugins/security_solution/server/lib/telemetry/routes/index.ts @@ -0,0 +1,50 @@ +/* + * 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 { Logger, IRouter } from '@kbn/core/server'; +import type { ITelemetryReceiver } from '../receiver'; +import type { ITelemetryEventsSender } from '../sender'; +import { TaskMetricsService } from '../task_metrics'; +import { createTelemetryIndicesMetadataTaskConfig } from '../tasks/indices.metadata'; + +// TODO: just to test the POC, remove +export const getTriggerIndicesMetadataTaskRoute = ( + router: IRouter, + logger: Logger, + receiver: ITelemetryReceiver, + sender: ITelemetryEventsSender +) => { + router.get( + { + path: '/internal/trigger-indices-metadata-task', + validate: false, + }, + async (_context, _request, response) => { + const taskMetricsService = new TaskMetricsService(logger, sender); + const task = createTelemetryIndicesMetadataTaskConfig(); + const timeStart = performance.now(); + let msgSuffix = ''; + if (global.gc) { + global.gc(); + } else { + msgSuffix = ' (Note: Garbage collection is not exposed. Start Node.js with --expose-gc.)'; + } + const initialMemory = process.memoryUsage().heapUsed; + const result = await task.runTask('id', logger, receiver, sender, taskMetricsService, { + last: 'last', + current: 'current', + }); + const memoryUsed = process.memoryUsage().heapUsed - initialMemory; + const elapsedTime = performance.now() - timeStart; + + return response.ok({ + body: { + message: `Task processed ${result} datastreams. It took ${elapsedTime} ms to run and required ${memoryUsed} bytes ${msgSuffix}`, + }, + }); + } + ); +}; diff --git a/x-pack/plugins/security_solution/server/lib/telemetry/tasks/indices.metadata.ts b/x-pack/plugins/security_solution/server/lib/telemetry/tasks/indices.metadata.ts index 21d402b60619f..32a5ac67dbd6f 100644 --- a/x-pack/plugins/security_solution/server/lib/telemetry/tasks/indices.metadata.ts +++ b/x-pack/plugins/security_solution/server/lib/telemetry/tasks/indices.metadata.ts @@ -23,7 +23,7 @@ export function createTelemetryIndicesMetadataTaskConfig() { return { type: taskType, title: 'Security Solution Telemetry Indices Metadata task', - interval: '1m', // TODO: update!!! + interval: '24h', timeout: '1m', version: '1.0.0', getLastExecutionTime: getPreviousDailyTaskTimestamp, @@ -31,7 +31,7 @@ export function createTelemetryIndicesMetadataTaskConfig() { taskId: string, logger: Logger, receiver: ITelemetryReceiver, - _sender: ITelemetryEventsSender, + sender: ITelemetryEventsSender, taskMetricsService: ITaskMetricsService, taskExecutionPeriod: TaskExecutionPeriod ) => { @@ -79,22 +79,22 @@ export function createTelemetryIndicesMetadataTaskConfig() { // 3. Send events policies.forEach((p) => { - _sender.reportEBT(TELEMETRY_ILM_POLICY_EVENT.eventType, p); + sender.reportEBT(TELEMETRY_ILM_POLICY_EVENT.eventType, p); }); ilmsStats.forEach((i) => { - _sender.reportEBT(TELEMETRY_ILM_STATS_EVENT.eventType, i); + sender.reportEBT(TELEMETRY_ILM_STATS_EVENT.eventType, i); }); dataStreams.forEach((ds) => { - _sender.reportEBT(TELEMETRY_DATA_STREAM_EVENT.eventType, ds); + sender.reportEBT(TELEMETRY_DATA_STREAM_EVENT.eventType, ds); }); indicesStats.forEach((is) => { - _sender.reportEBT(TELEMETRY_INDEX_STATS_EVENT.eventType, is); + sender.reportEBT(TELEMETRY_INDEX_STATS_EVENT.eventType, is); }); - return 0; + return dataStreams.length; } catch (err) { log.warn(`Error running indices metadata task`, { error: JSON.stringify(err), diff --git a/x-pack/plugins/security_solution/server/routes/index.ts b/x-pack/plugins/security_solution/server/routes/index.ts index 6f245bd04a02b..ea02dc96df390 100644 --- a/x-pack/plugins/security_solution/server/routes/index.ts +++ b/x-pack/plugins/security_solution/server/routes/index.ts @@ -61,6 +61,7 @@ import { suggestUserProfilesRoute } from '../lib/detection_engine/routes/users/s import { registerTimelineRoutes } from '../lib/timeline/routes'; import { getFleetManagedIndexTemplatesRoute } from '../lib/security_integrations/cribl/routes'; import { registerEntityAnalyticsRoutes } from '../lib/entity_analytics/register_entity_analytics_routes'; +import { getTriggerIndicesMetadataTaskRoute } from '../lib/telemetry/routes'; export const initRoutes = ( router: SecuritySolutionPluginRouter, @@ -147,4 +148,7 @@ export const initRoutes = ( registerEntityAnalyticsRoutes({ router, config, getStartServices, logger }); // Security Integrations getFleetManagedIndexTemplatesRoute(router); + + // TODO: just to test the POC, remove + getTriggerIndicesMetadataTaskRoute(router, logger, previewTelemetryReceiver, telemetrySender); };