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

[Telemetry] Migrate ui_metric plugin to NP under usageCollection #51972

Merged
merged 37 commits into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
675c7b0
move cloud dir to plugins from legacy
Bamieh Nov 27, 2019
efa654e
Merge branch 'master' of github.com:elastic/kibana
Bamieh Nov 30, 2019
bff2429
Merge branch 'master' of github.com:elastic/kibana
Bamieh Dec 2, 2019
648468e
create ui_metrics in NP
Bamieh Dec 2, 2019
fabfaf1
migrate first plugin
Bamieh Dec 4, 2019
3a3a7df
merge master
Bamieh Dec 9, 2019
381e7eb
Merge branch 'master' of github.com:elastic/kibana into telemetry/ui_…
Bamieh Dec 9, 2019
8f8a5e6
ui_metric plugin uses npStart
Bamieh Dec 10, 2019
344ea4f
Merge branch 'master' of github.com:elastic/kibana into telemetry/ui_…
Bamieh Dec 10, 2019
7b55677
sinin mock
Bamieh Dec 10, 2019
34ec5eb
karma mocks
Bamieh Dec 10, 2019
c3b9e26
type check fix
Bamieh Dec 10, 2019
f149a43
rename old configs
Bamieh Dec 10, 2019
9e02275
fix mocks and use configs
Bamieh Dec 10, 2019
1e441ad
use fo debug
Bamieh Dec 10, 2019
306f4b8
ui_metric deprecation configs
Bamieh Dec 10, 2019
69a29ca
remove commented out code
Bamieh Dec 10, 2019
ea84255
remove unused type import
Bamieh Dec 10, 2019
95ed045
Merge branch 'master' of github.com:elastic/kibana into telemetry/ui_…
Bamieh Dec 10, 2019
058420b
mock ui_metric in client_integration
Bamieh Dec 10, 2019
444c2f9
jest.mock ui/new_platform
Bamieh Dec 10, 2019
e34662e
fix all failing tests
Bamieh Dec 10, 2019
92f1611
platform team code review fixes
Bamieh Dec 11, 2019
02e8703
Merge branch 'master' of github.com:elastic/kibana into telemetry/ui_…
Bamieh Dec 11, 2019
a03b5c9
reset interval back to default
Bamieh Dec 11, 2019
205ac1e
apm cypress config use usageCollection
Bamieh Dec 11, 2019
397d241
Merge branch 'master' of github.com:elastic/kibana into telemetry/ui_…
Bamieh Dec 11, 2019
0671247
Merge branch 'master' into telemetry/ui_metric_to_usage_collection
elasticmachine Dec 11, 2019
a75a500
merge master
Bamieh Dec 13, 2019
b0c8dce
Merge branch 'telemetry/ui_metric_to_usage_collection' of github.com:…
Bamieh Dec 13, 2019
866e42c
revert kibana.yml change
Bamieh Dec 13, 2019
d0266ff
Merge branch 'master' of github.com:elastic/kibana into telemetry/ui_…
Bamieh Dec 13, 2019
609217a
remove license type from NP def
Bamieh Dec 13, 2019
7c006b8
undo revert of NP type
Bamieh Dec 13, 2019
2538296
code review fixes
Bamieh Dec 13, 2019
4fa94f3
merge master
Bamieh Dec 13, 2019
8b1a07c
report schema in a separate dir
Bamieh Dec 13, 2019
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
1 change: 1 addition & 0 deletions packages/kbn-analytics/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
export { ReportHTTP, Reporter, ReporterConfig } from './reporter';
export { UiStatsMetricType, METRIC_TYPE } from './metrics';
export { Report, ReportManager } from './report';
export { Storage } from './storage';
44 changes: 25 additions & 19 deletions packages/kbn-analytics/src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,25 @@ const REPORT_VERSION = 1;

export interface Report {
reportVersion: typeof REPORT_VERSION;
uiStatsMetrics: {
[key: string]: {
uiStatsMetrics?: Record<
string,
{
key: string;
appName: string;
eventName: string;
type: UiStatsMetricType;
stats: Stats;
};
};
userAgent?: {
[key: string]: {
}
>;
userAgent?: Record<
string,
{
userAgent: string;
key: string;
type: METRIC_TYPE.USER_AGENT;
appName: string;
};
};
}
>;
}

