Skip to content

Commit

Permalink
[ML] Transforms: Add telemetry for transform health rule check type (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov authored Jun 20, 2023
1 parent 1bb316d commit a1f55b8
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
15 changes: 14 additions & 1 deletion x-pack/plugins/transform/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -38,7 +39,13 @@ export class TransformServerPlugin implements Plugin<{}, void, any, any> {

setup(
coreSetup: CoreSetup<PluginStartDependencies>,
{ licensing, features, alerting, security: securitySetup }: PluginSetupDependencies
{
licensing,
features,
alerting,
security: securitySetup,
usageCollection,
}: PluginSetupDependencies
): {} {
const { http, getStartServices } = coreSetup;

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/transform/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -20,6 +21,7 @@ export interface PluginSetupDependencies {
alerting?: AlertingPlugin['setup'];
fieldFormats: FieldFormatsSetup;
security?: SecurityPluginSetup;
usageCollection?: UsageCollectionSetup;
}

export interface PluginStartDependencies {
Expand Down
120 changes: 120 additions & 0 deletions x-pack/plugins/transform/server/usage/collector.ts
Original file line number Diff line number Diff line change
@@ -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<TransformAlertsUsageData>({
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);
}
8 changes: 8 additions & 0 deletions x-pack/plugins/transform/server/usage/index.ts
Original file line number Diff line number Diff line change
@@ -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';

0 comments on commit a1f55b8

Please sign in to comment.