Skip to content

Commit e293b4d

Browse files
committed
Shim oss telemetry (elastic#51168)
1 parent 4f46ed9 commit e293b4d

File tree

12 files changed

+223
-201
lines changed

12 files changed

+223
-201
lines changed

x-pack/legacy/plugins/oss_telemetry/index.d.ts

Lines changed: 0 additions & 61 deletions
This file was deleted.

x-pack/legacy/plugins/oss_telemetry/index.js

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { Logger, PluginInitializerContext } from 'kibana/server';
8+
import { PLUGIN_ID } from './constants';
9+
import { OssTelemetryPlugin } from './server/plugin';
10+
import { LegacyPluginInitializer } from '../../../../src/legacy/plugin_discovery/types';
11+
12+
export const ossTelemetry: LegacyPluginInitializer = kibana => {
13+
return new kibana.Plugin({
14+
id: PLUGIN_ID,
15+
require: ['elasticsearch', 'xpack_main'],
16+
configPrefix: 'xpack.oss_telemetry',
17+
18+
init(server) {
19+
const plugin = new OssTelemetryPlugin({
20+
logger: {
21+
get: () =>
22+
({
23+
info: (message: string) => server.log(['info', 'task_manager'], message),
24+
debug: (message: string) => server.log(['debug', 'task_manager'], message),
25+
warn: (message: string) => server.log(['warn', 'task_manager'], message),
26+
error: (message: string) => server.log(['error', 'task_manager'], message),
27+
} as Logger),
28+
},
29+
} as PluginInitializerContext);
30+
plugin.setup(server.newPlatform.setup.core, {
31+
usageCollection: server.newPlatform.setup.plugins.usageCollection,
32+
taskManager: server.plugins.task_manager,
33+
__LEGACY: {
34+
config: server.config(),
35+
xpackMainStatus: ((server.plugins.xpack_main as unknown) as { status: any }).status
36+
.plugin,
37+
},
38+
});
39+
},
40+
});
41+
};

x-pack/legacy/plugins/oss_telemetry/server/lib/collectors/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
8-
import { HapiServer } from '../../../';
97
import { registerVisualizationsCollector } from './visualizations/register_usage_collector';
8+
import { OssTelemetrySetupDependencies } from '../../plugin';
109

11-
export function registerCollectors(usageCollection: UsageCollectionSetup, server: HapiServer) {
12-
registerVisualizationsCollector(usageCollection, server);
10+
export function registerCollectors(deps: OssTelemetrySetupDependencies) {
11+
registerVisualizationsCollector(deps.usageCollection, deps.taskManager);
1312
}

x-pack/legacy/plugins/oss_telemetry/server/lib/collectors/visualizations/get_usage_collector.test.ts

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,12 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import sinon from 'sinon';
8-
import { HapiServer } from '../../../../';
9-
import {
10-
getMockCallWithInternal,
11-
getMockKbnServer,
12-
getMockTaskFetch,
13-
} from '../../../../test_utils';
7+
import { getMockTaskFetch, getMockTaskManager } from '../../../../test_utils';
148
import { getUsageCollector } from './get_usage_collector';
159

1610
describe('getVisualizationsCollector#fetch', () => {
17-
let mockKbnServer: HapiServer;
18-
19-
beforeEach(() => {
20-
mockKbnServer = getMockKbnServer(getMockCallWithInternal(), getMockTaskFetch());
21-
});
22-
2311
test('can return empty stats', async () => {
24-
const { type, fetch } = getUsageCollector(mockKbnServer);
12+
const { type, fetch } = getUsageCollector(getMockTaskManager());
2513
expect(type).toBe('visualization_types');
2614
const fetchResult = await fetch();
2715
expect(fetchResult).toEqual({});
@@ -34,35 +22,33 @@ describe('getVisualizationsCollector#fetch', () => {
3422
runs: 1,
3523
stats: { comic_books: { total: 16, max: 12, min: 2, avg: 6 } },
3624
},
25+
taskType: 'test',
26+
params: {},
3727
},
3828
]);
39-
mockKbnServer = getMockKbnServer(getMockCallWithInternal(), mockTaskFetch);
40-
41-
const { type, fetch } = getUsageCollector(mockKbnServer);
29+
const { type, fetch } = getUsageCollector(getMockTaskManager(mockTaskFetch));
4230
expect(type).toBe('visualization_types');
4331
const fetchResult = await fetch();
4432
expect(fetchResult).toEqual({ comic_books: { avg: 6, max: 12, min: 2, total: 16 } });
4533
});
4634

4735
describe('Error handling', () => {
4836
test('Silently handles Task Manager NotInitialized', async () => {
49-
const mockTaskFetch = sinon.stub();
50-
mockTaskFetch.rejects(
51-
new Error('NotInitialized taskManager is still waiting for plugins to load')
52-
);
53-
mockKbnServer = getMockKbnServer(getMockCallWithInternal(), mockTaskFetch);
54-
55-
const { fetch } = getUsageCollector(mockKbnServer);
56-
await expect(fetch()).resolves.toBe(undefined);
37+
const mockTaskFetch = jest.fn(() => {
38+
throw new Error('NotInitialized taskManager is still waiting for plugins to load');
39+
});
40+
const { fetch } = getUsageCollector(getMockTaskManager(mockTaskFetch));
41+
const result = await fetch();
42+
expect(result).toBe(undefined);
5743
});
5844
// In real life, the CollectorSet calls fetch and handles errors
5945
test('defers the errors', async () => {
60-
const mockTaskFetch = sinon.stub();
61-
mockTaskFetch.rejects(new Error('BOOM'));
62-
mockKbnServer = getMockKbnServer(getMockCallWithInternal(), mockTaskFetch);
46+
const mockTaskFetch = jest.fn(() => {
47+
throw new Error('BOOM');
48+
});
6349

64-
const { fetch } = getUsageCollector(mockKbnServer);
65-
await expect(fetch()).rejects.toMatchObject(new Error('BOOM'));
50+
const { fetch } = getUsageCollector(getMockTaskManager(mockTaskFetch));
51+
await expect(fetch()).rejects.toThrowErrorMatchingInlineSnapshot(`"BOOM"`);
6652
});
6753
});
6854
});

