Skip to content

Commit

Permalink
[Telemetry] Skip collection when navigated by Synthetics monitors (el…
Browse files Browse the repository at this point in the history
  • Loading branch information
afharo authored Nov 14, 2023
1 parent ecb53e3 commit cedaa3f
Show file tree
Hide file tree
Showing 19 changed files with 190 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ packages/kbn-ambient-storybook-types @elastic/kibana-operations
packages/kbn-ambient-ui-types @elastic/kibana-operations
packages/kbn-analytics @elastic/kibana-core
packages/analytics/client @elastic/kibana-core
packages/analytics/utils/analytics_collection_utils @elastic/kibana-core
test/analytics/plugins/analytics_ftr_helpers @elastic/kibana-core
test/analytics/plugins/analytics_plugin_a @elastic/kibana-core
packages/analytics/shippers/elastic_v3/browser @elastic/kibana-core
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
"@kbn/alerts-ui-shared": "link:packages/kbn-alerts-ui-shared",
"@kbn/analytics": "link:packages/kbn-analytics",
"@kbn/analytics-client": "link:packages/analytics/client",
"@kbn/analytics-collection-utils": "link:packages/analytics/utils/analytics_collection_utils",
"@kbn/analytics-ftr-helpers-plugin": "link:test/analytics/plugins/analytics_ftr_helpers",
"@kbn/analytics-plugin-a-plugin": "link:test/analytics/plugins/analytics_plugin_a",
"@kbn/analytics-shippers-elastic-v3-browser": "link:packages/analytics/shippers/elastic_v3/browser",
Expand Down
3 changes: 3 additions & 0 deletions packages/analytics/utils/analytics_collection_utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @kbn/analytics-collection-utils

Empty package generated by @kbn/generate
9 changes: 9 additions & 0 deletions packages/analytics/utils/analytics_collection_utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

export { isSyntheticsMonitor } from './src';
13 changes: 13 additions & 0 deletions packages/analytics/utils/analytics_collection_utils/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

module.exports = {
preset: '@kbn/test',
rootDir: '../../../..',
roots: ['<rootDir>/packages/analytics/utils/analytics_collection_utils'],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "shared-common",
"id": "@kbn/analytics-collection-utils",
"owner": "@elastic/kibana-core"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@kbn/analytics-collection-utils",
"private": true,
"version": "1.0.0",
"license": "SSPL-1.0 OR Elastic License 2.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

export { isSyntheticsMonitor } from './is_synthetics_monitor';
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

import { isSyntheticsMonitor } from './is_synthetics_monitor';

describe('isSyntheticsMonitor', () => {
test('returns false for any user agent', () => {
expect(isSyntheticsMonitor()).toBe(false);
});

test('returns true for when the user agent contains "Elastic/Synthetics"', () => {
jest
.spyOn(window.navigator, 'userAgent', 'get')
.mockReturnValue(window.navigator.userAgent + 'Elastic/Synthetics');
expect(isSyntheticsMonitor()).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

/**
* Returns whether the current navigation is performed by a Synthetics monitor
* (and hence, telemetry should not be collected).
*/
export function isSyntheticsMonitor(): boolean {
return window.navigator.userAgent.includes('Elastic/Synthetics');
}
19 changes: 19 additions & 0 deletions packages/analytics/utils/analytics_collection_utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "../../../../tsconfig.base.json",
"compilerOptions": {
"outDir": "target/types",
"types": [
"jest",
"node",
"react"
]
},
"include": [
"**/*.ts",
"**/*.tsx",
],
"exclude": [
"target/**/*"
],
"kbn_references": []
}
12 changes: 12 additions & 0 deletions src/plugins/telemetry/public/plugin.test.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

export const isSyntheticsMonitorMock = jest.fn();
jest.doMock('@kbn/analytics-collection-utils', () => ({
isSyntheticsMonitor: isSyntheticsMonitorMock,
}));
42 changes: 42 additions & 0 deletions src/plugins/telemetry/public/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
* Side Public License, v 1.
*/

import { of } from 'rxjs';
import { ElasticV3BrowserShipper } from '@kbn/analytics-shippers-elastic-v3-browser';
import { coreMock } from '@kbn/core/public/mocks';
import { homePluginMock } from '@kbn/home-plugin/public/mocks';
import { screenshotModePluginMock } from '@kbn/screenshot-mode-plugin/public/mocks';
import { HomePublicPluginSetup } from '@kbn/home-plugin/public';
import { ScreenshotModePluginSetup } from '@kbn/screenshot-mode-plugin/public';
import { isSyntheticsMonitorMock } from './plugin.test.mock';
import { TelemetryPlugin } from './plugin';

describe('TelemetryPlugin', () => {
Expand All @@ -21,6 +23,7 @@ describe('TelemetryPlugin', () => {
beforeEach(() => {
screenshotMode = screenshotModePluginMock.createSetupContract();
home = homePluginMock.createSetupContract();
isSyntheticsMonitorMock.mockReturnValue(false);
});

describe('setup', () => {
Expand Down Expand Up @@ -82,5 +85,44 @@ describe('TelemetryPlugin', () => {
);
});
});

it('disables telemetry when in screenshot mode', async () => {
const initializerContext = coreMock.createPluginInitializerContext();

const plugin = new TelemetryPlugin(initializerContext);
const isScreenshotModeSpy = jest
.spyOn(screenshotMode, 'isScreenshotMode')
.mockReturnValue(true);
plugin.setup(coreMock.createSetup(), { screenshotMode, home });
expect(isScreenshotModeSpy).toBeCalledTimes(1);

const coreStartMock = coreMock.createStart();
coreStartMock.application = { ...coreStartMock.application, currentAppId$: of('some-app') };
const optInSpy = jest.spyOn(coreStartMock.analytics, 'optIn');
plugin.start(coreStartMock, { screenshotMode });
expect(isScreenshotModeSpy).toBeCalledTimes(2);
expect(optInSpy).toBeCalledTimes(1);
expect(optInSpy).toHaveBeenCalledWith({ global: { enabled: false } });
});

it('disables telemetry when the user agent contains Elastic/Synthetics', async () => {
const initializerContext = coreMock.createPluginInitializerContext();

const plugin = new TelemetryPlugin(initializerContext);
const isScreenshotModeSpy = jest
.spyOn(screenshotMode, 'isScreenshotMode')
.mockReturnValue(false);
plugin.setup(coreMock.createSetup(), { screenshotMode, home });
expect(isScreenshotModeSpy).toBeCalledTimes(1);

const coreStartMock = coreMock.createStart();
coreStartMock.application = { ...coreStartMock.application, currentAppId$: of('some-app') };
isSyntheticsMonitorMock.mockReturnValueOnce(true);
const optInSpy = jest.spyOn(coreStartMock.analytics, 'optIn');
plugin.start(coreStartMock, { screenshotMode });
expect(isScreenshotModeSpy).toBeCalledTimes(2);
expect(optInSpy).toBeCalledTimes(1);
expect(optInSpy).toHaveBeenCalledWith({ global: { enabled: false } });
});
});
});
25 changes: 22 additions & 3 deletions src/plugins/telemetry/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
} from '@kbn/screenshot-mode-plugin/public';
import type { HomePublicPluginSetup } from '@kbn/home-plugin/public';
import { ElasticV3BrowserShipper } from '@kbn/analytics-shippers-elastic-v3-browser';
import { isSyntheticsMonitor } from '@kbn/analytics-collection-utils';

import { BehaviorSubject, map, switchMap, tap } from 'rxjs';
import type { TelemetryConfigLabels } from '../server/config';
Expand Down Expand Up @@ -159,7 +160,7 @@ export class TelemetryPlugin
const currentKibanaVersion = this.currentKibanaVersion;
this.telemetryService = new TelemetryService({
config,
isScreenshotMode: screenshotMode.isScreenshotMode(),
isScreenshotMode: this.shouldSkipTelemetry(screenshotMode),
http,
notifications,
currentKibanaVersion,
Expand Down Expand Up @@ -200,7 +201,9 @@ export class TelemetryPlugin
this.telemetrySender = new TelemetrySender(this.telemetryService, async () => {
await this.refreshConfig(http);
analytics.optIn({
global: { enabled: this.telemetryService!.isOptedIn && !screenshotMode.isScreenshotMode() },
global: {
enabled: this.telemetryService!.isOptedIn && !this.shouldSkipTelemetry(screenshotMode),
},
});
});

Expand Down Expand Up @@ -249,12 +252,18 @@ export class TelemetryPlugin
application.currentAppId$
.pipe(
switchMap(async () => {
// Disable telemetry and terminate early if Kibana is running in a special "skip" mode
if (this.shouldSkipTelemetry(screenshotMode)) {
analytics.optIn({ global: { enabled: false } });
return;
}

// Refresh and get telemetry config
const updatedConfig = await this.refreshConfig(http);

analytics.optIn({
global: {
enabled: this.telemetryService!.isOptedIn && !screenshotMode.isScreenshotMode(),
enabled: this.telemetryService!.isOptedIn,
},
});

Expand Down Expand Up @@ -286,6 +295,16 @@ export class TelemetryPlugin
this.telemetrySender?.stop();
}

/**
* Kibana should skip telemetry collection if reporting is taking a screenshot
* or Synthetics monitoring is navigating Kibana.
* @param screenshotMode {@link ScreenshotModePluginSetup}
* @private
*/
private shouldSkipTelemetry(screenshotMode: ScreenshotModePluginSetup): boolean {
return screenshotMode.isScreenshotMode() || isSyntheticsMonitor();
}

private getTelemetryServicePublicApis(): TelemetryServicePublicApis {
const telemetryService = this.telemetryService!;
return {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/telemetry/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@kbn/core-http-browser-mocks",
"@kbn/core-http-browser",
"@kbn/core-http-server",
"@kbn/analytics-collection-utils",
],
"exclude": [
"target/**/*",
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/usage_collection/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
HttpSetup,
} from '@kbn/core/public';
import { Storage } from '@kbn/kibana-utils-plugin/public';
import { isSyntheticsMonitor } from '@kbn/analytics-collection-utils';
import type { ScreenshotModePluginStart } from '@kbn/screenshot-mode-plugin/public';
import { createReporter, trackApplicationUsageChange } from './services';
import { ApplicationUsageContext } from './components/track_application_view';
Expand Down Expand Up @@ -150,7 +151,8 @@ export class UsageCollectionPlugin
if (
this.config.uiCounters.enabled &&
!isUnauthenticated(http) &&
!screenshotMode.isScreenshotMode()
!screenshotMode.isScreenshotMode() &&
!isSyntheticsMonitor()
) {
this.reporter.start();
this.applicationUsageTracker.start();
Expand Down
1 change: 1 addition & 0 deletions src/plugins/usage_collection/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@kbn/utility-types",
"@kbn/i18n",
"@kbn/core-http-server-mocks",
"@kbn/analytics-collection-utils",
],
"exclude": [
"target/**/*",
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
"@kbn/analytics/*": ["packages/kbn-analytics/*"],
"@kbn/analytics-client": ["packages/analytics/client"],
"@kbn/analytics-client/*": ["packages/analytics/client/*"],
"@kbn/analytics-collection-utils": ["packages/analytics/utils/analytics_collection_utils"],
"@kbn/analytics-collection-utils/*": ["packages/analytics/utils/analytics_collection_utils/*"],
"@kbn/analytics-ftr-helpers-plugin": ["test/analytics/plugins/analytics_ftr_helpers"],
"@kbn/analytics-ftr-helpers-plugin/*": ["test/analytics/plugins/analytics_ftr_helpers/*"],
"@kbn/analytics-plugin-a-plugin": ["test/analytics/plugins/analytics_plugin_a"],
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3000,6 +3000,10 @@
version "0.0.0"
uid ""

"@kbn/analytics-collection-utils@link:packages/analytics/utils/analytics_collection_utils":
version "0.0.0"
uid ""

"@kbn/analytics-ftr-helpers-plugin@link:test/analytics/plugins/analytics_ftr_helpers":
version "0.0.0"
uid ""
Expand Down

0 comments on commit cedaa3f

Please sign in to comment.