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

[Usage-based] Attribute workspace instances to a Team on start #10574

Merged
merged 2 commits into from
Jun 16, 2022

Conversation

jankeromnes
Copy link
Contributor

@jankeromnes jankeromnes commented Jun 10, 2022

Description

When a workspace instance starts, we want to determine and persist which "cost center" that instance's usage should be attributed to:

  • If wsi.attributedTeamId is set to a specific Team ID, then that Team "owns" the instance's usage
    • This means that the instance's usage time will be reflected in the Team's Usage dashboard, and in their Stripe invoices if usage-based billing is enabled
  • If wsi.attributedTeamId is not set, then it's the workspace owner (User) who owns the instance's usage
    • This means that we default to user billing or their free credits when no Team is attributed

Reminder: We need to do this on instance start, because the usual attribution links can change over time.

Related Issue(s)

Fixes #10533

How to test

  1. Should build
  2. In the DB, d_b_workspace_instance should have a attributedTeamId column that defaults to empty string ('')
  3. If you start a workspace without any Project:
    • its attributedTeamId should be empty (i.e. usage is attributed to you)
  4. If you create a Project under your User, and start a workspace for that User Project:
    • the attributedTeamId should also be empty (i.e. usage still attributed to you)
  5. If you create a Project under a Team, and then start a Workspace for that Team Project:
    • the attributedTeamId should now be set to the Team's ID (i.e. usage is attributed to the team)

Release Notes

NONE

Documentation

@jankeromnes

This comment was marked as outdated.

* @param user
* @param projectId
*/
async getWorkspaceUsageAttributionTeamId(user: User, projectId?: string): Promise<string | undefined> {
Copy link
Contributor Author

@jankeromnes jankeromnes Jun 15, 2022

Choose a reason for hiding this comment

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

Placing this method in UserService is debatable. For example, this logic could be in-lined in workspace-starter.ts (which already has projectDb injected).

My reason for placing it here was to allow an extension point where ee/UserService could detect if payment is enabled, and use a different logic (i.e. attribute all usage to a "cost center" team, instead of the project's team).

I don't have any opinion on this -- feedback most appreciated.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not strongly opinionated here either but your reasoning here makes sense; the subject of cost center vs team has already come up in discussions.

@werft-gitpod-dev-com
Copy link

started the job as gitpod-build-jx-attributed-team.3 because the annotations in the pull request description changed
(with .werft/ from main)

@werft-gitpod-dev-com
Copy link

started the job as gitpod-build-jx-attributed-team.4 because the annotations in the pull request description changed
(with .werft/ from main)

@jankeromnes jankeromnes marked this pull request as ready for review June 15, 2022 15:34
@jankeromnes jankeromnes requested a review from a team June 15, 2022 15:34
@github-actions github-actions bot added the team: webapp Issue belongs to the WebApp team label Jun 15, 2022
@werft-gitpod-dev-com
Copy link

started the job as gitpod-build-jx-attributed-team.5 because the annotations in the pull request description changed
(with .werft/ from main)

@werft-gitpod-dev-com
Copy link

started the job as gitpod-build-jx-attributed-team.6 because the annotations in the pull request description changed
(with .werft/ from main)

@werft-gitpod-dev-com
Copy link

started the job as gitpod-build-jx-attributed-team.7 because the annotations in the pull request description changed
(with .werft/ from main)

@werft-gitpod-dev-com
Copy link

started the job as gitpod-build-jx-attributed-team.8 because the annotations in the pull request description changed
(with .werft/ from main)

Comment on lines +69 to +71
* If unset, the usage should be attributed to the workspace's owner (ws.ownerId).
*/
attributedTeamId?: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than leaving this unset in this case, should we explicitly fill in the ownerId?

Copy link
Contributor Author

@jankeromnes jankeromnes Jun 16, 2022

Choose a reason for hiding this comment

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

I believe leaving it unset for user attribution makes more sense:

  • It can only be a teamId (the billing is either delegated to a specific team, or it falls back to the natural owner -- delegating to a different specific user would not make sense, right? 🤔)
  • Setting it to possibly a userId makes the design more complex (need a second column attributedUserId, or maybe a prefix to the ID e.g. team-1324 vs user-1234)
  • The team attribution chain goes through instance > workspace > project > team (some of the links can change over time so persisting the endpoint makes sense). User attribution is more direct (instance > workspace > owner) -- I don't think we can even get to a state where we don't know which user an instance belongs to.

Copy link
Contributor

@andrew-farries andrew-farries Jun 16, 2022

Choose a reason for hiding this comment

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

delegating to a different user would not make sense, right?

Yes, that's my understanding too.

@andrew-farries
Copy link
Contributor

andrew-farries commented Jun 16, 2022

Tested this myself and saw the expected teamIds 👍

@roboquat roboquat merged commit 402894b into main Jun 16, 2022
@roboquat roboquat deleted the jx/attributed-team branch June 16, 2022 07:28
@jankeromnes
Copy link
Contributor Author

jankeromnes commented Jun 16, 2022

Hmm, I just realized how similar this is to #10632 🤔

My initial reflex was to revert my PR again -- but then I realized my PR will allow to re-attribute usage to a different team (e.g. if you have a team with usage-based billing that wants to handle all your workspace usage regardless of project/team). So, maybe the two are actually complementary (and when we re-attribute, we might have to update the workspace metadata too 💡)

@roboquat roboquat added deployed: webapp Meta team change is running in production deployed Change is completely running in production labels Jun 16, 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
None yet
Development

Successfully merging this pull request may close these issues.

[Usage-based] Attribute workspace instances on start
3 participants