diff --git a/x-pack/plugins/actions/common/index.ts b/x-pack/plugins/actions/common/index.ts index d3abfca83c8e8..cff876b5995a1 100644 --- a/x-pack/plugins/actions/common/index.ts +++ b/x-pack/plugins/actions/common/index.ts @@ -14,3 +14,4 @@ export * from './rewrite_request_case'; export const BASE_ACTION_API_PATH = '/api/actions'; export const INTERNAL_BASE_ACTION_API_PATH = '/internal/actions'; +export const ACTIONS_FEATURE_ID = 'actions'; diff --git a/x-pack/plugins/actions/server/lib/track_legacy_route_usage.test.ts b/x-pack/plugins/actions/server/lib/track_legacy_route_usage.test.ts new file mode 100644 index 0000000000000..f4a1ce07ab985 --- /dev/null +++ b/x-pack/plugins/actions/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/actions/server/lib/track_legacy_route_usage.ts b/x-pack/plugins/actions/server/lib/track_legacy_route_usage.ts new file mode 100644 index 0000000000000..528a75a0a18bd --- /dev/null +++ b/x-pack/plugins/actions/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/actions/server/plugin.ts b/x-pack/plugins/actions/server/plugin.ts index c3dcdbb94753b..fe133ddb6f0ac 100644 --- a/x-pack/plugins/actions/server/plugin.ts +++ b/x-pack/plugins/actions/server/plugin.ts @@ -84,7 +84,7 @@ import { ensureSufficientLicense } from './lib/ensure_sufficient_license'; import { renderMustacheObject } from './lib/mustache_renderer'; import { getAlertHistoryEsIndex } from './preconfigured_connectors/alert_history_es_index/alert_history_es_index'; import { createAlertHistoryIndexTemplate } from './preconfigured_connectors/alert_history_es_index/create_alert_history_index_template'; -import { AlertHistoryEsIndexConnectorId } from '../common'; +import { ACTIONS_FEATURE_ID, AlertHistoryEsIndexConnectorId } from '../common'; import { EVENT_LOG_ACTIONS, EVENT_LOG_PROVIDER } from './constants/event_log'; export interface PluginSetupContract { @@ -263,8 +263,15 @@ export class ActionsPlugin implements Plugin(), this.licenseState); + defineRoutes( + core.http.createRouter(), + this.licenseState, + usageCounter + ); // Cleanup failed execution task definition if (this.actionsConfig.cleanupFailedExecutionsTask.enabled) { diff --git a/x-pack/plugins/actions/server/routes/index.ts b/x-pack/plugins/actions/server/routes/index.ts index 0d39d87635d5e..a6470d366c039 100644 --- a/x-pack/plugins/actions/server/routes/index.ts +++ b/x-pack/plugins/actions/server/routes/index.ts @@ -6,6 +6,7 @@ */ import { IRouter } from 'kibana/server'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import { ILicenseState } from '../lib'; import { ActionsRequestHandlerContext } from '../types'; import { createActionRoute } from './create'; @@ -20,9 +21,10 @@ import { defineLegacyRoutes } from './legacy'; export function defineRoutes( router: IRouter, - licenseState: ILicenseState + licenseState: ILicenseState, + usageCounter?: UsageCounter ) { - defineLegacyRoutes(router, licenseState); + defineLegacyRoutes(router, licenseState, usageCounter); createActionRoute(router, licenseState); deleteActionRoute(router, licenseState); diff --git a/x-pack/plugins/actions/server/routes/legacy/create.test.ts b/x-pack/plugins/actions/server/routes/legacy/create.test.ts index 3993319d1471f..9e4ab8272dd6d 100644 --- a/x-pack/plugins/actions/server/routes/legacy/create.test.ts +++ b/x-pack/plugins/actions/server/routes/legacy/create.test.ts @@ -11,11 +11,20 @@ import { licenseStateMock } from '../../lib/license_state.mock'; import { mockHandlerArguments } from './_mock_handler_arguments'; import { actionsClientMock } from '../../actions_client.mock'; import { verifyAccessAndContext } from '../verify_access_and_context'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; jest.mock('../verify_access_and_context.ts', () => ({ verifyAccessAndContext: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + +const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); +const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + beforeEach(() => { jest.resetAllMocks(); (verifyAccessAndContext as jest.Mock).mockImplementation((license, handler) => handler); @@ -128,4 +137,16 @@ describe('createActionRoute', () => { expect(handler(context, req, res)).rejects.toMatchInlineSnapshot(`[Error: OMG]`); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const actionsClient = actionsClientMock.create(); + + createActionRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.post.mock.calls[0]; + const [context, req, res] = mockHandlerArguments({ actionsClient }, {}); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('create', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/actions/server/routes/legacy/create.ts b/x-pack/plugins/actions/server/routes/legacy/create.ts index caed699641673..9713c01905b7f 100644 --- a/x-pack/plugins/actions/server/routes/legacy/create.ts +++ b/x-pack/plugins/actions/server/routes/legacy/create.ts @@ -6,11 +6,13 @@ */ import { schema } from '@kbn/config-schema'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import { IRouter } from 'kibana/server'; import { ActionsRequestHandlerContext } from '../../types'; import { ILicenseState } from '../../lib'; import { BASE_ACTION_API_PATH } from '../../../common'; import { verifyAccessAndContext } from '../verify_access_and_context'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; export const bodySchema = schema.object({ name: schema.string(), @@ -21,7 +23,8 @@ export const bodySchema = schema.object({ export const createActionRoute = ( router: IRouter, - licenseState: ILicenseState + licenseState: ILicenseState, + usageCounter?: UsageCounter ) => { router.post( { @@ -34,6 +37,7 @@ export const createActionRoute = ( verifyAccessAndContext(licenseState, async function (context, req, res) { const actionsClient = context.actions.getActionsClient(); const action = req.body; + trackLegacyRouteUsage('create', usageCounter); return res.ok({ body: await actionsClient.create({ action }), }); diff --git a/x-pack/plugins/actions/server/routes/legacy/delete.test.ts b/x-pack/plugins/actions/server/routes/legacy/delete.test.ts index cee78d998d62a..7284d18919716 100644 --- a/x-pack/plugins/actions/server/routes/legacy/delete.test.ts +++ b/x-pack/plugins/actions/server/routes/legacy/delete.test.ts @@ -11,11 +11,20 @@ import { licenseStateMock } from '../../lib/license_state.mock'; import { verifyApiAccess } from '../../lib'; import { mockHandlerArguments } from './_mock_handler_arguments'; import { actionsClientMock } from '../../mocks'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; jest.mock('../../lib/verify_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + +const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); +const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + beforeEach(() => { jest.resetAllMocks(); }); @@ -107,4 +116,23 @@ describe('deleteActionRoute', () => { expect(verifyApiAccess).toHaveBeenCalledWith(licenseState); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const actionsClient = actionsClientMock.create(); + + deleteActionRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.delete.mock.calls[0]; + const [context, req, res] = mockHandlerArguments( + { actionsClient }, + { + params: { + id: '1', + }, + } + ); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('delete', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/actions/server/routes/legacy/delete.ts b/x-pack/plugins/actions/server/routes/legacy/delete.ts index 9b3c449607b4a..656ece6f1c6f5 100644 --- a/x-pack/plugins/actions/server/routes/legacy/delete.ts +++ b/x-pack/plugins/actions/server/routes/legacy/delete.ts @@ -6,10 +6,12 @@ */ import { schema } from '@kbn/config-schema'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import { IRouter } from 'kibana/server'; import { ILicenseState, verifyApiAccess, isErrorThatHandlesItsOwnResponse } from '../../lib'; import { BASE_ACTION_API_PATH } from '../../../common'; import { ActionsRequestHandlerContext } from '../../types'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const paramSchema = schema.object({ id: schema.string(), @@ -17,7 +19,8 @@ const paramSchema = schema.object({ export const deleteActionRoute = ( router: IRouter, - licenseState: ILicenseState + licenseState: ILicenseState, + usageCounter?: UsageCounter ) => { router.delete( { @@ -33,6 +36,7 @@ export const deleteActionRoute = ( } const actionsClient = context.actions.getActionsClient(); const { id } = req.params; + trackLegacyRouteUsage('delete', usageCounter); try { await actionsClient.delete({ id }); return res.noContent(); diff --git a/x-pack/plugins/actions/server/routes/legacy/execute.test.ts b/x-pack/plugins/actions/server/routes/legacy/execute.test.ts index 05b71819911a3..61716539f6e9d 100644 --- a/x-pack/plugins/actions/server/routes/legacy/execute.test.ts +++ b/x-pack/plugins/actions/server/routes/legacy/execute.test.ts @@ -12,11 +12,20 @@ import { mockHandlerArguments } from './_mock_handler_arguments'; import { verifyApiAccess, ActionTypeDisabledError, asHttpRequestExecutionSource } from '../../lib'; import { actionsClientMock } from '../../actions_client.mock'; import { ActionTypeExecutorResult } from '../../types'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; jest.mock('../../lib/verify_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + +const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); +const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + beforeEach(() => { jest.resetAllMocks(); }); @@ -192,4 +201,16 @@ describe('executeActionRoute', () => { expect(res.forbidden).toHaveBeenCalledWith({ body: { message: 'Fail' } }); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const actionsClient = actionsClientMock.create(); + + executeActionRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.post.mock.calls[0]; + const [context, req, res] = mockHandlerArguments({ actionsClient }, { body: {}, params: {} }); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('execute', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/actions/server/routes/legacy/execute.ts b/x-pack/plugins/actions/server/routes/legacy/execute.ts index d7ed8d2e15604..33174861122d4 100644 --- a/x-pack/plugins/actions/server/routes/legacy/execute.ts +++ b/x-pack/plugins/actions/server/routes/legacy/execute.ts @@ -6,12 +6,14 @@ */ import { schema } from '@kbn/config-schema'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import { IRouter } from 'kibana/server'; import { ILicenseState, verifyApiAccess, isErrorThatHandlesItsOwnResponse } from '../../lib'; import { ActionTypeExecutorResult, ActionsRequestHandlerContext } from '../../types'; import { BASE_ACTION_API_PATH } from '../../../common'; import { asHttpRequestExecutionSource } from '../../lib/action_execution_source'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const paramSchema = schema.object({ id: schema.string(), @@ -23,7 +25,8 @@ const bodySchema = schema.object({ export const executeActionRoute = ( router: IRouter, - licenseState: ILicenseState + licenseState: ILicenseState, + usageCounter?: UsageCounter ) => { router.post( { @@ -43,6 +46,7 @@ export const executeActionRoute = ( const actionsClient = context.actions.getActionsClient(); const { params } = req.body; const { id } = req.params; + trackLegacyRouteUsage('execute', usageCounter); try { const body: ActionTypeExecutorResult = await actionsClient.execute({ params, diff --git a/x-pack/plugins/actions/server/routes/legacy/get.test.ts b/x-pack/plugins/actions/server/routes/legacy/get.test.ts index 4d1265030141f..91b8e53b64e7d 100644 --- a/x-pack/plugins/actions/server/routes/legacy/get.test.ts +++ b/x-pack/plugins/actions/server/routes/legacy/get.test.ts @@ -11,11 +11,20 @@ import { licenseStateMock } from '../../lib/license_state.mock'; import { verifyApiAccess } from '../../lib'; import { mockHandlerArguments } from './_mock_handler_arguments'; import { actionsClientMock } from '../../actions_client.mock'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; jest.mock('../../lib/verify_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + +const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); +const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + beforeEach(() => { jest.resetAllMocks(); }); @@ -133,4 +142,16 @@ describe('getActionRoute', () => { expect(verifyApiAccess).toHaveBeenCalledWith(licenseState); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const actionsClient = actionsClientMock.create(); + + getActionRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.get.mock.calls[0]; + const [context, req, res] = mockHandlerArguments({ actionsClient }, { params: { id: '1' } }); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('get', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/actions/server/routes/legacy/get.ts b/x-pack/plugins/actions/server/routes/legacy/get.ts index 44780d4f8a14b..bd8c1096979e2 100644 --- a/x-pack/plugins/actions/server/routes/legacy/get.ts +++ b/x-pack/plugins/actions/server/routes/legacy/get.ts @@ -6,10 +6,12 @@ */ import { schema } from '@kbn/config-schema'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import { IRouter } from 'kibana/server'; import { ILicenseState, verifyApiAccess } from '../../lib'; import { BASE_ACTION_API_PATH } from '../../../common'; import { ActionsRequestHandlerContext } from '../../types'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const paramSchema = schema.object({ id: schema.string(), @@ -17,7 +19,8 @@ const paramSchema = schema.object({ export const getActionRoute = ( router: IRouter, - licenseState: ILicenseState + licenseState: ILicenseState, + usageCounter?: UsageCounter ) => { router.get( { @@ -33,6 +36,7 @@ export const getActionRoute = ( } const actionsClient = context.actions.getActionsClient(); const { id } = req.params; + trackLegacyRouteUsage('get', usageCounter); return res.ok({ body: await actionsClient.get({ id }), }); diff --git a/x-pack/plugins/actions/server/routes/legacy/get_all.test.ts b/x-pack/plugins/actions/server/routes/legacy/get_all.test.ts index 7f1003e564614..b4c6792f415ea 100644 --- a/x-pack/plugins/actions/server/routes/legacy/get_all.test.ts +++ b/x-pack/plugins/actions/server/routes/legacy/get_all.test.ts @@ -11,11 +11,20 @@ import { licenseStateMock } from '../../lib/license_state.mock'; import { verifyApiAccess } from '../../lib'; import { mockHandlerArguments } from './_mock_handler_arguments'; import { actionsClientMock } from '../../actions_client.mock'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; jest.mock('../../lib/verify_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + +const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); +const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + beforeEach(() => { jest.resetAllMocks(); }); @@ -92,4 +101,16 @@ describe('getAllActionRoute', () => { expect(verifyApiAccess).toHaveBeenCalledWith(licenseState); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const actionsClient = actionsClientMock.create(); + + getAllActionRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.get.mock.calls[0]; + const [context, req, res] = mockHandlerArguments({ actionsClient }, {}); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('getAll', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/actions/server/routes/legacy/get_all.ts b/x-pack/plugins/actions/server/routes/legacy/get_all.ts index 9ea5024d8672b..6c91c2466d70d 100644 --- a/x-pack/plugins/actions/server/routes/legacy/get_all.ts +++ b/x-pack/plugins/actions/server/routes/legacy/get_all.ts @@ -6,13 +6,16 @@ */ import { IRouter } from 'kibana/server'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import { ILicenseState, verifyApiAccess } from '../../lib'; import { BASE_ACTION_API_PATH } from '../../../common'; import { ActionsRequestHandlerContext } from '../../types'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; export const getAllActionRoute = ( router: IRouter, - licenseState: ILicenseState + licenseState: ILicenseState, + usageCounter?: UsageCounter ) => { router.get( { @@ -26,6 +29,7 @@ export const getAllActionRoute = ( } const actionsClient = context.actions.getActionsClient(); const result = await actionsClient.getAll(); + trackLegacyRouteUsage('getAll', usageCounter); return res.ok({ body: result, }); diff --git a/x-pack/plugins/actions/server/routes/legacy/index.ts b/x-pack/plugins/actions/server/routes/legacy/index.ts index 1a22cd9be5681..a974237f0a95d 100644 --- a/x-pack/plugins/actions/server/routes/legacy/index.ts +++ b/x-pack/plugins/actions/server/routes/legacy/index.ts @@ -6,6 +6,7 @@ */ import { IRouter } from 'kibana/server'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import { ILicenseState } from '../../lib'; import { ActionsRequestHandlerContext } from '../../types'; import { createActionRoute } from './create'; @@ -18,13 +19,14 @@ import { executeActionRoute } from './execute'; export function defineLegacyRoutes( router: IRouter, - licenseState: ILicenseState + licenseState: ILicenseState, + usageCounter?: UsageCounter ) { - createActionRoute(router, licenseState); - deleteActionRoute(router, licenseState); - getActionRoute(router, licenseState); - getAllActionRoute(router, licenseState); - updateActionRoute(router, licenseState); - listActionTypesRoute(router, licenseState); - executeActionRoute(router, licenseState); + createActionRoute(router, licenseState, usageCounter); + deleteActionRoute(router, licenseState, usageCounter); + getActionRoute(router, licenseState, usageCounter); + getAllActionRoute(router, licenseState, usageCounter); + updateActionRoute(router, licenseState, usageCounter); + listActionTypesRoute(router, licenseState, usageCounter); + executeActionRoute(router, licenseState, usageCounter); } diff --git a/x-pack/plugins/actions/server/routes/legacy/list_action_types.test.ts b/x-pack/plugins/actions/server/routes/legacy/list_action_types.test.ts index e49dd251136ad..a6536d42f7e8a 100644 --- a/x-pack/plugins/actions/server/routes/legacy/list_action_types.test.ts +++ b/x-pack/plugins/actions/server/routes/legacy/list_action_types.test.ts @@ -12,11 +12,20 @@ import { verifyApiAccess } from '../../lib'; import { mockHandlerArguments } from './_mock_handler_arguments'; import { LicenseType } from '../../../../../plugins/licensing/server'; import { actionsClientMock } from '../../mocks'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; jest.mock('../../lib/verify_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + +const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); +const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + beforeEach(() => { jest.resetAllMocks(); }); @@ -144,4 +153,16 @@ describe('listActionTypesRoute', () => { expect(verifyApiAccess).toHaveBeenCalledWith(licenseState); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const actionsClient = actionsClientMock.create(); + + listActionTypesRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.get.mock.calls[0]; + const [context, req, res] = mockHandlerArguments({ actionsClient }, {}); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('listActionTypes', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/actions/server/routes/legacy/list_action_types.ts b/x-pack/plugins/actions/server/routes/legacy/list_action_types.ts index 814f5fffd35ff..5ce25826ca5cc 100644 --- a/x-pack/plugins/actions/server/routes/legacy/list_action_types.ts +++ b/x-pack/plugins/actions/server/routes/legacy/list_action_types.ts @@ -6,13 +6,16 @@ */ import { IRouter } from 'kibana/server'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import { ILicenseState, verifyApiAccess } from '../../lib'; import { BASE_ACTION_API_PATH } from '../../../common'; import { ActionsRequestHandlerContext } from '../../types'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; export const listActionTypesRoute = ( router: IRouter, - licenseState: ILicenseState + licenseState: ILicenseState, + usageCounter?: UsageCounter ) => { router.get( { @@ -25,6 +28,7 @@ export const listActionTypesRoute = ( return res.badRequest({ body: 'RouteHandlerContext is not registered for actions' }); } const actionsClient = context.actions.getActionsClient(); + trackLegacyRouteUsage('listActionTypes', usageCounter); return res.ok({ body: await actionsClient.listTypes(), }); diff --git a/x-pack/plugins/actions/server/routes/legacy/update.test.ts b/x-pack/plugins/actions/server/routes/legacy/update.test.ts index 0ce49751753b2..d813b765150c6 100644 --- a/x-pack/plugins/actions/server/routes/legacy/update.test.ts +++ b/x-pack/plugins/actions/server/routes/legacy/update.test.ts @@ -11,11 +11,20 @@ import { licenseStateMock } from '../../lib/license_state.mock'; import { verifyApiAccess, ActionTypeDisabledError } from '../../lib'; import { mockHandlerArguments } from './_mock_handler_arguments'; import { actionsClientMock } from '../../actions_client.mock'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; +import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock'; jest.mock('../../lib/verify_api_access.ts', () => ({ verifyApiAccess: jest.fn(), })); +jest.mock('../../lib/track_legacy_route_usage', () => ({ + trackLegacyRouteUsage: jest.fn(), +})); + +const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract(); +const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test'); + beforeEach(() => { jest.resetAllMocks(); }); @@ -182,4 +191,19 @@ describe('updateActionRoute', () => { expect(res.forbidden).toHaveBeenCalledWith({ body: { message: 'Fail' } }); }); + + it('should track every call', async () => { + const licenseState = licenseStateMock.create(); + const router = httpServiceMock.createRouter(); + const actionsClient = actionsClientMock.create(); + + updateActionRoute(router, licenseState, mockUsageCounter); + const [, handler] = router.put.mock.calls[0]; + const [context, req, res] = mockHandlerArguments( + { actionsClient }, + { params: { id: '1' }, body: {} } + ); + await handler(context, req, res); + expect(trackLegacyRouteUsage).toHaveBeenCalledWith('update', mockUsageCounter); + }); }); diff --git a/x-pack/plugins/actions/server/routes/legacy/update.ts b/x-pack/plugins/actions/server/routes/legacy/update.ts index 6cbcd56af5048..7ddce9bc799c5 100644 --- a/x-pack/plugins/actions/server/routes/legacy/update.ts +++ b/x-pack/plugins/actions/server/routes/legacy/update.ts @@ -6,10 +6,12 @@ */ import { schema } from '@kbn/config-schema'; +import { UsageCounter } from 'src/plugins/usage_collection/server'; import { IRouter } from 'kibana/server'; import { ILicenseState, verifyApiAccess, isErrorThatHandlesItsOwnResponse } from '../../lib'; import { BASE_ACTION_API_PATH } from '../../../common'; import { ActionsRequestHandlerContext } from '../../types'; +import { trackLegacyRouteUsage } from '../../lib/track_legacy_route_usage'; const paramSchema = schema.object({ id: schema.string(), @@ -23,7 +25,8 @@ const bodySchema = schema.object({ export const updateActionRoute = ( router: IRouter, - licenseState: ILicenseState + licenseState: ILicenseState, + usageCounter?: UsageCounter ) => { router.put( { @@ -41,6 +44,7 @@ export const updateActionRoute = ( const actionsClient = context.actions.getActionsClient(); const { id } = req.params; const { name, config, secrets } = req.body; + trackLegacyRouteUsage('update', usageCounter); try { return res.ok({