Skip to content

Commit

Permalink
use core logger for kbn-analytics too.
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed May 30, 2024
1 parent eac180f commit b0ec347
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
30 changes: 13 additions & 17 deletions packages/kbn-analytics/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
* Side Public License, v 1.
*/

import type { Logger } from '@kbn/logging';
import { wrapArray } from './util';
import { Metric, createUiCounterMetric, trackUsageAgent, ApplicationUsageMetric } from './metrics';

import { Storage, ReportStorageManager } from './storage';
import { Report, ReportManager } from './report';

export interface ReporterConfig {
http: ReportHTTP;
logger: Logger;
storage?: Storage;
checkInterval?: number;
debug?: boolean;
storageKey?: string;
}

Expand All @@ -26,20 +26,21 @@ export class Reporter {
checkInterval: number;
private interval?: NodeJS.Timer;
private http: ReportHTTP;
private logger: Logger;
private reportManager: ReportManager;
private storageManager: ReportStorageManager;
private debug: boolean;
private retryCount = 0;
private readonly maxRetries = 3;

constructor(config: ReporterConfig) {
const { http, storage, debug, checkInterval = 90000, storageKey = 'analytics' } = config;
const { http, logger, storage, checkInterval = 90000, storageKey = 'analytics' } = config;

this.http = http;
this.logger = logger;
this.checkInterval = checkInterval;
this.storageManager = new ReportStorageManager(storageKey, storage);
const storedReport = this.storageManager.get();
this.reportManager = new ReportManager(storedReport);
this.debug = !!debug;
}

private saveToReport(newMetrics: Metric[]) {
Expand All @@ -62,36 +63,31 @@ export class Reporter {
}
};

private log(message: unknown) {
if (this.debug) {
// eslint-disable-next-line no-console
console.debug(message);
}
}

public reportUiCounter = (
appName: string,
type: string,
eventNames: string | string[],
count?: number
) => {
const metrics = wrapArray(eventNames).map((eventName) => {
this.log(`${type} Metric -> (${appName}:${eventName}):`);
this.logger.debug(`${type} Metric -> (${appName}:${eventName}):`);
const report = createUiCounterMetric({ type, appName, eventName, count });
this.log(report);
this.logger.debug(JSON.stringify(report));
return report;
});
this.saveToReport(metrics);
};

public reportUserAgent = (appName: string) => {
this.log(`Reporting user-agent.`);
this.logger.debug(`Reporting user-agent.`);
const report = trackUsageAgent(appName);
this.saveToReport([report]);
};

public reportApplicationUsage(appUsageReport: ApplicationUsageMetric) {
this.log(`Reporting application usage for ${appUsageReport.appId}, ${appUsageReport.viewId}`);
this.logger.debug(
`Reporting application usage for ${appUsageReport.appId}, ${appUsageReport.viewId}`
);
this.saveToReport([appUsageReport]);
}

Expand All @@ -101,7 +97,7 @@ export class Reporter {
await this.http(this.reportManager.report);
this.flushReport();
} catch (err) {
this.log(`Error Sending Metrics Report ${err}`);
this.logger.warn(`Error Sending Metrics Report ${err}`);
this.retryCount = this.retryCount + 1;
const versionMismatch =
this.reportManager.report.reportVersion !== ReportManager.REPORT_VERSION;
Expand Down
7 changes: 3 additions & 4 deletions src/plugins/usage_collection/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,16 @@ export class UsageCollectionPlugin
private subscriptions: Subscription[] = [];
private reporter?: Reporter;
private config: PublicConfigType;
constructor(initializerContext: PluginInitializerContext) {
this.config = initializerContext.config.get<PublicConfigType>();
constructor(private readonly initContext: PluginInitializerContext) {
this.config = initContext.config.get<PublicConfigType>();
}

public setup({ http }: CoreSetup): UsageCollectionSetup {
const localStorage = new Storage(window.localStorage);
const debug = this.config.uiCounters.debug;

this.reporter = createReporter({
localStorage,
debug,
logger: this.initContext.logger.get('reporter'),
fetch: http,
});

Expand Down
11 changes: 6 additions & 5 deletions src/plugins/usage_collection/public/services/create_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@
* Side Public License, v 1.
*/

import type { Logger } from '@kbn/logging';
import { Reporter, Storage } from '@kbn/analytics';
import { HttpSetup } from '@kbn/core/public';
import { UiCounters } from '../../common/types';

interface AnalyicsReporterConfig {
interface AnalyticsReporterConfig {
localStorage: Storage;
debug: boolean;
logger: Logger;
fetch: HttpSetup;
}

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

return new Reporter({
debug,
logger,
storage: localStorage,
async http(report) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down

0 comments on commit b0ec347

Please sign in to comment.