Skip to content

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

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

Merged
merged 2 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,10 @@ export class DBWorkspaceInstance implements WorkspaceInstance {
transformer: Transformer.MAP_EMPTY_STR_TO_UNDEFINED,
})
workspaceClass?: string;

@Column({
default: "",
transformer: Transformer.MAP_EMPTY_STR_TO_UNDEFINED,
})
attributedTeamId?: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/

import { MigrationInterface, QueryRunner } from "typeorm";
import { columnExists } from "./helper/helper";

const TABLE_NAME = "d_b_workspace_instance";
const COLUMN_NAME = "attributedTeamId";

export class WorkspaceInstanceAttributedTeam1654847406624 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
if (!(await columnExists(queryRunner, TABLE_NAME, COLUMN_NAME))) {
await queryRunner.query(
`ALTER TABLE ${TABLE_NAME} ADD COLUMN ${COLUMN_NAME} char(36) NOT NULL DEFAULT '', ALGORITHM=INPLACE, LOCK=NONE`,
);
}
}

public async down(queryRunner: QueryRunner): Promise<void> {
if (await columnExists(queryRunner, TABLE_NAME, COLUMN_NAME)) {
await queryRunner.query(
`ALTER TABLE ${TABLE_NAME} DROP COLUMN ${COLUMN_NAME}, ALGORITHM=INPLACE, LOCK=NONE`,
);
}
}
}
4 changes: 4 additions & 0 deletions components/gitpod-db/src/workspace-db.spec.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class WorkspaceDBSpec {
ideImage: "unknown",
},
deleted: false,
attributedTeamId: undefined,
};
readonly wsi2: WorkspaceInstance = {
workspaceId: this.ws.id,
Expand All @@ -88,6 +89,7 @@ class WorkspaceDBSpec {
ideImage: "unknown",
},
deleted: false,
attributedTeamId: undefined,
};
readonly ws2: Workspace = {
id: "2",
Expand Down Expand Up @@ -125,6 +127,7 @@ class WorkspaceDBSpec {
ideImage: "unknown",
},
deleted: false,
attributedTeamId: undefined,
};

readonly ws3: Workspace = {
Expand Down Expand Up @@ -162,6 +165,7 @@ class WorkspaceDBSpec {
ideImage: "unknown",
},
deleted: false,
attributedTeamId: undefined,
};

async before() {
Expand Down
7 changes: 7 additions & 0 deletions components/gitpod-protocol/src/workspace-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ export interface WorkspaceInstance {
* resources that are provided to the workspace.
*/
workspaceClass?: string;

/**
* Identifies the team to which this instance's runtime should be attributed to
* (e.g. for usage analytics or billing purposes).
* If unset, the usage should be attributed to the workspace's owner (ws.ownerId).
*/
attributedTeamId?: string;
Comment on lines +69 to +71
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.

}

// WorkspaceInstanceStatus describes the current state of a workspace instance
Expand Down
25 changes: 24 additions & 1 deletion components/server/src/user/user-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
WORKSPACE_TIMEOUT_EXTENDED,
WORKSPACE_TIMEOUT_EXTENDED_ALT,
} from "@gitpod/gitpod-protocol";
import { TermsAcceptanceDB, UserDB } from "@gitpod/gitpod-db/lib";
import { ProjectDB, TermsAcceptanceDB, UserDB } from "@gitpod/gitpod-db/lib";
import { HostContextProvider } from "../auth/host-context-provider";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
import { Config } from "../config";
Expand Down Expand Up @@ -63,6 +63,7 @@ export class UserService {
@inject(Config) protected readonly config: Config;
@inject(TermsAcceptanceDB) protected readonly termsAcceptanceDb: TermsAcceptanceDB;
@inject(TermsProvider) protected readonly termsProvider: TermsProvider;
@inject(ProjectDB) protected readonly projectDb: ProjectDB;

/**
* Takes strings in the form of <authHost>/<authName> and returns the matching User
Expand Down Expand Up @@ -205,6 +206,28 @@ export class UserService {
return false;
}

/**
* Identifies the team to which a workspace instance's running time should be attributed to
* (e.g. for usage analytics or billing purposes).
* If no specific team is identified, the usage will be attributed to the user instead (default).
*
* @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.

if (!projectId) {
// No project -- attribute to the user.
return undefined;
}
const project = await this.projectDb.findProjectById(projectId);
if (!project?.teamId) {
// The project doesn't exist, or it isn't owned by a team -- attribute to the user.
return undefined;
}
// Attribute workspace usage to the team that currently owns this project.
return project.teamId;
}

/**
* This might throw `AuthException`s.
*
Expand Down
9 changes: 6 additions & 3 deletions components/server/src/workspace/workspace-starter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,9 @@ export class WorkspaceStarter {
delete ideConfig.ideOptions.options["code-latest"];
delete ideConfig.ideOptions.options["code-desktop-insiders"];

const migratted = migrationIDESettings(user);
if (user.additionalData?.ideSettings && migratted) {
user.additionalData.ideSettings = migratted;
const migrated = migrationIDESettings(user);
if (user.additionalData?.ideSettings && migrated) {
user.additionalData.ideSettings = migrated;
}

const ideChoice = user.additionalData?.ideSettings?.defaultIde;
Expand Down Expand Up @@ -709,6 +709,8 @@ export class WorkspaceStarter {
configuration.featureFlags = featureFlags;
}

const attributedTeamId = await this.userService.getWorkspaceUsageAttributionTeamId(user, workspace.projectId);

const now = new Date().toISOString();
const instance: WorkspaceInstance = {
id: uuidv4(),
Expand All @@ -722,6 +724,7 @@ export class WorkspaceStarter {
phase: "preparing",
},
configuration,
attributedTeamId,
};
if (WithReferrerContext.is(workspace.context)) {
this.analytics.track({
Expand Down