forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.12] Create usage collectors for connectors (elastic#172280) (elast…
…ic#172774) # Backport This will backport the following commits from `main` to `8.12`: - [Create usage collectors for connectors (elastic#172280)](elastic#172280) <!--- Backport version: 8.9.7 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Chenhui Wang","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-12-07T07:04:18Z","message":"Create usage collectors for connectors (elastic#172280)\n\n## Closes https://github.com/elastic/enterprise-search-team/issues/6297\r\n\r\n## Summary\r\n\r\nThis PR adds usage collectors for connectors, to collect basic telemetry\r\nmetrics, in plugin `enterprise_search` and `serverless_search`.\r\n\r\n\r\n### Checklist\r\n\r\nDelete any items that are not applicable to this PR.\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)\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine <[email protected]>","sha":"f5c78b966b32ed3077b7795308f8902d1120013f","branchLabelMapping":{"^v8.13.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","auto-backport","v8.12.0"],"number":172280,"url":"https://github.com/elastic/kibana/pull/172280","mergeCommit":{"message":"Create usage collectors for connectors (elastic#172280)\n\n## Closes https://github.com/elastic/enterprise-search-team/issues/6297\r\n\r\n## Summary\r\n\r\nThis PR adds usage collectors for connectors, to collect basic telemetry\r\nmetrics, in plugin `enterprise_search` and `serverless_search`.\r\n\r\n\r\n### Checklist\r\n\r\nDelete any items that are not applicable to this PR.\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)\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine <[email protected]>","sha":"f5c78b966b32ed3077b7795308f8902d1120013f"}},"sourceBranch":"main","suggestedTargetBranches":["8.12"],"targetPullRequestStates":[{"branch":"8.12","label":"v8.12.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Chenhui Wang <[email protected]>
- Loading branch information
1 parent
ab3a7b2
commit f40e47c
Showing
10 changed files
with
361 additions
and
2 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
x-pack/plugins/enterprise_search/server/collectors/connectors/telemetry.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* 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 { createCollectorFetchContextMock } from '@kbn/usage-collection-plugin/server/mocks'; | ||
|
||
import { registerTelemetryUsageCollector } from './telemetry'; | ||
|
||
describe('Connectors Telemetry Usage Collector', () => { | ||
const makeUsageCollectorStub = jest.fn(); | ||
const registerStub = jest.fn(); | ||
const usageCollectionMock = { | ||
makeUsageCollector: makeUsageCollectorStub, | ||
registerCollector: registerStub, | ||
} as any; | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('registerTelemetryUsageCollector', () => { | ||
it('should make and register the usage collector', () => { | ||
registerTelemetryUsageCollector(usageCollectionMock); | ||
|
||
expect(registerStub).toHaveBeenCalledTimes(1); | ||
expect(makeUsageCollectorStub).toHaveBeenCalledTimes(1); | ||
expect(makeUsageCollectorStub.mock.calls[0][0].type).toBe('connectors'); | ||
expect(makeUsageCollectorStub.mock.calls[0][0].isReady()).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('fetchTelemetryMetrics', () => { | ||
it('should return telemetry data', async () => { | ||
const fetchContextMock = createCollectorFetchContextMock(); | ||
fetchContextMock.esClient.count = jest.fn().mockImplementation((query: any) => | ||
Promise.resolve({ | ||
count: query.query.bool.filter[0].term.is_native ? 5 : 2, | ||
}) | ||
); | ||
registerTelemetryUsageCollector(usageCollectionMock); | ||
const telemetryMetrics = await makeUsageCollectorStub.mock.calls[0][0].fetch( | ||
fetchContextMock | ||
); | ||
|
||
expect(telemetryMetrics).toEqual({ | ||
native: { | ||
total: 5, | ||
}, | ||
clients: { | ||
total: 2, | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
98 changes: 98 additions & 0 deletions
98
x-pack/plugins/enterprise_search/server/collectors/connectors/telemetry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* 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 { ElasticsearchClient } from '@kbn/core/server'; | ||
|
||
import { CONNECTORS_INDEX } from '@kbn/search-connectors'; | ||
import { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server'; | ||
|
||
interface Telemetry { | ||
native: { | ||
total: number; | ||
}; | ||
clients: { | ||
total: number; | ||
}; | ||
} | ||
|
||
/** | ||
* Register the telemetry collector | ||
*/ | ||
|
||
export const registerTelemetryUsageCollector = (usageCollection: UsageCollectionSetup) => { | ||
const telemetryUsageCollector = usageCollection.makeUsageCollector<Telemetry>({ | ||
type: 'connectors', | ||
isReady: () => true, | ||
schema: { | ||
native: { | ||
total: { type: 'long' }, | ||
}, | ||
clients: { | ||
total: { type: 'long' }, | ||
}, | ||
}, | ||
async fetch({ esClient }) { | ||
return await fetchTelemetryMetrics(esClient); | ||
}, | ||
}); | ||
usageCollection.registerCollector(telemetryUsageCollector); | ||
}; | ||
|
||
/** | ||
* Fetch the aggregated telemetry metrics | ||
*/ | ||
|
||
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', | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}), | ||
client.count({ | ||
index: CONNECTORS_INDEX, | ||
query: { | ||
bool: { | ||
filter: [ | ||
{ | ||
term: { | ||
is_native: false, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}), | ||
]); | ||
|
||
return { | ||
native: { | ||
total: nativeCountResponse.count, | ||
}, | ||
clients: { | ||
total: clientsCountResponse.count, | ||
}, | ||
} as Telemetry; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
], | ||
"optionalPlugins": [ | ||
"indexManagement", | ||
"usageCollection", | ||
], | ||
"requiredBundles": [ | ||
"kibanaReact" | ||
|
57 changes: 57 additions & 0 deletions
57
x-pack/plugins/serverless_search/server/collectors/connectors/telemetry.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* 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 { registerTelemetryUsageCollector } from './telemetry'; | ||
import { createCollectorFetchContextMock } from '@kbn/usage-collection-plugin/server/mocks'; | ||
|
||
describe('Connectors Serverless Telemetry Usage Collector', () => { | ||
const makeUsageCollectorStub = jest.fn(); | ||
const registerStub = jest.fn(); | ||
const usageCollectionMock = { | ||
makeUsageCollector: makeUsageCollectorStub, | ||
registerCollector: registerStub, | ||
} as any; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('registerTelemetryUsageCollector', () => { | ||
it('should make and register the usage collector', () => { | ||
registerTelemetryUsageCollector(usageCollectionMock); | ||
|
||
expect(registerStub).toHaveBeenCalledTimes(1); | ||
expect(makeUsageCollectorStub).toHaveBeenCalledTimes(1); | ||
expect(makeUsageCollectorStub.mock.calls[0][0].type).toBe('connectors_serverless'); | ||
expect(makeUsageCollectorStub.mock.calls[0][0].isReady()).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('fetchTelemetryMetrics', () => { | ||
it('should return telemetry data', async () => { | ||
const fetchContextMock = createCollectorFetchContextMock(); | ||
fetchContextMock.esClient.count = jest.fn().mockImplementation((query: any) => | ||
Promise.resolve({ | ||
count: query.query.bool.filter[0].term.is_native ? 5 : 2, | ||
}) | ||
); | ||
registerTelemetryUsageCollector(usageCollectionMock); | ||
const telemetryMetrics = await makeUsageCollectorStub.mock.calls[0][0].fetch( | ||
fetchContextMock | ||
); | ||
|
||
expect(telemetryMetrics).toEqual({ | ||
native: { | ||
total: 5, | ||
}, | ||
clients: { | ||
total: 2, | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
98 changes: 98 additions & 0 deletions
98
x-pack/plugins/serverless_search/server/collectors/connectors/telemetry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* 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 { ElasticsearchClient } from '@kbn/core/server'; | ||
|
||
import { CONNECTORS_INDEX } from '@kbn/search-connectors'; | ||
import { UsageCollectionSetup } from '@kbn/usage-collection-plugin/server'; | ||
|
||
interface Telemetry { | ||
native: { | ||
total: number; | ||
}; | ||
clients: { | ||
total: number; | ||
}; | ||
} | ||
|
||
/** | ||
* Register the telemetry collector | ||
*/ | ||
|
||
export const registerTelemetryUsageCollector = (usageCollection: UsageCollectionSetup) => { | ||
const telemetryUsageCollector = usageCollection.makeUsageCollector<Telemetry>({ | ||
type: 'connectors_serverless', | ||
isReady: () => true, | ||
schema: { | ||
native: { | ||
total: { type: 'long' }, | ||
}, | ||
clients: { | ||
total: { type: 'long' }, | ||
}, | ||
}, | ||
async fetch({ esClient }) { | ||
return await fetchTelemetryMetrics(esClient); | ||
}, | ||
}); | ||
usageCollection.registerCollector(telemetryUsageCollector); | ||
}; | ||
|
||
/** | ||
* Fetch the aggregated telemetry metrics | ||
*/ | ||
|
||
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', | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}), | ||
client.count({ | ||
index: CONNECTORS_INDEX, | ||
query: { | ||
bool: { | ||
filter: [ | ||
{ | ||
term: { | ||
is_native: false, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}), | ||
]); | ||
|
||
return { | ||
native: { | ||
total: nativeCountResponse.count, | ||
}, | ||
clients: { | ||
total: clientsCountResponse.count, | ||
}, | ||
} as Telemetry; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.