From 9190aa733344f7fddf7ae073b84b29d3520f4464 Mon Sep 17 00:00:00 2001 From: Chris Roberson Date: Mon, 13 Sep 2021 16:20:05 -0400 Subject: [PATCH] [7.x] [Alerting] Telemetry for calling legacy routes (#111885) (#112003) * [Alerting] Telemetry for calling legacy routes (#111885) * Telemetry for calling legacy routes * Fix types * Use different counter type * PR feedback * Fix this test too # Conflicts: # x-pack/plugins/alerting/server/routes/legacy/create.ts * Fix missing arg --- .../lib/track_legacy_route_usage.test.ts | 32 +++++++++++++++++ .../server/lib/track_legacy_route_usage.ts | 18 ++++++++++ .../server/routes/legacy/aggregate.test.ts | 27 +++++++++++++++ .../server/routes/legacy/aggregate.ts | 10 +++++- .../server/routes/legacy/create.test.ts | 27 ++++++++++++++- .../alerting/server/routes/legacy/create.ts | 2 ++ .../server/routes/legacy/delete.test.ts | 21 ++++++++++++ .../alerting/server/routes/legacy/delete.ts | 9 ++++- .../server/routes/legacy/disable.test.ts | 22 +++++++++++- .../alerting/server/routes/legacy/disable.ts | 9 ++++- .../server/routes/legacy/enable.test.ts | 22 +++++++++++- .../alerting/server/routes/legacy/enable.ts | 9 ++++- .../server/routes/legacy/find.test.ts | 22 +++++++++++- .../alerting/server/routes/legacy/find.ts | 9 ++++- .../alerting/server/routes/legacy/get.test.ts | 21 ++++++++++++ .../alerting/server/routes/legacy/get.ts | 9 ++++- .../legacy/get_alert_instance_summary.test.ts | 24 ++++++++++++- .../legacy/get_alert_instance_summary.ts | 6 +++- .../routes/legacy/get_alert_state.test.ts | 22 +++++++++++- .../server/routes/legacy/get_alert_state.ts | 9 ++++- .../server/routes/legacy/health.test.ts | 24 ++++++++++++- .../alerting/server/routes/legacy/health.ts | 6 +++- .../alerting/server/routes/legacy/index.ts | 34 +++++++++---------- .../routes/legacy/list_alert_types.test.ts | 25 ++++++++++++++ .../server/routes/legacy/list_alert_types.ts | 10 ++++-- .../server/routes/legacy/mute_all.test.ts | 21 ++++++++++++ .../alerting/server/routes/legacy/mute_all.ts | 9 ++++- .../routes/legacy/mute_instance.test.ts | 21 ++++++++++++ .../server/routes/legacy/mute_instance.ts | 11 +++++- .../server/routes/legacy/unmute_all.test.ts | 21 ++++++++++++ .../server/routes/legacy/unmute_all.ts | 9 ++++- .../routes/legacy/unmute_instance.test.ts | 21 ++++++++++++ .../server/routes/legacy/unmute_instance.ts | 9 ++++- .../server/routes/legacy/update.test.ts | 21 ++++++++++++ .../alerting/server/routes/legacy/update.ts | 9 ++++- .../routes/legacy/update_api_key.test.ts | 21 ++++++++++++ .../server/routes/legacy/update_api_key.ts | 9 ++++- 37 files changed, 570 insertions(+), 41 deletions(-) create mode 100644 x-pack/plugins/alerting/server/lib/track_legacy_route_usage.test.ts create mode 100644 x-pack/plugins/alerting/server/lib/track_legacy_route_usage.ts diff --git a/x-pack/plugins/alerting/server/lib/track_legacy_route_usage.test.ts b/x-pack/plugins/alerting/server/lib/track_legacy_route_usage.test.ts new file mode 100644 index 000000000000..f4a1ce07ab98 --- /dev/null +++ b/x-pack/plugins/alerting/server/lib/track_legacy_route_usage.test.ts @@ -0,0 +1,32 @@ +/* + * 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 { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; +import { trackLegacyRouteUsage } from './track_legacy_route_usage'; + +describe('trackLegacyRouteUsage', () => { + it('should call `usageCounter.incrementCounter`', () => { + const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); + const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + + trackLegacyRouteUsage('test', mockUsageCounter); + expect(mockUsageCounter.incrementCounter).toHaveBeenCalledWith({ + counterName: `legacyRoute_test`, + counterType: 'legacyApiUsage', + incrementBy: 1, + }); + }); + + it('should do nothing if no usage counter is provided', () => { + let err; + try { + trackLegacyRouteUsage('test', undefined); + } catch (e) { + err = e; + } + expect(err).toBeUndefined(); + }); +}); diff --git a/x-pack/plugins/alerting/server/lib/track_legacy_route_usage.ts b/x-pack/plugins/alerting/server/lib/track_legacy_route_usage.ts new file mode 100644 index 000000000000..528a75a0a18b --- /dev/null +++ b/x-pack/plugins/alerting/server/lib/track_legacy_route_usage.ts @@ -0,0 +1,18 @@ +/* + * 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 { UsageCounter } from 'src/plugins/usage_collection/server'; + +export function trackLegacyRouteUsage(route: string, usageCounter?: UsageCounter) { + if (usageCounter) { + usageCounter.incrementCounter({ + counterName: `legacyRoute_${route}`, + counterType: 'legacyApiUsage', + incrementBy: 1, + }); + } +} diff --git a/x-pack/plugins/alerting/server/routes/legacy/aggregate.test.ts b/x-pack/plugins/alerting/server/routes/legacy/aggregate.test.ts index 22d7d5c4bc94..d08e970eef69 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/aggregate.test.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/aggregate.test.ts @@ -11,8 +11,16 @@ import { licenseStateMock } from '../../lib/license_state.mock'; import { verifyApiAccess } from '../../lib/license_api_access'; import { mockHandlerArguments } from './../_mock_handler_arguments'; import { rulesClientMock } from '../../rules_client.mock'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; const rulesClient = rulesClientMock.create(); +const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); +const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); jest.mock('../../lib/license_api_access.ts', () => ({ verifyApiAccess: jest.fn(), @@ -139,4 +147,23 @@ describe('aggregateAlertRoute', () => { expect(verifyApiAccess).toHaveBeenCalledWith(licenseState); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + + aggregateAlertRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.get.mock.calls[0]; + const [context, req, res] = mockHandlerArguments( + { rulesClient }, + { + query: { + default_search_operator: 'AND', + }, + }, + ['ok'] + ); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('aggregate', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/aggregate.ts b/x-pack/plugins/alerting/server/routes/legacy/aggregate.ts index 099b088abdf3..d1e8d98a9540 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/aggregate.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/aggregate.ts @@ -6,12 +6,14 @@ */ import { schema } from '@kbn/config-schema'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import type { AlertingRouter } from '../../types'; import { ILicenseState } from '../../lib/license_state'; import { verifyApiAccess } from '../../lib/license_api_access'; import { LEGACY_BASE_ALERT_API_PATH } from '../../../common'; import { renameKeys } from './../lib/rename_keys'; import { FindOptions } from '../../rules_client'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; // config definition const querySchema = schema.object({ @@ -33,7 +35,11 @@ const querySchema = schema.object({ filter: schema.maybe(schema.string()), }); -export const aggregateAlertRoute = (router: AlertingRouter, licenseState: ILicenseState) => { +export const aggregateAlertRoute = ( + router: AlertingRouter, + licenseState: ILicenseState, + usageCounter?: UsageCounter +) => { router.get( { path: `${LEGACY_BASE_ALERT_API_PATH}/_aggregate`, @@ -48,6 +54,8 @@ export const aggregateAlertRoute = (router: AlertingRouter, licenseState: ILicen } const rulesClient = context.alerting.getRulesClient(); + trackLegacyRouteUsage('aggregate', usageCounter); + const query = req.query; const renameMap = { default_search_operator: 'defaultSearchOperator', diff --git a/x-pack/plugins/alerting/server/routes/legacy/create.test.ts b/x-pack/plugins/alerting/server/routes/legacy/create.test.ts index 921fe3a7e6fb..0398ac152742 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/create.test.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/create.test.ts @@ -8,6 +8,7 @@ import { MockedLogger, loggerMock } from '@kbn/logging/mocks'; import { createAlertRoute } from './create'; import { httpServiceMock } from 'src/core/server/mocks'; +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; import { licenseStateMock } from '../../lib/license_state.mock'; import { verifyApiAccess } from '../../lib/license_api_access'; import { mockHandlerArguments } from './../_mock_handler_arguments'; @@ -15,7 +16,7 @@ import { rulesClientMock } from '../../rules_client.mock'; import { Alert } from '../../../common/alert'; import { AlertTypeDisabledError } from '../../lib/errors/alert_type_disabled'; import { encryptedSavedObjectsMock } from '../../../../encrypted_saved_objects/server/mocks'; -import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const rulesClient = rulesClientMock.create(); let logger: MockedLogger; @@ -24,6 +25,10 @@ jest.mock('../../lib/license_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + beforeEach(() => { jest.resetAllMocks(); logger = loggerMock.create(); @@ -461,4 +466,24 @@ describe('createAlertRoute', () => { expect(res.forbidden).toHaveBeenCalledWith({ body: { message: 'Fail' } }); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const encryptedSavedObjects = encryptedSavedObjectsMock.createSetup({ canEncrypt: true }); + const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); + const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + + createAlertRoute({ + router, + licenseState, + logger, + encryptedSavedObjects, + usageCounter: mockUsageCounter, + }); + const [, handler] = router.post.mock.calls[0]; + const [context, req, res] = mockHandlerArguments({ rulesClient }, {}, ['ok']); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('create', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/create.ts b/x-pack/plugins/alerting/server/routes/legacy/create.ts index ba795b5eaa26..2b0d7fedbae2 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/create.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/create.ts @@ -19,6 +19,7 @@ import { import { AlertTypeDisabledError } from '../../lib/errors/alert_type_disabled'; import { RouteOptions } from '..'; import { countUsageOfPredefinedIds } from '../lib'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; export const bodySchema = schema.object({ name: schema.string(), @@ -67,6 +68,7 @@ export const createAlertRoute = ({ router, licenseState, logger, usageCounter }: const alert = req.body; const params = req.params; const notifyWhen = alert?.notifyWhen ? (alert.notifyWhen as AlertNotifyWhenType) : null; + trackLegacyRouteUsage('create', usageCounter); const spaceId = rulesClient.getSpaceId(); const shouldWarnId = params?.id && spaceId !== undefined && spaceId !== 'default'; diff --git a/x-pack/plugins/alerting/server/routes/legacy/delete.test.ts b/x-pack/plugins/alerting/server/routes/legacy/delete.test.ts index a016205b7890..ae1c5327bd27 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/delete.test.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/delete.test.ts @@ -5,12 +5,14 @@ * 2.0. */ +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; import { deleteAlertRoute } from './delete'; import { httpServiceMock } from 'src/core/server/mocks'; import { licenseStateMock } from '../../lib/license_state.mock'; import { verifyApiAccess } from '../../lib/license_api_access'; import { mockHandlerArguments } from './../_mock_handler_arguments'; import { rulesClientMock } from '../../rules_client.mock'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const rulesClient = rulesClientMock.create(); @@ -18,6 +20,10 @@ jest.mock('../../lib/license_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + beforeEach(() => { jest.resetAllMocks(); }); @@ -106,4 +112,19 @@ describe('deleteAlertRoute', () => { expect(verifyApiAccess).toHaveBeenCalledWith(licenseState); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); + const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + + deleteAlertRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.delete.mock.calls[0]; + const [context, req, res] = mockHandlerArguments({ rulesClient }, { params: { id: '1' } }, [ + 'ok', + ]); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('delete', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/delete.ts b/x-pack/plugins/alerting/server/routes/legacy/delete.ts index b8800114ebe9..66d05e580a3a 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/delete.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/delete.ts @@ -6,16 +6,22 @@ */ import { schema } from '@kbn/config-schema'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import type { AlertingRouter } from '../../types'; import { ILicenseState } from '../../lib/license_state'; import { verifyApiAccess } from '../../lib/license_api_access'; import { LEGACY_BASE_ALERT_API_PATH } from '../../../common'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const paramSchema = schema.object({ id: schema.string(), }); -export const deleteAlertRoute = (router: AlertingRouter, licenseState: ILicenseState) => { +export const deleteAlertRoute = ( + router: AlertingRouter, + licenseState: ILicenseState, + usageCounter?: UsageCounter +) => { router.delete( { path: `${LEGACY_BASE_ALERT_API_PATH}/alert/{id}`, @@ -28,6 +34,7 @@ export const deleteAlertRoute = (router: AlertingRouter, licenseState: ILicenseS if (!context.alerting) { return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); } + trackLegacyRouteUsage('delete', usageCounter); const rulesClient = context.alerting.getRulesClient(); const { id } = req.params; await rulesClient.delete({ id }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/disable.test.ts b/x-pack/plugins/alerting/server/routes/legacy/disable.test.ts index 3cabf6368b3a..ac44ab37761b 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/disable.test.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/disable.test.ts @@ -4,13 +4,14 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; import { disableAlertRoute } from './disable'; import { httpServiceMock } from 'src/core/server/mocks'; import { licenseStateMock } from '../../lib/license_state.mock'; import { mockHandlerArguments } from './../_mock_handler_arguments'; import { rulesClientMock } from '../../rules_client.mock'; import { AlertTypeDisabledError } from '../../lib/errors/alert_type_disabled'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const rulesClient = rulesClientMock.create(); @@ -18,6 +19,10 @@ jest.mock('../../lib/license_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + beforeEach(() => { jest.resetAllMocks(); }); @@ -78,4 +83,19 @@ describe('disableAlertRoute', () => { expect(res.forbidden).toHaveBeenCalledWith({ body: { message: 'Fail' } }); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); + const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + + disableAlertRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.post.mock.calls[0]; + const [context, req, res] = mockHandlerArguments({ rulesClient }, { params: { id: '1' } }, [ + 'ok', + ]); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('disable', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/disable.ts b/x-pack/plugins/alerting/server/routes/legacy/disable.ts index 0ac64395235c..1cba654e11a5 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/disable.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/disable.ts @@ -6,17 +6,23 @@ */ import { schema } from '@kbn/config-schema'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import type { AlertingRouter } from '../../types'; import { ILicenseState } from '../../lib/license_state'; import { verifyApiAccess } from '../../lib/license_api_access'; import { LEGACY_BASE_ALERT_API_PATH } from '../../../common'; import { AlertTypeDisabledError } from '../../lib/errors/alert_type_disabled'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const paramSchema = schema.object({ id: schema.string(), }); -export const disableAlertRoute = (router: AlertingRouter, licenseState: ILicenseState) => { +export const disableAlertRoute = ( + router: AlertingRouter, + licenseState: ILicenseState, + usageCounter?: UsageCounter +) => { router.post( { path: `${LEGACY_BASE_ALERT_API_PATH}/alert/{id}/_disable`, @@ -29,6 +35,7 @@ export const disableAlertRoute = (router: AlertingRouter, licenseState: ILicense if (!context.alerting) { return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); } + trackLegacyRouteUsage('disable', usageCounter); const rulesClient = context.alerting.getRulesClient(); const { id } = req.params; try { diff --git a/x-pack/plugins/alerting/server/routes/legacy/enable.test.ts b/x-pack/plugins/alerting/server/routes/legacy/enable.test.ts index 67a94ef2c134..c35fb191fae6 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/enable.test.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/enable.test.ts @@ -4,13 +4,14 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; import { enableAlertRoute } from './enable'; import { httpServiceMock } from 'src/core/server/mocks'; import { licenseStateMock } from '../../lib/license_state.mock'; import { mockHandlerArguments } from './../_mock_handler_arguments'; import { rulesClientMock } from '../../rules_client.mock'; import { AlertTypeDisabledError } from '../../lib/errors/alert_type_disabled'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const rulesClient = rulesClientMock.create(); @@ -18,6 +19,10 @@ jest.mock('../../lib/license_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + beforeEach(() => { jest.resetAllMocks(); }); @@ -78,4 +83,19 @@ describe('enableAlertRoute', () => { expect(res.forbidden).toHaveBeenCalledWith({ body: { message: 'Fail' } }); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); + const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + + enableAlertRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.post.mock.calls[0]; + const [context, req, res] = mockHandlerArguments({ rulesClient }, { params: { id: '1' } }, [ + 'ok', + ]); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('enable', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/enable.ts b/x-pack/plugins/alerting/server/routes/legacy/enable.ts index 5a4763f00bd7..000d165ff169 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/enable.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/enable.ts @@ -6,18 +6,24 @@ */ import { schema } from '@kbn/config-schema'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import type { AlertingRouter } from '../../types'; import { ILicenseState } from '../../lib/license_state'; import { verifyApiAccess } from '../../lib/license_api_access'; import { LEGACY_BASE_ALERT_API_PATH } from '../../../common'; import { handleDisabledApiKeysError } from './../lib/error_handler'; import { AlertTypeDisabledError } from '../../lib/errors/alert_type_disabled'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const paramSchema = schema.object({ id: schema.string(), }); -export const enableAlertRoute = (router: AlertingRouter, licenseState: ILicenseState) => { +export const enableAlertRoute = ( + router: AlertingRouter, + licenseState: ILicenseState, + usageCounter?: UsageCounter +) => { router.post( { path: `${LEGACY_BASE_ALERT_API_PATH}/alert/{id}/_enable`, @@ -31,6 +37,7 @@ export const enableAlertRoute = (router: AlertingRouter, licenseState: ILicenseS if (!context.alerting) { return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); } + trackLegacyRouteUsage('enable', usageCounter); const rulesClient = context.alerting.getRulesClient(); const { id } = req.params; try { diff --git a/x-pack/plugins/alerting/server/routes/legacy/find.test.ts b/x-pack/plugins/alerting/server/routes/legacy/find.test.ts index d6a74027e13d..de12c62f1e4f 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/find.test.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/find.test.ts @@ -4,13 +4,14 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; import { findAlertRoute } from './find'; import { httpServiceMock } from 'src/core/server/mocks'; import { licenseStateMock } from '../../lib/license_state.mock'; import { verifyApiAccess } from '../../lib/license_api_access'; import { mockHandlerArguments } from './../_mock_handler_arguments'; import { rulesClientMock } from '../../rules_client.mock'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const rulesClient = rulesClientMock.create(); @@ -18,6 +19,10 @@ jest.mock('../../lib/license_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + beforeEach(() => { jest.resetAllMocks(); }); @@ -140,4 +145,19 @@ describe('findAlertRoute', () => { expect(verifyApiAccess).toHaveBeenCalledWith(licenseState); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); + const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + + findAlertRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.get.mock.calls[0]; + const [context, req, res] = mockHandlerArguments({ rulesClient }, { params: {}, query: {} }, [ + 'ok', + ]); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('find', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/find.ts b/x-pack/plugins/alerting/server/routes/legacy/find.ts index c12ef8b349e2..f915f0e15afb 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/find.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/find.ts @@ -6,6 +6,7 @@ */ import { schema } from '@kbn/config-schema'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import type { AlertingRouter } from '../../types'; import { ILicenseState } from '../../lib/license_state'; @@ -13,6 +14,7 @@ import { verifyApiAccess } from '../../lib/license_api_access'; import { LEGACY_BASE_ALERT_API_PATH } from '../../../common'; import { renameKeys } from './../lib/rename_keys'; import { FindOptions } from '../../rules_client'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; // config definition const querySchema = schema.object({ @@ -39,7 +41,11 @@ const querySchema = schema.object({ filter: schema.maybe(schema.string()), }); -export const findAlertRoute = (router: AlertingRouter, licenseState: ILicenseState) => { +export const findAlertRoute = ( + router: AlertingRouter, + licenseState: ILicenseState, + usageCounter?: UsageCounter +) => { router.get( { path: `${LEGACY_BASE_ALERT_API_PATH}/_find`, @@ -52,6 +58,7 @@ export const findAlertRoute = (router: AlertingRouter, licenseState: ILicenseSta if (!context.alerting) { return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); } + trackLegacyRouteUsage('find', usageCounter); const rulesClient = context.alerting.getRulesClient(); const query = req.query; diff --git a/x-pack/plugins/alerting/server/routes/legacy/get.test.ts b/x-pack/plugins/alerting/server/routes/legacy/get.test.ts index 84d21ca6ea95..40c7b224c715 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/get.test.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/get.test.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; import { getAlertRoute } from './get'; import { httpServiceMock } from 'src/core/server/mocks'; import { licenseStateMock } from '../../lib/license_state.mock'; @@ -12,12 +13,17 @@ import { verifyApiAccess } from '../../lib/license_api_access'; import { mockHandlerArguments } from './../_mock_handler_arguments'; import { rulesClientMock } from '../../rules_client.mock'; import { Alert } from '../../../common'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const rulesClient = rulesClientMock.create(); jest.mock('../../lib/license_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + beforeEach(() => { jest.resetAllMocks(); }); @@ -139,4 +145,19 @@ describe('getAlertRoute', () => { expect(verifyApiAccess).toHaveBeenCalledWith(licenseState); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); + const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + + getAlertRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.get.mock.calls[0]; + const [context, req, res] = mockHandlerArguments({ rulesClient }, { params: { id: '1' } }, [ + 'ok', + ]); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('get', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/get.ts b/x-pack/plugins/alerting/server/routes/legacy/get.ts index 08d1308439aa..8b4c3e444151 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/get.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/get.ts @@ -6,16 +6,22 @@ */ import { schema } from '@kbn/config-schema'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import { ILicenseState } from '../../lib/license_state'; import { verifyApiAccess } from '../../lib/license_api_access'; import { LEGACY_BASE_ALERT_API_PATH } from '../../../common'; import type { AlertingRouter } from '../../types'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const paramSchema = schema.object({ id: schema.string(), }); -export const getAlertRoute = (router: AlertingRouter, licenseState: ILicenseState) => { +export const getAlertRoute = ( + router: AlertingRouter, + licenseState: ILicenseState, + usageCounter?: UsageCounter +) => { router.get( { path: `${LEGACY_BASE_ALERT_API_PATH}/alert/{id}`, @@ -28,6 +34,7 @@ export const getAlertRoute = (router: AlertingRouter, licenseState: ILicenseStat if (!context.alerting) { return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); } + trackLegacyRouteUsage('get', usageCounter); const rulesClient = context.alerting.getRulesClient(); const { id } = req.params; return res.ok({ diff --git a/x-pack/plugins/alerting/server/routes/legacy/get_alert_instance_summary.test.ts b/x-pack/plugins/alerting/server/routes/legacy/get_alert_instance_summary.test.ts index f3e7349ba55b..d529aec4162d 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/get_alert_instance_summary.test.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/get_alert_instance_summary.test.ts @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; import { getAlertInstanceSummaryRoute } from './get_alert_instance_summary'; import { httpServiceMock } from 'src/core/server/mocks'; import { licenseStateMock } from '../../lib/license_state.mock'; @@ -12,12 +12,17 @@ import { mockHandlerArguments } from './../_mock_handler_arguments'; import { SavedObjectsErrorHelpers } from 'src/core/server'; import { rulesClientMock } from '../../rules_client.mock'; import { AlertInstanceSummary } from '../../types'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const rulesClient = rulesClientMock.create(); jest.mock('../../lib/license_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + beforeEach(() => { jest.resetAllMocks(); }); @@ -103,4 +108,21 @@ describe('getAlertInstanceSummaryRoute', () => { expect(await handler(context, req, res)).toEqual(undefined); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); + const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + + getAlertInstanceSummaryRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.get.mock.calls[0]; + const [context, req, res] = mockHandlerArguments( + { rulesClient }, + { params: { id: '1' }, query: {} }, + ['ok'] + ); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('instanceSummary', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/get_alert_instance_summary.ts b/x-pack/plugins/alerting/server/routes/legacy/get_alert_instance_summary.ts index ecbb1bf18fb2..2eed14a913a8 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/get_alert_instance_summary.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/get_alert_instance_summary.ts @@ -6,10 +6,12 @@ */ import { schema } from '@kbn/config-schema'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import type { AlertingRouter } from '../../types'; import { ILicenseState } from '../../lib/license_state'; import { verifyApiAccess } from '../../lib/license_api_access'; import { LEGACY_BASE_ALERT_API_PATH } from '../../../common'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const paramSchema = schema.object({ id: schema.string(), @@ -21,7 +23,8 @@ const querySchema = schema.object({ export const getAlertInstanceSummaryRoute = ( router: AlertingRouter, - licenseState: ILicenseState + licenseState: ILicenseState, + usageCounter?: UsageCounter ) => { router.get( { @@ -36,6 +39,7 @@ export const getAlertInstanceSummaryRoute = ( if (!context.alerting) { return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); } + trackLegacyRouteUsage('instanceSummary', usageCounter); const rulesClient = context.alerting.getRulesClient(); const { id } = req.params; const { dateStart } = req.query; diff --git a/x-pack/plugins/alerting/server/routes/legacy/get_alert_state.test.ts b/x-pack/plugins/alerting/server/routes/legacy/get_alert_state.test.ts index f07d9cc79c3a..f164e0fa0bc8 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/get_alert_state.test.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/get_alert_state.test.ts @@ -4,19 +4,24 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; import { getAlertStateRoute } from './get_alert_state'; import { httpServiceMock } from 'src/core/server/mocks'; import { licenseStateMock } from '../../lib/license_state.mock'; import { mockHandlerArguments } from './../_mock_handler_arguments'; import { SavedObjectsErrorHelpers } from 'src/core/server'; import { rulesClientMock } from '../../rules_client.mock'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const rulesClient = rulesClientMock.create(); jest.mock('../../lib/license_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + beforeEach(() => { jest.resetAllMocks(); }); @@ -147,4 +152,19 @@ describe('getAlertStateRoute', () => { ] `); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); + const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + + getAlertStateRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.get.mock.calls[0]; + const [context, req, res] = mockHandlerArguments({ rulesClient }, { params: { id: '1' } }, [ + 'ok', + ]); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('state', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/get_alert_state.ts b/x-pack/plugins/alerting/server/routes/legacy/get_alert_state.ts index a3156fc5ed42..2bd2d343b98b 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/get_alert_state.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/get_alert_state.ts @@ -6,16 +6,22 @@ */ import { schema } from '@kbn/config-schema'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import type { AlertingRouter } from '../../types'; import { ILicenseState } from '../../lib/license_state'; import { verifyApiAccess } from '../../lib/license_api_access'; import { LEGACY_BASE_ALERT_API_PATH } from '../../../common'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const paramSchema = schema.object({ id: schema.string(), }); -export const getAlertStateRoute = (router: AlertingRouter, licenseState: ILicenseState) => { +export const getAlertStateRoute = ( + router: AlertingRouter, + licenseState: ILicenseState, + usageCounter?: UsageCounter +) => { router.get( { path: `${LEGACY_BASE_ALERT_API_PATH}/alert/{id}/state`, @@ -28,6 +34,7 @@ export const getAlertStateRoute = (router: AlertingRouter, licenseState: ILicens if (!context.alerting) { return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); } + trackLegacyRouteUsage('state', usageCounter); const rulesClient = context.alerting.getRulesClient(); const { id } = req.params; const state = await rulesClient.getAlertState({ id }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/health.test.ts b/x-pack/plugins/alerting/server/routes/legacy/health.test.ts index ed8e6763d66b..59ede356add0 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/health.test.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/health.test.ts @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; import { healthRoute } from './health'; import { httpServiceMock } from 'src/core/server/mocks'; import { mockHandlerArguments } from './../_mock_handler_arguments'; @@ -13,12 +13,18 @@ import { encryptedSavedObjectsMock } from '../../../../encrypted_saved_objects/s import { rulesClientMock } from '../../rules_client.mock'; import { HealthStatus } from '../../types'; import { alertsMock } from '../../mocks'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; + const rulesClient = rulesClientMock.create(); jest.mock('../../lib/license_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + const alerting = alertsMock.createStart(); const currentDate = new Date().toISOString(); @@ -256,4 +262,20 @@ describe('healthRoute', () => { }, }); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const encryptedSavedObjects = encryptedSavedObjectsMock.createSetup({ canEncrypt: true }); + const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); + const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + + healthRoute(router, licenseState, encryptedSavedObjects, mockUsageCounter); + const [, handler] = router.get.mock.calls[0]; + const [context, req, res] = mockHandlerArguments({ rulesClient }, { params: { id: '1' } }, [ + 'ok', + ]); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('health', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/health.ts b/x-pack/plugins/alerting/server/routes/legacy/health.ts index 03a574ca62c3..8c654f103ea8 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/health.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/health.ts @@ -5,16 +5,19 @@ * 2.0. */ +import { UsageCounter } from 'src/plugins/usage_collection/server'; import type { AlertingRouter } from '../../types'; import { ILicenseState } from '../../lib/license_state'; import { verifyApiAccess } from '../../lib/license_api_access'; import { AlertingFrameworkHealth } from '../../types'; import { EncryptedSavedObjectsPluginSetup } from '../../../../encrypted_saved_objects/server'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; export function healthRoute( router: AlertingRouter, licenseState: ILicenseState, - encryptedSavedObjects: EncryptedSavedObjectsPluginSetup + encryptedSavedObjects: EncryptedSavedObjectsPluginSetup, + usageCounter?: UsageCounter ) { router.get( { @@ -26,6 +29,7 @@ export function healthRoute( if (!context.alerting) { return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); } + trackLegacyRouteUsage('health', usageCounter); try { const isEsSecurityEnabled: boolean | null = licenseState.getIsSecurityEnabled(); const alertingFrameworkHeath = await context.alerting.getFrameworkHealth(); diff --git a/x-pack/plugins/alerting/server/routes/legacy/index.ts b/x-pack/plugins/alerting/server/routes/legacy/index.ts index e53df8e5aa8b..a89ccf0920b2 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/index.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/index.ts @@ -25,23 +25,23 @@ import { healthRoute } from './health'; import { RouteOptions } from '..'; export function defineLegacyRoutes(opts: RouteOptions) { - const { router, licenseState, encryptedSavedObjects } = opts; + const { router, licenseState, encryptedSavedObjects, usageCounter } = opts; createAlertRoute(opts); - aggregateAlertRoute(router, licenseState); - deleteAlertRoute(router, licenseState); - findAlertRoute(router, licenseState); - getAlertRoute(router, licenseState); - getAlertStateRoute(router, licenseState); - getAlertInstanceSummaryRoute(router, licenseState); - listAlertTypesRoute(router, licenseState); - updateAlertRoute(router, licenseState); - enableAlertRoute(router, licenseState); - disableAlertRoute(router, licenseState); - updateApiKeyRoute(router, licenseState); - muteAllAlertRoute(router, licenseState); - unmuteAllAlertRoute(router, licenseState); - muteAlertInstanceRoute(router, licenseState); - unmuteAlertInstanceRoute(router, licenseState); - healthRoute(router, licenseState, encryptedSavedObjects); + aggregateAlertRoute(router, licenseState, usageCounter); + deleteAlertRoute(router, licenseState, usageCounter); + findAlertRoute(router, licenseState, usageCounter); + getAlertRoute(router, licenseState, usageCounter); + getAlertStateRoute(router, licenseState, usageCounter); + getAlertInstanceSummaryRoute(router, licenseState, usageCounter); + listAlertTypesRoute(router, licenseState, usageCounter); + updateAlertRoute(router, licenseState, usageCounter); + enableAlertRoute(router, licenseState, usageCounter); + disableAlertRoute(router, licenseState, usageCounter); + updateApiKeyRoute(router, licenseState, usageCounter); + muteAllAlertRoute(router, licenseState, usageCounter); + unmuteAllAlertRoute(router, licenseState, usageCounter); + muteAlertInstanceRoute(router, licenseState, usageCounter); + unmuteAlertInstanceRoute(router, licenseState, usageCounter); + healthRoute(router, licenseState, encryptedSavedObjects, usageCounter); } diff --git a/x-pack/plugins/alerting/server/routes/legacy/list_alert_types.test.ts b/x-pack/plugins/alerting/server/routes/legacy/list_alert_types.test.ts index c047c78a8606..45c5c6291399 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/list_alert_types.test.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/list_alert_types.test.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; import { listAlertTypesRoute } from './list_alert_types'; import { httpServiceMock } from 'src/core/server/mocks'; import { licenseStateMock } from '../../lib/license_state.mock'; @@ -13,6 +14,7 @@ import { mockHandlerArguments } from './../_mock_handler_arguments'; import { rulesClientMock } from '../../rules_client.mock'; import { RecoveredActionGroup } from '../../../common'; import { RegistryAlertTypeWithAuth } from '../../authorization'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const rulesClient = rulesClientMock.create(); @@ -20,6 +22,10 @@ jest.mock('../../lib/license_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + beforeEach(() => { jest.resetAllMocks(); }); @@ -201,4 +207,23 @@ describe('listAlertTypesRoute', () => { expect(verifyApiAccess).toHaveBeenCalledWith(licenseState); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); + const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + + rulesClient.listAlertTypes.mockResolvedValueOnce(new Set([])); + + listAlertTypesRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.get.mock.calls[0]; + const [context, req, res] = mockHandlerArguments( + { rulesClient }, + { params: { id: '1' }, body: {} }, + ['ok'] + ); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('listAlertTypes', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/list_alert_types.ts b/x-pack/plugins/alerting/server/routes/legacy/list_alert_types.ts index 6d5ab21c0b0b..250d3d1bdeed 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/list_alert_types.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/list_alert_types.ts @@ -4,13 +4,18 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ - +import { UsageCounter } from 'src/plugins/usage_collection/server'; import type { AlertingRouter } from '../../types'; import { ILicenseState } from '../../lib/license_state'; import { verifyApiAccess } from '../../lib/license_api_access'; import { LEGACY_BASE_ALERT_API_PATH } from '../../../common'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; -export const listAlertTypesRoute = (router: AlertingRouter, licenseState: ILicenseState) => { +export const listAlertTypesRoute = ( + router: AlertingRouter, + licenseState: ILicenseState, + usageCounter?: UsageCounter +) => { router.get( { path: `${LEGACY_BASE_ALERT_API_PATH}/list_alert_types`, @@ -21,6 +26,7 @@ export const listAlertTypesRoute = (router: AlertingRouter, licenseState: ILicen if (!context.alerting) { return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); } + trackLegacyRouteUsage('listAlertTypes', usageCounter); return res.ok({ body: Array.from(await context.alerting.getRulesClient().listAlertTypes()), }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/mute_all.test.ts b/x-pack/plugins/alerting/server/routes/legacy/mute_all.test.ts index 538e9d92c43d..131b5ed7960b 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/mute_all.test.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/mute_all.test.ts @@ -5,18 +5,24 @@ * 2.0. */ +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; import { muteAllAlertRoute } from './mute_all'; import { httpServiceMock } from 'src/core/server/mocks'; import { licenseStateMock } from '../../lib/license_state.mock'; import { mockHandlerArguments } from './../_mock_handler_arguments'; import { rulesClientMock } from '../../rules_client.mock'; import { AlertTypeDisabledError } from '../../lib/errors/alert_type_disabled'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const rulesClient = rulesClientMock.create(); jest.mock('../../lib/license_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + beforeEach(() => { jest.resetAllMocks(); }); @@ -77,4 +83,19 @@ describe('muteAllAlertRoute', () => { expect(res.forbidden).toHaveBeenCalledWith({ body: { message: 'Fail' } }); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); + const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + + muteAllAlertRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.post.mock.calls[0]; + const [context, req, res] = mockHandlerArguments({ rulesClient }, { params: {}, body: {} }, [ + 'ok', + ]); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('muteAll', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/mute_all.ts b/x-pack/plugins/alerting/server/routes/legacy/mute_all.ts index 506ffa0570b6..222abc806ede 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/mute_all.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/mute_all.ts @@ -6,17 +6,23 @@ */ import { schema } from '@kbn/config-schema'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import type { AlertingRouter } from '../../types'; import { ILicenseState } from '../../lib/license_state'; import { verifyApiAccess } from '../../lib/license_api_access'; import { LEGACY_BASE_ALERT_API_PATH } from '../../../common'; import { AlertTypeDisabledError } from '../../lib/errors/alert_type_disabled'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const paramSchema = schema.object({ id: schema.string(), }); -export const muteAllAlertRoute = (router: AlertingRouter, licenseState: ILicenseState) => { +export const muteAllAlertRoute = ( + router: AlertingRouter, + licenseState: ILicenseState, + usageCounter?: UsageCounter +) => { router.post( { path: `${LEGACY_BASE_ALERT_API_PATH}/alert/{id}/_mute_all`, @@ -29,6 +35,7 @@ export const muteAllAlertRoute = (router: AlertingRouter, licenseState: ILicense if (!context.alerting) { return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); } + trackLegacyRouteUsage('muteAll', usageCounter); const rulesClient = context.alerting.getRulesClient(); const { id } = req.params; try { diff --git a/x-pack/plugins/alerting/server/routes/legacy/mute_instance.test.ts b/x-pack/plugins/alerting/server/routes/legacy/mute_instance.test.ts index 05ecd3978493..19ce9e1d2b10 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/mute_instance.test.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/mute_instance.test.ts @@ -5,18 +5,24 @@ * 2.0. */ +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; import { muteAlertInstanceRoute } from './mute_instance'; import { httpServiceMock } from 'src/core/server/mocks'; import { licenseStateMock } from '../../lib/license_state.mock'; import { mockHandlerArguments } from './../_mock_handler_arguments'; import { rulesClientMock } from '../../rules_client.mock'; import { AlertTypeDisabledError } from '../../lib/errors/alert_type_disabled'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const rulesClient = rulesClientMock.create(); jest.mock('../../lib/license_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + beforeEach(() => { jest.resetAllMocks(); }); @@ -83,4 +89,19 @@ describe('muteAlertInstanceRoute', () => { expect(res.forbidden).toHaveBeenCalledWith({ body: { message: 'Fail' } }); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); + const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + + muteAlertInstanceRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.post.mock.calls[0]; + const [context, req, res] = mockHandlerArguments({ rulesClient }, { params: {}, body: {} }, [ + 'ok', + ]); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('muteInstance', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/mute_instance.ts b/x-pack/plugins/alerting/server/routes/legacy/mute_instance.ts index caf224006677..be1c0876d5d2 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/mute_instance.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/mute_instance.ts @@ -6,6 +6,7 @@ */ import { schema } from '@kbn/config-schema'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import type { AlertingRouter } from '../../types'; import { ILicenseState } from '../../lib/license_state'; import { verifyApiAccess } from '../../lib/license_api_access'; @@ -13,13 +14,18 @@ import { LEGACY_BASE_ALERT_API_PATH } from '../../../common'; import { renameKeys } from './../lib/rename_keys'; import { MuteOptions } from '../../rules_client'; import { AlertTypeDisabledError } from '../../lib/errors/alert_type_disabled'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const paramSchema = schema.object({ alert_id: schema.string(), alert_instance_id: schema.string(), }); -export const muteAlertInstanceRoute = (router: AlertingRouter, licenseState: ILicenseState) => { +export const muteAlertInstanceRoute = ( + router: AlertingRouter, + licenseState: ILicenseState, + usageCounter?: UsageCounter +) => { router.post( { path: `${LEGACY_BASE_ALERT_API_PATH}/alert/{alert_id}/alert_instance/{alert_instance_id}/_mute`, @@ -32,6 +38,9 @@ export const muteAlertInstanceRoute = (router: AlertingRouter, licenseState: ILi if (!context.alerting) { return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); } + + trackLegacyRouteUsage('muteInstance', usageCounter); + const rulesClient = context.alerting.getRulesClient(); const renameMap = { diff --git a/x-pack/plugins/alerting/server/routes/legacy/unmute_all.test.ts b/x-pack/plugins/alerting/server/routes/legacy/unmute_all.test.ts index 49bfb63efa80..8259d2305b94 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/unmute_all.test.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/unmute_all.test.ts @@ -5,18 +5,24 @@ * 2.0. */ +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; import { unmuteAllAlertRoute } from './unmute_all'; import { httpServiceMock } from 'src/core/server/mocks'; import { licenseStateMock } from '../../lib/license_state.mock'; import { mockHandlerArguments } from './../_mock_handler_arguments'; import { rulesClientMock } from '../../rules_client.mock'; import { AlertTypeDisabledError } from '../../lib/errors/alert_type_disabled'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const rulesClient = rulesClientMock.create(); jest.mock('../../lib/license_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + beforeEach(() => { jest.resetAllMocks(); }); @@ -77,4 +83,19 @@ describe('unmuteAllAlertRoute', () => { expect(res.forbidden).toHaveBeenCalledWith({ body: { message: 'Fail' } }); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); + const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + + unmuteAllAlertRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.post.mock.calls[0]; + const [context, req, res] = mockHandlerArguments({ rulesClient }, { params: {}, body: {} }, [ + 'ok', + ]); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('unmuteAll', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/unmute_all.ts b/x-pack/plugins/alerting/server/routes/legacy/unmute_all.ts index 4e01b20d8569..40c303e5d763 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/unmute_all.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/unmute_all.ts @@ -6,17 +6,23 @@ */ import { schema } from '@kbn/config-schema'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import type { AlertingRouter } from '../../types'; import { ILicenseState } from '../../lib/license_state'; import { verifyApiAccess } from '../../lib/license_api_access'; import { LEGACY_BASE_ALERT_API_PATH } from '../../../common'; import { AlertTypeDisabledError } from '../../lib/errors/alert_type_disabled'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const paramSchema = schema.object({ id: schema.string(), }); -export const unmuteAllAlertRoute = (router: AlertingRouter, licenseState: ILicenseState) => { +export const unmuteAllAlertRoute = ( + router: AlertingRouter, + licenseState: ILicenseState, + usageCounter?: UsageCounter +) => { router.post( { path: `${LEGACY_BASE_ALERT_API_PATH}/alert/{id}/_unmute_all`, @@ -29,6 +35,7 @@ export const unmuteAllAlertRoute = (router: AlertingRouter, licenseState: ILicen if (!context.alerting) { return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); } + trackLegacyRouteUsage('unmuteAll', usageCounter); const rulesClient = context.alerting.getRulesClient(); const { id } = req.params; try { diff --git a/x-pack/plugins/alerting/server/routes/legacy/unmute_instance.test.ts b/x-pack/plugins/alerting/server/routes/legacy/unmute_instance.test.ts index 602fb38eb9af..bbe61d715a52 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/unmute_instance.test.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/unmute_instance.test.ts @@ -5,18 +5,24 @@ * 2.0. */ +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; import { unmuteAlertInstanceRoute } from './unmute_instance'; import { httpServiceMock } from 'src/core/server/mocks'; import { licenseStateMock } from '../../lib/license_state.mock'; import { mockHandlerArguments } from './../_mock_handler_arguments'; import { rulesClientMock } from '../../rules_client.mock'; import { AlertTypeDisabledError } from '../../lib/errors/alert_type_disabled'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const rulesClient = rulesClientMock.create(); jest.mock('../../lib/license_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + beforeEach(() => { jest.resetAllMocks(); }); @@ -83,4 +89,19 @@ describe('unmuteAlertInstanceRoute', () => { expect(res.forbidden).toHaveBeenCalledWith({ body: { message: 'Fail' } }); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); + const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + + unmuteAlertInstanceRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.post.mock.calls[0]; + const [context, req, res] = mockHandlerArguments({ rulesClient }, { params: {}, body: {} }, [ + 'ok', + ]); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('unmuteInstance', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/unmute_instance.ts b/x-pack/plugins/alerting/server/routes/legacy/unmute_instance.ts index f188cfcbbf79..1c65af9961ad 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/unmute_instance.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/unmute_instance.ts @@ -6,18 +6,24 @@ */ import { schema } from '@kbn/config-schema'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import type { AlertingRouter } from '../../types'; import { ILicenseState } from '../../lib/license_state'; import { verifyApiAccess } from '../../lib/license_api_access'; import { LEGACY_BASE_ALERT_API_PATH } from '../../../common'; import { AlertTypeDisabledError } from '../../lib/errors/alert_type_disabled'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const paramSchema = schema.object({ alertId: schema.string(), alertInstanceId: schema.string(), }); -export const unmuteAlertInstanceRoute = (router: AlertingRouter, licenseState: ILicenseState) => { +export const unmuteAlertInstanceRoute = ( + router: AlertingRouter, + licenseState: ILicenseState, + usageCounter?: UsageCounter +) => { router.post( { path: `${LEGACY_BASE_ALERT_API_PATH}/alert/{alertId}/alert_instance/{alertInstanceId}/_unmute`, @@ -30,6 +36,7 @@ export const unmuteAlertInstanceRoute = (router: AlertingRouter, licenseState: I if (!context.alerting) { return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); } + trackLegacyRouteUsage('unmuteInstance', usageCounter); const rulesClient = context.alerting.getRulesClient(); const { alertId, alertInstanceId } = req.params; try { diff --git a/x-pack/plugins/alerting/server/routes/legacy/update.test.ts b/x-pack/plugins/alerting/server/routes/legacy/update.test.ts index 7e9f6a23efc6..799672c3cf43 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/update.test.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/update.test.ts @@ -5,6 +5,7 @@ * 2.0. */ +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; import { updateAlertRoute } from './update'; import { httpServiceMock } from 'src/core/server/mocks'; import { licenseStateMock } from '../../lib/license_state.mock'; @@ -13,12 +14,17 @@ import { mockHandlerArguments } from './../_mock_handler_arguments'; import { rulesClientMock } from '../../rules_client.mock'; import { AlertTypeDisabledError } from '../../lib/errors/alert_type_disabled'; import { AlertNotifyWhenType } from '../../../common'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const rulesClient = rulesClientMock.create(); jest.mock('../../lib/license_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + beforeEach(() => { jest.resetAllMocks(); }); @@ -234,4 +240,19 @@ describe('updateAlertRoute', () => { expect(res.forbidden).toHaveBeenCalledWith({ body: { message: 'Fail' } }); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); + const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + + updateAlertRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.put.mock.calls[0]; + const [context, req, res] = mockHandlerArguments({ rulesClient }, { params: {}, body: {} }, [ + 'ok', + ]); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('update', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/update.ts b/x-pack/plugins/alerting/server/routes/legacy/update.ts index 40b88b4eab06..d6e6a2b3c4ae 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/update.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/update.ts @@ -6,12 +6,14 @@ */ import { schema } from '@kbn/config-schema'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import type { AlertingRouter } from '../../types'; import { ILicenseState } from '../../lib/license_state'; import { verifyApiAccess } from '../../lib/license_api_access'; import { validateDurationSchema } from '../../lib'; import { handleDisabledApiKeysError } from './../lib/error_handler'; import { AlertTypeDisabledError } from '../../lib/errors/alert_type_disabled'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; import { AlertNotifyWhenType, LEGACY_BASE_ALERT_API_PATH, @@ -42,7 +44,11 @@ const bodySchema = schema.object({ notifyWhen: schema.nullable(schema.string({ validate: validateNotifyWhenType })), }); -export const updateAlertRoute = (router: AlertingRouter, licenseState: ILicenseState) => { +export const updateAlertRoute = ( + router: AlertingRouter, + licenseState: ILicenseState, + usageCounter?: UsageCounter +) => { router.put( { path: `${LEGACY_BASE_ALERT_API_PATH}/alert/{id}`, @@ -57,6 +63,7 @@ export const updateAlertRoute = (router: AlertingRouter, licenseState: ILicenseS if (!context.alerting) { return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); } + trackLegacyRouteUsage('update', usageCounter); const rulesClient = context.alerting.getRulesClient(); const { id } = req.params; const { name, actions, params, schedule, tags, throttle, notifyWhen } = req.body; diff --git a/x-pack/plugins/alerting/server/routes/legacy/update_api_key.test.ts b/x-pack/plugins/alerting/server/routes/legacy/update_api_key.test.ts index 6cef59834933..7c48f5fff357 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/update_api_key.test.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/update_api_key.test.ts @@ -5,18 +5,24 @@ * 2.0. */ +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; import { updateApiKeyRoute } from './update_api_key'; import { httpServiceMock } from 'src/core/server/mocks'; import { licenseStateMock } from '../../lib/license_state.mock'; import { mockHandlerArguments } from './../_mock_handler_arguments'; import { rulesClientMock } from '../../rules_client.mock'; import { AlertTypeDisabledError } from '../../lib/errors/alert_type_disabled'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const rulesClient = rulesClientMock.create(); jest.mock('../../lib/license_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + beforeEach(() => { jest.resetAllMocks(); }); @@ -79,4 +85,19 @@ describe('updateApiKeyRoute', () => { expect(res.forbidden).toHaveBeenCalledWith({ body: { message: 'Fail' } }); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); + const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + + updateApiKeyRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.post.mock.calls[0]; + const [context, req, res] = mockHandlerArguments({ rulesClient }, { params: {}, body: {} }, [ + 'ok', + ]); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('updateApiKey', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/alerting/server/routes/legacy/update_api_key.ts b/x-pack/plugins/alerting/server/routes/legacy/update_api_key.ts index 82d322d2332b..a45767851c5c 100644 --- a/x-pack/plugins/alerting/server/routes/legacy/update_api_key.ts +++ b/x-pack/plugins/alerting/server/routes/legacy/update_api_key.ts @@ -6,18 +6,24 @@ */ import { schema } from '@kbn/config-schema'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import type { AlertingRouter } from '../../types'; import { ILicenseState } from '../../lib/license_state'; import { verifyApiAccess } from '../../lib/license_api_access'; import { LEGACY_BASE_ALERT_API_PATH } from '../../../common'; import { handleDisabledApiKeysError } from './../lib/error_handler'; import { AlertTypeDisabledError } from '../../lib/errors/alert_type_disabled'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const paramSchema = schema.object({ id: schema.string(), }); -export const updateApiKeyRoute = (router: AlertingRouter, licenseState: ILicenseState) => { +export const updateApiKeyRoute = ( + router: AlertingRouter, + licenseState: ILicenseState, + usageCounter?: UsageCounter +) => { router.post( { path: `${LEGACY_BASE_ALERT_API_PATH}/alert/{id}/_update_api_key`, @@ -31,6 +37,7 @@ export const updateApiKeyRoute = (router: AlertingRouter, licenseState: ILicense if (!context.alerting) { return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); } + trackLegacyRouteUsage('updateApiKey', usageCounter); const rulesClient = context.alerting.getRulesClient(); const { id } = req.params; try {