Skip to content

Commit 3658220

Browse files
authored
[Telemetry] Migrate ui_metric plugin to NP under usageCollecti… (#51972)
* move cloud dir to plugins from legacy * create ui_metrics in NP * migrate first plugin * ui_metric plugin uses npStart * sinin mock * karma mocks * type check fix * rename old configs * fix mocks and use configs * use fo debug * ui_metric deprecation configs * remove commented out code * remove unused type import * mock ui_metric in client_integration * jest.mock ui/new_platform * fix all failing tests * platform team code review fixes * reset interval back to default * apm cypress config use usageCollection * revert kibana.yml change * remove license type from NP def * undo revert of NP type * code review fixes * report schema in a separate dir
1 parent 0dac435 commit 3658220

File tree

46 files changed

+566
-336
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+566
-336
lines changed

packages/kbn-analytics/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@
2020
export { ReportHTTP, Reporter, ReporterConfig } from './reporter';
2121
export { UiStatsMetricType, METRIC_TYPE } from './metrics';
2222
export { Report, ReportManager } from './report';
23+
export { Storage } from './storage';

packages/kbn-analytics/src/report.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,25 @@ const REPORT_VERSION = 1;
2323

2424
export interface Report {
2525
reportVersion: typeof REPORT_VERSION;
26-
uiStatsMetrics: {
27-
[key: string]: {
26+
uiStatsMetrics?: Record<
27+
string,
28+
{
2829
key: string;
2930
appName: string;
3031
eventName: string;
3132
type: UiStatsMetricType;
3233
stats: Stats;
33-
};
34-
};
35-
userAgent?: {
36-
[key: string]: {
34+
}
35+
>;
36+
userAgent?: Record<
37+
string,
38+
{
3739
userAgent: string;
3840
key: string;
3941
type: METRIC_TYPE.USER_AGENT;
4042
appName: string;
41-
};
42-
};
43+
}
44+
>;
4345
}
4446

4547
export class ReportManager {
@@ -49,14 +51,15 @@ export class ReportManager {
4951
this.report = report || ReportManager.createReport();
5052
}
5153
static createReport(): Report {
52-
return { reportVersion: REPORT_VERSION, uiStatsMetrics: {} };
54+
return { reportVersion: REPORT_VERSION };
5355
}
5456
public clearReport() {
5557
this.report = ReportManager.createReport();
5658
}
5759
public isReportEmpty(): boolean {
58-
const noUiStats = Object.keys(this.report.uiStatsMetrics).length === 0;
59-
const noUserAgent = !this.report.userAgent || Object.keys(this.report.userAgent).length === 0;
60+
const { uiStatsMetrics, userAgent } = this.report;
61+
const noUiStats = !uiStatsMetrics || Object.keys(uiStatsMetrics).length === 0;
62+
const noUserAgent = !userAgent || Object.keys(userAgent).length === 0;
6063
return noUiStats && noUserAgent;
6164
}
6265
private incrementStats(count: number, stats?: Stats): Stats {
@@ -113,14 +116,17 @@ export class ReportManager {
113116
case METRIC_TYPE.LOADED:
114117
case METRIC_TYPE.COUNT: {
115118
const { appName, type, eventName, count } = metric;
116-
const existingStats = (report.uiStatsMetrics[key] || {}).stats;
117-
this.report.uiStatsMetrics[key] = {
118-
key,
119-
appName,
120-
eventName,
121-
type,
122-
stats: this.incrementStats(count, existingStats),
123-
};
119+
if (report.uiStatsMetrics) {
120+
const existingStats = (report.uiStatsMetrics[key] || {}).stats;
121+
this.report.uiStatsMetrics = this.report.uiStatsMetrics || {};
122+
this.report.uiStatsMetrics[key] = {
123+
key,
124+
appName,
125+
eventName,
126+
type,
127+
stats: this.incrementStats(count, existingStats),
128+
};
129+
}
124130
return;
125131
}
126132
default:

packages/kbn-analytics/src/storage.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@
1919

2020
import { Report } from './report';
2121

22-
export type Storage = Map<string, any>;
22+
export interface Storage<T = any, S = void> {
23+
get: (key: string) => T | null;
24+
set: (key: string, value: T) => S;
25+
remove: (key: string) => T | null;
26+
clear: () => void;
27+
}
28+
2329
export class ReportStorageManager {
2430
storageKey: string;
2531
private storage?: Storage;

src/legacy/core_plugins/kibana/public/visualize/wizard/new_vis_modal.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { mountWithIntl } from 'test_utils/enzyme_helpers';
2323
import { VisType } from '../legacy_imports';
2424
import { TypesStart } from '../../../../visualizations/public/np_ready/public/types';
2525

26+
jest.mock('ui/new_platform');
2627
jest.mock('../legacy_imports', () => ({
2728
State: () => null,
2829
AppState: () => null,

src/legacy/core_plugins/telemetry/public/services/telemetry_opt_in.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919

2020
import moment from 'moment';
21-
import { setCanTrackUiMetrics } from 'ui/ui_metric';
2221
// @ts-ignore
2322
import { banners, toastNotifications } from 'ui/notify';
2423
import { npStart } from 'ui/new_platform';
@@ -69,8 +68,6 @@ export function TelemetryOptInProvider($injector: any, chrome: any, sendOptInSta
6968
'telemetryNotifyUserAboutOptInDefault'
7069
) as boolean;
7170

72-
setCanTrackUiMetrics(currentOptInStatus);
73-
7471
const provider = {
7572
getBannerId: () => bannerId,
7673
getOptInBannerNoticeId: () => optInBannerNoticeId,
@@ -116,7 +113,6 @@ export function TelemetryOptInProvider($injector: any, chrome: any, sendOptInSta
116113
if (!allowChangingOptInStatus) {
117114
return;
118115
}
119-
setCanTrackUiMetrics(enabled);
120116
const $http = $injector.get('$http');
121117

122118
try {

src/legacy/core_plugins/ui_metric/README.md

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

src/legacy/core_plugins/ui_metric/index.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,24 @@
1818
*/
1919

2020
import { resolve } from 'path';
21-
import JoiNamespace from 'joi';
22-
import { Server } from 'hapi';
2321
import { Legacy } from '../../../../kibana';
24-
import { registerUiMetricRoute } from './server/routes/api/ui_metric';
2522

2623
// eslint-disable-next-line import/no-default-export
2724
export default function(kibana: any) {
2825
return new kibana.Plugin({
2926
id: 'ui_metric',
3027
require: ['kibana', 'elasticsearch'],
3128
publicDir: resolve(__dirname, 'public'),
32-
config(Joi: typeof JoiNamespace) {
33-
return Joi.object({
34-
enabled: Joi.boolean().default(true),
35-
debug: Joi.boolean().default(Joi.ref('$dev')),
36-
}).default();
37-
},
3829
uiExports: {
39-
injectDefaultVars(server: Server) {
40-
const config = server.config();
41-
return {
42-
uiMetricEnabled: config.get('ui_metric.enabled'),
43-
debugUiMetric: config.get('ui_metric.debug'),
44-
};
45-
},
4630
mappings: require('./mappings.json'),
47-
hacks: ['plugins/ui_metric/hacks/ui_metric_init'],
4831
},
4932
init(server: Legacy.Server) {
50-
registerUiMetricRoute(server);
33+
const { getSavedObjectsRepository } = server.savedObjects;
34+
const { callWithInternalUser } = server.plugins.elasticsearch.getCluster('admin');
35+
const internalRepository = getSavedObjectsRepository(callWithInternalUser);
36+
const { usageCollection } = server.newPlatform.setup.plugins;
37+
38+
usageCollection.registerLegacySavedObjects(internalRepository);
5139
},
5240
});
5341
}

src/legacy/core_plugins/ui_metric/public/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
* under the License.
1818
*/
1919

20-
export { createUiStatsReporter, trackUserAgent } from './services/telemetry_analytics';
20+
export { createUiStatsReporter } from './services/telemetry_analytics';
2121
export { METRIC_TYPE, UiStatsMetricType } from '@kbn/analytics';

src/legacy/core_plugins/ui_metric/public/services/telemetry_analytics.ts

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,61 +16,9 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19+
import { npSetup } from 'ui/new_platform';
1920

20-
import { Reporter, UiStatsMetricType } from '@kbn/analytics';
21-
// @ts-ignore
22-
import { addSystemApiHeader } from 'ui/system_api';
23-
24-
let telemetryReporter: Reporter;
25-
26-
export const setTelemetryReporter = (aTelemetryReporter: Reporter): void => {
27-
telemetryReporter = aTelemetryReporter;
28-
};
29-
30-
export const getTelemetryReporter = () => {
31-
return telemetryReporter;
32-
};
33-
34-
export const createUiStatsReporter = (appName: string) => (
35-
type: UiStatsMetricType,
36-
eventNames: string | string[],
37-
count?: number
38-
): void => {
39-
if (telemetryReporter) {
40-
return telemetryReporter.reportUiStats(appName, type, eventNames, count);
41-
}
21+
export const createUiStatsReporter = (appName: string) => {
22+
const { usageCollection } = npSetup.plugins;
23+
return usageCollection.reportUiStats.bind(usageCollection, appName);
4224
};
43-
44-
export const trackUserAgent = (appName: string) => {
45-
if (telemetryReporter) {
46-
return telemetryReporter.reportUserAgent(appName);
47-
}
48-
};
49-
50-
interface AnalyicsReporterConfig {
51-
localStorage: any;
52-
debug: boolean;
53-
kfetch: any;
54-
}
55-
56-
export function createAnalyticsReporter(config: AnalyicsReporterConfig) {
57-
const { localStorage, debug, kfetch } = config;
58-
59-
return new Reporter({
60-
debug,
61-
storage: localStorage,
62-
async http(report) {
63-
const response = await kfetch({
64-
method: 'POST',
65-
pathname: '/api/telemetry/report',
66-
body: JSON.stringify(report),
67-
headers: addSystemApiHeader({}),
68-
});
69-
70-
if (response.status !== 'ok') {
71-
throw Error('Unable to store report.');
72-
}
73-
return response;
74-
},
75-
});
76-
}

0 commit comments

Comments
 (0)