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

Makes usage collection methods available on start #69836

Merged
Changes from all commits
Commits
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
15 changes: 13 additions & 2 deletions src/plugins/usage_collection/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,17 @@ export interface UsageCollectionSetup {
};
}

export interface UsageCollectionStart {
reportUiStats: Reporter['reportUiStats'];
METRIC_TYPE: typeof METRIC_TYPE;
}

export function isUnauthenticated(http: HttpSetup) {
const { anonymousPaths } = http;
return anonymousPaths.isAnonymous(window.location.pathname);
}

export class UsageCollectionPlugin implements Plugin<UsageCollectionSetup> {
export class UsageCollectionPlugin implements Plugin<UsageCollectionSetup, UsageCollectionStart> {
private readonly legacyAppId$ = new Subject<string>();
private trackUserAgent: boolean = true;
private reporter?: Reporter;
Expand Down Expand Up @@ -90,7 +95,7 @@ export class UsageCollectionPlugin implements Plugin<UsageCollectionSetup> {

public start({ http, application }: CoreStart) {
if (!this.reporter) {
return;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure the best way to handle this but it shouldn't ever happen as far as I understand it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, i believe its there since private reporter?: Reporter; is not initialized by the constructor so it must be optional.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Now we have to always return the contract, so I switched it to throw in this ("impossible"?) case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing an error sounds reasonable here, if for any reason it was not registered consumers will start throwing -harder to understand- errors anyways.

throw new Error('Usage collection reporter not set up correctly');
}

if (this.config.uiMetric.enabled && !isUnauthenticated(http)) {
Expand All @@ -100,7 +105,13 @@ export class UsageCollectionPlugin implements Plugin<UsageCollectionSetup> {
if (this.trackUserAgent) {
this.reporter.reportUserAgent('kibana');
}

reportApplicationUsage(merge(application.currentAppId$, this.legacyAppId$), this.reporter);

return {
reportUiStats: this.reporter.reportUiStats,
METRIC_TYPE,
};
}

public stop() {}
Expand Down