Skip to content

Commit

Permalink
[Alerting] Track fields usage in find api (#112096)
Browse files Browse the repository at this point in the history
* Track fields usage

* Add test

* Add missing change
  • Loading branch information
chrisronline authored Sep 20, 2021
1 parent 6c73b2a commit 49977b2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
39 changes: 37 additions & 2 deletions x-pack/plugins/alerting/server/routes/find_rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +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 { findRulesRoute } from './find_rules';
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 { trackLegacyTerminology } from './lib/track_legacy_terminology';
import { usageCountersServiceMock } from 'src/plugins/usage_collection/server/usage_counters/usage_counters_service.mock';

const rulesClient = rulesClientMock.create();
const mockUsageCountersSetup = usageCountersServiceMock.createSetupContract();
Expand Down Expand Up @@ -187,4 +186,40 @@ describe('findRulesRoute', () => {
'alertTypeId',
]);
});

it('should track calls to deprecated functionality', async () => {
const licenseState = licenseStateMock.create();
const router = httpServiceMock.createRouter();

findRulesRoute(router, licenseState, mockUsageCounter);

const findResult = {
page: 1,
perPage: 1,
total: 0,
data: [],
};
rulesClient.find.mockResolvedValueOnce(findResult);

const [, handler] = router.get.mock.calls[0];
const [context, req, res] = mockHandlerArguments(
{ rulesClient },
{
params: {},
query: {
fields: ['foo', 'bar'],
per_page: 1,
page: 1,
default_search_operator: 'OR',
},
},
['ok']
);
await handler(context, req, res);
expect(mockUsageCounter.incrementCounter).toHaveBeenCalledWith({
counterName: `alertingFieldsUsage`,
counterType: 'alertingFieldsUsage',
incrementBy: 1,
});
});
});
8 changes: 8 additions & 0 deletions x-pack/plugins/alerting/server/routes/find_rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ export const findRulesRoute = (
search_fields: searchFieldsAsArray(req.query.search_fields),
});

if (req.query.fields) {
usageCounter?.incrementCounter({
counterName: `alertingFieldsUsage`,
counterType: 'alertingFieldsUsage',
incrementBy: 1,
});
}

const findResult = await rulesClient.find({ options });
return res.ok({
body: rewriteBodyRes(findResult),
Expand Down
26 changes: 26 additions & 0 deletions x-pack/plugins/alerting/server/routes/legacy/find.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,30 @@ describe('findAlertRoute', () => {
'alertTypeId',
]);
});

it('should track calls to deprecated functionality', 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: {
fields: ['foo', 'bar'],
},
},
['ok']
);
await handler(context, req, res);
expect(mockUsageCounter.incrementCounter).toHaveBeenCalledWith({
counterName: `legacyAlertingFieldsUsage`,
counterType: 'alertingFieldsUsage',
incrementBy: 1,
});
});
});
8 changes: 8 additions & 0 deletions x-pack/plugins/alerting/server/routes/legacy/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ export const findAlertRoute = (
: [query.search_fields];
}

if (query.fields) {
usageCounter?.incrementCounter({
counterName: `legacyAlertingFieldsUsage`,
counterType: 'alertingFieldsUsage',
incrementBy: 1,
});
}

const findResult = await rulesClient.find({ options });
return res.ok({
body: findResult,
Expand Down

0 comments on commit 49977b2

Please sign in to comment.