Skip to content

Commit

Permalink
Enabling UI Metric Collector (#6203) (#6689)
Browse files Browse the repository at this point in the history
Signed-off-by: Suchit Sahoo <suchsah@amazon.com>
(cherry picked from commit 5258d83)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
(cherry picked from commit aac0ecb)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed Apr 30, 2024
1 parent 950f9d8 commit 11cec39
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 25 deletions.
4 changes: 4 additions & 0 deletions config/opensearch_dashboards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,7 @@

# Set the value to true to enable workspace feature
# workspace.enabled: false

# Set the value to true to enable Ui Metric Collectors in Usage Collector
# This publishes the Application Usage and UI Metrics into the saved object, which can be accessed by /api/stats?extended=true&legacy=true&exclude_usage=false
# usageCollection.uiMetric.enabled: false
14 changes: 7 additions & 7 deletions packages/osd-analytics/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Check warning on line 63 in packages/osd-analytics/src/reporter.ts

View check run for this annotation

Codecov / codecov/patch

packages/osd-analytics/src/reporter.ts#L63

Added line #L63 was not covered by tests
this.checkInterval = checkInterval;
this.applicationUsage = new ApplicationUsage();
this.storageManager = new ReportStorageManager(storageKey, storage);
Expand Down Expand Up @@ -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);

Check warning on line 147 in packages/osd-analytics/src/reporter.ts

View check run for this annotation

Codecov / codecov/patch

packages/osd-analytics/src/reporter.ts#L147

Added line #L147 was not covered by tests
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);

Check warning on line 154 in packages/osd-analytics/src/reporter.ts

View check run for this annotation

Codecov / codecov/patch

packages/osd-analytics/src/reporter.ts#L154

Added line #L154 was not covered by tests
this.flushReport();
} catch (err) {
this.log(`Error Sending Metrics Report ${err}`);
Expand Down
20 changes: 10 additions & 10 deletions src/plugins/usage_collection/public/services/create_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ interface AnalyicsReporterConfig {
}

export function createReporter(config: AnalyicsReporterConfig): Reporter {
const { localStorage, debug } = config;
const { localStorage, debug, fetch } = config;

Check warning on line 41 in src/plugins/usage_collection/public/services/create_reporter.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/usage_collection/public/services/create_reporter.ts#L41

Added line #L41 was not covered by tests

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', {

Check warning on line 47 in src/plugins/usage_collection/public/services/create_reporter.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/usage_collection/public/services/create_reporter.ts#L47

Added line #L47 was not covered by tests
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.');

Check warning on line 52 in src/plugins/usage_collection/public/services/create_reporter.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/usage_collection/public/services/create_reporter.ts#L52

Added line #L52 was not covered by tests
}
return response;

Check warning on line 54 in src/plugins/usage_collection/public/services/create_reporter.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/usage_collection/public/services/create_reporter.ts#L54

Added line #L54 was not covered by tests
},
});
}
2 changes: 1 addition & 1 deletion src/plugins/usage_collection/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { DEFAULT_MAXIMUM_WAIT_TIME_FOR_ALL_COLLECTORS_IN_S } from '../common/con

export const configSchema = schema.object({
uiMetric: schema.object({
enabled: schema.boolean({ defaultValue: true }),
enabled: schema.boolean({ defaultValue: false }),
debug: schema.boolean({ defaultValue: schema.contextRef('dev') }),
}),
maximumWaitTimeForAllCollectorsInS: schema.number({
Expand Down
7 changes: 3 additions & 4 deletions src/plugins/usage_collection/server/routes/report_metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -59,5 +58,5 @@ export function registerUiMetricRoute(
return res.ok({ body: { status: 'fail' } });
}
}
);*/
);
}
2 changes: 1 addition & 1 deletion src/plugins/vis_type_vega/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { DataSourcePluginSetup } from 'src/plugins/data_source/server';
import { HomeServerPluginSetup } from '../../home/server';
import { UsageCollectionSetup } from '../../usage_collection/server';

export type ConfigObservable = Observable<{ kibana: { index: string } }>;
export type ConfigObservable = Observable<{ opensearchDashboards: { index: string } }>;

export interface VegaSavedObjectAttributes {
title: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const getMockCallCluster = (hits?: unknown[]) =>
jest.fn().mockReturnValue(Promise.resolve({ hits: { hits } }) as unknown) as LegacyAPICaller;

describe('Vega visualization usage collector', () => {
const configMock = of({ kibana: { index: '' } });
const configMock = of({ opensearchDashboards: { index: '' } });
const usageCollector = getUsageCollector(configMock, {
home: ({
sampleData: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export function getUsageCollector(
type: VEGA_USAGE_TYPE,
isReady: () => true,
fetch: async (callCluster: LegacyAPICaller) => {
const { index } = (await config.pipe(first()).toPromise()).kibana;
const { index } = (await config.pipe(first()).toPromise()).opensearchDashboards;

return await getStats(callCluster, index, dependencies);
},
Expand Down

0 comments on commit 11cec39

Please sign in to comment.