Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Response Ops] Add deprecated telemetry for _mute_all #130458

Merged
merged 1 commit into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 '@kbn/usage-collection-plugin/server/usage_counters/usage_counters_service.mock';
import { trackDeprecatedRouteUsage } from './track_deprecated_route_usage';

describe('trackDeprecatedRouteUsage', () => {
it('should call `usageCounter.incrementCounter`', () => {
const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract();
const mockUsageCounter = mockUsageCountersSetup.createUsageCounter('test');

trackDeprecatedRouteUsage('test', mockUsageCounter);
expect(mockUsageCounter.incrementCounter).toHaveBeenCalledWith({
counterName: `deprecatedRoute_test`,
counterType: 'deprecatedApiUsage',
incrementBy: 1,
});
});

it('should do nothing if no usage counter is provided', () => {
let err;
try {
trackDeprecatedRouteUsage('test', undefined);
} catch (e) {
err = e;
}
expect(err).toBeUndefined();
});
});
18 changes: 18 additions & 0 deletions x-pack/plugins/alerting/server/lib/track_deprecated_route_usage.ts
Original file line number Diff line number Diff line change
@@ -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 '@kbn/usage-collection-plugin/server';

export function trackDeprecatedRouteUsage(route: string, usageCounter?: UsageCounter) {
if (usageCounter) {
usageCounter.incrementCounter({
counterName: `deprecatedRoute_${route}`,
counterType: 'deprecatedApiUsage',
incrementBy: 1,
});
}
}
2 changes: 1 addition & 1 deletion x-pack/plugins/alerting/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function defineRoutes(opts: RouteOptions) {
getRuleStateRoute(router, licenseState);
healthRoute(router, licenseState, encryptedSavedObjects);
ruleTypesRoute(router, licenseState);
muteAllRuleRoute(router, licenseState);
muteAllRuleRoute(router, licenseState, usageCounter);
muteAlertRoute(router, licenseState);
unmuteAllRuleRoute(router, licenseState);
unmuteAlertRoute(router, licenseState);
Expand Down
22 changes: 21 additions & 1 deletion x-pack/plugins/alerting/server/routes/mute_all_rule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { usageCountersServiceMock } from '@kbn/usage-collection-plugin/server/usage_counters/usage_counters_service.mock';
import { muteAllRuleRoute } from './mute_all_rule';
import { httpServiceMock } from '@kbn/core/server/mocks';
import { licenseStateMock } from '../lib/license_state.mock';
import { mockHandlerArguments } from './_mock_handler_arguments';
import { rulesClientMock } from '../rules_client.mock';
import { RuleTypeDisabledError } from '../lib/errors/rule_type_disabled';
import { trackDeprecatedRouteUsage } from '../lib/track_deprecated_route_usage';

const rulesClient = rulesClientMock.create();
jest.mock('../lib/license_api_access', () => ({
verifyApiAccess: jest.fn(),
}));

jest.mock('../lib/track_deprecated_route_usage', () => ({
trackDeprecatedRouteUsage: jest.fn(),
}));

beforeEach(() => {
jest.resetAllMocks();
});
Expand Down Expand Up @@ -77,4 +82,19 @@ describe('muteAllRuleRoute', () => {

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');

muteAllRuleRoute(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(trackDeprecatedRouteUsage).toHaveBeenCalledWith('muteAll', mockUsageCounter);
});
});
6 changes: 5 additions & 1 deletion x-pack/plugins/alerting/server/routes/mute_all_rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@

import { IRouter } from '@kbn/core/server';
import { schema } from '@kbn/config-schema';
import { UsageCounter } from '@kbn/usage-collection-plugin/server';
import { ILicenseState, RuleTypeDisabledError } from '../lib';
import { verifyAccessAndContext } from './lib';
import { AlertingRequestHandlerContext, BASE_ALERTING_API_PATH } from '../types';
import { trackDeprecatedRouteUsage } from '../lib/track_deprecated_route_usage';

const paramSchema = schema.object({
id: schema.string(),
});

export const muteAllRuleRoute = (
router: IRouter<AlertingRequestHandlerContext>,
licenseState: ILicenseState
licenseState: ILicenseState,
usageCounter?: UsageCounter
) => {
router.post(
{
Expand All @@ -30,6 +33,7 @@ export const muteAllRuleRoute = (
verifyAccessAndContext(licenseState, async function (context, req, res) {
const rulesClient = context.alerting.getRulesClient();
const { id } = req.params;
trackDeprecatedRouteUsage('muteAll', usageCounter);
try {
await rulesClient.muteAll({ id });
return res.noContent();
Expand Down