[8.12] Catch IndexNotFoundException and return default telemetry (#173604) (#173619)

# Backport

This will backport the following commits from `main` to `8.12`:
- [Catch IndexNotFoundException and return default telemetry
(#173604)](https://github.com/elastic/kibana/pull/173604)

<!--- Backport version: 8.9.7 -->

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

<!--BACKPORT [{"author":{"name":"Chenhui
Wang","email":"54903978+wangch079@users.noreply.github.com"},"sourceCommit":{"committedDate":"2023-12-19T12:28:24Z","message":"Catch
IndexNotFoundException and return default telemetry (#173604)\n\n##
Follow up on PR https://github.com/elastic/kibana/pull/172280\r\n\r\n##
Summary\r\n\r\nThis PR makes changes to usage collectors in plugin
`enterprise_search`\r\nand `serverless_search`, to catch
`IndexNotFoundException` and return\r\nthe default
telemetry.\r\n\r\n\r\n### Checklist\r\n\r\n- [x ] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n\r\n### For
maintainers\r\n\r\n- [ ] This was checked for breaking API changes and
was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"c01b3a450a2bf607ee69d9540b77859695e6e5e1","branchLabelMapping":{"^v8.13.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:EnterpriseSearch","v8.12.0","v8.13.0"],"number":173604,"url":"https://github.com/elastic/kibana/pull/173604","mergeCommit":{"message":"Catch
IndexNotFoundException and return default telemetry (#173604)\n\n##
Follow up on PR https://github.com/elastic/kibana/pull/172280\r\n\r\n##
Summary\r\n\r\nThis PR makes changes to usage collectors in plugin
`enterprise_search`\r\nand `serverless_search`, to catch
`IndexNotFoundException` and return\r\nthe default
telemetry.\r\n\r\n\r\n### Checklist\r\n\r\n- [x ] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n\r\n### For
maintainers\r\n\r\n- [ ] This was checked for breaking API changes and
was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"c01b3a450a2bf607ee69d9540b77859695e6e5e1"}},"sourceBranch":"main","suggestedTargetBranches":["8.12"],"targetPullRequestStates":[{"branch":"8.12","label":"v8.12.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.13.0","labelRegex":"^v8.13.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/173604","number":173604,"mergeCommit":{"message":"Catch
IndexNotFoundException and return default telemetry (#173604)\n\n##
Follow up on PR https://github.com/elastic/kibana/pull/172280\r\n\r\n##
Summary\r\n\r\nThis PR makes changes to usage collectors in plugin
`enterprise_search`\r\nand `serverless_search`, to catch
`IndexNotFoundException` and return\r\nthe default
telemetry.\r\n\r\n\r\n### Checklist\r\n\r\n- [x ] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n\r\n### For
maintainers\r\n\r\n- [ ] This was checked for breaking API changes and
was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)","sha":"c01b3a450a2bf607ee69d9540b77859695e6e5e1"}}]}]
BACKPORT-->

Co-authored-by: Chenhui Wang <54903978+wangch079@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2023-12-19 08:50:58 -05:00 committed by GitHub
parent 7bbef5c249
commit 7e6ab21b0a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 177 additions and 86 deletions

View file

@ -9,6 +9,16 @@ import { createCollectorFetchContextMock } from '@kbn/usage-collection-plugin/se
import { registerTelemetryUsageCollector } from './telemetry';
const indexNotFoundError = {
meta: {
body: {
error: {
type: 'index_not_found_exception',
},
},
},
};
describe('Connectors Telemetry Usage Collector', () => {
const makeUsageCollectorStub = jest.fn();
const registerStub = jest.fn();
@ -53,5 +63,23 @@ describe('Connectors Telemetry Usage Collector', () => {
},
});
});
it('should return default telemetry on index not found error', async () => {
const fetchContextMock = createCollectorFetchContextMock();
fetchContextMock.esClient.count = jest
.fn()
.mockImplementation(() => Promise.reject(indexNotFoundError));
registerTelemetryUsageCollector(usageCollectionMock);
const telemetryMetrics = await makeUsageCollectorStub.mock.calls[0][0].fetch(
fetchContextMock
);
expect(telemetryMetrics).toEqual({
native: {
total: 0,
},
clients: {
total: 0,
},
});
});
});
});

View file

@ -10,6 +10,8 @@ import { ElasticsearchClient } from '@kbn/core/server';
import { CONNECTORS_INDEX } from '@kbn/search-connectors';
import { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server';
import { isIndexNotFoundException } from '../../utils/identify_exceptions';
interface Telemetry {
native: {
total: number;
@ -19,6 +21,15 @@ interface Telemetry {
};
}
const defaultTelemetryMetrics: Telemetry = {
native: {
total: 0,
},
clients: {
total: 0,
},
};
/**
* Register the telemetry collector
*/
@ -46,53 +57,60 @@ export const registerTelemetryUsageCollector = (usageCollection: UsageCollection
* Fetch the aggregated telemetry metrics
*/
// @ts-ignore
export const fetchTelemetryMetrics = async (client: ElasticsearchClient): Promise<Telemetry> => {
const [nativeCountResponse, clientsCountResponse] = await Promise.all([
client.count({
index: CONNECTORS_INDEX,
query: {
bool: {
filter: [
{
term: {
is_native: true,
},
},
],
must_not: [
{
term: {
service_type: {
value: 'elastic-crawler',
try {
const [nativeCountResponse, clientsCountResponse] = await Promise.all([
client.count({
index: CONNECTORS_INDEX,
query: {
bool: {
filter: [
{
term: {
is_native: true,
},
},
},
],
},
},
}),
client.count({
index: CONNECTORS_INDEX,
query: {
bool: {
filter: [
{
term: {
is_native: false,
],
must_not: [
{
term: {
service_type: {
value: 'elastic-crawler',
},
},
},
},
],
],
},
},
},
}),
]);
}),
client.count({
index: CONNECTORS_INDEX,
query: {
bool: {
filter: [
{
term: {
is_native: false,
},
},
],
},
},
}),
]);
return {
native: {
total: nativeCountResponse.count,
},
clients: {
total: clientsCountResponse.count,
},
} as Telemetry;
return {
native: {
total: nativeCountResponse.count,
},
clients: {
total: clientsCountResponse.count,
},
} as Telemetry;
} catch (error) {
if (isIndexNotFoundException(error)) {
return defaultTelemetryMetrics;
}
}
};

View file

@ -8,6 +8,16 @@
import { registerTelemetryUsageCollector } from './telemetry';
import { createCollectorFetchContextMock } from '@kbn/usage-collection-plugin/server/mocks';
const indexNotFoundError = {
meta: {
body: {
error: {
type: 'index_not_found_exception',
},
},
},
};
describe('Connectors Serverless Telemetry Usage Collector', () => {
const makeUsageCollectorStub = jest.fn();
const registerStub = jest.fn();
@ -53,5 +63,23 @@ describe('Connectors Serverless Telemetry Usage Collector', () => {
},
});
});
it('should return default telemetry on index not found error', async () => {
const fetchContextMock = createCollectorFetchContextMock();
fetchContextMock.esClient.count = jest
.fn()
.mockImplementation(() => Promise.reject(indexNotFoundError));
registerTelemetryUsageCollector(usageCollectionMock);
const telemetryMetrics = await makeUsageCollectorStub.mock.calls[0][0].fetch(
fetchContextMock
);
expect(telemetryMetrics).toEqual({
native: {
total: 0,
},
clients: {
total: 0,
},
});
});
});
});

View file

@ -9,6 +9,7 @@ import { ElasticsearchClient } from '@kbn/core/server';
import { CONNECTORS_INDEX } from '@kbn/search-connectors';
import { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server';
import { isIndexNotFoundException } from '@kbn/search-connectors/utils/identify_exceptions';
interface Telemetry {
native: {
@ -19,6 +20,15 @@ interface Telemetry {
};
}
const defaultTelemetryMetrics: Telemetry = {
native: {
total: 0,
},
clients: {
total: 0,
},
};
/**
* Register the telemetry collector
*/
@ -46,53 +56,60 @@ export const registerTelemetryUsageCollector = (usageCollection: UsageCollection
* Fetch the aggregated telemetry metrics
*/
// @ts-ignore
export const fetchTelemetryMetrics = async (client: ElasticsearchClient): Promise<Telemetry> => {
const [nativeCountResponse, clientsCountResponse] = await Promise.all([
client.count({
index: CONNECTORS_INDEX,
query: {
bool: {
filter: [
{
term: {
is_native: true,
},
},
],
must_not: [
{
term: {
service_type: {
value: 'elastic-crawler',
try {
const [nativeCountResponse, clientsCountResponse] = await Promise.all([
client.count({
index: CONNECTORS_INDEX,
query: {
bool: {
filter: [
{
term: {
is_native: true,
},
},
},
],
},
},
}),
client.count({
index: CONNECTORS_INDEX,
query: {
bool: {
filter: [
{
term: {
is_native: false,
],
must_not: [
{
term: {
service_type: {
value: 'elastic-crawler',
},
},
},
},
],
],
},
},
},
}),
]);
}),
client.count({
index: CONNECTORS_INDEX,
query: {
bool: {
filter: [
{
term: {
is_native: false,
},
},
],
},
},
}),
]);
return {
native: {
total: nativeCountResponse.count,
},
clients: {
total: clientsCountResponse.count,
},
} as Telemetry;
return {
native: {
total: nativeCountResponse.count,
},
clients: {
total: clientsCountResponse.count,
},
} as Telemetry;
} catch (error) {
if (isIndexNotFoundException(error)) {
return defaultTelemetryMetrics;
}
}
};