Skip to content

Commit

Permalink
[9.0] [Rule Migration] Telemetry improvements (#210275) (#210582)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `9.0`:
- [[Rule Migration] Telemetry improvements
(#210275)](#210275)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Marius
Iversen","email":"marius.iversen@elastic.co"},"sourceCommit":{"committedDate":"2025-02-11T14:01:53Z","message":"[Rule
Migration] Telemetry improvements (#210275)\n\n## Summary\n\nThis PR
resolves a minor bug in one of the telemetry events, it also\nadds a
try/catch around the event reporting to prevent it crashing
on\nfailures.\n\nI also moved our reportEvent wrapper function to use
generics so that we\nget proper typehint/checks on the events to prevent
issues similar in\nthe
future.","sha":"d22fae166acf604539859aa2dfab44a8847aa78d","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:
SecuritySolution","backport:version","v8.18.0","v9.1.0","v8.19.0"],"title":"[Rule
Migration] Telemetry
improvements","number":210275,"url":"https://github.com/elastic/kibana/pull/210275","mergeCommit":{"message":"[Rule
Migration] Telemetry improvements (#210275)\n\n## Summary\n\nThis PR
resolves a minor bug in one of the telemetry events, it also\nadds a
try/catch around the event reporting to prevent it crashing
on\nfailures.\n\nI also moved our reportEvent wrapper function to use
generics so that we\nget proper typehint/checks on the events to prevent
issues similar in\nthe
future.","sha":"d22fae166acf604539859aa2dfab44a8847aa78d"}},"sourceBranch":"main","suggestedTargetBranches":["9.0","8.18","8.x"],"targetPullRequestStates":[{"branch":"9.0","label":"v9.0.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.18","label":"v8.18.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/210275","number":210275,"mergeCommit":{"message":"[Rule
Migration] Telemetry improvements (#210275)\n\n## Summary\n\nThis PR
resolves a minor bug in one of the telemetry events, it also\nadds a
try/catch around the event reporting to prevent it crashing
on\nfailures.\n\nI also moved our reportEvent wrapper function to use
generics so that we\nget proper typehint/checks on the events to prevent
issues similar in\nthe
future.","sha":"d22fae166acf604539859aa2dfab44a8847aa78d"}},{"branch":"8.x","label":"v8.19.0","branchLabelMappingKey":"^v8.19.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Marius Iversen <marius.iversen@elastic.co>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 21, 2025
1 parent 172592a commit 2a4e4c0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class RuleMigrationsTaskClient {
const stats = { completed: 0, failed: 0 };
const telemetryClient = new SiemMigrationTelemetryClient(
this.dependencies.telemetry,
this.logger,
migrationId,
model.model
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { coreMock } from '@kbn/core/server/mocks';
import { loggerMock } from '@kbn/logging-mocks';
import { SiemMigrationTelemetryClient } from './rule_migrations_telemetry_client';
import type { RuleMigrationIntegration, RuleMigrationPrebuiltRule } from '../types';
import type { MigrateRuleState } from './agent/types';
Expand Down Expand Up @@ -68,8 +69,10 @@ const preFilterIntegrationMocks: RuleMigrationIntegration[] = [
];

const mockTelemetry = coreMock.createSetup().analytics;
const mockLogger = loggerMock.create();
const siemTelemetryClient = new SiemMigrationTelemetryClient(
mockTelemetry,
mockLogger,
'testmigration',
'testModel'
);
Expand Down Expand Up @@ -199,7 +202,7 @@ describe('siemMigrationTelemetry', () => {
migrationId: 'testmigration',
model: 'testModel',
postFilterRuleCount: 1,
postFilterRuleNames: 'rule1id',
postFilterRuleName: 'rule1id',
preFilterRuleCount: 2,
preFilterRuleNames: ['rule1id', 'rule2id'],
});
Expand All @@ -212,7 +215,7 @@ describe('siemMigrationTelemetry', () => {
migrationId: 'testmigration',
model: 'testModel',
postFilterRuleCount: 0,
postFilterRuleNames: '',
postFilterRuleName: '',
preFilterRuleCount: 2,
preFilterRuleNames: ['rule1id', 'rule2id'],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import type { AnalyticsServiceSetup } from '@kbn/core/public';
import type { AnalyticsServiceSetup, Logger, EventTypeOpts } from '@kbn/core/server';
import {
SIEM_MIGRATIONS_INTEGRATIONS_MATCH,
SIEM_MIGRATIONS_MIGRATION_FAILURE,
Expand Down Expand Up @@ -43,17 +43,24 @@ interface SiemMigrationEvent {
export class SiemMigrationTelemetryClient {
constructor(
private readonly telemetry: AnalyticsServiceSetup,
private readonly logger: Logger,
private readonly migrationId: string,
private readonly modelName: string | undefined
) {
this.modelName = modelName || '';
private readonly modelName: string = ''
) {}

private reportEvent<T extends object>(eventTypeOpts: EventTypeOpts<T>, data: T): void {
try {
this.telemetry.reportEvent(eventTypeOpts.eventType, data);
} catch (e) {
this.logger.error(`Error reporting event ${eventTypeOpts.eventType}: ${e.message}`);
}
}

public reportIntegrationsMatch({
preFilterIntegrations,
postFilterIntegration,
}: IntegrationMatchEvent): void {
this.telemetry.reportEvent(SIEM_MIGRATIONS_INTEGRATIONS_MATCH.eventType, {
this.reportEvent(SIEM_MIGRATIONS_INTEGRATIONS_MATCH, {
model: this.modelName,
migrationId: this.migrationId,
preFilterIntegrationNames: preFilterIntegrations.map((integration) => integration.id) || [],
Expand All @@ -66,12 +73,12 @@ export class SiemMigrationTelemetryClient {
preFilterRules,
postFilterRule,
}: PrebuiltRuleMatchEvent): void {
this.telemetry.reportEvent(SIEM_MIGRATIONS_PREBUILT_RULES_MATCH.eventType, {
this.reportEvent(SIEM_MIGRATIONS_PREBUILT_RULES_MATCH, {
model: this.modelName,
migrationId: this.migrationId,
preFilterRuleNames: preFilterRules.map((rule) => rule.rule_id) || [],
preFilterRuleCount: preFilterRules.length,
postFilterRuleNames: postFilterRule ? postFilterRule.rule_id : '',
postFilterRuleName: postFilterRule ? postFilterRule.rule_id : '',
postFilterRuleCount: postFilterRule ? 1 : 0,
});
}
Expand All @@ -84,17 +91,17 @@ export class SiemMigrationTelemetryClient {
const duration = Date.now() - startTime;

if (error) {
this.telemetry.reportEvent(SIEM_MIGRATIONS_RULE_TRANSLATION_FAILURE.eventType, {
this.reportEvent(SIEM_MIGRATIONS_RULE_TRANSLATION_FAILURE, {
migrationId: this.migrationId,
error: error.message,
model: this.modelName,
});
return;
}

this.telemetry.reportEvent(SIEM_MIGRATIONS_RULE_TRANSLATION_SUCCESS.eventType, {
this.reportEvent(SIEM_MIGRATIONS_RULE_TRANSLATION_SUCCESS, {
migrationId: this.migrationId,
translationResult: migrationResult?.translation_result,
translationResult: migrationResult?.translation_result || '',
duration,
model: this.modelName,
prebuiltMatch: migrationResult?.elastic_rule?.prebuilt_rule_id ? true : false,
Expand All @@ -106,26 +113,26 @@ export class SiemMigrationTelemetryClient {

return ({ error, stats }) => {
const duration = Date.now() - startTime;
const total = stats?.completed + stats?.failed;
const total = stats ? stats.completed + stats.failed : 0;

if (error) {
this.telemetry.reportEvent(SIEM_MIGRATIONS_MIGRATION_FAILURE.eventType, {
this.reportEvent(SIEM_MIGRATIONS_MIGRATION_FAILURE, {
migrationId: this.migrationId,
model: this.modelName || '',
completed: stats?.completed,
failed: stats?.failed,
completed: stats ? stats.completed : 0,
failed: stats ? stats.failed : 0,
total,
duration,
error: error.message,
});
return;
}

this.telemetry.reportEvent(SIEM_MIGRATIONS_MIGRATION_SUCCESS.eventType, {
this.reportEvent(SIEM_MIGRATIONS_MIGRATION_SUCCESS, {
migrationId: this.migrationId,
model: this.modelName || '',
completed: stats?.completed,
failed: stats?.failed,
completed: stats ? stats.completed : 0,
failed: stats ? stats.failed : 0,
total,
duration,
});
Expand Down

0 comments on commit 2a4e4c0

Please sign in to comment.