Skip to content

Commit

Permalink
[Telemetry] send X-Elastic-Stack-Version and add some tests (#80942)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bamieh authored Oct 19, 2020
1 parent dbb4942 commit 8b6b1aa
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/plugins/telemetry/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ import { TelemetryPluginStart, TelemetryPluginSetup, TelemetryPluginConfig } fro

export interface TelemetryServiceMockOptions {
reportOptInStatusChange?: boolean;
currentKibanaVersion?: string;
config?: Partial<TelemetryPluginConfig>;
}

export function mockTelemetryService({
reportOptInStatusChange,
currentKibanaVersion = 'mockKibanaVersion',
config: configOverride = {},
}: TelemetryServiceMockOptions = {}) {
const config = {
Expand All @@ -56,6 +58,7 @@ export function mockTelemetryService({
config,
http: httpServiceMock.createStartContract(),
notifications: notificationServiceMock.createStartContract(),
currentKibanaVersion,
reportOptInStatusChange,
});

Expand Down
8 changes: 7 additions & 1 deletion src/plugins/telemetry/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ export class TelemetryPlugin implements Plugin<TelemetryPluginSetup, TelemetryPl

public setup({ http, notifications }: CoreSetup): TelemetryPluginSetup {
const config = this.config;
this.telemetryService = new TelemetryService({ config, http, notifications });
const currentKibanaVersion = this.currentKibanaVersion;
this.telemetryService = new TelemetryService({
config,
http,
notifications,
currentKibanaVersion,
});

this.telemetrySender = new TelemetrySender(this.telemetryService);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ describe('TelemetrySender', () => {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Elastic-Stack-Version': telemetryService.currentKibanaVersion,
},
body: mockTelemetryPayload[0],
});
Expand Down
1 change: 1 addition & 0 deletions src/plugins/telemetry/public/services/telemetry_sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class TelemetrySender {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Elastic-Stack-Version': this.telemetryService.currentKibanaVersion,
},
body: cluster,
})
Expand Down
56 changes: 56 additions & 0 deletions src/plugins/telemetry/public/services/telemetry_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,60 @@ describe('TelemetryService', () => {
expect(telemetryService.getUserShouldSeeOptInNotice()).toBe(true);
});
});

describe('reportOptInStatus', () => {
let originalFetch: typeof window['fetch'];
let mockFetch: jest.Mock<typeof window['fetch']>;

beforeAll(() => {
originalFetch = window.fetch;
});

// @ts-ignore
beforeEach(() => (window.fetch = mockFetch = jest.fn()));
// @ts-ignore
afterAll(() => (window.fetch = originalFetch));

it('reports opt-in status to telemetry url', async () => {
const telemetryService = mockTelemetryService({
config: { userCanChangeSettings: undefined },
});
const mockPayload = ['mock_hashed_opt_in_status_payload'];
const mockUrl = 'mock_telemetry_optin_status_url';

const mockGetOptInStatusUrl = jest.fn().mockReturnValue(mockUrl);
telemetryService.getOptInStatusUrl = mockGetOptInStatusUrl;
const result = await telemetryService['reportOptInStatus'](mockPayload);
expect(result).toBeUndefined();
expect(mockGetOptInStatusUrl).toBeCalledTimes(1);
expect(mockFetch).toBeCalledTimes(1);

expect(mockFetch).toBeCalledWith(mockUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Elastic-Stack-Version': 'mockKibanaVersion',
},
body: JSON.stringify(mockPayload),
});
});

it('swallows errors if fetch fails', async () => {
const telemetryService = mockTelemetryService({
config: { userCanChangeSettings: undefined },
});
const mockPayload = ['mock_hashed_opt_in_status_payload'];
const mockUrl = 'mock_telemetry_optin_status_url';

const mockGetOptInStatusUrl = jest.fn().mockReturnValue(mockUrl);
mockFetch.mockImplementation(() => {
throw Error('Error sending usage');
});

telemetryService.getOptInStatusUrl = mockGetOptInStatusUrl;
const result = await telemetryService['reportOptInStatus'](mockPayload);
expect(result).toBeUndefined();
expect(mockFetch).toHaveBeenCalledTimes(1);
});
});
});
6 changes: 6 additions & 0 deletions src/plugins/telemetry/public/services/telemetry_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface TelemetryServiceConstructor {
config: TelemetryPluginConfig;
http: CoreStart['http'];
notifications: CoreStart['notifications'];
currentKibanaVersion: string;
reportOptInStatusChange?: boolean;
}

