Skip to content

Commit 247ebc5

Browse files
author
Andrea Falzetti
committed
feat(gitpod-cli): report errors to ide-metrics-api
1 parent 52a5bcd commit 247ebc5

File tree

22 files changed

+206
-83
lines changed

22 files changed

+206
-83
lines changed

components/gitpod-cli/BUILD.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ packages:
1212
- components/supervisor-api/go:lib
1313
- components/gitpod-protocol/go:lib
1414
- components/common-go:lib
15+
- components/ide-metrics-api/go:lib
1516
config:
1617
packaging: app
1718
buildCommand: ["go", "build", "-trimpath", "-ldflags", "-buildid= -w -s -X 'github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod.Version=commit-${__git_commit}'"]

components/gitpod-cli/cmd/info.go

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import (
88
"context"
99
"encoding/json"
1010
"fmt"
11+
"os"
12+
"time"
13+
1114
"github.com/gitpod-io/gitpod/common-go/log"
1215
supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper"
1316
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
1417
"github.com/olekukonko/tablewriter"
1518
"github.com/spf13/cobra"
16-
"google.golang.org/grpc"
17-
"os"
18-
"time"
1919
)
2020

2121
var infoCmdOpts struct {
@@ -37,7 +37,16 @@ var infoCmd = &cobra.Command{
3737
}
3838
defer conn.Close()
3939

40-
data, err := fetchInfo(ctx, conn)
40+
wsInfo, err := supervisor_helper.FetchInfo(ctx, conn)
41+
42+
data := &infoData{
43+
WorkspaceId: wsInfo.WorkspaceId,
44+
InstanceId: wsInfo.InstanceId,
45+
WorkspaceClass: wsInfo.WorkspaceClass,
46+
WorkspaceUrl: wsInfo.WorkspaceUrl,
47+
ClusterHost: wsInfo.WorkspaceClusterHost,
48+
}
49+
4150
if err != nil {
4251
log.Fatal(err)
4352
}
@@ -52,19 +61,12 @@ var infoCmd = &cobra.Command{
5261
},
5362
}
5463

