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

[server] add http endpoint workspacePageClose for beacon #11643

Merged
merged 1 commit into from
Aug 1, 2022
Merged

Conversation

mustard-mh
Copy link
Contributor

@mustard-mh mustard-mh commented Jul 26, 2022

Description

Add HTTP endpoint for supervisor frontend to send heartbeat when user's leaving the browser

In the current implementation of supervisor-frontend, we will send some requests before/during browser unload, which may cause some data loss since requests will be canceled after 1 second once user close the tab.

const beforeUnloadListener = () => {
unloadTimeout = setTimeout(() => {
// if unload was cancelled then resume heartbeating
sendHeartBeat();
}, 2000);
sendHeartBeat(true);
};
const unloadListener = () => {
if (unloadTimeout) {
clearTimeout(unloadTimeout);
}
}
window.addEventListener('beforeunload', beforeUnloadListener);
window.addEventListener('unload', unloadListener);

We will move those requests to use Navigator.sendBeacon to avoid data loss.

So this PR is going to add HTTP post endpoint for sendBeacon

see also internal chat

Related Issue(s)

Fixes #

How to test

  • Open a workspace in preview env
  • Get needed info from cookie (preview env logged in cookie and workspace auth cookie)
  • CURL to see if API works
curl -X "POST" "https://hw-hb.preview.gitpod-dev.com/api/auth/workspacePageClose/<instance_id>" \
     -H 'Cookie: _hw_hb_preview_gitpod_dev_com_=****;_hw_hb_preview_gitpod_dev_com_ws_****_owner_=****' \
     -H 'Content-Type: application/json; charset=utf-8' \
     -d $'{
  "sessionId": "1"
}'
  • Use gpctl workspaces last-heartbeat <instance_id> to see if heartbeat sent
  • Use kubectl get pods ws-<instance_id> -oyaml | yq4 .metadata.annotations | grep closed to see if wasClosed works

Release Notes

NONE

Documentation

Werft options:

  • /werft with-preview

@werft-gitpod-dev-com
Copy link

started the job as gitpod-build-hw-hb-server.1 because the annotations in the pull request description changed
(with .werft/ from main)

components/server/src/analytics.ts Outdated Show resolved Hide resolved
components/server/src/user/user-controller.ts Outdated Show resolved Hide resolved
components/server/src/user/user-controller.ts Outdated Show resolved Hide resolved
components/server/src/analytics.ts Outdated Show resolved Hide resolved
@akosyakov
Copy link
Member

add http send heartbeat endpoint

Please make it sure to update the PR description to explain why we do it instead of using Gitpod API, i.e. it is not about general heartbeating, but making sure that we report close signal always. General heartbeating still happens using Gitpod API, but it is not reliable to use while page is unloading.

@mustard-mh mustard-mh marked this pull request as ready for review July 29, 2022 07:57
@mustard-mh mustard-mh requested a review from a team July 29, 2022 07:57
@github-actions github-actions bot added the team: webapp Issue belongs to the WebApp team label Jul 29, 2022
]);
if (!workspace || instance?.status.phase !== "running") {
res.sendStatus(404);
log.warn("attempted to send heartbeat for non-existent workspace instance", {
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be log.warn(<context>, <message>)? [1]

I think userId, istanceId are part of LogContext

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@andreafalzetti See here

log.warn("blocked user attempted to fetch workspace cookie", {
instanceId: req.params.instanceID,
userId: user.id,
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought log support a lot of param pairs, and we used to make log string as first param in most cases

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not quite sure if the order of params will affect the query in gcloud, I unresolved this conversation and maybe webApp team can answer us @andreafalzetti

Copy link
Member

Choose a reason for hiding this comment

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

I'm not quite sure if the order of params will affect the query in gcloud

@mustard-mh It does indeed.

we used to make log string as first param in most cases

That's not good. The expected order is: logContext, message, error, payload.

@akosyakov
Copy link
Member

We need to test it coupled with supervisor-frontend. If it does not work there reliably, then this PR won't make sense.

@mustard-mh mustard-mh changed the title [server] add http send heartbeat endpoint [server] add http endpoint workspacePageClose for beacon Jul 29, 2022
@geropl geropl self-assigned this Aug 1, 2022
server.initialize(
undefined,
user,
new OwnerResourceGuard(user.id),
Copy link
Member

Choose a reason for hiding this comment

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

Note: This line ensures that only the owner can access the sendHeartbeat method for their instances.

Copy link
Member

Choose a reason for hiding this comment

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

ouch, it is a valid point in case of live sharing cc @mustard-mh we need to check how construct proper guard similar how we do for web socket connections, i think there should be a service fro HTTP connections as well already

Copy link
Member

Choose a reason for hiding this comment

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

ouch, it is a valid point in case of live sharing

💡

This is the code we use for the websocket connection.

Copy link
Member

@geropl geropl left a comment

Choose a reason for hiding this comment

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

Code LGTM

@roboquat roboquat merged commit 9438a3d into main Aug 1, 2022
@roboquat roboquat deleted the hw/hb-server branch August 1, 2022 08:44
res.sendStatus(200);
} catch (e) {
log.error("workspacePageClose failed", e);
res.sendStatus(500);
Copy link
Member

@akosyakov akosyakov Aug 1, 2022

Choose a reason for hiding this comment

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

@mustard-mh I just realised that it is wrong, i.e. if a user tries to access a workspace to which they don't have access it will pop up in our metrics like 500 (i.e. our failure), then it should be 403. Could you have a look sendHeartbeat method and translate exceptions properly? 🙏 cc @gitpod-io/engineering-webapp

@roboquat roboquat added deployed: webapp Meta team change is running in production deployed Change is completely running in production labels Aug 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deployed: webapp Meta team change is running in production deployed Change is completely running in production release-note-none size/M team: webapp Issue belongs to the WebApp team
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

5 participants