From ceb38e7063a4d80e1fca6ab438b3a52801676346 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Tue, 18 Jun 2024 13:15:04 -0400 Subject: [PATCH] [Fleet] Add global data tags telemetry (#186390) --- .../fleet/server/collectors/agent_policies.ts | 20 +++++++++++++++++++ .../fleet_usage_telemetry.test.ts | 10 ++++++++++ .../services/telemetry/fleet_usages_schema.ts | 13 ++++++++++++ 3 files changed, 43 insertions(+) diff --git a/x-pack/plugins/fleet/server/collectors/agent_policies.ts b/x-pack/plugins/fleet/server/collectors/agent_policies.ts index d70524e5f1e32..5210e7f3b229d 100644 --- a/x-pack/plugins/fleet/server/collectors/agent_policies.ts +++ b/x-pack/plugins/fleet/server/collectors/agent_policies.ts @@ -18,6 +18,8 @@ import type { OutputSOAttributes, AgentPolicy } from '../types'; export interface AgentPoliciesUsage { count: number; output_types: string[]; + count_with_global_data_tags: number; + avg_number_global_data_tags_per_policy?: number; } export const getAgentPoliciesUsage = async ( @@ -52,8 +54,26 @@ export const getAgentPoliciesUsage = async ( }) ); + const [policiesWithGlobalDataTag, totalNumberOfGlobalDataTagFields] = agentPolicies.reduce( + ([policiesNumber, fieldsNumber], agentPolicy) => { + if (agentPolicy.attributes.global_data_tags?.length ?? 0 > 0) { + return [ + policiesNumber + 1, + fieldsNumber + (agentPolicy.attributes.global_data_tags?.length ?? 0), + ]; + } + return [policiesNumber, fieldsNumber]; + }, + [0, 0] + ); + return { count: totalAgentPolicies, output_types: Array.from(uniqueOutputTypes), + count_with_global_data_tags: policiesWithGlobalDataTag, + avg_number_global_data_tags_per_policy: + policiesWithGlobalDataTag > 0 + ? Math.round(totalNumberOfGlobalDataTagFields / policiesWithGlobalDataTag) + : undefined, }; }; diff --git a/x-pack/plugins/fleet/server/integration_tests/fleet_usage_telemetry.test.ts b/x-pack/plugins/fleet/server/integration_tests/fleet_usage_telemetry.test.ts index 3ee659793f53f..77b3278bad39a 100644 --- a/x-pack/plugins/fleet/server/integration_tests/fleet_usage_telemetry.test.ts +++ b/x-pack/plugins/fleet/server/integration_tests/fleet_usage_telemetry.test.ts @@ -428,6 +428,10 @@ describe('fleet usage telemetry', () => { schema_version: '1.0.0', data_output_id: 'output2', monitoring_output_id: 'output3', + global_data_tags: [ + { name: 'test', value: 'test1' }, + { name: 'test2', value: 'test2' }, + ], }, { id: 'policy2' } ); @@ -446,6 +450,10 @@ describe('fleet usage telemetry', () => { schema_version: '1.0.0', data_output_id: 'output4', monitoring_output_id: 'output4', + global_data_tags: [ + { name: 'test', value: 'test1' }, + { name: 'test2', value: 'test2' }, + ], }, { id: 'policy3' } ); @@ -566,6 +574,8 @@ describe('fleet usage telemetry', () => { agent_policies: { count: 3, output_types: expect.arrayContaining(['elasticsearch', 'logstash', 'third_type']), + count_with_global_data_tags: 2, + avg_number_global_data_tags_per_policy: 2, }, agent_logs_panics_last_hour: [ { diff --git a/x-pack/plugins/fleet/server/services/telemetry/fleet_usages_schema.ts b/x-pack/plugins/fleet/server/services/telemetry/fleet_usages_schema.ts index 35138145a7141..1720470b65ad8 100644 --- a/x-pack/plugins/fleet/server/services/telemetry/fleet_usages_schema.ts +++ b/x-pack/plugins/fleet/server/services/telemetry/fleet_usages_schema.ts @@ -346,6 +346,19 @@ export const fleetUsagesSchema: RootSchema = { _meta: { description: 'Output types of agent policies' }, }, }, + count_with_global_data_tags: { + type: 'long', + _meta: { + description: 'Number of agent policies using global data tags', + }, + }, + avg_number_global_data_tags_per_policy: { + type: 'long', + _meta: { + description: + 'Average number of global data tags defined per agent policy (accross policies using global data tags)', + }, + }, }, }, agent_checkin_status: {