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

[Canvas] Migrate usage collector to NP plugin #53303

Merged
merged 5 commits into from
Dec 30, 2019
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
2 changes: 0 additions & 2 deletions x-pack/legacy/plugins/canvas/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import { CoreSetup, PluginsSetup } from './shim';
import { routes } from './routes';
import { functions } from '../canvas_plugin_src/functions/server';
import { registerCanvasUsageCollector } from './usage';
import { loadSampleData } from './sample_data';

export class Plugin {
Expand Down Expand Up @@ -61,7 +60,6 @@ export class Plugin {
},
});

registerCanvasUsageCollector(plugins.usageCollection, core);
loadSampleData(
plugins.home.sampleData.addSavedObjectsToSampleDataset,
plugins.home.sampleData.addAppLinksToSampleDataset
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/canvas/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"configPath": ["xpack", "canvas"],
"server": true,
"ui": false,
"requiredPlugins": []
"requiredPlugins": [],
"optionalPlugins": ["usageCollection"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for making it optional vs required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be wrong about this reason but I suspect that users (like government folks in airgap environments) don't want that bundled.

In general though, I was following the conventions set here: https://github.com/elastic/kibana/blob/master/src/plugins/usage_collection/README.md

}

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@

import { CallCluster } from 'src/legacy/core_plugins/elasticsearch';
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { CoreSetup } from '../shim';
// @ts-ignore missing local declaration
import { CANVAS_USAGE_TYPE } from '../../common/lib/constants';
import { CANVAS_USAGE_TYPE } from '../../../../legacy/plugins/canvas/common/lib/constants';
import { TelemetryCollector } from '../../types';

import { workpadCollector } from './workpad_collector';
import { customElementCollector } from './custom_element_collector';
import { TelemetryCollector } from '../../types';

const collectors: TelemetryCollector[] = [workpadCollector, customElementCollector];

Expand All @@ -24,10 +23,13 @@ const collectors: TelemetryCollector[] = [workpadCollector, customElementCollect
A usage collector function returns an object derived from current data in the ES Cluster.
*/
export function registerCanvasUsageCollector(
usageCollection: UsageCollectionSetup,
core: CoreSetup
usageCollection: UsageCollectionSetup | undefined,
kibanaIndex: string
) {
const kibanaIndex = core.getServerConfig().get<string>('kibana.index');
if (!usageCollection) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think this check should move to the plugin defintion instead of here? So we don't try to register unless we have the usage_collection plugin?

return;
}

const canvasCollector = usageCollection.makeUsageCollector({
type: CANVAS_USAGE_TYPE,
isReady: () => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import { SearchParams } from 'elasticsearch';
import { get } from 'lodash';
import { fromExpression } from '@kbn/interpreter/common';
import { collectFns } from './collector_helpers';
import { TelemetryCollector } from '../../types';
import { ExpressionAST, TelemetryCustomElement, TelemetryCustomElementDocument } from '../../types';
import {
ExpressionAST,
TelemetryCollector,
TelemetryCustomElement,
TelemetryCustomElementDocument,
} from '../../types';

const CUSTOM_ELEMENT_TYPE = 'canvas-element';
interface CustomElementSearch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import clonedeep from 'lodash.clonedeep';
import { summarizeWorkpads } from './workpad_collector';
import { workpads } from '../../__tests__/fixtures/workpads';
import { workpads } from '../../../../legacy/plugins/canvas/__tests__/fixtures/workpads';

describe('usage collector handle es response data', () => {
it('should summarize workpads, pages, and elements', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { SearchParams } from 'elasticsearch';
import { sum as arraySum, min as arrayMin, max as arrayMax, get } from 'lodash';
import { fromExpression } from '@kbn/interpreter/common';
import { CANVAS_TYPE } from '../../common/lib/constants';
import { CANVAS_TYPE } from '../../../../legacy/plugins/canvas/common/lib/constants';
import { collectFns } from './collector_helpers';
import { ExpressionAST, TelemetryCollector, CanvasWorkpad } from '../../types';

Expand Down
17 changes: 15 additions & 2 deletions x-pack/plugins/canvas/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,32 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { first } from 'rxjs/operators';
import { CoreSetup, PluginInitializerContext, Plugin, Logger } from 'src/core/server';
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { initRoutes } from './routes';
import { registerCanvasUsageCollector } from './collectors';

interface PluginsSetup {
usageCollection?: UsageCollectionSetup;
}

export class CanvasPlugin implements Plugin {
private readonly logger: Logger;
constructor(initializerContext: PluginInitializerContext) {
constructor(public readonly initializerContext: PluginInitializerContext) {
this.logger = initializerContext.logger.get();
}

public setup(coreSetup: CoreSetup): void {
public async setup(coreSetup: CoreSetup, plugins: PluginsSetup) {
const canvasRouter = coreSetup.http.createRouter();

initRoutes({ router: canvasRouter, logger: this.logger });

// we need the kibana index provided by global config for the Canvas usage collector
const globalConfig = await this.initializerContext.config.legacy.globalConfig$
.pipe(first())
.toPromise();
registerCanvasUsageCollector(plugins.usageCollection, globalConfig.kibana.index);
}

public start() {}
Expand Down
7 changes: 7 additions & 0 deletions x-pack/plugins/canvas/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* 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.
*/

export * from '../../../legacy/plugins/canvas/types';