x-pack/legacy/plugins/oss_telemetry/server/lib/collectors/visualizations/get_usage_collector.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55
*/
66

77
import { get } from 'lodash';
8-
import { HapiServer } from '../../../../';
8+
import { PluginSetupContract as TaskManagerPluginSetupContract } from '../../../../../task_manager/plugin';
99
import { PLUGIN_ID, VIS_TELEMETRY_TASK, VIS_USAGE_TYPE } from '../../../../constants';
1010

11-
async function isTaskManagerReady(server: HapiServer) {
12-
const result = await fetch(server);
11+
async function isTaskManagerReady(taskManager: TaskManagerPluginSetupContract | undefined) {
12+
const result = await fetch(taskManager);
1313
return result !== null;
1414
}
1515

16-
async function fetch(server: HapiServer) {
17-
const taskManager = server.plugins.task_manager;
18-
16+
async function fetch(taskManager: TaskManagerPluginSetupContract | undefined) {
1917
if (!taskManager) {
2018
return null;
2119
}
@@ -40,12 +38,12 @@ async function fetch(server: HapiServer) {
4038
return docs;
4139
}
4240

43-
export function getUsageCollector(server: HapiServer) {
41+
export function getUsageCollector(taskManager: TaskManagerPluginSetupContract | undefined) {
4442
let isCollectorReady = false;
4543
async function determineIfTaskManagerIsReady() {
4644
let isReady = false;
4745
try {
48-
isReady = await isTaskManagerReady(server);
46+
isReady = await isTaskManagerReady(taskManager);
4947
} catch (err) {} // eslint-disable-line
5048

5149
if (isReady) {
@@ -60,7 +58,7 @@ export function getUsageCollector(server: HapiServer) {
6058
type: VIS_USAGE_TYPE,
6159
isReady: () => isCollectorReady,
6260
fetch: async () => {
63-
const docs = await fetch(server);
61+
const docs = await fetch(taskManager);
6462
// get the accumulated state from the recurring task
6563
return get(docs, '[0].state.stats');
6664
},

x-pack/legacy/plugins/oss_telemetry/server/lib/collectors/visualizations/register_usage_collector.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
*/
66

77
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
8-
import { HapiServer } from '../../../../';
8+
import { PluginSetupContract as TaskManagerPluginSetupContract } from '../../../../../task_manager/plugin';
99
import { getUsageCollector } from './get_usage_collector';
1010

1111
export function registerVisualizationsCollector(
12-
usageCollection: UsageCollectionSetup,
13-
server: HapiServer
12+
collectorSet: UsageCollectionSetup,
13+
taskManager: TaskManagerPluginSetupContract | undefined
1414
): void {
15-
const collector = usageCollection.makeUsageCollector(getUsageCollector(server));
16-
usageCollection.registerCollector(collector);
15+
const collector = collectorSet.makeUsageCollector(getUsageCollector(taskManager));
16+
collectorSet.registerCollector(collector);
1717
}

x-pack/legacy/plugins/oss_telemetry/server/lib/tasks/index.ts

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,58 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { HapiServer } from '../../../';
7+
import { CoreSetup, Logger } from 'kibana/server';
8+
import { PluginSetupContract as TaskManagerPluginSetupContract } from '../../../../task_manager/plugin';
89
import { PLUGIN_ID, VIS_TELEMETRY_TASK } from '../../../constants';
910
import { visualizationsTaskRunner } from './visualizations/task_runner';
11+
import KbnServer from '../../../../../../../src/legacy/server/kbn_server';
12+
import { LegacyConfig } from '../../plugin';
13+
import { TaskInstance } from '../../../../task_manager';
1014

11-
export function registerTasks(server: HapiServer) {
12-
const taskManager = server.plugins.task_manager;
13-
15+
export function registerTasks({
16+
taskManager,
17+
logger,
18+
elasticsearch,
19+
config,
20+
}: {
21+
taskManager?: TaskManagerPluginSetupContract;
22+
logger: Logger;
23+
elasticsearch: CoreSetup['elasticsearch'];
24+
config: LegacyConfig;
25+
}) {
1426
if (!taskManager) {
15-
server.log(['debug', 'telemetry'], `Task manager is not available`);
27+
logger.debug('Task manager is not available');
1628
return;
1729
}
1830

1931
taskManager.registerTaskDefinitions({
2032
[VIS_TELEMETRY_TASK]: {
2133
title: 'X-Pack telemetry calculator for Visualizations',
2234
type: VIS_TELEMETRY_TASK,
23-
createTaskRunner({ taskInstance }: { taskInstance: any }) {
35+
createTaskRunner({ taskInstance }: { taskInstance: TaskInstance }) {
2436
return {
25-
run: visualizationsTaskRunner(taskInstance, server),
37+
run: visualizationsTaskRunner(taskInstance, config, elasticsearch),
2638
};
2739
},
2840
},
2941
});
3042
}
3143

32-
export function scheduleTasks(server: HapiServer) {
33-
const taskManager = server.plugins.task_manager;
34-
const { kbnServer } = server.plugins.xpack_main.status.plugin;
44+
export function scheduleTasks({
45+
taskManager,
46+
xpackMainStatus,
47+
logger,
48+
}: {
49+
taskManager?: TaskManagerPluginSetupContract;
50+
xpackMainStatus: { kbnServer: KbnServer };
51+
logger: Logger;
52+
}) {
53+
if (!taskManager) {
54+
logger.debug('Task manager is not available');
55+
return;
56+
}
57+
58+
const { kbnServer } = xpackMainStatus;
3559

3660
kbnServer.afterPluginsInit(() => {
3761
// The code block below can't await directly within "afterPluginsInit"
@@ -46,9 +70,10 @@ export function scheduleTasks(server: HapiServer) {
4670
id: `${PLUGIN_ID}-${VIS_TELEMETRY_TASK}`,
4771
taskType: VIS_TELEMETRY_TASK,
4872
state: { stats: {}, runs: 0 },
73+
params: {},
4974
});
5075
} catch (e) {
51-
server.log(['debug', 'telemetry'], `Error scheduling task, received ${e.message}`);
76+
logger.debug(`Error scheduling task, received ${e.message}`);
5277
}
5378
})();
5479
});

0 commit comments

Comments
 (0)