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

[Lens] Clean up usage collector #89109

Merged
merged 4 commits into from
Jan 27, 2021
Merged
Changes from 1 commit
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
32 changes: 8 additions & 24 deletions x-pack/plugins/lens/server/usage/collectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ export function registerLensUsageCollector(
usageCollection: UsageCollectionSetup,
taskManager: Promise<TaskManagerStartContract>
) {
let isCollectorReady = false;
taskManager.then(() => {
// mark lensUsageCollector as ready to collect when the TaskManager is ready
isCollectorReady = true;
});
const lensUsageCollector = usageCollection.makeUsageCollector<LensUsage>({
type: 'lens',
async fetch() {
Expand Down Expand Up @@ -55,7 +50,10 @@ export function registerLensUsageCollector(
};
}
},
isReady: () => isCollectorReady,
isReady: async () => {
await taskManager;
return true;
},
schema: lensUsageSchema,
});

Expand All @@ -69,24 +67,10 @@ function addEvents(prevEvents: Record<string, number>, newEvents: Record<string,
}

async function getLatestTaskState(taskManager: TaskManagerStartContract) {
try {
const result = await taskManager.fetch({
query: { bool: { filter: { term: { _id: `task:Lens-lens_telemetry` } } } },
});
return result.docs;
} catch (err) {
const errMessage = err && err.message ? err.message : err.toString();
/*
The usage service WILL to try to fetch from this collector before the task manager has been initialized, because the
task manager has to wait for all plugins to initialize first. It's fine to ignore it as next time around it will be
initialized (or it will throw a different type of error)
*/
if (!errMessage.includes('NotInitialized')) {
throw err;
}
}

return null;
const result = await taskManager.fetch({
query: { bool: { filter: { term: { _id: `task:Lens-lens_telemetry` } } } },
});
return result.docs;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why removing the try/catch here?
It seems unrelated to the isReady change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The try catch was only necessary because early task manager didn't have an isReady check. As that's in place now, we don't need to fake everything's working fine (which is what the catch was doing here).

Copy link
Contributor

Choose a reason for hiding this comment

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

I see what you mean, but when calling this function there was already an await taskManager here: https://github.com/elastic/kibana/pull/89109/files#diff-baaab05f5a10da483776bde09fa1a8aefa3f83ba17db0b437a630bf1fc70330fR23

So probably the try/catch had no sense before as well : await taskManager is the whole isReady logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking into the commit where this got introduced I think you are right, I remembered it the wrong way. Reintroduced the catch.

}

function getDataByDate(dates: Record<string, Record<string, number>>) {
Expand Down