-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create usage collectors for connectors (#172280)
## Closes elastic/search-team#6297 ## Summary This PR adds usage collectors for connectors, to collect basic telemetry metrics, in plugin `enterprise_search` and `serverless_search`. ### Checklist Delete any items that are not applicable to this PR. - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios ### For maintainers - [ ] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) --------- Co-authored-by: kibanamachine <[email protected]> (cherry picked from commit f5c78b9)
- Loading branch information
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.