Fetch Snapshot telemetry on UI nodes only (#180833)

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Alejandro Fernández Haro 2024-04-16 23:25:07 +02:00 committed by GitHub
parent 89321dc0ab
commit e661eea406
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 50 additions and 1 deletions

View file

@ -11,6 +11,7 @@ import { coreMock } from '@kbn/core/server/mocks';
import { usageCollectionPluginMock } from '@kbn/usage-collection-plugin/server/mocks';
import { telemetryCollectionManagerPluginMock } from '@kbn/telemetry-collection-manager-plugin/server/mocks';
import { TelemetryPlugin } from './plugin';
import type { NodeRoles } from '@kbn/core-node-server';
describe('TelemetryPlugin', () => {
describe('setup', () => {
@ -66,4 +67,43 @@ describe('TelemetryPlugin', () => {
});
});
});
describe('start', () => {
describe('per node behavior', () => {
function createPluginForNodeRole(roles: Partial<NodeRoles>) {
const initializerContext = coreMock.createPluginInitializerContext();
initializerContext.node.roles = { ...initializerContext.node.roles, ...roles };
const plugin = new TelemetryPlugin(initializerContext);
// eslint-disable-next-line dot-notation
const startFetcherMock = (plugin['startFetcher'] = jest.fn());
plugin.setup(coreMock.createSetup(), {
usageCollection: usageCollectionPluginMock.createSetupContract(),
telemetryCollectionManager: telemetryCollectionManagerPluginMock.createSetupContract(),
});
plugin.start(coreMock.createStart(), {
telemetryCollectionManager: telemetryCollectionManagerPluginMock.createStartContract(),
});
return { startFetcherMock };
}
afterEach(() => {
jest.resetAllMocks();
});
it('calls startFetcher when it is a UI node', () => {
const { startFetcherMock } = createPluginForNodeRole({ ui: true });
expect(startFetcherMock).toHaveBeenCalledTimes(1);
});
it('does not call startFetcher when not a UI node', () => {
const { startFetcherMock } = createPluginForNodeRole({ ui: false });
expect(startFetcherMock).toHaveBeenCalledTimes(0);
});
});
});
});

View file

@ -103,6 +103,7 @@ export class TelemetryPlugin implements Plugin<TelemetryPluginSetup, TelemetryPl
private isOptedIn?: boolean;
private readonly isDev: boolean;
private readonly fetcherTask: FetcherTask;
private readonly shouldStartSnapshotTelemetryFetcher: boolean;
/**
* @private Used to mark the completion of the old UI Settings migration
*/
@ -133,6 +134,10 @@ export class TelemetryPlugin implements Plugin<TelemetryPluginSetup, TelemetryPl
logger: this.logger,
});
// Only generate and report the snapshot telemetry in the UI node.
// This allows better cache optimizations and allowing background tasks to focus on alerts and similar.
this.shouldStartSnapshotTelemetryFetcher = initializerContext.node.roles.ui;
// If the opt-in selection cannot be changed, set it as early as possible.
const { optIn, allowChangingOptInStatus } = this.initialConfig;
this.isOptedIn = allowChangingOptInStatus === false ? optIn : undefined;
@ -240,7 +245,10 @@ export class TelemetryPlugin implements Plugin<TelemetryPluginSetup, TelemetryPl
this.security = security;
this.startFetcher(core, telemetryCollectionManager);
if (this.shouldStartSnapshotTelemetryFetcher) {
// Only generate and report the snapshot telemetry if we are on the appropriate node role
this.startFetcher(core, telemetryCollectionManager);
}
return {
getIsOptedIn: async () => this.isOptedIn === true,

View file

@ -36,6 +36,7 @@
"@kbn/core-http-browser",
"@kbn/core-http-server",
"@kbn/analytics-collection-utils",
"@kbn/core-node-server",
],
"exclude": [
"target/**/*",