55-
func fetchInfo(ctx context.Context, conn *grpc.ClientConn) (*infoData, error) {
56-
wsInfo, err := supervisor.NewInfoServiceClient(conn).WorkspaceInfo(ctx, &supervisor.WorkspaceInfoRequest{})
57-
if err != nil {
58-
return nil, fmt.Errorf("failed to retrieve workspace info: %w", err)
59-
}
60-
61-
return &infoData{
62-
WorkspaceId: wsInfo.WorkspaceId,
63-
InstanceId: wsInfo.InstanceId,
64-
WorkspaceClass: wsInfo.WorkspaceClass,
65-
WorkspaceUrl: wsInfo.WorkspaceUrl,
66-
ClusterHost: wsInfo.WorkspaceClusterHost,
67-
}, nil
64+
type infoData struct {
65+
WorkspaceId string `json:"workspace_id"`
66+
InstanceId string `json:"instance_id"`
67+
WorkspaceClass *supervisor.WorkspaceInfoResponse_WorkspaceClass `json:"workspace_class"`
68+
WorkspaceUrl string `json:"workspace_url"`
69+
ClusterHost string `json:"cluster_host"`
6870
}
6971

7072
func outputInfo(info *infoData) {
@@ -80,14 +82,6 @@ func outputInfo(info *infoData) {
8082
table.Render()
8183
}
8284

83-
type infoData struct {
84-
WorkspaceId string `json:"workspace_id"`
85-
InstanceId string `json:"instance_id"`
86-
WorkspaceClass *supervisor.WorkspaceInfoResponse_WorkspaceClass `json:"workspace_class"`
87-
WorkspaceUrl string `json:"workspace_url"`
88-
ClusterHost string `json:"cluster_host"`
89-
}
90-
9185
func init() {
9286
infoCmd.Flags().BoolVarP(&infoCmdOpts.Json, "json", "j", false, "Output in JSON format")
9387
rootCmd.AddCommand(infoCmd)

components/gitpod-cli/cmd/ports-list.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper"
1515
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"
1616
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
17-
log "github.com/sirupsen/logrus"
1817
"github.com/spf13/cobra"
1918

2019
"github.com/olekukonko/tablewriter"
@@ -30,7 +29,7 @@ var listPortsCmd = &cobra.Command{
3029
ports, portsListError := supervisor_helper.GetPortsList(ctx)
3130

3231
if portsListError != nil {
33-
log.WithError(portsListError).Error("Could not get the ports list.")
32+
utils.LogError(ctx, portsListError, "Could not get the ports list.")
3433
return
3534
}
3635

components/gitpod-cli/go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ require (
66
github.com/creack/pty v1.1.17
77
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
88
github.com/gitpod-io/gitpod/gitpod-protocol v0.0.0-00010101000000-000000000000
9+
github.com/gitpod-io/gitpod/ide-metrics-api v0.0.0-00010101000000-000000000000
910
github.com/gitpod-io/gitpod/supervisor/api v0.0.0-00010101000000-000000000000
11+
github.com/go-errors/errors v1.4.2
1012
github.com/golang/mock v1.6.0
1113
github.com/google/go-cmp v0.5.8
1214
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf
@@ -21,7 +23,7 @@ require (
2123
github.com/spf13/cobra v1.1.3
2224
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d
2325
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467
24-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
26+
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f
2527
google.golang.org/grpc v1.49.0
2628
gopkg.in/yaml.v2 v2.4.0
2729
)
@@ -46,4 +48,6 @@ replace github.com/gitpod-io/gitpod/gitpod-protocol => ../gitpod-protocol/go //
4648

4749
replace github.com/gitpod-io/gitpod/supervisor/api => ../supervisor-api/go // leeway
4850

51+
replace github.com/gitpod-io/gitpod/ide-metrics-api => ../ide-metrics-api/go // leeway
52+
4953
replace github.com/gitpod-io/gitpod/common-go => ../common-go // leeway

components/gitpod-cli/go.sum

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 supervisor_helper
6+
7+
import (
8+
"context"
9+
"fmt"
10+
11+
"github.com/gitpod-io/gitpod/supervisor/api"
12+
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
13+
"google.golang.org/grpc"
14+
)
15+
16+
func FetchInfo(ctx context.Context, conn *grpc.ClientConn) (*api.WorkspaceInfoResponse, error) {
17+
wsInfo, err := supervisor.NewInfoServiceClient(conn).WorkspaceInfo(ctx, &supervisor.WorkspaceInfoRequest{})
18+
if err != nil {
19+
return nil, fmt.Errorf("failed to retrieve workspace info: %w", err)
20+
}
21+
22+
return wsInfo, nil
23+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
"bytes"
9+
"context"
10+
"encoding/json"
11+
"fmt"
12+
"io/ioutil"
13+
"net/http"
14+
"net/url"
15+
16+
gitpod "github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"
17+
supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper"
18+
ide_metrics "github.com/gitpod-io/gitpod/ide-metrics-api"
19+
"github.com/go-errors/errors"
20+
log "github.com/sirupsen/logrus"
21+
)
22+
23+
func LogError(ctx context.Context, errToReport error, errorMessage string) {
24+
log.WithError(errToReport).Error(errorMessage)
25+
26+
conn, err := supervisor_helper.Dial(ctx)
27+
if err != nil {
28+
log.Fatal(err)
29+
}
30+
defer conn.Close()
31+
32+
wsInfo, err := supervisor_helper.FetchInfo(ctx, conn)
33+
if err != nil {
34+
log.WithError(err).Fatal("failed to retrieve workspace info")
35+
return
36+
}
37+
38+
parsedUrl, err := url.Parse(wsInfo.GitpodHost)
39+
if err != nil {
40+
log.WithError(err).Fatal("cannot parse GitpodHost")
41+
return
42+
}
43+
44+
ideMetricsUrl := fmt.Sprintf("https://ide.%s/metrics-api/reportError", parsedUrl.Host)
45+
46+
reportErrorRequest := &ide_metrics.ReportErrorRequest{
47+
ErrorStack: errToReport.(*errors.Error).ErrorStack(),
48+
Component: "gitpod-cli",
49+
Version: gitpod.Version,
50+
UserId: "", // todo: retrieve this from server
51+
WorkspaceId: wsInfo.WorkspaceId,
52+
InstanceId: wsInfo.InstanceId,
53+
Properties: map[string]string{},
54+
}
55+
56+
payload, err := json.Marshal(reportErrorRequest)
57+
if err != nil {
58+
log.WithError(err).Fatal("failed to marshal json while attempting to report error")
59+
return
60+
}
61+
62+
req, err := http.NewRequest("POST", ideMetricsUrl, bytes.NewBuffer(payload))
63+
if err != nil {
64+
log.WithError(err).Fatal("failed to init request for ide-metrics-api")
65+
return
66+
}
67+
68+
client := &http.Client{}
69+
resp, err := client.Do(req)
70+
if err != nil {
71+
log.WithError(err).Fatal("cannot report error to ide-metrics-api")
72+
return
73+
}
74+
75+
// todo: remove before merging
76+
defer resp.Body.Close()
77+
body, _ := ioutil.ReadAll(resp.Body)
78+
fmt.Println("Error reported succerssfully")
79+
fmt.Println(string(body))
80+
fmt.Println(resp.StatusCode)
81+
}

components/ide-metrics-api/go/idemetrics.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)