Skip to content

Commit

Permalink
Add testing route to trigger new task
Browse files Browse the repository at this point in the history
  • Loading branch information
szaffarano committed Sep 26, 2024
1 parent f0bf108 commit 5c1a6da
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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}`,
},
});
}
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ 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,
runTask: async (
taskId: string,
logger: Logger,
receiver: ITelemetryReceiver,
_sender: ITelemetryEventsSender,
sender: ITelemetryEventsSender,
taskMetricsService: ITaskMetricsService,
taskExecutionPeriod: TaskExecutionPeriod
) => {
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/security_solution/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
};

0 comments on commit 5c1a6da

Please sign in to comment.