Expand All @@ -36,15 +37,19 @@ export class TelemetryService {
private readonly defaultConfig: TelemetryPluginConfig;
private updatedConfig?: TelemetryPluginConfig;

public readonly currentKibanaVersion: string;

constructor({
config,
http,
notifications,
currentKibanaVersion,
reportOptInStatusChange = true,
}: TelemetryServiceConstructor) {
this.defaultConfig = config;
this.reportOptInStatusChange = reportOptInStatusChange;
this.notifications = notifications;
this.currentKibanaVersion = currentKibanaVersion;
this.http = http;
}

Expand Down Expand Up @@ -194,6 +199,7 @@ export class TelemetryService {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Elastic-Stack-Version': this.currentKibanaVersion,
},
body: JSON.stringify(optInPayload),
});
Expand Down
1 change: 1 addition & 0 deletions src/plugins/telemetry/server/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ export class FetcherTask {
await fetch(url, {
method: 'post',
body: cluster,
headers: { 'X-Elastic-Stack-Version': this.currentKibanaVersion },
});
}
}
2 changes: 1 addition & 1 deletion src/plugins/telemetry/server/routes/telemetry_opt_in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function registerTelemetryOptInRoutes({
const optInStatusUrl = config.optInStatusUrl;
sendTelemetryOptInStatus(
telemetryCollectionManager,
{ optInStatusUrl, newOptInStatus },
{ optInStatusUrl, newOptInStatus, currentKibanaVersion },
statsGetterConfig
).catch((err) => {
// The server is likely behind a firewall and can't reach the remote service
Expand Down
54 changes: 54 additions & 0 deletions src/plugins/telemetry/server/routes/telemetry_opt_in_stats.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

jest.mock('node-fetch');
// @ts-ignore
import fetch from 'node-fetch';
import { sendTelemetryOptInStatus } from './telemetry_opt_in_stats';
import { StatsGetterConfig } from 'src/plugins/telemetry_collection_manager/server';

describe('sendTelemetryOptInStatus', () => {
it('calls fetch with the opt in status returned from the telemetryCollectionManager', async () => {
const mockOptInStatus = ['mock_opt_in_hashed_value'];
const mockTelemetryCollectionManager = {
getOptInStats: jest.fn().mockResolvedValue(mockOptInStatus),
};
const mockConfig = {
optInStatusUrl: 'some_url',
newOptInStatus: true,
currentKibanaVersion: 'mock_kibana_version',
};
const mockStatsGetterConfig = {
unencrypted: false,
};

const result = await sendTelemetryOptInStatus(
mockTelemetryCollectionManager,
mockConfig,
mockStatsGetterConfig as StatsGetterConfig
);
expect(result).toBeUndefined();
expect(fetch).toBeCalledTimes(1);
expect(fetch).toBeCalledWith(mockConfig.optInStatusUrl, {
method: 'post',
body: mockOptInStatus,
headers: { 'X-Elastic-Stack-Version': mockConfig.currentKibanaVersion },
});
});
});
6 changes: 4 additions & 2 deletions src/plugins/telemetry/server/routes/telemetry_opt_in_stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ import {
interface SendTelemetryOptInStatusConfig {
optInStatusUrl: string;
newOptInStatus: boolean;
currentKibanaVersion: string;
}

export async function sendTelemetryOptInStatus(
telemetryCollectionManager: TelemetryCollectionManagerPluginSetup,
telemetryCollectionManager: Pick<TelemetryCollectionManagerPluginSetup, 'getOptInStats'>,
config: SendTelemetryOptInStatusConfig,
statsGetterConfig: StatsGetterConfig
) {
const { optInStatusUrl, newOptInStatus } = config;
const { optInStatusUrl, newOptInStatus, currentKibanaVersion } = config;
const optInStatus = await telemetryCollectionManager.getOptInStats(
newOptInStatus,
statsGetterConfig
Expand All @@ -47,6 +48,7 @@ export async function sendTelemetryOptInStatus(
await fetch(optInStatusUrl, {
method: 'post',
body: optInStatus,
headers: { 'X-Elastic-Stack-Version': currentKibanaVersion },
});
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe('TelemetryManagementSectionComponent', () => {
sendUsageFrom: 'browser',
},
reportOptInStatusChange: false,
currentKibanaVersion: 'mock_kibana_version',
notifications: coreStart.notifications,
http: coreSetup.http,
});
Expand Down Expand Up @@ -71,6 +72,7 @@ describe('TelemetryManagementSectionComponent', () => {
},
reportOptInStatusChange: false,
notifications: coreStart.notifications,
currentKibanaVersion: 'mock_kibana_version',
http: coreSetup.http,
});

Expand Down Expand Up @@ -120,6 +122,7 @@ describe('TelemetryManagementSectionComponent', () => {
},
reportOptInStatusChange: false,
notifications: coreStart.notifications,
currentKibanaVersion: 'mock_kibana_version',
http: coreSetup.http,
});

Expand Down Expand Up @@ -164,6 +167,7 @@ describe('TelemetryManagementSectionComponent', () => {
},
reportOptInStatusChange: false,
notifications: coreStart.notifications,
currentKibanaVersion: 'mock_kibana_version',
http: coreSetup.http,
});

Expand Down Expand Up @@ -199,6 +203,7 @@ describe('TelemetryManagementSectionComponent', () => {
},
reportOptInStatusChange: false,
notifications: coreStart.notifications,
currentKibanaVersion: 'mock_kibana_version',
http: coreSetup.http,
});

Expand Down Expand Up @@ -235,6 +240,7 @@ describe('TelemetryManagementSectionComponent', () => {
},
reportOptInStatusChange: false,
notifications: coreStart.notifications,
currentKibanaVersion: 'mock_kibana_version',
http: coreSetup.http,
});

Expand Down Expand Up @@ -271,6 +277,7 @@ describe('TelemetryManagementSectionComponent', () => {
},
reportOptInStatusChange: false,
notifications: coreStart.notifications,
currentKibanaVersion: 'mock_kibana_version',
http: coreSetup.http,
});

Expand Down Expand Up @@ -316,6 +323,7 @@ describe('TelemetryManagementSectionComponent', () => {
},
reportOptInStatusChange: false,
notifications: coreStart.notifications,
currentKibanaVersion: 'mock_kibana_version',
http: coreSetup.http,
});

Expand Down

0 comments on commit 8b6b1aa

Please sign in to comment.