Skip to content

Commit

Permalink
Move oss telemetry service into NP (#56481) (#56985)
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 authored Feb 6, 2020
1 parent 3805fc1 commit 612936d
Show file tree
Hide file tree
Showing 18 changed files with 145 additions and 217 deletions.
2 changes: 0 additions & 2 deletions x-pack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import { remoteClusters } from './legacy/plugins/remote_clusters';
import { crossClusterReplication } from './legacy/plugins/cross_cluster_replication';
import { upgradeAssistant } from './legacy/plugins/upgrade_assistant';
import { uptime } from './legacy/plugins/uptime';
import { ossTelemetry } from './legacy/plugins/oss_telemetry';
import { fileUpload } from './legacy/plugins/file_upload';
import { encryptedSavedObjects } from './legacy/plugins/encrypted_saved_objects';
import { snapshotRestore } from './legacy/plugins/snapshot_restore';
Expand Down Expand Up @@ -73,7 +72,6 @@ module.exports = function(kibana) {
crossClusterReplication(kibana),
upgradeAssistant(kibana),
uptime(kibana),
ossTelemetry(kibana),
fileUpload(kibana),
encryptedSavedObjects(kibana),
lens(kibana),
Expand Down
53 changes: 0 additions & 53 deletions x-pack/legacy/plugins/oss_telemetry/index.ts

This file was deleted.

12 changes: 0 additions & 12 deletions x-pack/legacy/plugins/oss_telemetry/server/lib/collectors/index.ts

This file was deleted.

59 changes: 0 additions & 59 deletions x-pack/legacy/plugins/oss_telemetry/server/plugin.ts

This file was deleted.

File renamed without changes.
8 changes: 8 additions & 0 deletions x-pack/plugins/oss_telemetry/kibana.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "ossTelemetry",
"server": true,
"version": "8.0.0",
"kibanaVersion": "kibana",
"requiredPlugins": ["usageCollection", "taskManager"],
"ui": false
}
12 changes: 12 additions & 0 deletions x-pack/plugins/oss_telemetry/server/index.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;
* you may not use this file except in compliance with the Elastic License.
*/

import { PluginInitializerContext } from 'src/core/server';
import { OssTelemetryPlugin } from './plugin';

export const plugin = (context: PluginInitializerContext) => new OssTelemetryPlugin(context);

export * from './plugin';
16 changes: 16 additions & 0 deletions x-pack/plugins/oss_telemetry/server/lib/collectors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { registerVisualizationsCollector } from './visualizations/register_usage_collector';
import { UsageCollectionSetup } from '../../../../../../src/plugins/usage_collection/server';
import { TaskManagerStartContract } from '../../../../task_manager/server';

export function registerCollectors(
usageCollection: UsageCollectionSetup,
taskManager: Promise<TaskManagerStartContract>
) {
registerVisualizationsCollector(usageCollection, taskManager);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,35 @@ import {
getMockTaskFetch,
getMockThrowingTaskFetch,
getMockTaskInstance,
} from '../../../../test_utils';
import { taskManagerMock } from '../../../../../../../plugins/task_manager/server/task_manager.mock';
} from '../../../test_utils';
import { taskManagerMock } from '../../../../../task_manager/server/task_manager.mock';
import { getUsageCollector } from './get_usage_collector';

describe('getVisualizationsCollector#fetch', () => {
test('can return empty stats', async () => {
const { type, fetch } = getUsageCollector(taskManagerMock.start(getMockTaskFetch()));
const { type, fetch } = getUsageCollector(
Promise.resolve(taskManagerMock.start(getMockTaskFetch()))
);
expect(type).toBe('visualization_types');
const fetchResult = await fetch();
expect(fetchResult).toEqual({});
});

test('provides known stats', async () => {
const { type, fetch } = getUsageCollector(
taskManagerMock.start(
getMockTaskFetch([
getMockTaskInstance({
state: {
runs: 1,
stats: { comic_books: { total: 16, max: 12, min: 2, avg: 6 } },
},
taskType: 'test',
params: {},
}),
])
Promise.resolve(
taskManagerMock.start(
getMockTaskFetch([
getMockTaskInstance({
state: {
runs: 1,
stats: { comic_books: { total: 16, max: 12, min: 2, avg: 6 } },
},
taskType: 'test',
params: {},
}),
])
)
)
);
expect(type).toBe('visualization_types');
Expand All @@ -43,9 +47,11 @@ describe('getVisualizationsCollector#fetch', () => {
describe('Error handling', () => {
test('Silently handles Task Manager NotInitialized', async () => {
const { fetch } = getUsageCollector(
taskManagerMock.start(
getMockThrowingTaskFetch(
new Error('NotInitialized taskManager is still waiting for plugins to load')
Promise.resolve(
taskManagerMock.start(
getMockThrowingTaskFetch(
new Error('NotInitialized taskManager is still waiting for plugins to load')
)
)
)
);
Expand All @@ -55,7 +61,7 @@ describe('getVisualizationsCollector#fetch', () => {
// In real life, the CollectorSet calls fetch and handles errors
test('defers the errors', async () => {
const { fetch } = getUsageCollector(
taskManagerMock.start(getMockThrowingTaskFetch(new Error('BOOM')))
Promise.resolve(taskManagerMock.start(getMockThrowingTaskFetch(new Error('BOOM'))))
);
await expect(fetch()).rejects.toThrowErrorMatchingInlineSnapshot(`"BOOM"`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,9 @@

import { get } from 'lodash';
import { PLUGIN_ID, VIS_TELEMETRY_TASK, VIS_USAGE_TYPE } from '../../../../constants';
import { TaskManagerStartContract } from '../../../../../../../plugins/task_manager/server';

async function isTaskManagerReady(taskManager?: TaskManagerStartContract) {
const result = await fetch(taskManager);
return result !== null;
}

async function fetch(taskManager?: TaskManagerStartContract) {
if (!taskManager) {
return null;
}
import { TaskManagerStartContract } from '../../../../../task_manager/server';

async function fetch(taskManager: TaskManagerStartContract) {
let docs;
try {
({ docs } = await taskManager.fetch({
Expand All @@ -38,27 +29,12 @@ async function fetch(taskManager?: TaskManagerStartContract) {
return docs;
}

export function getUsageCollector(taskManager?: TaskManagerStartContract) {
let isCollectorReady = false;
async function determineIfTaskManagerIsReady() {
let isReady = false;
try {
isReady = await isTaskManagerReady(taskManager);
} catch (err) {} // eslint-disable-line

if (isReady) {
isCollectorReady = true;
} else {
setTimeout(determineIfTaskManagerIsReady, 500);
}
}
determineIfTaskManagerIsReady();

export function getUsageCollector(taskManager: Promise<TaskManagerStartContract>) {
return {
type: VIS_USAGE_TYPE,
isReady: () => isCollectorReady,
isReady: () => true,
fetch: async () => {
const docs = await fetch(taskManager);
const docs = await fetch(await taskManager);
// get the accumulated state from the recurring task
return get(docs, '[0].state.stats');
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
*/

import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { TaskManagerStartContract } from '../../../../../../../plugins/task_manager/server';
import { TaskManagerStartContract } from '../../../../../task_manager/server';
import { getUsageCollector } from './get_usage_collector';

export function registerVisualizationsCollector(
collectorSet: UsageCollectionSetup,
taskManager?: TaskManagerStartContract
taskManager: Promise<TaskManagerStartContract>
): void {
const collector = collectorSet.makeUsageCollector(getUsageCollector(taskManager));
collectorSet.registerCollector(collector);
Expand Down
Loading

0 comments on commit 612936d

Please sign in to comment.