Skip to content

Commit

Permalink
add mocks and expose telemetry url
Browse files Browse the repository at this point in the history
  • Loading branch information
Bamieh committed Sep 7, 2020
1 parent 5acc4b8 commit b84dc7f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/plugins/telemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ This plugin is responsible for sending usage data to the telemetry cluster. For

### Setup

The `setup` function does not expose any public api.
The `setup` function exposes the following interface:

- `getTelemetryUrl: () => Promise<URL>`:
An async function that resolves into the telemetry Url used to send telemetry. The url is wrapped with node's [URL constructor](https://nodejs.org/api/url.html). Here is an example on how to grab the url origin:
```
const telemetryUrl = await getTelemetryUrl();
> telemetryUrl.origin; // 'https://telemetry.elastic.co'
```
Note that the telemetry URL is a kibana.yml configuration hence it is recommended to call the `getTelemetryUrl` everytime before using the actual url.

### Start

Expand All @@ -23,7 +31,6 @@ The `start` function exposes the following interface:
Resolves to `false` if the user explicitly opted out of sending usage data to Elastic or did not choose
to opt-in or out yet after an minor or major upgrade.


### Usage

To use the exposed plugin start and setup contracts:
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/telemetry/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { configSchema, TelemetryConfigType } from './config';

export { FetcherTask } from './fetcher';
export { handleOldSettings } from './handle_old_settings';
export { TelemetryPluginsSetup, TelemetryPluginsStart } from './plugin';
export { TelemetryPluginSetup, TelemetryPluginStart } from './plugin';

export const config: PluginConfigDescriptor<TelemetryConfigType> = {
schema: configSchema,
Expand Down
46 changes: 46 additions & 0 deletions src/plugins/telemetry/server/mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.
*/

import { URL } from 'url';
import { TelemetryPluginStart, TelemetryPluginSetup } from './plugin';

export type Setup = jest.Mocked<TelemetryPluginSetup>;
export type Start = jest.Mocked<TelemetryPluginStart>;

export const telemetryPluginMock = {
createSetupContract,
createStartContract,
};

function createSetupContract(): Setup {
const telemetryUrl = new URL('https://telemetry-staging.elastic.co/xpack/MOCK_URL/send');
const setupContract: Setup = {
getTelemetryUrl: jest.fn().mockResolvedValue(telemetryUrl),
};

return setupContract;
}

function createStartContract(): Start {
const startContract: Start = {
getIsOptedIn: jest.fn(),
};

return startContract;
}
25 changes: 20 additions & 5 deletions src/plugins/telemetry/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/

import { URL } from 'url';
import { Observable } from 'rxjs';
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import {
Expand Down Expand Up @@ -55,8 +56,15 @@ interface TelemetryPluginsDepsStart {
telemetryCollectionManager: TelemetryCollectionManagerPluginStart;
}

export type TelemetryPluginsSetup = void;
export interface TelemetryPluginsStart {
export interface TelemetryPluginSetup {
/**
* Resolves into the telemetry Url used to send telemetry.
* The url is wrapped with node's [URL constructor](https://nodejs.org/api/url.html).
*/
getTelemetryUrl: () => Promise<URL>;
}

export interface TelemetryPluginStart {
/**
* Resolves `true` if the user has opted into send Elastic usage data.
* Resolves `false` if the user explicitly opted out of sending usage data to Elastic
Expand All @@ -67,7 +75,7 @@ export interface TelemetryPluginsStart {

type SavedObjectsRegisterType = CoreSetup['savedObjects']['registerType'];

export class TelemetryPlugin implements Plugin<TelemetryPluginsSetup, TelemetryPluginsStart> {
export class TelemetryPlugin implements Plugin<TelemetryPluginSetup, TelemetryPluginStart> {
private readonly logger: Logger;
private readonly currentKibanaVersion: string;
private readonly config$: Observable<TelemetryConfigType>;
Expand All @@ -90,7 +98,7 @@ export class TelemetryPlugin implements Plugin<TelemetryPluginsSetup, TelemetryP
public async setup(
{ elasticsearch, http, savedObjects }: CoreSetup,
{ usageCollection, telemetryCollectionManager }: TelemetryPluginsDepsSetup
): Promise<TelemetryPluginsSetup> {
): Promise<TelemetryPluginSetup> {
const currentKibanaVersion = this.currentKibanaVersion;
const config$ = this.config$;
const isDev = this.isDev;
Expand All @@ -109,12 +117,19 @@ export class TelemetryPlugin implements Plugin<TelemetryPluginsSetup, TelemetryP

this.registerMappings((opts) => savedObjects.registerType(opts));
this.registerUsageCollectors(usageCollection);

return {
getTelemetryUrl: async () => {
const config = await config$.pipe(take(1)).toPromise();
return new URL(config.url);
},
};
}

public async start(
core: CoreStart,
{ telemetryCollectionManager }: TelemetryPluginsDepsStart
): Promise<TelemetryPluginsStart> {
): Promise<TelemetryPluginStart> {
const { savedObjects, uiSettings } = core;
this.savedObjectsClient = savedObjects.createInternalRepository();
const savedObjectsClient = new SavedObjectsClient(this.savedObjectsClient);
Expand Down

0 comments on commit b84dc7f

Please sign in to comment.