kibana/test/api_integration/apis/ui_counters/ui_counters.ts
Kibana Machine ff543226e8
[9.0] [UI Counters] fix flaky test retry logic (#224151) (#224167)
# Backport

This will backport the following commits from `main` to `9.0`:
- [[UI Counters] fix flaky test retry logic
(#224151)](https://github.com/elastic/kibana/pull/224151)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Ahmad
Bamieh","email":"ahmad.bamyeh@elastic.co"},"sourceCommit":{"committedDate":"2025-06-17T01:37:37Z","message":"[UI
Counters] fix flaky test retry logic (#224151)\n\nRevisit
`waitForWithTimeout` to use 5 retries and an initial
waiting\ntime\ncloses
https://github.com/elastic/kibana/issues/98240\n\n---------\n\nCo-authored-by:
kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"699212fa7eb7a5440759c8a01f095e0bf2ca9479","branchLabelMapping":{"^v9.1.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","backport:prev-minor","backport:prev-major","v9.1.0","v9.0.3"],"title":"[UI
Counters] fix flaky test retry
logic","number":224151,"url":"https://github.com/elastic/kibana/pull/224151","mergeCommit":{"message":"[UI
Counters] fix flaky test retry logic (#224151)\n\nRevisit
`waitForWithTimeout` to use 5 retries and an initial
waiting\ntime\ncloses
https://github.com/elastic/kibana/issues/98240\n\n---------\n\nCo-authored-by:
kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"699212fa7eb7a5440759c8a01f095e0bf2ca9479"}},"sourceBranch":"main","suggestedTargetBranches":["9.0"],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/224151","number":224151,"mergeCommit":{"message":"[UI
Counters] fix flaky test retry logic (#224151)\n\nRevisit
`waitForWithTimeout` to use 5 retries and an initial
waiting\ntime\ncloses
https://github.com/elastic/kibana/issues/98240\n\n---------\n\nCo-authored-by:
kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"699212fa7eb7a5440759c8a01f095e0bf2ca9479"}},{"branch":"9.0","label":"v9.0.3","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Ahmad Bamieh <ahmad.bamyeh@elastic.co>
2025-06-17 05:27:19 +02:00

161 lines
5.7 KiB
TypeScript

/*
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import expect from '@kbn/expect';
import { ReportManager, METRIC_TYPE, UiCounterMetricType, Report } from '@kbn/analytics';
import { UsageCountersSavedObject } from '@kbn/usage-collection-plugin/server';
import { X_ELASTIC_INTERNAL_ORIGIN_REQUEST } from '@kbn/core-http-common';
import { FtrProviderContext } from '../../ftr_provider_context';
const APP_NAME = 'myApp';
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');
const retry = getService('retry');
const createUiCounterEvent = (eventName: string, type: UiCounterMetricType, count = 1) => ({
eventName,
appName: APP_NAME,
type,
count,
});
const fetchUsageCountersObjects = async (): Promise<UsageCountersSavedObject[]> => {
const {
body: { saved_objects: savedObjects },
} = await supertest
.get('/api/saved_objects/_find?type=usage-counter')
.set('kbn-xsrf', 'kibana')
.set(X_ELASTIC_INTERNAL_ORIGIN_REQUEST, 'kibana')
.expect(200);
return savedObjects;
};
const sendReport = async (report: Report): Promise<void> => {
await supertest
.post('/api/ui_counters/_report')
.set('kbn-xsrf', 'kibana')
.set('content-type', 'application/json')
.set(X_ELASTIC_INTERNAL_ORIGIN_REQUEST, 'kibana')
.send({ report })
.expect(200);
};
const getCounter = (
savedObjects: UsageCountersSavedObject[],
eventName: string,
counterType: UiCounterMetricType
): UsageCountersSavedObject[] => {
const matchingEventName = savedObjects.filter(
({ attributes: { domainId, counterName } }) =>
domainId === APP_NAME && counterName === eventName
);
if (!matchingEventName.length) {
throw new Error(
`Unable to find savedObject with Event Name ${eventName}, got ${JSON.stringify(
savedObjects
)}`
);
}
const matchingCounterType = matchingEventName.filter(
({ attributes }) => attributes.counterType === counterType
);
if (!matchingCounterType.length) {
throw new Error(
`Unable to find savedObject with matching Counter type ${counterType}, got ${JSON.stringify(
savedObjects
)}`
);
}
return matchingCounterType;
};
describe('UI Counters API', () => {
beforeEach(async () => await esArchiver.emptyKibanaIndex());
it('stores ui counter events in usage counters savedObjects', async () => {
const reportManager = new ReportManager();
const eventName = 'my_event';
const counterEvent = createUiCounterEvent(eventName, METRIC_TYPE.COUNT);
const { report } = reportManager.assignReports([counterEvent]);
await sendReport(report);
// Wait for the report to be query-able in ES since sending report uses (refresh = false)
await delay(3000);
await retry.tryWithRetries(
'reported events to be stored into ES',
async () => {
const savedObjects = await fetchUsageCountersObjects();
const countTypeEvents = getCounter(savedObjects, eventName, METRIC_TYPE.COUNT);
expect(countTypeEvents.length).to.eql(1);
expect(countTypeEvents[0].attributes.count).to.eql(1);
return true;
},
{ retryCount: 6, retryDelay: 1500 }
);
});
it('supports multiple events', async () => {
const reportManager = new ReportManager();
const hrTime = process.hrtime();
const nano = hrTime[0] * 1000000000 + hrTime[1];
const firstUniqueEventName = `my_event_${nano}`;
const secondUniqueEventName = `my_event_${nano}_2`;
const { report } = reportManager.assignReports([
createUiCounterEvent(firstUniqueEventName, METRIC_TYPE.COUNT),
createUiCounterEvent(secondUniqueEventName, METRIC_TYPE.COUNT),
createUiCounterEvent(firstUniqueEventName, METRIC_TYPE.CLICK, 2),
]);
await sendReport(report);
// Wait for the report to be query-able in ES since sending report uses (refresh = false)
await delay(3000);
await retry.tryWithRetries(
'reported events to be stored into ES',
async () => {
const savedObjects = await fetchUsageCountersObjects();
const firstEventWithCountTypeEvents = getCounter(
savedObjects,
firstUniqueEventName,
METRIC_TYPE.COUNT
);
expect(firstEventWithCountTypeEvents.length).to.eql(1);
expect(firstEventWithCountTypeEvents[0].attributes.count).to.eql(1);
const firstEventWithClickTypeEvents = getCounter(
savedObjects,
firstUniqueEventName,
METRIC_TYPE.CLICK
);
expect(firstEventWithClickTypeEvents.length).to.eql(1);
expect(firstEventWithClickTypeEvents[0].attributes.count).to.eql(2);
const secondEventWithCountTypeEvents = getCounter(
savedObjects,
secondUniqueEventName,
METRIC_TYPE.COUNT
);
expect(secondEventWithCountTypeEvents.length).to.eql(1);
expect(secondEventWithCountTypeEvents[0].attributes.count).to.eql(1);
return true;
},
{ retryCount: 6, retryDelay: 1500 }
);
});
});
}