From 1f31cddba02cec60e71157a1ecfb0d98eeb03426 Mon Sep 17 00:00:00 2001 From: Suchit Sahoo Date: Wed, 13 Mar 2024 00:38:01 +0000 Subject: [PATCH] Enabling UI Metric Collector Signed-off-by: Suchit Sahoo --- CHANGELOG.md | 2 +- packages/osd-analytics/src/reporter.ts | 14 ++++++------- .../public/services/create_reporter.ts | 20 +++++++++---------- .../server/routes/report_metrics.ts | 7 +++---- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e17bb7d1a207..aaf804ef5adc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,7 +56,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [Dynamic Configurations] Pass request headers when making application config calls ([#6164](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6164)) - [Discover] Options button to configure legacy mode and remove the top navigation option ([#6170](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6170)) - [Multiple Datasource] Add default functionality for customer to choose default datasource ([#6058](https://github.com/opensearch-project/OpenSearch-Dashboards/issues/6058)) - +- Enable UI Metric Collector to collect UI Metrics and Application Usage ([#6203](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6203)) ### 🐛 Bug Fixes diff --git a/packages/osd-analytics/src/reporter.ts b/packages/osd-analytics/src/reporter.ts index d4d7b68a25a2..7dedf5b7ac38 100644 --- a/packages/osd-analytics/src/reporter.ts +++ b/packages/osd-analytics/src/reporter.ts @@ -36,7 +36,7 @@ import { Report, ReportManager } from './report'; import { ApplicationUsage } from './metrics'; export interface ReporterConfig { - // http: ReportHTTP; + http: ReportHTTP; storage?: Storage; checkInterval?: number; debug?: boolean; @@ -49,7 +49,7 @@ export class Reporter { checkInterval: number; private interval?: NodeJS.Timer; private lastAppId?: string; - // private http: ReportHTTP; + private http: ReportHTTP; private reportManager: ReportManager; private storageManager: ReportStorageManager; private readonly applicationUsage: ApplicationUsage; @@ -59,8 +59,8 @@ export class Reporter { private started = false; constructor(config: ReporterConfig) { - const { storage, debug, checkInterval = 90000, storageKey = 'analytics' } = config; - // this.http = http; + const { http, storage, debug, checkInterval = 90000, storageKey = 'analytics' } = config; + this.http = http; this.checkInterval = checkInterval; this.applicationUsage = new ApplicationUsage(); this.storageManager = new ReportStorageManager(storageKey, storage); @@ -144,14 +144,14 @@ export class Reporter { public reportApplicationUsage(appId?: string) { this.log(`Reporting application changed to ${appId}`); this.lastAppId = appId || this.lastAppId; - // const appChangedReport = this.applicationUsage.appChanged(appId); - // if (appChangedReport) this.saveToReport([appChangedReport]); + const appChangedReport = this.applicationUsage.appChanged(appId); + if (appChangedReport) this.saveToReport([appChangedReport]); } public sendReports = async () => { if (!this.reportManager.isReportEmpty()) { try { - // await this.http(this.reportManager.report); + await this.http(this.reportManager.report); this.flushReport(); } catch (err) { this.log(`Error Sending Metrics Report ${err}`); diff --git a/src/plugins/usage_collection/public/services/create_reporter.ts b/src/plugins/usage_collection/public/services/create_reporter.ts index 31b77eb862eb..6485e5b45f1f 100644 --- a/src/plugins/usage_collection/public/services/create_reporter.ts +++ b/src/plugins/usage_collection/public/services/create_reporter.ts @@ -38,20 +38,20 @@ interface AnalyicsReporterConfig { } export function createReporter(config: AnalyicsReporterConfig): Reporter { - const { localStorage, debug } = config; + const { localStorage, debug, fetch } = config; return new Reporter({ debug, storage: localStorage, - // async http(report) { - // const response = await fetch.post('/api/ui_metric/report', { - // body: JSON.stringify({ report }), - // }); + async http(report) { + const response = await fetch.post('/api/ui_metric/report', { + body: JSON.stringify({ report }), + }); - // if (response.status !== 'ok') { - // throw Error('Unable to store report.'); - // } - // return response; - // }, + if (response.status !== 'ok') { + throw Error('Unable to store report.'); + } + return response; + }, }); } diff --git a/src/plugins/usage_collection/server/routes/report_metrics.ts b/src/plugins/usage_collection/server/routes/report_metrics.ts index 75080a6b440d..1964e1bf27e1 100644 --- a/src/plugins/usage_collection/server/routes/report_metrics.ts +++ b/src/plugins/usage_collection/server/routes/report_metrics.ts @@ -28,15 +28,14 @@ * under the License. */ -// import { schema } from '@osd/config-schema'; +import { schema } from '@osd/config-schema'; import { IRouter, ISavedObjectsRepository } from 'opensearch-dashboards/server'; -// import { storeReport, reportSchema } from '../report'; +import { storeReport, reportSchema } from '../report'; export function registerUiMetricRoute( router: IRouter, getSavedObjects: () => ISavedObjectsRepository | undefined ) { - /* router.post( { path: '/api/ui_metric/report', @@ -59,5 +58,5 @@ export function registerUiMetricRoute( return res.ok({ body: { status: 'fail' } }); } } - );*/ + ); }