|
| 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 | + |
| 5 | +package utils |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "net/url" |
| 11 | + |
| 12 | + gitpod "github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod" |
| 13 | + supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper" |
| 14 | + ide_metrics "github.com/gitpod-io/gitpod/ide-metrics-api" |
| 15 | + supervisor "github.com/gitpod-io/gitpod/supervisor/api" |
| 16 | + "github.com/go-errors/errors" |
| 17 | + log "github.com/sirupsen/logrus" |
| 18 | + "golang.org/x/xerrors" |
| 19 | + "google.golang.org/grpc" |
| 20 | + "google.golang.org/grpc/credentials/insecure" |
| 21 | +) |
| 22 | + |
| 23 | +func LogError(ctx context.Context, errToReport error) { |
| 24 | + // todo: move supervisor dial into singleton helper |
| 25 | + conn, err := supervisor_helper.Dial(ctx) |
| 26 | + if err != nil { |
| 27 | + log.Fatal(err) |
| 28 | + } |
| 29 | + defer conn.Close() |
| 30 | + |
| 31 | + // todo: move wsInfo fetch into its own utility as used multiple times already |
| 32 | + wsInfo, err := supervisor.NewInfoServiceClient(conn).WorkspaceInfo(ctx, &supervisor.WorkspaceInfoRequest{}) |
| 33 | + if err != nil { |
| 34 | + fmt.Errorf("failed to retrieve workspace info: %w", err) |
| 35 | + } |
| 36 | + parsedUrl, err := url.Parse(wsInfo.GitpodHost) |
| 37 | + if err != nil { |
| 38 | + // todo: proper msg |
| 39 | + fmt.Errorf("todo", err) |
| 40 | + } |
| 41 | + |
| 42 | + ideMetricsUrl := fmt.Sprintf("https://ide.%s/metrics-api", parsedUrl.Host) |
| 43 | + |
| 44 | + fmt.Println(ideMetricsUrl) |
| 45 | + |
| 46 | + // todo: move this into its own singleton utiltiy to retrieve the ideMetricsConn |
| 47 | + ideMetricsConn, err := grpc.DialContext(ctx, ideMetricsUrl, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 48 | + if err != nil { |
| 49 | + err = xerrors.Errorf("failed connecting to supervisor: %w", err) |
| 50 | + } |
| 51 | + defer ideMetricsConn.Close() |
| 52 | + |
| 53 | + ideMetricsClient := ide_metrics.NewMetricsServiceClient(ideMetricsConn) |
| 54 | + |
| 55 | + reportErrorRequest := &ide_metrics.ReportErrorRequest{ |
| 56 | + ErrorStack: errToReport.(*errors.Error).ErrorStack(), |
| 57 | + Component: "gp-cli", |
| 58 | + Version: gitpod.Version, |
| 59 | + UserId: "", // todo: retrieve from server |
| 60 | + WorkspaceId: wsInfo.WorkspaceId, |
| 61 | + InstanceId: wsInfo.InstanceId, |
| 62 | + Properties: map[string]string{}, |
| 63 | + } |
| 64 | + |
| 65 | + ideMetricsClient.ReportError(ctx, reportErrorRequest) |
| 66 | + fmt.Println("Error reported succerssfully") |
| 67 | + |
| 68 | +} |
0 commit comments