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 f999c1f497344..cc7e743cd1055 100644 --- a/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json +++ b/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json @@ -14426,6 +14426,40 @@ } } }, + "transform": { + "properties": { + "alertRules": { + "properties": { + "transform_health": { + "properties": { + "count_by_check_type": { + "properties": { + "notStarted": { + "type": "long", + "_meta": { + "description": "total number of alerting rules performing the not started health check" + } + }, + "errorMessages": { + "type": "long", + "_meta": { + "description": "total number of alerting rules performing the error message health check" + } + }, + "healthCheck": { + "type": "long", + "_meta": { + "description": "total number of alerting rules performing the health check with the stats API" + } + } + } + } + } + } + } + } + } + }, "upgrade-assistant-telemetry": { "properties": { "features": { diff --git a/x-pack/plugins/transform/server/lib/alerting/transform_health_rule_type/register_transform_health_rule_type.ts b/x-pack/plugins/transform/server/lib/alerting/transform_health_rule_type/register_transform_health_rule_type.ts index 9128c91c1f965..1be8410b84786 100644 --- a/x-pack/plugins/transform/server/lib/alerting/transform_health_rule_type/register_transform_health_rule_type.ts +++ b/x-pack/plugins/transform/server/lib/alerting/transform_health_rule_type/register_transform_health_rule_type.ts @@ -13,9 +13,9 @@ import type { AlertInstanceState, RuleTypeState, } from '@kbn/alerting-plugin/common'; -import { RuleType } from '@kbn/alerting-plugin/server'; +import type { RuleType } from '@kbn/alerting-plugin/server'; import type { PluginSetupContract as AlertingSetup } from '@kbn/alerting-plugin/server'; -import { FieldFormatsStart } from '@kbn/field-formats-plugin/server'; +import type { FieldFormatsStart } from '@kbn/field-formats-plugin/server'; import { PLUGIN, type TransformHealth, TRANSFORM_RULE_TYPE } from '../../../../common/constants'; import { transformHealthRuleParams, TransformHealthRuleParams } from './schema'; import { transformHealthServiceProvider } from './transform_health_service'; diff --git a/x-pack/plugins/transform/server/plugin.ts b/x-pack/plugins/transform/server/plugin.ts index 95d80f3545c42..91759dfb1a914 100644 --- a/x-pack/plugins/transform/server/plugin.ts +++ b/x-pack/plugins/transform/server/plugin.ts @@ -10,6 +10,7 @@ import { CoreSetup, CoreStart, Plugin, Logger, PluginInitializerContext } from ' import { LicenseType } from '@kbn/licensing-plugin/common/types'; +import { registerCollector } from './usage'; import { setupCapabilities } from './capabilities'; import { PluginSetupDependencies, PluginStartDependencies } from './types'; import { registerRoutes } from './routes'; @@ -38,7 +39,13 @@ export class TransformServerPlugin implements Plugin<{}, void, any, any> { setup( coreSetup: CoreSetup, - { licensing, features, alerting, security: securitySetup }: PluginSetupDependencies + { + licensing, + features, + alerting, + security: securitySetup, + usageCollection, + }: PluginSetupDependencies ): {} { const { http, getStartServices } = coreSetup; @@ -77,6 +84,12 @@ export class TransformServerPlugin implements Plugin<{}, void, any, any> { coreStart, security: securityStart, }); + + const alertIndex = coreStart.savedObjects.getIndexForType('alert'); + + if (usageCollection) { + registerCollector(usageCollection, alertIndex); + } }); if (alerting) { diff --git a/x-pack/plugins/transform/server/types.ts b/x-pack/plugins/transform/server/types.ts index 6aa17d59db353..e86a750b93618 100644 --- a/x-pack/plugins/transform/server/types.ts +++ b/x-pack/plugins/transform/server/types.ts @@ -12,6 +12,7 @@ import type { PluginSetupContract as FeaturesPluginSetup } from '@kbn/features-p import type { AlertingPlugin } from '@kbn/alerting-plugin/server'; import type { FieldFormatsSetup, FieldFormatsStart } from '@kbn/field-formats-plugin/server'; import type { SecurityPluginSetup, SecurityPluginStart } from '@kbn/security-plugin/server'; +import { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server'; import type { License } from './services'; export interface PluginSetupDependencies { @@ -20,6 +21,7 @@ export interface PluginSetupDependencies { alerting?: AlertingPlugin['setup']; fieldFormats: FieldFormatsSetup; security?: SecurityPluginSetup; + usageCollection?: UsageCollectionSetup; } export interface PluginStartDependencies { diff --git a/x-pack/plugins/transform/server/usage/collector.ts b/x-pack/plugins/transform/server/usage/collector.ts new file mode 100644 index 0000000000000..1c386c55ee8dd --- /dev/null +++ b/x-pack/plugins/transform/server/usage/collector.ts @@ -0,0 +1,120 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import type { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server'; +import { getResultTestConfig } from '../../common/utils/alerts'; +import { TransformHealthRuleParams } from '../../common/types/alerting'; +import { TRANSFORM_RULE_TYPE } from '../../common'; + +export interface TransformAlertsUsageData { + alertRules: { + transform_health: { + count_by_check_type: { + notStarted: number; + errorMessages: number; + healthCheck: number; + }; + }; + }; +} + +export function registerCollector(usageCollection: UsageCollectionSetup, alertIndex: string) { + const collector = usageCollection.makeUsageCollector({ + type: 'transform', + schema: { + alertRules: { + transform_health: { + count_by_check_type: { + notStarted: { + type: 'long', + _meta: { + description: + 'total number of alerting rules performing the not started health check', + }, + }, + errorMessages: { + type: 'long', + _meta: { + description: + 'total number of alerting rules performing the error message health check', + }, + }, + healthCheck: { + type: 'long', + _meta: { + description: + 'total number of alerting rules performing the health check with the stats API', + }, + }, + }, + }, + }, + }, + isReady: () => true, + fetch: async ({ esClient }) => { + const transformHealthRuleInstances = await esClient.search<{ + alert: { + params: TransformHealthRuleParams; + }; + }>( + { + index: alertIndex, + size: 10000, + query: { + bool: { + filter: [ + { term: { type: 'alert' } }, + { + term: { + 'alert.alertTypeId': TRANSFORM_RULE_TYPE.TRANSFORM_HEALTH, + }, + }, + ], + }, + }, + }, + { maxRetries: 0 } + ); + + const resultsByCheckType = transformHealthRuleInstances.hits.hits.reduce( + (acc, curr) => { + const doc = curr._source; + if (!doc) return acc; + + const { + alert: { + params: { testsConfig }, + }, + } = doc; + + const resultConfig = getResultTestConfig(testsConfig); + + acc.notStarted += resultConfig?.notStarted?.enabled ? 1 : 0; + acc.errorMessages += resultConfig?.errorMessages?.enabled ? 1 : 0; + acc.healthCheck += resultConfig?.healthCheck?.enabled ? 1 : 0; + + return acc; + }, + { + notStarted: 0, + errorMessages: 0, + healthCheck: 0, + } + ); + + return { + alertRules: { + [TRANSFORM_RULE_TYPE.TRANSFORM_HEALTH]: { + count_by_check_type: resultsByCheckType, + }, + }, + }; + }, + }); + + usageCollection.registerCollector(collector); +} diff --git a/x-pack/plugins/transform/server/usage/index.ts b/x-pack/plugins/transform/server/usage/index.ts new file mode 100644 index 0000000000000..adadedd200fe9 --- /dev/null +++ b/x-pack/plugins/transform/server/usage/index.ts @@ -0,0 +1,8 @@ +/* + * 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. + */ + +export { registerCollector } from './collector';