Skip to content

Commit 3c07280

Browse files
Andrea Falzettiroboquat
authored andcommitted
feat(gitpod-cli): report errors to ide-metrics-api
1 parent b99f7b9 commit 3c07280

File tree

22 files changed

+212
-86
lines changed

22 files changed

+212
-86
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@ 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
1315
github.com/google/tcpproxy v0.0.0-20180808230851-dfa16c61dad2
1416
github.com/gorilla/handlers v1.5.1
1517
github.com/manifoldco/promptui v0.9.0
1618
github.com/olekukonko/tablewriter v0.0.5
17-
github.com/pkg/errors v0.9.1
1819
github.com/prometheus/procfs v0.8.0
1920
github.com/sirupsen/logrus v1.8.1
2021
github.com/sourcegraph/jsonrpc2 v0.0.0-20200429184054-15c2290dcb37
2122
github.com/spf13/cobra v1.1.3
2223
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d
2324
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467
24-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
25+
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f
2526
google.golang.org/grpc v1.49.0
2627
gopkg.in/yaml.v2 v2.4.0
2728
)
@@ -46,4 +47,6 @@ replace github.com/gitpod-io/gitpod/gitpod-protocol => ../gitpod-protocol/go //
4647

4748
replace github.com/gitpod-io/gitpod/supervisor/api => ../supervisor-api/go // leeway
4849

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

components/gitpod-cli/go.sum

Lines changed: 4 additions & 3 deletions
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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
"google.golang.org/grpc"
13+
"net/http"
14+
"net/url"
15+
"os"
16+
17+
gitpod "github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"
18+
supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper"
19+
ide_metrics "github.com/gitpod-io/gitpod/ide-metrics-api"
20+
"github.com/go-errors/errors"
21+
log "github.com/sirupsen/logrus"
22+
)
23+
24+
func LogError(ctx context.Context, errToReport error, errorMessage string) {
25+
log.WithError(errToReport).Error(errorMessage)
26+
27+
file, err := os.OpenFile(os.TempDir()+"/gitpod-cli-errors.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
28+
if err == nil {
29+
log.SetOutput(file)
30+
} else {
31+
log.SetLevel(log.FatalLevel)
32+
}
33+
34+
conn, err := supervisor_helper.Dial(ctx)
35+
if err != nil {
36+
log.WithError(err).Error(err)
37+
}
38+
defer func(conn *grpc.ClientConn) {
39+
err := conn.Close()
40+
if err != nil {
41+
log.WithError(err).Error(err)
42+
}
43+
}(conn)
44+
45+
wsInfo, err := supervisor_helper.FetchInfo(ctx, conn)
46+
if err != nil {
47+
log.WithError(err).Error("failed to retrieve workspace info")
48+
return
49+
}
50+
51+
parsedUrl, err := url.Parse(wsInfo.GitpodHost)
52+
if err != nil {
53+
log.WithError(err).Error("cannot parse GitpodHost")
54+
return
55+
}
56+
57+
ideMetricsUrl := fmt.Sprintf("https://ide.%s/metrics-api/reportError", parsedUrl.Host)
58+
59+
reportErrorRequest := &ide_metrics.ReportErrorRequest{
60+
ErrorStack: errors.New(errToReport).ErrorStack(),
61+
Component: "gitpod-cli",
62+
Version: gitpod.Version,
63+
UserId: "", // todo: retrieve this from server
64+
WorkspaceId: wsInfo.WorkspaceId,
65+
InstanceId: wsInfo.InstanceId,
66+
Properties: map[string]string{},
67+
}
68+
69+
payload, err := json.Marshal(reportErrorRequest)
70+
if err != nil {
71+
log.WithError(err).Error("failed to marshal json while attempting to report error")
72+
return
73+
}
74+
75+
req, err := http.NewRequest("POST", ideMetricsUrl, bytes.NewBuffer(payload))
76+
if err != nil {
77+
log.WithError(err).Error("failed to init request for ide-metrics-api")
78+
return
79+
}
80+
81+
client := &http.Client{}
82+
_, err = client.Do(req)
83+
if err != nil {
84+
log.WithError(err).Error("cannot report error to ide-metrics-api")
85+
return
86+
}
87+
}

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)