Skip to content

Commit

Permalink
[supervisor-frontend] fix metrics reporting (gitpod-io#17361)
Browse files Browse the repository at this point in the history
* [supervisor-frontend] fix metrics reporting

when workspace URL contains search, hash or path

* ensure that GitpodHostUrl always trim search, hash, path

+ remove unused arg type and function
  • Loading branch information
akosyakov authored Apr 27, 2023
1 parent 96bc402 commit 8f1640e
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 40 deletions.
2 changes: 1 addition & 1 deletion components/dashboard/src/service/service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class IDEFrontendService implements IDEFrontendDashboardService.IServer {
// send last heartbeat (wasClosed: true)
const data = { sessionId: this.sessionId };
const blob = new Blob([JSON.stringify(data)], { type: "application/json" });
const gitpodHostUrl = new GitpodHostUrl(new URL(window.location.toString()));
const gitpodHostUrl = new GitpodHostUrl(window.location.toString());
const url = gitpodHostUrl.withApi({ pathname: `/auth/workspacePageClose/${this.instanceID}` }).toString();
navigator.sendBeacon(url, blob);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TestGenerateWorkspaceId {
@test public async testGenerateWorkspaceId() {
for (let i = 0; i < 10; i++) {
const id = await generateWorkspaceID();
expect(new GitpodHostUrl().withWorkspacePrefix(id, "eu").workspaceId).to.equal(id);
expect(new GitpodHostUrl("https://gitpod.io").withWorkspacePrefix(id, "eu").workspaceId).to.equal(id);
}
}

Expand Down Expand Up @@ -50,7 +50,7 @@ class TestGenerateWorkspaceId {
for (const d of data) {
const id = await generateWorkspaceID(d[0], d[1]);
expect(id).match(new RegExp("^" + d[2]));
expect(new GitpodHostUrl().withWorkspacePrefix(id, "eu").workspaceId).to.equal(id);
expect(new GitpodHostUrl("https://gitpod.io").withWorkspacePrefix(id, "eu").workspaceId).to.equal(id);
expect(id.length <= 36, `"${id}" is longer than 36 chars (${id.length})`).to.be.true;
}
}
Expand Down
29 changes: 9 additions & 20 deletions components/gitpod-protocol/src/util/gitpod-host-url.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,52 @@ const expect = chai.expect;

@suite
export class GitpodHostUrlTest {
@test public parseWorkspaceId_pathBased() {
const actual = GitpodHostUrl.fromWorkspaceUrl(
"http://35.223.201.195/workspace/bc77e03d-c781-4235-bca0-e24087f5e472/",
).workspaceId;
expect(actual).to.equal("bc77e03d-c781-4235-bca0-e24087f5e472");
}

@test public parseWorkspaceId_hosts_withEnvVarsInjected() {
const actual = GitpodHostUrl.fromWorkspaceUrl(
const actual = new GitpodHostUrl(
"https://gray-grasshopper-nfbitfia.ws-eu02.gitpod-staging.com/#passedin=test%20value/https://github.com/gitpod-io/gitpod-test-repo",
).workspaceId;
expect(actual).to.equal("gray-grasshopper-nfbitfia");
}

@test public async testWithoutWorkspacePrefix() {
expect(
GitpodHostUrl.fromWorkspaceUrl("https://3000-moccasin-ferret-155799b3.ws-eu02.gitpod-staging.com/")
new GitpodHostUrl("https://3000-moccasin-ferret-155799b3.ws-eu02.gitpod-staging.com/")
.withoutWorkspacePrefix()
.toString(),
).to.equal("https://gitpod-staging.com/");
}

@test public async testWithoutWorkspacePrefix2() {
expect(
GitpodHostUrl.fromWorkspaceUrl("https://gitpod-staging.com/").withoutWorkspacePrefix().toString(),
).to.equal("https://gitpod-staging.com/");
expect(new GitpodHostUrl("https://gitpod-staging.com/").withoutWorkspacePrefix().toString()).to.equal(
"https://gitpod-staging.com/",
);
}

@test public async testWithoutWorkspacePrefix3() {
expect(
GitpodHostUrl.fromWorkspaceUrl("https://gray-rook-5523v5d8.ws-dev.my-branch-1234.staging.gitpod-dev.com/")
new GitpodHostUrl("https://gray-rook-5523v5d8.ws-dev.my-branch-1234.staging.gitpod-dev.com/")
.withoutWorkspacePrefix()
.toString(),
).to.equal("https://my-branch-1234.staging.gitpod-dev.com/");
}

@test public async testWithoutWorkspacePrefix4() {
expect(
GitpodHostUrl.fromWorkspaceUrl("https://my-branch-1234.staging.gitpod-dev.com/")
.withoutWorkspacePrefix()
.toString(),
new GitpodHostUrl("https://my-branch-1234.staging.gitpod-dev.com/").withoutWorkspacePrefix().toString(),
).to.equal("https://my-branch-1234.staging.gitpod-dev.com/");
}

@test public async testWithoutWorkspacePrefix5() {
expect(
GitpodHostUrl.fromWorkspaceUrl("https://abc-nice-brunch-4224.staging.gitpod-dev.com/")
new GitpodHostUrl("https://abc-nice-brunch-4224.staging.gitpod-dev.com/")
.withoutWorkspacePrefix()
.toString(),
).to.equal("https://abc-nice-brunch-4224.staging.gitpod-dev.com/");
}

@test public async testWithoutWorkspacePrefix6() {
expect(
GitpodHostUrl.fromWorkspaceUrl(
"https://gray-rook-5523v5d8.ws-dev.abc-nice-brunch-4224.staging.gitpod-dev.com/",
)
new GitpodHostUrl("https://gray-rook-5523v5d8.ws-dev.abc-nice-brunch-4224.staging.gitpod-dev.com/")
.withoutWorkspacePrefix()
.toString(),
).to.equal("https://abc-nice-brunch-4224.staging.gitpod-dev.com/");
Expand Down
13 changes: 6 additions & 7 deletions components/gitpod-protocol/src/util/gitpod-host-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,22 @@ const workspaceUrlPrefixRegex = RegExp(`^(([0-9]{4,6}|debug)-)?${baseWorkspaceID
export class GitpodHostUrl {
readonly url: URL;

constructor(urlParam?: string | URL) {
if (urlParam === undefined || typeof urlParam === "string") {
this.url = new URL(urlParam || "https://gitpod.io");
constructor(url: string) {
const urlParam = url as any;
if (typeof urlParam === "string") {
// public constructor
this.url = new URL(url);
this.url.search = "";
this.url.hash = "";
this.url.pathname = "";
} else if (urlParam instanceof URL) {
// internal constructor, see with
this.url = urlParam;
} else {
log.error("Unexpected urlParam", { urlParam });
}
}

static fromWorkspaceUrl(url: string) {
return new GitpodHostUrl(new URL(url));
}

withWorkspacePrefix(workspaceId: string, region: string) {
return this.withDomainPrefix(`${workspaceId}.ws-${region}.`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class TestBitbucketServerApi {
createGitpodToken: async () => ({ token: { value: "foobar123-token" } }),
} as any);
bind(Config).toConstantValue({
hostUrl: new GitpodHostUrl(),
hostUrl: new GitpodHostUrl("https://gitpod.io"),
});
bind(TokenProvider).toConstantValue(<TokenProvider>{
getTokenForHost: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class TestBitbucketServerContextParser {
createGitpodToken: async () => ({ token: { value: "foobar123-token" } }),
} as any);
bind(Config).toConstantValue({
hostUrl: new GitpodHostUrl(),
hostUrl: new GitpodHostUrl("https://gitpod.io"),
});
bind(TokenProvider).toConstantValue(<TokenProvider>{
getTokenForHost: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class TestBitbucketServerFileProvider {
createGitpodToken: async () => ({ token: { value: "foobar123-token" } }),
} as any);
bind(Config).toConstantValue({
hostUrl: new GitpodHostUrl(),
hostUrl: new GitpodHostUrl("https://gitpod.io"),
});
bind(TokenProvider).toConstantValue(<TokenProvider>{
getTokenForHost: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class TestBitbucketServerRepositoryProvider {
createGitpodToken: async () => ({ token: { value: "foobar123-token" } }),
} as any);
bind(Config).toConstantValue({
hostUrl: new GitpodHostUrl(),
hostUrl: new GitpodHostUrl("https://gitpod.io"),
});
bind(TokenProvider).toConstantValue(<TokenProvider>{
getTokenForHost: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class TestBitbucketServerService {
createGitpodToken: async () => ({ token: { value: "foobar123-token" } }),
} as any);
bind(Config).toConstantValue({
hostUrl: new GitpodHostUrl(),
hostUrl: new GitpodHostUrl("https://gitpod.io"),
});
bind(TokenProvider).toConstantValue(<TokenProvider>{
getTokenForHost: async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ContentStatusResponse,
} from "@gitpod/supervisor-api-grpc/lib/status_pb";
import { WorkspaceInfoResponse } from "@gitpod/supervisor-api-grpc/lib/info_pb";
import { GitpodHostUrl } from "@gitpod/gitpod-protocol/lib/util/gitpod-host-url";
import { workspaceUrl } from "../shared/urls";

export class SupervisorServiceClient {
private static _instance: SupervisorServiceClient | undefined;
Expand Down Expand Up @@ -41,7 +41,7 @@ export class SupervisorServiceClient {
await new Promise((resolve) => setTimeout(resolve, 1000));
}
try {
const wsSupervisorStatusUrl = GitpodHostUrl.fromWorkspaceUrl(window.location.href).with((url) => {
const wsSupervisorStatusUrl = workspaceUrl.with(() => {
return {
pathname: "/_supervisor/v1/status/supervisor/willShutdown/true",
};
Expand Down Expand Up @@ -85,7 +85,7 @@ export class SupervisorServiceClient {
wait = "";
}
try {
const wsSupervisorStatusUrl = GitpodHostUrl.fromWorkspaceUrl(window.location.href).with((url) => {
const wsSupervisorStatusUrl = workspaceUrl.with(() => {
return {
pathname: "/_supervisor/v1/status/" + kind + wait,
};
Expand Down Expand Up @@ -121,7 +121,7 @@ export class SupervisorServiceClient {
await new Promise((resolve) => setTimeout(resolve, 1000));
}
try {
const getWorkspaceInfoUrl = GitpodHostUrl.fromWorkspaceUrl(window.location.href).with((url) => {
const getWorkspaceInfoUrl = workspaceUrl.with(() => {
return {
pathname: "_supervisor/v1/info/workspace",
};
Expand Down
2 changes: 1 addition & 1 deletion components/supervisor/frontend/src/shared/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { GitpodHostUrl } from "@gitpod/gitpod-protocol/lib/util/gitpod-host-url";

export const workspaceUrl = GitpodHostUrl.fromWorkspaceUrl(window.location.href);
export const workspaceUrl = new GitpodHostUrl(window.location.href);

export const serverUrl = workspaceUrl.withoutWorkspacePrefix();

Expand Down

0 comments on commit 8f1640e

Please sign in to comment.