Path: blob/main/components/supervisor/frontend/src/ide/ide-metrics-service-client.ts
2501 views
/**1* Copyright (c) 2022 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { serverUrl, workspaceUrl } from "../shared/urls";7const commit = require("../../config.json").commit;8import { v4 } from "uuid";910import { MetricsReporter } from "@gitpod/gitpod-protocol/lib/metrics";1112export const metricsReporter = new MetricsReporter({13gitpodUrl: serverUrl.toString(),14clientName: "supervisor-frontend",15clientVersion: commit,16log: console,17commonErrorDetails: {18sessionId: v4(),19},20});21metricsReporter.startReporting();2223import { FrontendDashboardServiceClient } from "../shared/frontend-dashboard-service";2425// TODO(ak) migrate to MetricsReporter26const MetricsUrl = serverUrl.asIDEMetrics().toString();2728export enum MetricsName {29SupervisorFrontendClientTotal = "gitpod_supervisor_frontend_client_total",30SupervisorFrontendErrorTotal = "gitpod_supervisor_frontend_error_total",31SupervisorFrontendLoadTotal = "gitpod_vscode_web_load_total",32}3334interface AddCounterParam {35value?: number;36labels?: Record<string, string>;37}3839interface ReportErrorParam {40workspaceId: string;41instanceId: string;42errorStack: string;43userId: string;44component: string;45version: string;46properties?: Record<string, string>;47}48export class IDEMetricsServiceClient {49static workspaceId? = workspaceUrl.workspaceId;50static debugWorkspace = workspaceUrl.debugWorkspace;51static serviceClient: FrontendDashboardServiceClient;5253static get instanceId(): string {54return this.serviceClient.latestInfo?.instanceId ?? "";55}56static get userId(): string {57return this.serviceClient.latestInfo?.loggedUserId ?? "";58}5960static async addCounter(61metricsName: MetricsName,62labels?: Record<string, string>,63value?: number,64): Promise<boolean> {65const url = `${MetricsUrl}/metrics/counter/add/${metricsName}`;66const params: AddCounterParam = { value, labels };67try {68const response = await fetch(url, {69method: "POST",70body: JSON.stringify(params),71credentials: "omit",72priority: "low",73});74if (!response.ok) {75const data = await response.json(); // { code: number; message: string; }76console.error(`Cannot report metrics with addCounter: ${response.status} ${response.statusText}`, data);77return false;78}79return true;80} catch (err) {81console.error("Cannot report metrics with addCounter, error:", err);82return false;83}84}8586static async reportError(error: Error, properties?: Record<string, string>): Promise<boolean> {87const p = Object.assign({}, properties);88p.error_name = error.name;89p.error_message = error.message;90p.debug_workspace = String(this.debugWorkspace);9192const url = `${MetricsUrl}/reportError`;93const params: ReportErrorParam = {94errorStack: error.stack ?? String(error),95component: "supervisor-frontend",96version: commit,97workspaceId: this.workspaceId ?? "",98instanceId: this.instanceId,99userId: this.userId,100properties: p,101};102try {103const response = await fetch(url, {104method: "POST",105body: JSON.stringify(params),106credentials: "omit",107});108if (!response.ok) {109const data = await response.json();110console.error(`Cannot report error: ${response.status} ${response.statusText}`, data);111return false;112}113return true;114} catch (err) {115console.error("Cannot report errorr, error:", err);116return false;117}118}119120static loadWorkspaceInfo(serviceClient: FrontendDashboardServiceClient) {121this.serviceClient = serviceClient;122}123}124125126