export class ReportManager {
Expand All @@ -49,14 +51,15 @@ export class ReportManager {
this.report = report || ReportManager.createReport();
}
static createReport(): Report {
return { reportVersion: REPORT_VERSION, uiStatsMetrics: {} };
return { reportVersion: REPORT_VERSION };
}
public clearReport() {
this.report = ReportManager.createReport();
}
public isReportEmpty(): boolean {
const noUiStats = Object.keys(this.report.uiStatsMetrics).length === 0;
const noUserAgent = !this.report.userAgent || Object.keys(this.report.userAgent).length === 0;
const { uiStatsMetrics, userAgent } = this.report;
const noUiStats = !uiStatsMetrics || Object.keys(uiStatsMetrics).length === 0;
const noUserAgent = !userAgent || Object.keys(userAgent).length === 0;
return noUiStats && noUserAgent;
}
private incrementStats(count: number, stats?: Stats): Stats {
Expand Down Expand Up @@ -113,14 +116,17 @@ export class ReportManager {
case METRIC_TYPE.LOADED:
case METRIC_TYPE.COUNT: {
const { appName, type, eventName, count } = metric;
const existingStats = (report.uiStatsMetrics[key] || {}).stats;
this.report.uiStatsMetrics[key] = {
key,
appName,
eventName,
type,
stats: this.incrementStats(count, existingStats),
};
if (report.uiStatsMetrics) {
const existingStats = (report.uiStatsMetrics[key] || {}).stats;
this.report.uiStatsMetrics = this.report.uiStatsMetrics || {};
this.report.uiStatsMetrics[key] = {
key,
appName,
eventName,
type,
stats: this.incrementStats(count, existingStats),
};
}
return;
}
default:
Expand Down
8 changes: 7 additions & 1 deletion packages/kbn-analytics/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@

import { Report } from './report';

export type Storage = Map<string, any>;
export interface Storage<T = any, S = void> {
get: (key: string) => T | null;
set: (key: string, value: T) => S;
remove: (key: string) => T | null;
clear: () => void;
}

export class ReportStorageManager {
storageKey: string;
private storage?: Storage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { mountWithIntl } from 'test_utils/enzyme_helpers';
import { VisType } from '../legacy_imports';
import { TypesStart } from '../../../../visualizations/public/np_ready/public/types';

jest.mock('ui/new_platform');
jest.mock('../legacy_imports', () => ({
State: () => null,
AppState: () => null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

import moment from 'moment';
import { setCanTrackUiMetrics } from 'ui/ui_metric';
// @ts-ignore
import { banners, toastNotifications } from 'ui/notify';
import { npStart } from 'ui/new_platform';
Expand Down Expand Up @@ -69,8 +68,6 @@ export function TelemetryOptInProvider($injector: any, chrome: any, sendOptInSta
'telemetryNotifyUserAboutOptInDefault'
) as boolean;

setCanTrackUiMetrics(currentOptInStatus);

const provider = {
getBannerId: () => bannerId,
getOptInBannerNoticeId: () => optInBannerNoticeId,
Expand Down Expand Up @@ -116,7 +113,6 @@ export function TelemetryOptInProvider($injector: any, chrome: any, sendOptInSta
if (!allowChangingOptInStatus) {
return;
}
setCanTrackUiMetrics(enabled);
const $http = $injector.get('$http');

try {
Expand Down
78 changes: 0 additions & 78 deletions src/legacy/core_plugins/ui_metric/README.md

This file was deleted.

24 changes: 6 additions & 18 deletions src/legacy/core_plugins/ui_metric/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,24 @@
*/

import { resolve } from 'path';
import JoiNamespace from 'joi';
import { Server } from 'hapi';
import { Legacy } from '../../../../kibana';
import { registerUiMetricRoute } from './server/routes/api/ui_metric';

// eslint-disable-next-line import/no-default-export
export default function(kibana: any) {
return new kibana.Plugin({
id: 'ui_metric',
require: ['kibana', 'elasticsearch'],
publicDir: resolve(__dirname, 'public'),
config(Joi: typeof JoiNamespace) {
return Joi.object({
enabled: Joi.boolean().default(true),
debug: Joi.boolean().default(Joi.ref('$dev')),
}).default();
},
uiExports: {
injectDefaultVars(server: Server) {
const config = server.config();
return {
uiMetricEnabled: config.get('ui_metric.enabled'),
debugUiMetric: config.get('ui_metric.debug'),
};
},
mappings: require('./mappings.json'),
hacks: ['plugins/ui_metric/hacks/ui_metric_init'],
},
init(server: Legacy.Server) {
registerUiMetricRoute(server);
const { getSavedObjectsRepository } = server.savedObjects;
const { callWithInternalUser } = server.plugins.elasticsearch.getCluster('admin');
const internalRepository = getSavedObjectsRepository(callWithInternalUser);
const { usageCollection } = server.newPlatform.setup.plugins;

usageCollection.registerLegacySavedObjects(internalRepository);
},
});
}
2 changes: 1 addition & 1 deletion src/legacy/core_plugins/ui_metric/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
* under the License.
*/

export { createUiStatsReporter, trackUserAgent } from './services/telemetry_analytics';
export { createUiStatsReporter } from './services/telemetry_analytics';
export { METRIC_TYPE, UiStatsMetricType } from '@kbn/analytics';
Original file line number Diff line number Diff line change
Expand Up @@ -16,61 +16,9 @@
* specific language governing permissions and limitations
* under the License.
*/
import { npSetup } from 'ui/new_platform';

import { Reporter, UiStatsMetricType } from '@kbn/analytics';
// @ts-ignore
import { addSystemApiHeader } from 'ui/system_api';

let telemetryReporter: Reporter;

export const setTelemetryReporter = (aTelemetryReporter: Reporter): void => {
telemetryReporter = aTelemetryReporter;
};

export const getTelemetryReporter = () => {
return telemetryReporter;
};

export const createUiStatsReporter = (appName: string) => (
type: UiStatsMetricType,
eventNames: string | string[],
count?: number
): void => {
if (telemetryReporter) {
return telemetryReporter.reportUiStats(appName, type, eventNames, count);
}
export const createUiStatsReporter = (appName: string) => {
const { usageCollection } = npSetup.plugins;
return usageCollection.reportUiStats.bind(usageCollection, appName);
};

export const trackUserAgent = (appName: string) => {
if (telemetryReporter) {
return telemetryReporter.reportUserAgent(appName);
}
};

interface AnalyicsReporterConfig {
localStorage: any;
debug: boolean;
kfetch: any;
}

export function createAnalyticsReporter(config: AnalyicsReporterConfig) {
const { localStorage, debug, kfetch } = config;

return new Reporter({
debug,
storage: localStorage,
async http(report) {
const response = await kfetch({
method: 'POST',
pathname: '/api/telemetry/report',
body: JSON.stringify(report),
headers: addSystemApiHeader({}),
});

if (response.status !== 'ok') {
throw Error('Unable to store report.');
}
return response;
},
});
}
Loading