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

[Alerting] Track fields usage in find api #112096

Merged
merged 5 commits into from
Sep 20, 2021
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
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