Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.9] [Telemetry] Synchronous setup and start methods (#79457) #79642

Merged
merged 1 commit into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/plugins/telemetry/server/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import moment from 'moment';
import { Observable } from 'rxjs';
import { Observable, Subscription, timer } from 'rxjs';
import { take } from 'rxjs/operators';
// @ts-ignore
import fetch from 'node-fetch';
Expand Down Expand Up @@ -58,7 +58,7 @@ export class FetcherTask {
private readonly config$: Observable<TelemetryConfigType>;
private readonly currentKibanaVersion: string;
private readonly logger: Logger;
private intervalId?: NodeJS.Timeout;
private intervalId?: Subscription;
private lastReported?: number;
private isSending = false;
private internalRepository?: SavedObjectsClientContract;
Expand All @@ -79,15 +79,14 @@ export class FetcherTask {
this.telemetryCollectionManager = telemetryCollectionManager;
this.elasticsearchClient = elasticsearch.legacy.createClient('telemetry-fetcher');

setTimeout(() => {
this.sendIfDue();
this.intervalId = setInterval(() => this.sendIfDue(), this.checkIntervalMs);
}, this.initialCheckDelayMs);
this.intervalId = timer(this.initialCheckDelayMs, this.checkIntervalMs).subscribe(() =>
this.sendIfDue()
);
}

public stop() {
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId.unsubscribe();
}
if (this.elasticsearchClient) {
this.elasticsearchClient.close();
Expand Down
33 changes: 26 additions & 7 deletions src/plugins/telemetry/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@
* under the License.
*/

import { Observable } from 'rxjs';
import { AsyncSubject, Observable } from 'rxjs';
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import {
TelemetryCollectionManagerPluginSetup,
TelemetryCollectionManagerPluginStart,
} from 'src/plugins/telemetry_collection_manager/server';
import { take } from 'rxjs/operators';
import {
CoreSetup,
PluginInitializerContext,
ISavedObjectsRepository,
CoreStart,
IUiSettingsClient,
SavedObjectsClient,
Plugin,
Logger,
UiSettingsServiceStart,
} from '../../../core/server';
import { registerRoutes } from './routes';
import { registerCollection } from './telemetry_collection';
Expand Down Expand Up @@ -60,8 +61,11 @@ export class TelemetryPlugin implements Plugin {
private readonly config$: Observable<TelemetryConfigType>;
private readonly isDev: boolean;
private readonly fetcherTask: FetcherTask;
/**
* @private Used to mark the completion of the old UI Settings migration
*/
private readonly oldUiSettingsHandled$ = new AsyncSubject();
private savedObjectsClient?: ISavedObjectsRepository;
private uiSettingsClient?: IUiSettingsClient;

constructor(initializerContext: PluginInitializerContext<TelemetryConfigType>) {
this.logger = initializerContext.logger.get();
Expand All @@ -74,7 +78,7 @@ export class TelemetryPlugin implements Plugin {
});
}

public async setup(
public setup(
{ elasticsearch, http, savedObjects }: CoreSetup,
{ usageCollection, telemetryCollectionManager }: TelemetryPluginsSetup
) {
Expand All @@ -101,15 +105,30 @@ export class TelemetryPlugin implements Plugin {
public async start(core: CoreStart, { telemetryCollectionManager }: TelemetryPluginsStart) {
const { savedObjects, uiSettings } = core;
this.savedObjectsClient = savedObjects.createInternalRepository();
const savedObjectsClient = new SavedObjectsClient(this.savedObjectsClient);
this.uiSettingsClient = uiSettings.asScopedToClient(savedObjectsClient);

// Not catching nor awaiting these promises because they should never reject
this.handleOldUiSettings(uiSettings);
this.startFetcherWhenOldSettingsAreHandled(core, telemetryCollectionManager);
}

private async handleOldUiSettings(uiSettings: UiSettingsServiceStart) {
const savedObjectsClient = new SavedObjectsClient(this.savedObjectsClient!);
const uiSettingsClient = uiSettings.asScopedToClient(savedObjectsClient);

try {
await handleOldSettings(savedObjectsClient, this.uiSettingsClient);
await handleOldSettings(savedObjectsClient, uiSettingsClient);
} catch (error) {
this.logger.warn('Unable to update legacy telemetry configs.');
}
// Set the mark in the AsyncSubject as complete so all the methods that require this method to be completed before working, can move on
this.oldUiSettingsHandled$.complete();
}

private async startFetcherWhenOldSettingsAreHandled(
core: CoreStart,
telemetryCollectionManager: TelemetryCollectionManagerPluginStart
) {
await this.oldUiSettingsHandled$.pipe(take(1)).toPromise(); // Wait for the old settings to be handled
this.fetcherTask.start(core, { telemetryCollectionManager });
}

Expand Down