From 5a2adfa902e38b08161d2d199c22999aa8968849 Mon Sep 17 00:00:00 2001 From: Ersin Erdal Date: Mon, 19 Sep 2022 11:57:19 +0200 Subject: [PATCH 01/11] Add new telemetry data from eventLog index. count_rules_by_execution_status_per_day, count_connector_types_by_action_run_outcome_per_day, avg_actions_run_duration_by_connector_type --- .../server/usage/alerting_usage_collector.ts | 22 +++ .../lib/get_telemetry_from_event_log.test.ts | 133 ++++++++++++++- .../usage/lib/get_telemetry_from_event_log.ts | 143 ++++++++++++++++ .../lib/parse_connector_type_bucket.test.ts | 155 ++++++++++++++++++ .../usage/lib/parse_connector_type_bucket.ts | 50 ++++++ x-pack/plugins/alerting/server/usage/task.ts | 9 + x-pack/plugins/alerting/server/usage/types.ts | 3 + .../schema/xpack_plugins.json | 37 +++++ .../alerting_and_actions_telemetry.ts | 20 ++- 9 files changed, 570 insertions(+), 2 deletions(-) create mode 100644 x-pack/plugins/alerting/server/usage/lib/parse_connector_type_bucket.test.ts create mode 100644 x-pack/plugins/alerting/server/usage/lib/parse_connector_type_bucket.ts diff --git a/x-pack/plugins/alerting/server/usage/alerting_usage_collector.ts b/x-pack/plugins/alerting/server/usage/alerting_usage_collector.ts index 6e6eb3cb43f0d..4f051eb238b9e 100644 --- a/x-pack/plugins/alerting/server/usage/alerting_usage_collector.ts +++ b/x-pack/plugins/alerting/server/usage/alerting_usage_collector.ts @@ -116,6 +116,22 @@ const byStatusSchema: MakeSchemaFrom['count_rules_by_execution_st warning: { type: 'long' }, }; +const byStatusPerDaySchema: MakeSchemaFrom['count_rules_by_execution_status_per_day'] = + { + success: { type: 'long' }, + failure: { type: 'long' }, + unknown: { type: 'long' }, + }; + +const byConnectorTypeStatusPerDaySchema: MakeSchemaFrom['count_connector_types_by_action_run_outcome_per_day'] = + { + DYNAMIC_KEY: { + success: { type: 'long' }, + failure: { type: 'long' }, + unknown: { type: 'long' }, + }, + }; + const byNotifyWhenSchema: MakeSchemaFrom['count_rules_by_notify_when'] = { on_action_group_change: { type: 'long' }, on_active_alert: { type: 'long' }, @@ -200,12 +216,15 @@ export function createAlertingUsageCollector( count_rules_muted: 0, count_rules_with_muted_alerts: 0, count_connector_types_by_consumers: {}, + count_rules_by_execution_status_per_day: {}, + count_connector_types_by_action_run_outcome_per_day: {}, avg_execution_time_per_day: 0, avg_execution_time_by_type_per_day: {}, avg_es_search_duration_per_day: 0, avg_es_search_duration_by_type_per_day: {}, avg_total_search_duration_per_day: 0, avg_total_search_duration_by_type_per_day: {}, + avg_actions_run_duration_by_connector_type: {}, percentile_num_generated_actions_per_day: { p50: 0, p90: 0, @@ -283,12 +302,15 @@ export function createAlertingUsageCollector( count_rules_muted: { type: 'long' }, count_rules_with_muted_alerts: { type: 'long' }, count_connector_types_by_consumers: { DYNAMIC_KEY: { DYNAMIC_KEY: { type: 'long' } } }, + count_rules_by_execution_status_per_day: byStatusPerDaySchema, + count_connector_types_by_action_run_outcome_per_day: byConnectorTypeStatusPerDaySchema, avg_execution_time_per_day: { type: 'long' }, avg_execution_time_by_type_per_day: byTypeSchema, avg_es_search_duration_per_day: { type: 'long' }, avg_es_search_duration_by_type_per_day: byTypeSchema, avg_total_search_duration_per_day: { type: 'long' }, avg_total_search_duration_by_type_per_day: byTypeSchema, + avg_actions_run_duration_by_connector_type: { DYNAMIC_KEY: { type: 'long' } }, percentile_num_generated_actions_per_day: byPercentileSchema, percentile_num_generated_actions_by_type_per_day: byPercentileSchemaByType, percentile_num_alerts_per_day: byPercentileSchema, diff --git a/x-pack/plugins/alerting/server/usage/lib/get_telemetry_from_event_log.test.ts b/x-pack/plugins/alerting/server/usage/lib/get_telemetry_from_event_log.test.ts index f3e21f6a161fa..622822810f732 100644 --- a/x-pack/plugins/alerting/server/usage/lib/get_telemetry_from_event_log.test.ts +++ b/x-pack/plugins/alerting/server/usage/lib/get_telemetry_from_event_log.test.ts @@ -13,6 +13,7 @@ import { parsePercentileAggs, parseExecutionCountAggregationResults, getExecutionTimeoutsPerDayCount, + getActionExecutionsTelemetryPerDay, } from './get_telemetry_from_event_log'; const elasticsearch = elasticsearchServiceMock.createStart(); @@ -1291,6 +1292,14 @@ describe('event log telemetry', () => { avg_total_search_duration: { value: 28.630434782608695, }, + by_execution_status: { + doc_count_error_upper_bound: 0, + sum_other_doc_count: 0, + buckets: [ + { key: 'success', doc_count: 21 }, + { key: 'failure', doc_count: 22 }, + ], + }, }, }); @@ -1377,7 +1386,6 @@ describe('event log telemetry', () => { logs__alert__document__count: 0, }, }, - alertsPercentilesByType: { p50: { '__index-threshold': 1, @@ -1398,6 +1406,10 @@ describe('event log telemetry', () => { logs__alert__document__count: 0, }, }, + countRulesByExecutionStatus: { + failure: 22, + success: 21, + }, hasErrors: false, }); }); @@ -1437,6 +1449,7 @@ describe('event log telemetry', () => { generatedActionsPercentilesByType: {}, alertsPercentiles: {}, alertsPercentilesByType: {}, + countRulesByExecutionStatus: {}, }); }); }); @@ -1527,4 +1540,122 @@ describe('event log telemetry', () => { }); }); }); + + describe('getActionExecutionsTelemetryPerDay', () => { + test('should return telemetry data from actions eventLog ', async () => { + esClient.search.mockResponse({ + took: 4, + timed_out: false, + _shards: { + total: 1, + successful: 1, + skipped: 0, + failed: 0, + }, + hits: { + total: { + value: 148, + relation: 'eq', + }, + max_score: null, + hits: [], + }, + aggregations: { + avg_run_duration_by_connector_type: { + connector_types: { + buckets: [ + { + key: '.slack', + duration: { + average: { + value: 10, + }, + }, + }, + { + key: '.email', + duration: { + average: { + value: 11, + }, + }, + }, + ], + }, + }, + count_connector_types_by_action_run_outcome_per_day: { + connector_types: { + buckets: [ + { + key: '.slack', + outcome: { + count: { + buckets: [ + { key: 'success', doc_count: 12 }, + { key: 'failure', doc_count: 1 }, + ], + }, + }, + }, + { + key: '.email', + outcome: { + count: { + buckets: [ + { key: 'success', doc_count: 13 }, + { key: 'failure', doc_count: 2 }, + ], + }, + }, + }, + ], + }, + }, + }, + }); + + const telemetry = await getActionExecutionsTelemetryPerDay({ + esClient, + eventLogIndex: 'test', + logger, + }); + + expect(esClient.search).toHaveBeenCalledTimes(1); + + expect(telemetry).toStrictEqual({ + hasErrors: false, + avgRunDurationByConnectorType: { + __email: 11, + __slack: 10, + }, + countRunOutcomeByConnectorType: { + __email: { + failure: 2, + success: 13, + }, + __slack: { + failure: 1, + success: 12, + }, + }, + }); + }); + + test('should return empty results and log warning if query throws error', async () => { + esClient.search.mockRejectedValue(new Error('oh no')); + + const telemetry = await getActionExecutionsTelemetryPerDay({ + esClient, + eventLogIndex: 'test', + logger, + }); + + expect(telemetry).toEqual({ + avgRunDurationByConnectorType: {}, + countRunOutcomeByConnectorType: {}, + errorMessage: 'oh no', + hasErrors: true, + }); + }); + }); }); diff --git a/x-pack/plugins/alerting/server/usage/lib/get_telemetry_from_event_log.ts b/x-pack/plugins/alerting/server/usage/lib/get_telemetry_from_event_log.ts index 703d579e66c25..bd9cd070685b7 100644 --- a/x-pack/plugins/alerting/server/usage/lib/get_telemetry_from_event_log.ts +++ b/x-pack/plugins/alerting/server/usage/lib/get_telemetry_from_event_log.ts @@ -16,6 +16,12 @@ import type { AggregationsBuckets, } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import { ElasticsearchClient, Logger } from '@kbn/core/server'; +import { + AvgActionRunDurationByConnectorTypeBucket, + AvgActionRunOutcomeByConnectorTypeBucket, + parseActionRunOutcomeByConnectorTypesBucket, + parseDurationsByConnectorTypesBucket, +} from './parse_connector_type_bucket'; import { NUM_ALERTING_RULE_TYPES, NUM_ALERTING_EXECUTION_FAILURE_REASON_TYPES, @@ -54,6 +60,14 @@ interface GetExecutionsPerDayCountResults { generatedActionsPercentilesByType: Record>; alertsPercentiles: Record; alertsPercentilesByType: Record>; + countRulesByExecutionStatus: Record; +} + +interface GetActionExecutionsTelemetryPerDay { + hasErrors: boolean; + errorMessage?: string; + avgRunDurationByConnectorType: Record; + countRunOutcomeByConnectorType: Record; } interface GetExecutionTimeoutsPerDayCountResults { @@ -145,6 +159,11 @@ export async function getExecutionsPerDayCount({ }, aggs: eventLogAggs, }, + by_execution_status: { + terms: { + field: 'event.outcome', + }, + }, }, }, }; @@ -165,6 +184,7 @@ export async function getExecutionsPerDayCount({ avg_execution_time: AggregationsSingleMetricAggregateBase; avg_es_search_duration: AggregationsSingleMetricAggregateBase; avg_total_search_duration: AggregationsSingleMetricAggregateBase; + by_execution_status: AggregationsTermsAggregateBase; }; const aggregationsByRuleTypeId: AggregationsBuckets = @@ -176,6 +196,9 @@ export async function getExecutionsPerDayCount({ ...parseExecutionFailureByRuleType(aggregationsByRuleTypeId), ...parseExecutionCountAggregationResults(aggregations), countTotalRuleExecutions: totalRuleExecutions ?? 0, + countRulesByExecutionStatus: parseSimpleRuleTypeBucket( + aggregations.by_execution_status.buckets + ), }; } catch (err) { const errorMessage = err && err.message ? err.message : err.toString(); @@ -204,6 +227,118 @@ export async function getExecutionsPerDayCount({ generatedActionsPercentilesByType: {}, alertsPercentiles: {}, alertsPercentilesByType: {}, + countRulesByExecutionStatus: {}, + }; + } +} + +export async function getActionExecutionsTelemetryPerDay({ + esClient, + eventLogIndex, + logger, +}: Opts): Promise { + try { + const eventLogAggs = { + avg_run_duration_by_connector_type: { + nested: { + path: 'kibana.saved_objects', + }, + aggs: { + connector_types: { + terms: { + field: 'kibana.saved_objects.type_id', + }, + aggs: { + duration: { + reverse_nested: {}, + aggs: { + average: { + avg: { + field: 'event.duration', + }, + }, + }, + }, + }, + }, + }, + }, + count_connector_types_by_action_run_outcome_per_day: { + nested: { + path: 'kibana.saved_objects', + }, + aggs: { + connector_types: { + terms: { + field: 'kibana.saved_objects.type_id', + }, + aggs: { + outcome: { + reverse_nested: {}, + aggs: { + count: { + terms: { + field: 'event.outcome', + }, + }, + }, + }, + }, + }, + }, + }, + }; + + const query = { + index: eventLogIndex, + size: 0, + body: { + query: getProviderAndActionFilterForTimeRange('execute', 'actions'), + aggs: eventLogAggs, + }, + }; + + logger.debug(`query for getActionExecutionsTelemetryPerDay - ${JSON.stringify(query)}`); + const results = await esClient.search(query); + + logger.debug( + `results for getActionExecutionsTelemetryPerDay query - ${JSON.stringify(results)}` + ); + + const aggregations = results.aggregations as { + avg_run_duration_by_connector_type: { + connector_types: AggregationsTermsAggregateBase; + }; + count_connector_types_by_action_run_outcome_per_day: { + connector_types: AggregationsTermsAggregateBase; + }; + }; + + return { + hasErrors: false, + avgRunDurationByConnectorType: parseDurationsByConnectorTypesBucket( + aggregations.avg_run_duration_by_connector_type.connector_types.buckets + ), + countRunOutcomeByConnectorType: parseActionRunOutcomeByConnectorTypesBucket( + aggregations.count_connector_types_by_action_run_outcome_per_day.connector_types.buckets + ), + }; + } catch (err) { + const errorMessage = err && err.message ? err.message : err.toString(); + logger.warn( + `Error executing alerting telemetry task: getActionExecutionsTelemetryPerDay - ${JSON.stringify( + err + )}`, + { + tags: ['alerting', 'telemetry-failed'], + error: { stack_trace: err.stack }, + } + ); + return { + hasErrors: true, + errorMessage, + avgRunDurationByConnectorType: {}, + countRunOutcomeByConnectorType: {}, }; } } @@ -313,6 +448,14 @@ export async function getExecutionTimeoutsPerDayCount({ * avg_total_search_duration: { // average total search duration across executions * value: 43.74647887323944, * }, + * by_execution_status: { + * "doc_count_error_upper_bound":0, + * "sum_other_doc_count":0, + * "buckets":[ + * {"key":"success","doc_count":48}, + * {"key":"failure","doc_count":1} + * ] + * } * } */ diff --git a/x-pack/plugins/alerting/server/usage/lib/parse_connector_type_bucket.test.ts b/x-pack/plugins/alerting/server/usage/lib/parse_connector_type_bucket.test.ts new file mode 100644 index 0000000000000..f43bc43720058 --- /dev/null +++ b/x-pack/plugins/alerting/server/usage/lib/parse_connector_type_bucket.test.ts @@ -0,0 +1,155 @@ +/* + * 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 { + parseDurationsByConnectorTypesBucket, + parseActionRunOutcomeByConnectorTypesBucket, +} from './parse_connector_type_bucket'; + +describe('parseDurationsByConnectorTypesBucket', () => { + test('should correctly parse connector type bucket results', () => { + expect( + parseDurationsByConnectorTypesBucket([ + { + key: '.server-log', + doc_count: 78, + duration: { average: { value: 10.2 } }, + }, + { + key: '.index', + doc_count: 42, + duration: { average: { value: 11.6 } }, + }, + { + key: '.slack', + doc_count: 28, + duration: { average: { value: 12.4 } }, + }, + ]) + ).toEqual({ + '__server-log': 10, + __index: 12, + __slack: 12, + }); + }); + + test('should handle missing values', () => { + expect( + parseDurationsByConnectorTypesBucket([ + // @ts-expect-error + { + key: '.server-log', + doc_count: 78, + }, + { + key: '.index', + doc_count: 42, + // @ts-expect-error + duration: {}, + }, + { + key: '.slack', + doc_count: 28, + // @ts-expect-error + duration: { average: {} }, + }, + ]) + ).toEqual({ + '__server-log': 0, + __index: 0, + __slack: 0, + }); + }); + + test('should handle empty input', () => { + expect(parseDurationsByConnectorTypesBucket([])).toEqual({}); + }); + // + test('should handle undefined input', () => { + expect(parseDurationsByConnectorTypesBucket(undefined)).toEqual({}); + }); +}); + +describe('parseActionRunOutcomeByConnectorTypesBucket', () => { + test('should correctly parse connector type bucket results', () => { + expect( + parseActionRunOutcomeByConnectorTypesBucket([ + { + key: '.server-log', + doc_count: 78, + outcome: { + count: { + buckets: [ + { key: 'success', doc_count: 2 }, + { key: 'failure', doc_count: 1 }, + ], + }, + }, + }, + { + key: '.index', + doc_count: 42, + outcome: { + count: { + buckets: [ + { key: 'success', doc_count: 3 }, + { key: 'failure', doc_count: 4 }, + ], + }, + }, + }, + ]) + ).toEqual({ + __index: { + failure: 4, + success: 3, + }, + '__server-log': { + failure: 1, + success: 2, + }, + }); + }); + + test('should handle missing values', () => { + expect( + parseActionRunOutcomeByConnectorTypesBucket([ + { + key: '.server-log', + doc_count: 78, + outcome: { + count: { + // @ts-expect-error + buckets: [{ key: 'success', doc_count: 2 }, { key: 'failure' }], + }, + }, + }, + { + key: '.index', + outcome: { + // @ts-expect-error + count: {}, + }, + }, + ]) + ).toEqual({ + '__server-log': { + failure: 0, + success: 2, + }, + __index: {}, + }); + }); + + test('should handle empty input', () => { + expect(parseActionRunOutcomeByConnectorTypesBucket([])).toEqual({}); + }); + // + test('should handle undefined input', () => { + expect(parseActionRunOutcomeByConnectorTypesBucket(undefined)).toEqual({}); + }); +}); diff --git a/x-pack/plugins/alerting/server/usage/lib/parse_connector_type_bucket.ts b/x-pack/plugins/alerting/server/usage/lib/parse_connector_type_bucket.ts new file mode 100644 index 0000000000000..17d477e15e8dd --- /dev/null +++ b/x-pack/plugins/alerting/server/usage/lib/parse_connector_type_bucket.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 { AggregationsBuckets } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; +import { replaceDotSymbols } from './replace_dots_with_underscores'; + +export interface AvgActionRunDurationByConnectorTypeBucket { + key: string; + doc_count: number; // Not used for duration telemetry but can be helpful later. + duration: { average: { value: number } }; +} + +export function parseDurationsByConnectorTypesBucket( + connectorTypeBuckets: AggregationsBuckets = [] +) { + const buckets = connectorTypeBuckets as AvgActionRunDurationByConnectorTypeBucket[]; + return buckets.reduce((acc, connectorType) => { + return { + ...acc, + [replaceDotSymbols(connectorType.key)]: Math.round( + connectorType.duration?.average?.value || 0 + ), + }; + }, {}); +} + +export interface AvgActionRunOutcomeByConnectorTypeBucket { + key: string; + doc_count: number; // Not used for duration telemetry but can be helpful later. + outcome: { count: { buckets: Array<{ key: string; doc_count: number }> } }; +} + +export function parseActionRunOutcomeByConnectorTypesBucket( + connectorTypeBuckets: AggregationsBuckets = [] +) { + const connectorTypes = connectorTypeBuckets as AvgActionRunOutcomeByConnectorTypeBucket[]; + return connectorTypes.reduce((acc, connectorType) => { + const outcomes = connectorType.outcome?.count?.buckets ?? []; + return { + ...acc, + [replaceDotSymbols(connectorType.key)]: outcomes.reduce((accBucket, bucket) => { + return { ...accBucket, [replaceDotSymbols(bucket.key)]: bucket.doc_count || 0 }; + }, {}), + }; + }, {}); +} diff --git a/x-pack/plugins/alerting/server/usage/task.ts b/x-pack/plugins/alerting/server/usage/task.ts index 4f835f3c4a64f..364f5fbcd6871 100644 --- a/x-pack/plugins/alerting/server/usage/task.ts +++ b/x-pack/plugins/alerting/server/usage/task.ts @@ -16,6 +16,7 @@ import { import { getFailedAndUnrecognizedTasksPerDay } from './lib/get_telemetry_from_task_manager'; import { getTotalCountAggregations, getTotalCountInUse } from './lib/get_telemetry_from_kibana'; import { + getActionExecutionsTelemetryPerDay, getExecutionsPerDayCount, getExecutionTimeoutsPerDayCount, } from './lib/get_telemetry_from_event_log'; @@ -102,6 +103,7 @@ export function telemetryTaskRunner( getExecutionsPerDayCount({ esClient, eventLogIndex, logger }), getExecutionTimeoutsPerDayCount({ esClient, eventLogIndex, logger }), getFailedAndUnrecognizedTasksPerDay({ esClient, taskManagerIndex, logger }), + getActionExecutionsTelemetryPerDay({ esClient, eventLogIndex, logger }), ]) .then( ([ @@ -110,6 +112,7 @@ export function telemetryTaskRunner( dailyExecutionCounts, dailyExecutionTimeoutCounts, dailyFailedAndUnrecognizedTasks, + dailyActionExecutionAggregations, ]) => { const hasErrors = totalCountAggregations.hasErrors || @@ -161,6 +164,8 @@ export function telemetryTaskRunner( dailyExecutionCounts.countFailedExecutionsByReason, count_rules_executions_failured_by_reason_by_type_per_day: dailyExecutionCounts.countFailedExecutionsByReasonByType, + count_rules_by_execution_status_per_day: + dailyExecutionCounts.countRulesByExecutionStatus, count_rules_executions_timeouts_per_day: dailyExecutionTimeoutCounts.countExecutionTimeouts, count_rules_executions_timeouts_by_type_per_day: @@ -179,6 +184,10 @@ export function telemetryTaskRunner( avg_total_search_duration_per_day: dailyExecutionCounts.avgTotalSearchDuration, avg_total_search_duration_by_type_per_day: dailyExecutionCounts.avgTotalSearchDurationByType, + avg_action_execution_duration_by_connector_type_per_day: + dailyActionExecutionAggregations.avgRunDurationByConnectorType, + count_connector_types_by_action_run_outcome_per_day: + dailyActionExecutionAggregations.countRunOutcomeByConnectorType, percentile_num_generated_actions_per_day: dailyExecutionCounts.generatedActionsPercentiles, percentile_num_generated_actions_by_type_per_day: diff --git a/x-pack/plugins/alerting/server/usage/types.ts b/x-pack/plugins/alerting/server/usage/types.ts index 29a32de29bc1a..90c48dbb875e8 100644 --- a/x-pack/plugins/alerting/server/usage/types.ts +++ b/x-pack/plugins/alerting/server/usage/types.ts @@ -42,6 +42,8 @@ export interface AlertingUsage { count_rules_snoozed: number; count_rules_muted: number; count_rules_with_muted_alerts: number; + count_rules_by_execution_status_per_day: Record; + count_connector_types_by_action_run_outcome_per_day: Record>; percentile_num_generated_actions_per_day: { p50: number; p90: number; @@ -68,6 +70,7 @@ export interface AlertingUsage { avg_es_search_duration_by_type_per_day: Record; avg_total_search_duration_per_day: number; avg_total_search_duration_by_type_per_day: Record; + avg_actions_run_duration_by_connector_type: Record; throttle_time: { min: string; avg: string; diff --git a/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json b/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json index 797b290d245c5..8f6ade66e6776 100644 --- a/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json +++ b/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json @@ -1700,6 +1700,36 @@ } } }, + "count_rules_by_execution_status_per_day": { + "properties": { + "success": { + "type": "long" + }, + "failure": { + "type": "long" + }, + "unknown": { + "type": "long" + } + } + }, + "count_connector_types_by_action_run_outcome_per_day": { + "properties": { + "DYNAMIC_KEY": { + "properties": { + "success": { + "type": "long" + }, + "failure": { + "type": "long" + }, + "unknown": { + "type": "long" + } + } + } + } + }, "avg_execution_time_per_day": { "type": "long" }, @@ -2018,6 +2048,13 @@ } } }, + "avg_actions_run_duration_by_connector_type": { + "properties": { + "DYNAMIC_KEY": { + "type": "long" + } + } + }, "percentile_num_generated_actions_per_day": { "properties": { "p50": { diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts b/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts index 7e6aec1347753..d0c767711dad4 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts @@ -25,7 +25,7 @@ export default function createAlertingAndActionsTelemetryTests({ getService }: F const esTestIndexTool = new ESTestIndexTool(es, retry); const supertestWithoutAuth = getService('supertestWithoutAuth'); - describe('telemetry', () => { + describe('telemetry xxx', () => { const alwaysFiringRuleId: { [key: string]: string } = {}; beforeEach(async () => { @@ -528,6 +528,24 @@ export default function createAlertingAndActionsTelemetryTests({ getService }: F // eslint-disable-next-line @typescript-eslint/naming-convention alertsFixture: { test__noop: 9, test__throw: 9, __slack: 3 }, }); + + expect( + telemetry.avg_action_execution_duration_by_connector_type_per_day['test__cumulative-firing'] + ).to.be.greaterThan(0); + expect( + telemetry.avg_action_execution_duration_by_connector_type_per_day.test__throw + ).to.be.greaterThan(0); + + expect( + telemetry.count_connector_types_by_action_run_outcome_per_day['test__cumulative-firing'] + .failure + ).to.greaterThan(0); + expect( + telemetry.count_connector_types_by_action_run_outcome_per_day.test__throw.failure + ).to.greaterThan(0); + + expect(telemetry.count_rules_by_execution_status_per_day.failure).to.greaterThan(0); + expect(telemetry.count_rules_by_execution_status_per_day.success).to.greaterThan(0); } it('should retrieve telemetry data in the expected format', async () => { From 8ba7779a543628ee347e1e7eb3ddfe02c76a7978 Mon Sep 17 00:00:00 2001 From: Ersin Erdal Date: Mon, 19 Sep 2022 14:40:38 +0200 Subject: [PATCH 02/11] fix the wrong telemetry field --- .../alerting/server/usage/alerting_usage_collector.ts | 4 ++-- x-pack/plugins/alerting/server/usage/task.ts | 2 +- x-pack/plugins/alerting/server/usage/types.ts | 2 +- .../telemetry_collection_xpack/schema/xpack_plugins.json | 2 +- .../tests/telemetry/alerting_and_actions_telemetry.ts | 7 +++++-- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/x-pack/plugins/alerting/server/usage/alerting_usage_collector.ts b/x-pack/plugins/alerting/server/usage/alerting_usage_collector.ts index 4f051eb238b9e..f804a90afd21e 100644 --- a/x-pack/plugins/alerting/server/usage/alerting_usage_collector.ts +++ b/x-pack/plugins/alerting/server/usage/alerting_usage_collector.ts @@ -224,7 +224,7 @@ export function createAlertingUsageCollector( avg_es_search_duration_by_type_per_day: {}, avg_total_search_duration_per_day: 0, avg_total_search_duration_by_type_per_day: {}, - avg_actions_run_duration_by_connector_type: {}, + avg_actions_run_duration_by_connector_type_per_day: {}, percentile_num_generated_actions_per_day: { p50: 0, p90: 0, @@ -310,7 +310,7 @@ export function createAlertingUsageCollector( avg_es_search_duration_by_type_per_day: byTypeSchema, avg_total_search_duration_per_day: { type: 'long' }, avg_total_search_duration_by_type_per_day: byTypeSchema, - avg_actions_run_duration_by_connector_type: { DYNAMIC_KEY: { type: 'long' } }, + avg_actions_run_duration_by_connector_type_per_day: { DYNAMIC_KEY: { type: 'long' } }, percentile_num_generated_actions_per_day: byPercentileSchema, percentile_num_generated_actions_by_type_per_day: byPercentileSchemaByType, percentile_num_alerts_per_day: byPercentileSchema, diff --git a/x-pack/plugins/alerting/server/usage/task.ts b/x-pack/plugins/alerting/server/usage/task.ts index 364f5fbcd6871..ca4f8e9cf2065 100644 --- a/x-pack/plugins/alerting/server/usage/task.ts +++ b/x-pack/plugins/alerting/server/usage/task.ts @@ -184,7 +184,7 @@ export function telemetryTaskRunner( avg_total_search_duration_per_day: dailyExecutionCounts.avgTotalSearchDuration, avg_total_search_duration_by_type_per_day: dailyExecutionCounts.avgTotalSearchDurationByType, - avg_action_execution_duration_by_connector_type_per_day: + avg_actions_run_duration_by_connector_type_per_day: dailyActionExecutionAggregations.avgRunDurationByConnectorType, count_connector_types_by_action_run_outcome_per_day: dailyActionExecutionAggregations.countRunOutcomeByConnectorType, diff --git a/x-pack/plugins/alerting/server/usage/types.ts b/x-pack/plugins/alerting/server/usage/types.ts index 90c48dbb875e8..652ca4f58664d 100644 --- a/x-pack/plugins/alerting/server/usage/types.ts +++ b/x-pack/plugins/alerting/server/usage/types.ts @@ -70,7 +70,7 @@ export interface AlertingUsage { avg_es_search_duration_by_type_per_day: Record; avg_total_search_duration_per_day: number; avg_total_search_duration_by_type_per_day: Record; - avg_actions_run_duration_by_connector_type: Record; + avg_actions_run_duration_by_connector_type_per_day: Record; throttle_time: { min: string; avg: string; diff --git a/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json b/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json index 8f6ade66e6776..6d8a1e1dcc1ac 100644 --- a/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json +++ b/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json @@ -2048,7 +2048,7 @@ } } }, - "avg_actions_run_duration_by_connector_type": { + "avg_actions_run_duration_by_connector_type_per_day": { "properties": { "DYNAMIC_KEY": { "type": "long" diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts b/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts index d0c767711dad4..cf7d7f5c74eb1 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts @@ -530,10 +530,10 @@ export default function createAlertingAndActionsTelemetryTests({ getService }: F }); expect( - telemetry.avg_action_execution_duration_by_connector_type_per_day['test__cumulative-firing'] + telemetry.avg_actions_run_duration_by_connector_type_per_day['test__cumulative-firing'] ).to.be.greaterThan(0); expect( - telemetry.avg_action_execution_duration_by_connector_type_per_day.test__throw + telemetry.avg_actions_run_duration_by_connector_type_per_day.test__throw ).to.be.greaterThan(0); expect( @@ -546,6 +546,9 @@ export default function createAlertingAndActionsTelemetryTests({ getService }: F expect(telemetry.count_rules_by_execution_status_per_day.failure).to.greaterThan(0); expect(telemetry.count_rules_by_execution_status_per_day.success).to.greaterThan(0); + + expect(telemetry.count_rules_by_execution_status_per_day.failure).to.greaterThan(0); + expect(telemetry.count_rules_by_execution_status_per_day.success).to.greaterThan(0); } it('should retrieve telemetry data in the expected format', async () => { From 5a2b569a21dac8065a0890465465d276d2d19164 Mon Sep 17 00:00:00 2001 From: Ersin Erdal Date: Mon, 19 Sep 2022 20:32:14 +0200 Subject: [PATCH 03/11] Move actions telemetry from alerting plugin to actions plugin --- .../actions/server/usage/actions_telemetry.ts | 74 ++++++++++ .../server/usage/actions_usage_collector.ts | 15 ++- .../lib/parse_connector_type_bucket.test.ts | 0 .../usage/lib/parse_connector_type_bucket.ts | 2 +- x-pack/plugins/actions/server/usage/task.ts | 6 +- x-pack/plugins/actions/server/usage/types.ts | 2 + .../server/usage/alerting_usage_collector.ts | 13 -- .../usage/lib/get_telemetry_from_event_log.ts | 126 ------------------ x-pack/plugins/alerting/server/usage/task.ts | 7 - x-pack/plugins/alerting/server/usage/types.ts | 2 - .../alerting_and_actions_telemetry.ts | 2 +- 11 files changed, 97 insertions(+), 152 deletions(-) rename x-pack/plugins/{alerting => actions}/server/usage/lib/parse_connector_type_bucket.test.ts (100%) rename x-pack/plugins/{alerting => actions}/server/usage/lib/parse_connector_type_bucket.ts (94%) diff --git a/x-pack/plugins/actions/server/usage/actions_telemetry.ts b/x-pack/plugins/actions/server/usage/actions_telemetry.ts index 1e2320e2b1a5d..2601990af8c33 100644 --- a/x-pack/plugins/actions/server/usage/actions_telemetry.ts +++ b/x-pack/plugins/actions/server/usage/actions_telemetry.ts @@ -7,6 +7,13 @@ import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types'; import { ElasticsearchClient, Logger } from '@kbn/core/server'; +import { AggregationsTermsAggregateBase } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; +import { + AvgActionRunDurationByConnectorTypeBucket, + AvgActionRunOutcomeByConnectorTypeBucket, + parseActionRunOutcomeByConnectorTypesBucket, + parseDurationsByConnectorTypesBucket, +} from './lib/parse_connector_type_bucket'; import { AlertHistoryEsIndexConnectorId } from '../../common'; import { ActionResult, PreConfiguredAction } from '../types'; @@ -415,6 +422,8 @@ export async function getExecutionsPerDayCount( countFailedByType: Record; avgExecutionTime: number; avgExecutionTimeByType: Record; + avgRunDurationByConnectorType: Record; + countRunOutcomeByConnectorType: Record; }> { const scriptedMetric = { scripted_metric: { @@ -536,6 +545,54 @@ export async function getExecutionsPerDayCount( }, }, }, + avg_run_duration_by_connector_type: { + nested: { + path: 'kibana.saved_objects', + }, + aggs: { + connector_types: { + terms: { + field: 'kibana.saved_objects.type_id', + }, + aggs: { + duration: { + reverse_nested: {}, + aggs: { + average: { + avg: { + field: 'event.duration', + }, + }, + }, + }, + }, + }, + }, + }, + count_connector_types_by_action_run_outcome_per_day: { + nested: { + path: 'kibana.saved_objects', + }, + aggs: { + connector_types: { + terms: { + field: 'kibana.saved_objects.type_id', + }, + aggs: { + outcome: { + reverse_nested: {}, + aggs: { + count: { + terms: { + field: 'event.outcome', + }, + }, + }, + }, + }, + }, + }, + }, }, }, }); @@ -564,6 +621,15 @@ export async function getExecutionsPerDayCount( {} ); + const aggregations = actionResults.aggregations as { + avg_run_duration_by_connector_type: { + connector_types: AggregationsTermsAggregateBase; + }; + count_connector_types_by_action_run_outcome_per_day: { + connector_types: AggregationsTermsAggregateBase; + }; + }; + return { hasErrors: false, countTotal: aggsExecutions.total, @@ -586,6 +652,12 @@ export async function getExecutionsPerDayCount( ), avgExecutionTime: aggsAvgExecutionTime, avgExecutionTimeByType, + avgRunDurationByConnectorType: parseDurationsByConnectorTypesBucket( + aggregations.avg_run_duration_by_connector_type.connector_types.buckets + ), + countRunOutcomeByConnectorType: parseActionRunOutcomeByConnectorTypesBucket( + aggregations.count_connector_types_by_action_run_outcome_per_day.connector_types.buckets + ), }; } catch (err) { const errorMessage = err && err.message ? err.message : err.toString(); @@ -601,6 +673,8 @@ export async function getExecutionsPerDayCount( countFailedByType: {}, avgExecutionTime: 0, avgExecutionTimeByType: {}, + avgRunDurationByConnectorType: {}, + countRunOutcomeByConnectorType: {}, }; } } diff --git a/x-pack/plugins/actions/server/usage/actions_usage_collector.ts b/x-pack/plugins/actions/server/usage/actions_usage_collector.ts index f6e02b2e0fec9..a66ed2da67f80 100644 --- a/x-pack/plugins/actions/server/usage/actions_usage_collector.ts +++ b/x-pack/plugins/actions/server/usage/actions_usage_collector.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server'; +import { MakeSchemaFrom, UsageCollectionSetup } from '@kbn/usage-collection-plugin/server'; import { get } from 'lodash'; import { TaskManagerStartContract } from '@kbn/task-manager-plugin/server'; import { ActionsUsage, byServiceProviderTypeSchema, byTypeSchema } from './types'; @@ -16,6 +16,15 @@ export function createActionsUsageCollector( config: ActionsConfig, taskManager: Promise ) { + const byConnectorTypeStatusPerDaySchema: MakeSchemaFrom['count_connector_types_by_action_run_outcome_per_day'] = + { + DYNAMIC_KEY: { + success: { type: 'long' }, + failure: { type: 'long' }, + unknown: { type: 'long' }, + }, + }; + return usageCollection.makeUsageCollector({ type: 'actions', isReady: async () => { @@ -47,6 +56,8 @@ export function createActionsUsageCollector( count_actions_executions_failed_by_type_per_day: byTypeSchema, avg_execution_time_per_day: { type: 'long' }, avg_execution_time_by_type_per_day: byTypeSchema, + count_connector_types_by_action_run_outcome_per_day: byConnectorTypeStatusPerDaySchema, + avg_actions_run_duration_by_connector_type_per_day: { DYNAMIC_KEY: { type: 'long' } }, }, fetch: async () => { try { @@ -77,6 +88,8 @@ export function createActionsUsageCollector( count_actions_executions_failed_by_type_per_day: {}, avg_execution_time_per_day: 0, avg_execution_time_by_type_per_day: {}, + count_connector_types_by_action_run_outcome_per_day: {}, + avg_actions_run_duration_by_connector_type_per_day: {}, }; } }, diff --git a/x-pack/plugins/alerting/server/usage/lib/parse_connector_type_bucket.test.ts b/x-pack/plugins/actions/server/usage/lib/parse_connector_type_bucket.test.ts similarity index 100% rename from x-pack/plugins/alerting/server/usage/lib/parse_connector_type_bucket.test.ts rename to x-pack/plugins/actions/server/usage/lib/parse_connector_type_bucket.test.ts diff --git a/x-pack/plugins/alerting/server/usage/lib/parse_connector_type_bucket.ts b/x-pack/plugins/actions/server/usage/lib/parse_connector_type_bucket.ts similarity index 94% rename from x-pack/plugins/alerting/server/usage/lib/parse_connector_type_bucket.ts rename to x-pack/plugins/actions/server/usage/lib/parse_connector_type_bucket.ts index 17d477e15e8dd..1539acf7126e7 100644 --- a/x-pack/plugins/alerting/server/usage/lib/parse_connector_type_bucket.ts +++ b/x-pack/plugins/actions/server/usage/lib/parse_connector_type_bucket.ts @@ -6,7 +6,7 @@ */ import { AggregationsBuckets } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; -import { replaceDotSymbols } from './replace_dots_with_underscores'; +import { replaceDotSymbols } from '@kbn/alerting-plugin/server/usage/lib/replace_dots_with_underscores'; export interface AvgActionRunDurationByConnectorTypeBucket { key: string; diff --git a/x-pack/plugins/actions/server/usage/task.ts b/x-pack/plugins/actions/server/usage/task.ts index 15f70529d3852..c6eac09614cfe 100644 --- a/x-pack/plugins/actions/server/usage/task.ts +++ b/x-pack/plugins/actions/server/usage/task.ts @@ -130,6 +130,10 @@ export function telemetryTaskRunner( totalExecutionsPerDay.countFailedByType, avg_execution_time_per_day: totalExecutionsPerDay.avgExecutionTime, avg_execution_time_by_type_per_day: totalExecutionsPerDay.avgExecutionTimeByType, + avg_actions_run_duration_by_connector_type_per_day: + totalExecutionsPerDay.avgRunDurationByConnectorType, + count_connector_types_by_action_run_outcome_per_day: + totalExecutionsPerDay.countRunOutcomeByConnectorType, }, runAt: getNextMidnight(), }; @@ -140,5 +144,5 @@ export function telemetryTaskRunner( } function getNextMidnight() { - return moment().add(1, 'd').startOf('d').toDate(); + return moment().add(1, 'm').startOf('m').toDate(); } diff --git a/x-pack/plugins/actions/server/usage/types.ts b/x-pack/plugins/actions/server/usage/types.ts index b58ac9c096f63..08cd14c7e7b95 100644 --- a/x-pack/plugins/actions/server/usage/types.ts +++ b/x-pack/plugins/actions/server/usage/types.ts @@ -22,8 +22,10 @@ export interface ActionsUsage { count_actions_executions_by_type_per_day: Record; count_actions_executions_failed_per_day: number; count_actions_executions_failed_by_type_per_day: Record; + count_connector_types_by_action_run_outcome_per_day: Record>; avg_execution_time_per_day: number; avg_execution_time_by_type_per_day: Record; + avg_actions_run_duration_by_connector_type_per_day: Record; } export const byTypeSchema: MakeSchemaFrom['count_by_type'] = { diff --git a/x-pack/plugins/alerting/server/usage/alerting_usage_collector.ts b/x-pack/plugins/alerting/server/usage/alerting_usage_collector.ts index f804a90afd21e..5efd2d7a49152 100644 --- a/x-pack/plugins/alerting/server/usage/alerting_usage_collector.ts +++ b/x-pack/plugins/alerting/server/usage/alerting_usage_collector.ts @@ -123,15 +123,6 @@ const byStatusPerDaySchema: MakeSchemaFrom['count_rules_by_execut unknown: { type: 'long' }, }; -const byConnectorTypeStatusPerDaySchema: MakeSchemaFrom['count_connector_types_by_action_run_outcome_per_day'] = - { - DYNAMIC_KEY: { - success: { type: 'long' }, - failure: { type: 'long' }, - unknown: { type: 'long' }, - }, - }; - const byNotifyWhenSchema: MakeSchemaFrom['count_rules_by_notify_when'] = { on_action_group_change: { type: 'long' }, on_active_alert: { type: 'long' }, @@ -217,14 +208,12 @@ export function createAlertingUsageCollector( count_rules_with_muted_alerts: 0, count_connector_types_by_consumers: {}, count_rules_by_execution_status_per_day: {}, - count_connector_types_by_action_run_outcome_per_day: {}, avg_execution_time_per_day: 0, avg_execution_time_by_type_per_day: {}, avg_es_search_duration_per_day: 0, avg_es_search_duration_by_type_per_day: {}, avg_total_search_duration_per_day: 0, avg_total_search_duration_by_type_per_day: {}, - avg_actions_run_duration_by_connector_type_per_day: {}, percentile_num_generated_actions_per_day: { p50: 0, p90: 0, @@ -303,14 +292,12 @@ export function createAlertingUsageCollector( count_rules_with_muted_alerts: { type: 'long' }, count_connector_types_by_consumers: { DYNAMIC_KEY: { DYNAMIC_KEY: { type: 'long' } } }, count_rules_by_execution_status_per_day: byStatusPerDaySchema, - count_connector_types_by_action_run_outcome_per_day: byConnectorTypeStatusPerDaySchema, avg_execution_time_per_day: { type: 'long' }, avg_execution_time_by_type_per_day: byTypeSchema, avg_es_search_duration_per_day: { type: 'long' }, avg_es_search_duration_by_type_per_day: byTypeSchema, avg_total_search_duration_per_day: { type: 'long' }, avg_total_search_duration_by_type_per_day: byTypeSchema, - avg_actions_run_duration_by_connector_type_per_day: { DYNAMIC_KEY: { type: 'long' } }, percentile_num_generated_actions_per_day: byPercentileSchema, percentile_num_generated_actions_by_type_per_day: byPercentileSchemaByType, percentile_num_alerts_per_day: byPercentileSchema, diff --git a/x-pack/plugins/alerting/server/usage/lib/get_telemetry_from_event_log.ts b/x-pack/plugins/alerting/server/usage/lib/get_telemetry_from_event_log.ts index bd9cd070685b7..e21df2376f4f8 100644 --- a/x-pack/plugins/alerting/server/usage/lib/get_telemetry_from_event_log.ts +++ b/x-pack/plugins/alerting/server/usage/lib/get_telemetry_from_event_log.ts @@ -16,12 +16,6 @@ import type { AggregationsBuckets, } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import { ElasticsearchClient, Logger } from '@kbn/core/server'; -import { - AvgActionRunDurationByConnectorTypeBucket, - AvgActionRunOutcomeByConnectorTypeBucket, - parseActionRunOutcomeByConnectorTypesBucket, - parseDurationsByConnectorTypesBucket, -} from './parse_connector_type_bucket'; import { NUM_ALERTING_RULE_TYPES, NUM_ALERTING_EXECUTION_FAILURE_REASON_TYPES, @@ -62,21 +56,12 @@ interface GetExecutionsPerDayCountResults { alertsPercentilesByType: Record>; countRulesByExecutionStatus: Record; } - -interface GetActionExecutionsTelemetryPerDay { - hasErrors: boolean; - errorMessage?: string; - avgRunDurationByConnectorType: Record; - countRunOutcomeByConnectorType: Record; -} - interface GetExecutionTimeoutsPerDayCountResults { hasErrors: boolean; errorMessage?: string; countExecutionTimeouts: number; countExecutionTimeoutsByType: Record; } - interface GetExecutionCountsExecutionFailures extends AggregationsSingleBucketAggregateBase { by_reason: AggregationsTermsAggregateBase; } @@ -232,117 +217,6 @@ export async function getExecutionsPerDayCount({ } } -export async function getActionExecutionsTelemetryPerDay({ - esClient, - eventLogIndex, - logger, -}: Opts): Promise { - try { - const eventLogAggs = { - avg_run_duration_by_connector_type: { - nested: { - path: 'kibana.saved_objects', - }, - aggs: { - connector_types: { - terms: { - field: 'kibana.saved_objects.type_id', - }, - aggs: { - duration: { - reverse_nested: {}, - aggs: { - average: { - avg: { - field: 'event.duration', - }, - }, - }, - }, - }, - }, - }, - }, - count_connector_types_by_action_run_outcome_per_day: { - nested: { - path: 'kibana.saved_objects', - }, - aggs: { - connector_types: { - terms: { - field: 'kibana.saved_objects.type_id', - }, - aggs: { - outcome: { - reverse_nested: {}, - aggs: { - count: { - terms: { - field: 'event.outcome', - }, - }, - }, - }, - }, - }, - }, - }, - }; - - const query = { - index: eventLogIndex, - size: 0, - body: { - query: getProviderAndActionFilterForTimeRange('execute', 'actions'), - aggs: eventLogAggs, - }, - }; - - logger.debug(`query for getActionExecutionsTelemetryPerDay - ${JSON.stringify(query)}`); - const results = await esClient.search(query); - - logger.debug( - `results for getActionExecutionsTelemetryPerDay query - ${JSON.stringify(results)}` - ); - - const aggregations = results.aggregations as { - avg_run_duration_by_connector_type: { - connector_types: AggregationsTermsAggregateBase; - }; - count_connector_types_by_action_run_outcome_per_day: { - connector_types: AggregationsTermsAggregateBase; - }; - }; - - return { - hasErrors: false, - avgRunDurationByConnectorType: parseDurationsByConnectorTypesBucket( - aggregations.avg_run_duration_by_connector_type.connector_types.buckets - ), - countRunOutcomeByConnectorType: parseActionRunOutcomeByConnectorTypesBucket( - aggregations.count_connector_types_by_action_run_outcome_per_day.connector_types.buckets - ), - }; - } catch (err) { - const errorMessage = err && err.message ? err.message : err.toString(); - logger.warn( - `Error executing alerting telemetry task: getActionExecutionsTelemetryPerDay - ${JSON.stringify( - err - )}`, - { - tags: ['alerting', 'telemetry-failed'], - error: { stack_trace: err.stack }, - } - ); - return { - hasErrors: true, - errorMessage, - avgRunDurationByConnectorType: {}, - countRunOutcomeByConnectorType: {}, - }; - } -} - export async function getExecutionTimeoutsPerDayCount({ esClient, eventLogIndex, diff --git a/x-pack/plugins/alerting/server/usage/task.ts b/x-pack/plugins/alerting/server/usage/task.ts index ca4f8e9cf2065..ab20844948011 100644 --- a/x-pack/plugins/alerting/server/usage/task.ts +++ b/x-pack/plugins/alerting/server/usage/task.ts @@ -16,7 +16,6 @@ import { import { getFailedAndUnrecognizedTasksPerDay } from './lib/get_telemetry_from_task_manager'; import { getTotalCountAggregations, getTotalCountInUse } from './lib/get_telemetry_from_kibana'; import { - getActionExecutionsTelemetryPerDay, getExecutionsPerDayCount, getExecutionTimeoutsPerDayCount, } from './lib/get_telemetry_from_event_log'; @@ -103,7 +102,6 @@ export function telemetryTaskRunner( getExecutionsPerDayCount({ esClient, eventLogIndex, logger }), getExecutionTimeoutsPerDayCount({ esClient, eventLogIndex, logger }), getFailedAndUnrecognizedTasksPerDay({ esClient, taskManagerIndex, logger }), - getActionExecutionsTelemetryPerDay({ esClient, eventLogIndex, logger }), ]) .then( ([ @@ -112,7 +110,6 @@ export function telemetryTaskRunner( dailyExecutionCounts, dailyExecutionTimeoutCounts, dailyFailedAndUnrecognizedTasks, - dailyActionExecutionAggregations, ]) => { const hasErrors = totalCountAggregations.hasErrors || @@ -184,10 +181,6 @@ export function telemetryTaskRunner( avg_total_search_duration_per_day: dailyExecutionCounts.avgTotalSearchDuration, avg_total_search_duration_by_type_per_day: dailyExecutionCounts.avgTotalSearchDurationByType, - avg_actions_run_duration_by_connector_type_per_day: - dailyActionExecutionAggregations.avgRunDurationByConnectorType, - count_connector_types_by_action_run_outcome_per_day: - dailyActionExecutionAggregations.countRunOutcomeByConnectorType, percentile_num_generated_actions_per_day: dailyExecutionCounts.generatedActionsPercentiles, percentile_num_generated_actions_by_type_per_day: diff --git a/x-pack/plugins/alerting/server/usage/types.ts b/x-pack/plugins/alerting/server/usage/types.ts index 652ca4f58664d..15c0f0a962710 100644 --- a/x-pack/plugins/alerting/server/usage/types.ts +++ b/x-pack/plugins/alerting/server/usage/types.ts @@ -43,7 +43,6 @@ export interface AlertingUsage { count_rules_muted: number; count_rules_with_muted_alerts: number; count_rules_by_execution_status_per_day: Record; - count_connector_types_by_action_run_outcome_per_day: Record>; percentile_num_generated_actions_per_day: { p50: number; p90: number; @@ -70,7 +69,6 @@ export interface AlertingUsage { avg_es_search_duration_by_type_per_day: Record; avg_total_search_duration_per_day: number; avg_total_search_duration_by_type_per_day: Record; - avg_actions_run_duration_by_connector_type_per_day: Record; throttle_time: { min: string; avg: string; diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts b/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts index cf7d7f5c74eb1..86f94e826d95f 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts @@ -25,7 +25,7 @@ export default function createAlertingAndActionsTelemetryTests({ getService }: F const esTestIndexTool = new ESTestIndexTool(es, retry); const supertestWithoutAuth = getService('supertestWithoutAuth'); - describe('telemetry xxx', () => { + describe('telemetry', () => { const alwaysFiringRuleId: { [key: string]: string } = {}; beforeEach(async () => { From 126f515d09067494ea2af392755810a7e1fa1b39 Mon Sep 17 00:00:00 2001 From: Ersin Erdal Date: Mon, 19 Sep 2022 20:38:04 +0200 Subject: [PATCH 04/11] fix test --- .../server/usage/actions_telemetry.test.ts | 66 ++++++++++ .../lib/get_telemetry_from_event_log.test.ts | 119 ------------------ 2 files changed, 66 insertions(+), 119 deletions(-) diff --git a/x-pack/plugins/actions/server/usage/actions_telemetry.test.ts b/x-pack/plugins/actions/server/usage/actions_telemetry.test.ts index b5606543b04e5..cb30d3e58663b 100644 --- a/x-pack/plugins/actions/server/usage/actions_telemetry.test.ts +++ b/x-pack/plugins/actions/server/usage/actions_telemetry.test.ts @@ -721,6 +721,56 @@ Object { }, }, }, + avg_run_duration_by_connector_type: { + connector_types: { + buckets: [ + { + key: '.slack', + duration: { + average: { + value: 10, + }, + }, + }, + { + key: '.email', + duration: { + average: { + value: 11, + }, + }, + }, + ], + }, + }, + count_connector_types_by_action_run_outcome_per_day: { + connector_types: { + buckets: [ + { + key: '.slack', + outcome: { + count: { + buckets: [ + { key: 'success', doc_count: 12 }, + { key: 'failure', doc_count: 1 }, + ], + }, + }, + }, + { + key: '.email', + outcome: { + count: { + buckets: [ + { key: 'success', doc_count: 13 }, + { key: 'failure', doc_count: 2 }, + ], + }, + }, + }, + ], + }, + }, }, } ); @@ -754,6 +804,20 @@ Object { __slack: 7, }, countTotal: 120, + avgRunDurationByConnectorType: { + __email: 11, + __slack: 10, + }, + countRunOutcomeByConnectorType: { + __email: { + failure: 2, + success: 13, + }, + __slack: { + failure: 1, + success: 12, + }, + }, hasErrors: false, }); }); @@ -772,9 +836,11 @@ Object { Object { "avgExecutionTime": 0, "avgExecutionTimeByType": Object {}, + "avgRunDurationByConnectorType": Object {}, "countByType": Object {}, "countFailed": 0, "countFailedByType": Object {}, + "countRunOutcomeByConnectorType": Object {}, "countTotal": 0, "errorMessage": "oh no", "hasErrors": true, diff --git a/x-pack/plugins/alerting/server/usage/lib/get_telemetry_from_event_log.test.ts b/x-pack/plugins/alerting/server/usage/lib/get_telemetry_from_event_log.test.ts index 622822810f732..64bc0ae8be0fb 100644 --- a/x-pack/plugins/alerting/server/usage/lib/get_telemetry_from_event_log.test.ts +++ b/x-pack/plugins/alerting/server/usage/lib/get_telemetry_from_event_log.test.ts @@ -13,7 +13,6 @@ import { parsePercentileAggs, parseExecutionCountAggregationResults, getExecutionTimeoutsPerDayCount, - getActionExecutionsTelemetryPerDay, } from './get_telemetry_from_event_log'; const elasticsearch = elasticsearchServiceMock.createStart(); @@ -1540,122 +1539,4 @@ describe('event log telemetry', () => { }); }); }); - - describe('getActionExecutionsTelemetryPerDay', () => { - test('should return telemetry data from actions eventLog ', async () => { - esClient.search.mockResponse({ - took: 4, - timed_out: false, - _shards: { - total: 1, - successful: 1, - skipped: 0, - failed: 0, - }, - hits: { - total: { - value: 148, - relation: 'eq', - }, - max_score: null, - hits: [], - }, - aggregations: { - avg_run_duration_by_connector_type: { - connector_types: { - buckets: [ - { - key: '.slack', - duration: { - average: { - value: 10, - }, - }, - }, - { - key: '.email', - duration: { - average: { - value: 11, - }, - }, - }, - ], - }, - }, - count_connector_types_by_action_run_outcome_per_day: { - connector_types: { - buckets: [ - { - key: '.slack', - outcome: { - count: { - buckets: [ - { key: 'success', doc_count: 12 }, - { key: 'failure', doc_count: 1 }, - ], - }, - }, - }, - { - key: '.email', - outcome: { - count: { - buckets: [ - { key: 'success', doc_count: 13 }, - { key: 'failure', doc_count: 2 }, - ], - }, - }, - }, - ], - }, - }, - }, - }); - - const telemetry = await getActionExecutionsTelemetryPerDay({ - esClient, - eventLogIndex: 'test', - logger, - }); - - expect(esClient.search).toHaveBeenCalledTimes(1); - - expect(telemetry).toStrictEqual({ - hasErrors: false, - avgRunDurationByConnectorType: { - __email: 11, - __slack: 10, - }, - countRunOutcomeByConnectorType: { - __email: { - failure: 2, - success: 13, - }, - __slack: { - failure: 1, - success: 12, - }, - }, - }); - }); - - test('should return empty results and log warning if query throws error', async () => { - esClient.search.mockRejectedValue(new Error('oh no')); - - const telemetry = await getActionExecutionsTelemetryPerDay({ - esClient, - eventLogIndex: 'test', - logger, - }); - - expect(telemetry).toEqual({ - avgRunDurationByConnectorType: {}, - countRunOutcomeByConnectorType: {}, - errorMessage: 'oh no', - hasErrors: true, - }); - }); - }); }); From 04598901d6792a67f778b15a5f5450f3c4e8e3c2 Mon Sep 17 00:00:00 2001 From: Ersin Erdal Date: Mon, 19 Sep 2022 20:49:11 +0200 Subject: [PATCH 05/11] fix schema --- .../server/usage/actions_usage_collector.ts | 19 +- .../schema/xpack_plugins.json | 174 +++++++++--------- 2 files changed, 95 insertions(+), 98 deletions(-) diff --git a/x-pack/plugins/actions/server/usage/actions_usage_collector.ts b/x-pack/plugins/actions/server/usage/actions_usage_collector.ts index a66ed2da67f80..72b4d3f5dbb4d 100644 --- a/x-pack/plugins/actions/server/usage/actions_usage_collector.ts +++ b/x-pack/plugins/actions/server/usage/actions_usage_collector.ts @@ -5,7 +5,7 @@ * 2.0. */ -import { MakeSchemaFrom, UsageCollectionSetup } from '@kbn/usage-collection-plugin/server'; +import { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server'; import { get } from 'lodash'; import { TaskManagerStartContract } from '@kbn/task-manager-plugin/server'; import { ActionsUsage, byServiceProviderTypeSchema, byTypeSchema } from './types'; @@ -16,15 +16,6 @@ export function createActionsUsageCollector( config: ActionsConfig, taskManager: Promise ) { - const byConnectorTypeStatusPerDaySchema: MakeSchemaFrom['count_connector_types_by_action_run_outcome_per_day'] = - { - DYNAMIC_KEY: { - success: { type: 'long' }, - failure: { type: 'long' }, - unknown: { type: 'long' }, - }, - }; - return usageCollection.makeUsageCollector({ type: 'actions', isReady: async () => { @@ -56,7 +47,13 @@ export function createActionsUsageCollector( count_actions_executions_failed_by_type_per_day: byTypeSchema, avg_execution_time_per_day: { type: 'long' }, avg_execution_time_by_type_per_day: byTypeSchema, - count_connector_types_by_action_run_outcome_per_day: byConnectorTypeStatusPerDaySchema, + count_connector_types_by_action_run_outcome_per_day: { + DYNAMIC_KEY: { + success: { type: 'long' }, + failure: { type: 'long' }, + unknown: { type: 'long' }, + }, + }, avg_actions_run_duration_by_connector_type_per_day: { DYNAMIC_KEY: { type: 'long' } }, }, fetch: async () => { diff --git a/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json b/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json index 6d8a1e1dcc1ac..38e439ebe1a06 100644 --- a/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json +++ b/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json @@ -272,6 +272,30 @@ "type": "long" } } + }, + "count_connector_types_by_action_run_outcome_per_day": { + "properties": { + "DYNAMIC_KEY": { + "properties": { + "success": { + "type": "long" + }, + "failure": { + "type": "long" + }, + "unknown": { + "type": "long" + } + } + } + } + }, + "avg_actions_run_duration_by_connector_type_per_day": { + "properties": { + "DYNAMIC_KEY": { + "type": "long" + } + } } } }, @@ -1713,23 +1737,6 @@ } } }, - "count_connector_types_by_action_run_outcome_per_day": { - "properties": { - "DYNAMIC_KEY": { - "properties": { - "success": { - "type": "long" - }, - "failure": { - "type": "long" - }, - "unknown": { - "type": "long" - } - } - } - } - }, "avg_execution_time_per_day": { "type": "long" }, @@ -2048,13 +2055,6 @@ } } }, - "avg_actions_run_duration_by_connector_type_per_day": { - "properties": { - "DYNAMIC_KEY": { - "type": "long" - } - } - }, "percentile_num_generated_actions_per_day": { "properties": { "p50": { @@ -2919,13 +2919,6 @@ } } }, - "cloud": { - "properties": { - "isCloudEnabled": { - "type": "boolean" - } - } - }, "cases": { "properties": { "cases": { @@ -3199,6 +3192,13 @@ } } }, + "cloud": { + "properties": { + "isCloudEnabled": { + "type": "boolean" + } + } + }, "fileUpload": { "properties": { "file_upload": { @@ -3606,62 +3606,6 @@ } } }, - "rollups": { - "properties": { - "index_patterns": { - "properties": { - "total": { - "type": "long", - "_meta": { - "description": "Counts all the rollup index patterns" - } - } - } - }, - "saved_searches": { - "properties": { - "total": { - "type": "long", - "_meta": { - "description": "Counts all the rollup saved searches" - } - } - } - }, - "visualizations": { - "properties": { - "saved_searches": { - "properties": { - "total": { - "type": "long", - "_meta": { - "description": "Counts all the visualizations that are based on rollup saved searches" - } - }, - "lens_total": { - "type": "long", - "_meta": { - "description": "Counts all the lens visualizations that are based on rollup saved searches" - } - } - } - }, - "total": { - "type": "long", - "_meta": { - "description": "Counts all the visualizations that are based on rollup index patterns" - } - }, - "lens_total": { - "type": "long", - "_meta": { - "description": "Counts all the lens visualizations that are based on rollup index patterns" - } - } - } - } - } - }, "reporting": { "properties": { "csv_searchsource": { @@ -6416,6 +6360,62 @@ } } }, + "rollups": { + "properties": { + "index_patterns": { + "properties": { + "total": { + "type": "long", + "_meta": { + "description": "Counts all the rollup index patterns" + } + } + } + }, + "saved_searches": { + "properties": { + "total": { + "type": "long", + "_meta": { + "description": "Counts all the rollup saved searches" + } + } + } + }, + "visualizations": { + "properties": { + "saved_searches": { + "properties": { + "total": { + "type": "long", + "_meta": { + "description": "Counts all the visualizations that are based on rollup saved searches" + } + }, + "lens_total": { + "type": "long", + "_meta": { + "description": "Counts all the lens visualizations that are based on rollup saved searches" + } + } + } + }, + "total": { + "type": "long", + "_meta": { + "description": "Counts all the visualizations that are based on rollup index patterns" + } + }, + "lens_total": { + "type": "long", + "_meta": { + "description": "Counts all the lens visualizations that are based on rollup index patterns" + } + } + } + } + } + }, "saved_objects_tagging": { "properties": { "usedTags": { From b1ded0a0b7cbb540a9af3d637cbe99b891a2b0aa Mon Sep 17 00:00:00 2001 From: Ersin Erdal Date: Mon, 19 Sep 2022 21:05:11 +0200 Subject: [PATCH 06/11] revert unnecessary changes on schema --- .../schema/xpack_plugins.json | 126 +++++++++--------- 1 file changed, 63 insertions(+), 63 deletions(-) diff --git a/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json b/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json index 38e439ebe1a06..b22be0eef3b7b 100644 --- a/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json +++ b/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json @@ -2919,6 +2919,13 @@ } } }, + "cloud": { + "properties": { + "isCloudEnabled": { + "type": "boolean" + } + } + }, "cases": { "properties": { "cases": { @@ -3192,13 +3199,6 @@ } } }, - "cloud": { - "properties": { - "isCloudEnabled": { - "type": "boolean" - } - } - }, "fileUpload": { "properties": { "file_upload": { @@ -3606,6 +3606,62 @@ } } }, + "rollups": { + "properties": { + "index_patterns": { + "properties": { + "total": { + "type": "long", + "_meta": { + "description": "Counts all the rollup index patterns" + } + } + } + }, + "saved_searches": { + "properties": { + "total": { + "type": "long", + "_meta": { + "description": "Counts all the rollup saved searches" + } + } + } + }, + "visualizations": { + "properties": { + "saved_searches": { + "properties": { + "total": { + "type": "long", + "_meta": { + "description": "Counts all the visualizations that are based on rollup saved searches" + } + }, + "lens_total": { + "type": "long", + "_meta": { + "description": "Counts all the lens visualizations that are based on rollup saved searches" + } + } + } + }, + "total": { + "type": "long", + "_meta": { + "description": "Counts all the visualizations that are based on rollup index patterns" + } + }, + "lens_total": { + "type": "long", + "_meta": { + "description": "Counts all the lens visualizations that are based on rollup index patterns" + } + } + } + } + } + }, "reporting": { "properties": { "csv_searchsource": { @@ -6360,62 +6416,6 @@ } } }, - "rollups": { - "properties": { - "index_patterns": { - "properties": { - "total": { - "type": "long", - "_meta": { - "description": "Counts all the rollup index patterns" - } - } - } - }, - "saved_searches": { - "properties": { - "total": { - "type": "long", - "_meta": { - "description": "Counts all the rollup saved searches" - } - } - } - }, - "visualizations": { - "properties": { - "saved_searches": { - "properties": { - "total": { - "type": "long", - "_meta": { - "description": "Counts all the visualizations that are based on rollup saved searches" - } - }, - "lens_total": { - "type": "long", - "_meta": { - "description": "Counts all the lens visualizations that are based on rollup saved searches" - } - } - } - }, - "total": { - "type": "long", - "_meta": { - "description": "Counts all the visualizations that are based on rollup index patterns" - } - }, - "lens_total": { - "type": "long", - "_meta": { - "description": "Counts all the lens visualizations that are based on rollup index patterns" - } - } - } - } - } - }, "saved_objects_tagging": { "properties": { "usedTags": { From 693cc89d2e31df79e6f1327f20e1a6e8e79ada80 Mon Sep 17 00:00:00 2001 From: Ersin Erdal Date: Mon, 19 Sep 2022 21:38:36 +0200 Subject: [PATCH 07/11] use replaceFirstAndLastDotSymbols --- x-pack/plugins/actions/server/usage/actions_telemetry.ts | 2 +- .../server/usage/lib/parse_connector_type_bucket.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/x-pack/plugins/actions/server/usage/actions_telemetry.ts b/x-pack/plugins/actions/server/usage/actions_telemetry.ts index 2601990af8c33..c1813bb9b5a95 100644 --- a/x-pack/plugins/actions/server/usage/actions_telemetry.ts +++ b/x-pack/plugins/actions/server/usage/actions_telemetry.ts @@ -402,7 +402,7 @@ export async function getInUseTotalCount( } } -function replaceFirstAndLastDotSymbols(strToReplace: string) { +export function replaceFirstAndLastDotSymbols(strToReplace: string) { const hasFirstSymbolDot = strToReplace.startsWith('.'); const appliedString = hasFirstSymbolDot ? strToReplace.replace('.', '__') : strToReplace; const hasLastSymbolDot = strToReplace.endsWith('.'); diff --git a/x-pack/plugins/actions/server/usage/lib/parse_connector_type_bucket.ts b/x-pack/plugins/actions/server/usage/lib/parse_connector_type_bucket.ts index 1539acf7126e7..61825a6b8c366 100644 --- a/x-pack/plugins/actions/server/usage/lib/parse_connector_type_bucket.ts +++ b/x-pack/plugins/actions/server/usage/lib/parse_connector_type_bucket.ts @@ -6,7 +6,7 @@ */ import { AggregationsBuckets } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; -import { replaceDotSymbols } from '@kbn/alerting-plugin/server/usage/lib/replace_dots_with_underscores'; +import { replaceFirstAndLastDotSymbols } from '../actions_telemetry'; export interface AvgActionRunDurationByConnectorTypeBucket { key: string; @@ -21,7 +21,7 @@ export function parseDurationsByConnectorTypesBucket( return buckets.reduce((acc, connectorType) => { return { ...acc, - [replaceDotSymbols(connectorType.key)]: Math.round( + [replaceFirstAndLastDotSymbols(connectorType.key)]: Math.round( connectorType.duration?.average?.value || 0 ), }; @@ -42,8 +42,8 @@ export function parseActionRunOutcomeByConnectorTypesBucket( const outcomes = connectorType.outcome?.count?.buckets ?? []; return { ...acc, - [replaceDotSymbols(connectorType.key)]: outcomes.reduce((accBucket, bucket) => { - return { ...accBucket, [replaceDotSymbols(bucket.key)]: bucket.doc_count || 0 }; + [replaceFirstAndLastDotSymbols(connectorType.key)]: outcomes.reduce((accBucket, bucket) => { + return { ...accBucket, [replaceFirstAndLastDotSymbols(bucket.key)]: bucket.doc_count || 0 }; }, {}), }; }, {}); From de4a05270c0d7d93be43da14558cd02b2b50cf54 Mon Sep 17 00:00:00 2001 From: Ersin Erdal Date: Tue, 20 Sep 2022 13:42:29 +0200 Subject: [PATCH 08/11] fix task interval --- x-pack/plugins/actions/server/usage/task.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/actions/server/usage/task.ts b/x-pack/plugins/actions/server/usage/task.ts index c6eac09614cfe..3565892a9186f 100644 --- a/x-pack/plugins/actions/server/usage/task.ts +++ b/x-pack/plugins/actions/server/usage/task.ts @@ -144,5 +144,5 @@ export function telemetryTaskRunner( } function getNextMidnight() { - return moment().add(1, 'm').startOf('m').toDate(); + return moment().add(1, 'd').startOf('d').toDate(); } From b9c8252ea77f2b3b3aca77b30c4b3748f1c954df Mon Sep 17 00:00:00 2001 From: Ersin Erdal Date: Tue, 20 Sep 2022 14:48:26 +0200 Subject: [PATCH 09/11] remove avg_actions_run_duration_by_connector_type_per_day --- .../server/usage/actions_telemetry.test.ts | 70 ++++++------------- .../actions/server/usage/actions_telemetry.ts | 66 ++++++----------- .../server/usage/actions_usage_collector.ts | 2 - .../lib/parse_connector_type_bucket.test.ts | 69 +----------------- .../usage/lib/parse_connector_type_bucket.ts | 20 ------ x-pack/plugins/actions/server/usage/task.ts | 2 - x-pack/plugins/actions/server/usage/types.ts | 1 - .../alerting_and_actions_telemetry.ts | 24 ++----- 8 files changed, 49 insertions(+), 205 deletions(-) diff --git a/x-pack/plugins/actions/server/usage/actions_telemetry.test.ts b/x-pack/plugins/actions/server/usage/actions_telemetry.test.ts index cb30d3e58663b..7a471d58fa75b 100644 --- a/x-pack/plugins/actions/server/usage/actions_telemetry.test.ts +++ b/x-pack/plugins/actions/server/usage/actions_telemetry.test.ts @@ -721,54 +721,34 @@ Object { }, }, }, - avg_run_duration_by_connector_type: { - connector_types: { - buckets: [ - { - key: '.slack', - duration: { - average: { - value: 10, - }, - }, - }, - { - key: '.email', - duration: { - average: { - value: 11, - }, - }, - }, - ], - }, - }, count_connector_types_by_action_run_outcome_per_day: { - connector_types: { - buckets: [ - { - key: '.slack', - outcome: { - count: { - buckets: [ - { key: 'success', doc_count: 12 }, - { key: 'failure', doc_count: 1 }, - ], + actionSavedObjects: { + connector_types: { + buckets: [ + { + key: '.slack', + outcome: { + count: { + buckets: [ + { key: 'success', doc_count: 12 }, + { key: 'failure', doc_count: 1 }, + ], + }, }, }, - }, - { - key: '.email', - outcome: { - count: { - buckets: [ - { key: 'success', doc_count: 13 }, - { key: 'failure', doc_count: 2 }, - ], + { + key: '.email', + outcome: { + count: { + buckets: [ + { key: 'success', doc_count: 13 }, + { key: 'failure', doc_count: 2 }, + ], + }, }, }, - }, - ], + ], + }, }, }, }, @@ -804,10 +784,6 @@ Object { __slack: 7, }, countTotal: 120, - avgRunDurationByConnectorType: { - __email: 11, - __slack: 10, - }, countRunOutcomeByConnectorType: { __email: { failure: 2, diff --git a/x-pack/plugins/actions/server/usage/actions_telemetry.ts b/x-pack/plugins/actions/server/usage/actions_telemetry.ts index c1813bb9b5a95..15d2d6f4d12e5 100644 --- a/x-pack/plugins/actions/server/usage/actions_telemetry.ts +++ b/x-pack/plugins/actions/server/usage/actions_telemetry.ts @@ -9,10 +9,8 @@ import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types'; import { ElasticsearchClient, Logger } from '@kbn/core/server'; import { AggregationsTermsAggregateBase } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import { - AvgActionRunDurationByConnectorTypeBucket, AvgActionRunOutcomeByConnectorTypeBucket, parseActionRunOutcomeByConnectorTypesBucket, - parseDurationsByConnectorTypesBucket, } from './lib/parse_connector_type_bucket'; import { AlertHistoryEsIndexConnectorId } from '../../common'; import { ActionResult, PreConfiguredAction } from '../types'; @@ -422,7 +420,6 @@ export async function getExecutionsPerDayCount( countFailedByType: Record; avgExecutionTime: number; avgExecutionTimeByType: Record; - avgRunDurationByConnectorType: Record; countRunOutcomeByConnectorType: Record; }> { const scriptedMetric = { @@ -545,46 +542,27 @@ export async function getExecutionsPerDayCount( }, }, }, - avg_run_duration_by_connector_type: { - nested: { - path: 'kibana.saved_objects', - }, - aggs: { - connector_types: { - terms: { - field: 'kibana.saved_objects.type_id', - }, - aggs: { - duration: { - reverse_nested: {}, - aggs: { - average: { - avg: { - field: 'event.duration', - }, - }, - }, - }, - }, - }, - }, - }, count_connector_types_by_action_run_outcome_per_day: { nested: { path: 'kibana.saved_objects', }, aggs: { - connector_types: { - terms: { - field: 'kibana.saved_objects.type_id', - }, + actionSavedObjects: { + filter: { term: { 'kibana.saved_objects.type': 'action' } }, aggs: { - outcome: { - reverse_nested: {}, + connector_types: { + terms: { + field: 'kibana.saved_objects.type_id', + }, aggs: { - count: { - terms: { - field: 'event.outcome', + outcome: { + reverse_nested: {}, + aggs: { + count: { + terms: { + field: 'event.outcome', + }, + }, }, }, }, @@ -621,12 +599,11 @@ export async function getExecutionsPerDayCount( {} ); - const aggregations = actionResults.aggregations as { - avg_run_duration_by_connector_type: { - connector_types: AggregationsTermsAggregateBase; - }; + const aggsCountConnectorTypeByActionRun = actionResults.aggregations as { count_connector_types_by_action_run_outcome_per_day: { - connector_types: AggregationsTermsAggregateBase; + actionSavedObjects: { + connector_types: AggregationsTermsAggregateBase; + }; }; }; @@ -652,11 +629,9 @@ export async function getExecutionsPerDayCount( ), avgExecutionTime: aggsAvgExecutionTime, avgExecutionTimeByType, - avgRunDurationByConnectorType: parseDurationsByConnectorTypesBucket( - aggregations.avg_run_duration_by_connector_type.connector_types.buckets - ), countRunOutcomeByConnectorType: parseActionRunOutcomeByConnectorTypesBucket( - aggregations.count_connector_types_by_action_run_outcome_per_day.connector_types.buckets + aggsCountConnectorTypeByActionRun.count_connector_types_by_action_run_outcome_per_day + .actionSavedObjects.connector_types.buckets ), }; } catch (err) { @@ -673,7 +648,6 @@ export async function getExecutionsPerDayCount( countFailedByType: {}, avgExecutionTime: 0, avgExecutionTimeByType: {}, - avgRunDurationByConnectorType: {}, countRunOutcomeByConnectorType: {}, }; } diff --git a/x-pack/plugins/actions/server/usage/actions_usage_collector.ts b/x-pack/plugins/actions/server/usage/actions_usage_collector.ts index 72b4d3f5dbb4d..f49525a4eec05 100644 --- a/x-pack/plugins/actions/server/usage/actions_usage_collector.ts +++ b/x-pack/plugins/actions/server/usage/actions_usage_collector.ts @@ -54,7 +54,6 @@ export function createActionsUsageCollector( unknown: { type: 'long' }, }, }, - avg_actions_run_duration_by_connector_type_per_day: { DYNAMIC_KEY: { type: 'long' } }, }, fetch: async () => { try { @@ -86,7 +85,6 @@ export function createActionsUsageCollector( avg_execution_time_per_day: 0, avg_execution_time_by_type_per_day: {}, count_connector_types_by_action_run_outcome_per_day: {}, - avg_actions_run_duration_by_connector_type_per_day: {}, }; } }, diff --git a/x-pack/plugins/actions/server/usage/lib/parse_connector_type_bucket.test.ts b/x-pack/plugins/actions/server/usage/lib/parse_connector_type_bucket.test.ts index f43bc43720058..1241cddfeb55e 100644 --- a/x-pack/plugins/actions/server/usage/lib/parse_connector_type_bucket.test.ts +++ b/x-pack/plugins/actions/server/usage/lib/parse_connector_type_bucket.test.ts @@ -5,74 +5,7 @@ * 2.0. */ -import { - parseDurationsByConnectorTypesBucket, - parseActionRunOutcomeByConnectorTypesBucket, -} from './parse_connector_type_bucket'; - -describe('parseDurationsByConnectorTypesBucket', () => { - test('should correctly parse connector type bucket results', () => { - expect( - parseDurationsByConnectorTypesBucket([ - { - key: '.server-log', - doc_count: 78, - duration: { average: { value: 10.2 } }, - }, - { - key: '.index', - doc_count: 42, - duration: { average: { value: 11.6 } }, - }, - { - key: '.slack', - doc_count: 28, - duration: { average: { value: 12.4 } }, - }, - ]) - ).toEqual({ - '__server-log': 10, - __index: 12, - __slack: 12, - }); - }); - - test('should handle missing values', () => { - expect( - parseDurationsByConnectorTypesBucket([ - // @ts-expect-error - { - key: '.server-log', - doc_count: 78, - }, - { - key: '.index', - doc_count: 42, - // @ts-expect-error - duration: {}, - }, - { - key: '.slack', - doc_count: 28, - // @ts-expect-error - duration: { average: {} }, - }, - ]) - ).toEqual({ - '__server-log': 0, - __index: 0, - __slack: 0, - }); - }); - - test('should handle empty input', () => { - expect(parseDurationsByConnectorTypesBucket([])).toEqual({}); - }); - // - test('should handle undefined input', () => { - expect(parseDurationsByConnectorTypesBucket(undefined)).toEqual({}); - }); -}); +import { parseActionRunOutcomeByConnectorTypesBucket } from './parse_connector_type_bucket'; describe('parseActionRunOutcomeByConnectorTypesBucket', () => { test('should correctly parse connector type bucket results', () => { diff --git a/x-pack/plugins/actions/server/usage/lib/parse_connector_type_bucket.ts b/x-pack/plugins/actions/server/usage/lib/parse_connector_type_bucket.ts index 61825a6b8c366..96e1610c635d8 100644 --- a/x-pack/plugins/actions/server/usage/lib/parse_connector_type_bucket.ts +++ b/x-pack/plugins/actions/server/usage/lib/parse_connector_type_bucket.ts @@ -8,26 +8,6 @@ import { AggregationsBuckets } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import { replaceFirstAndLastDotSymbols } from '../actions_telemetry'; -export interface AvgActionRunDurationByConnectorTypeBucket { - key: string; - doc_count: number; // Not used for duration telemetry but can be helpful later. - duration: { average: { value: number } }; -} - -export function parseDurationsByConnectorTypesBucket( - connectorTypeBuckets: AggregationsBuckets = [] -) { - const buckets = connectorTypeBuckets as AvgActionRunDurationByConnectorTypeBucket[]; - return buckets.reduce((acc, connectorType) => { - return { - ...acc, - [replaceFirstAndLastDotSymbols(connectorType.key)]: Math.round( - connectorType.duration?.average?.value || 0 - ), - }; - }, {}); -} - export interface AvgActionRunOutcomeByConnectorTypeBucket { key: string; doc_count: number; // Not used for duration telemetry but can be helpful later. diff --git a/x-pack/plugins/actions/server/usage/task.ts b/x-pack/plugins/actions/server/usage/task.ts index 3565892a9186f..e8861b30ae8b4 100644 --- a/x-pack/plugins/actions/server/usage/task.ts +++ b/x-pack/plugins/actions/server/usage/task.ts @@ -130,8 +130,6 @@ export function telemetryTaskRunner( totalExecutionsPerDay.countFailedByType, avg_execution_time_per_day: totalExecutionsPerDay.avgExecutionTime, avg_execution_time_by_type_per_day: totalExecutionsPerDay.avgExecutionTimeByType, - avg_actions_run_duration_by_connector_type_per_day: - totalExecutionsPerDay.avgRunDurationByConnectorType, count_connector_types_by_action_run_outcome_per_day: totalExecutionsPerDay.countRunOutcomeByConnectorType, }, diff --git a/x-pack/plugins/actions/server/usage/types.ts b/x-pack/plugins/actions/server/usage/types.ts index 08cd14c7e7b95..62ee11af72f8f 100644 --- a/x-pack/plugins/actions/server/usage/types.ts +++ b/x-pack/plugins/actions/server/usage/types.ts @@ -25,7 +25,6 @@ export interface ActionsUsage { count_connector_types_by_action_run_outcome_per_day: Record>; avg_execution_time_per_day: number; avg_execution_time_by_type_per_day: Record; - avg_actions_run_duration_by_connector_type_per_day: Record; } export const byTypeSchema: MakeSchemaFrom['count_by_type'] = { diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts b/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts index 86f94e826d95f..85e143f44f854 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts @@ -25,7 +25,7 @@ export default function createAlertingAndActionsTelemetryTests({ getService }: F const esTestIndexTool = new ESTestIndexTool(es, retry); const supertestWithoutAuth = getService('supertestWithoutAuth'); - describe('telemetry', () => { + describe('telemetry xxx', () => { const alwaysFiringRuleId: { [key: string]: string } = {}; beforeEach(async () => { @@ -246,6 +246,10 @@ export default function createAlertingAndActionsTelemetryTests({ getService }: F expect(telemetry.count_actions_executions_failed_by_type_per_day['test.throw'] > 0).to.be( true ); + + expect( + telemetry.count_connector_types_by_action_run_outcome_per_day['test.throw'].failure + ).to.greaterThan(0); } function verifyAlertingTelemetry(telemetry: any) { @@ -529,24 +533,6 @@ export default function createAlertingAndActionsTelemetryTests({ getService }: F alertsFixture: { test__noop: 9, test__throw: 9, __slack: 3 }, }); - expect( - telemetry.avg_actions_run_duration_by_connector_type_per_day['test__cumulative-firing'] - ).to.be.greaterThan(0); - expect( - telemetry.avg_actions_run_duration_by_connector_type_per_day.test__throw - ).to.be.greaterThan(0); - - expect( - telemetry.count_connector_types_by_action_run_outcome_per_day['test__cumulative-firing'] - .failure - ).to.greaterThan(0); - expect( - telemetry.count_connector_types_by_action_run_outcome_per_day.test__throw.failure - ).to.greaterThan(0); - - expect(telemetry.count_rules_by_execution_status_per_day.failure).to.greaterThan(0); - expect(telemetry.count_rules_by_execution_status_per_day.success).to.greaterThan(0); - expect(telemetry.count_rules_by_execution_status_per_day.failure).to.greaterThan(0); expect(telemetry.count_rules_by_execution_status_per_day.success).to.greaterThan(0); } From 6632748ea6cbef2894a2639f719279917e292270 Mon Sep 17 00:00:00 2001 From: Ersin Erdal Date: Tue, 20 Sep 2022 14:57:56 +0200 Subject: [PATCH 10/11] remove avg_actions_run_duration_by_connector_type_per_day --- x-pack/plugins/actions/server/usage/actions_telemetry.test.ts | 1 - .../group2/tests/telemetry/alerting_and_actions_telemetry.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/x-pack/plugins/actions/server/usage/actions_telemetry.test.ts b/x-pack/plugins/actions/server/usage/actions_telemetry.test.ts index 7a471d58fa75b..7216f627ea783 100644 --- a/x-pack/plugins/actions/server/usage/actions_telemetry.test.ts +++ b/x-pack/plugins/actions/server/usage/actions_telemetry.test.ts @@ -812,7 +812,6 @@ Object { Object { "avgExecutionTime": 0, "avgExecutionTimeByType": Object {}, - "avgRunDurationByConnectorType": Object {}, "countByType": Object {}, "countFailed": 0, "countFailedByType": Object {}, diff --git a/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts b/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts index 85e143f44f854..c89e5b48b236b 100644 --- a/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts +++ b/x-pack/test/alerting_api_integration/security_and_spaces/group2/tests/telemetry/alerting_and_actions_telemetry.ts @@ -25,7 +25,7 @@ export default function createAlertingAndActionsTelemetryTests({ getService }: F const esTestIndexTool = new ESTestIndexTool(es, retry); const supertestWithoutAuth = getService('supertestWithoutAuth'); - describe('telemetry xxx', () => { + describe('telemetry', () => { const alwaysFiringRuleId: { [key: string]: string } = {}; beforeEach(async () => { From 4985a53691ed51c3b29dea730f6a3424da0128b9 Mon Sep 17 00:00:00 2001 From: Ersin Erdal Date: Tue, 20 Sep 2022 15:09:43 +0200 Subject: [PATCH 11/11] fix telemetry schema --- .../telemetry_collection_xpack/schema/xpack_plugins.json | 7 ------- 1 file changed, 7 deletions(-) diff --git a/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json b/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json index 2dcc44e603f94..43c70ee6c8125 100644 --- a/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json +++ b/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json @@ -282,13 +282,6 @@ } } } - }, - "avg_actions_run_duration_by_connector_type_per_day": { - "properties": { - "DYNAMIC_KEY": { - "type": "long" - } - } } } },