Skip to content

feat(gitpod-cli): report errors to ide-metrics-api #13752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions components/gitpod-cli/BUILD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ packages:
- components/supervisor-api/go:lib
- components/gitpod-protocol/go:lib
- components/common-go:lib
- components/ide-metrics-api/go:lib
config:
packaging: app
buildCommand: ["go", "build", "-trimpath", "-ldflags", "-buildid= -w -s -X 'github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod.Version=commit-${__git_commit}'"]
44 changes: 19 additions & 25 deletions components/gitpod-cli/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"time"

"github.com/gitpod-io/gitpod/common-go/log"
supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper"
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"os"
"time"
)

var infoCmdOpts struct {
Expand All @@ -37,7 +37,16 @@ var infoCmd = &cobra.Command{
}
defer conn.Close()

data, err := fetchInfo(ctx, conn)
wsInfo, err := supervisor_helper.FetchInfo(ctx, conn)

data := &infoData{
WorkspaceId: wsInfo.WorkspaceId,
InstanceId: wsInfo.InstanceId,
WorkspaceClass: wsInfo.WorkspaceClass,
WorkspaceUrl: wsInfo.WorkspaceUrl,
ClusterHost: wsInfo.WorkspaceClusterHost,
}

if err != nil {
log.Fatal(err)
}
Expand All @@ -52,19 +61,12 @@ var infoCmd = &cobra.Command{
},
}

func fetchInfo(ctx context.Context, conn *grpc.ClientConn) (*infoData, error) {
wsInfo, err := supervisor.NewInfoServiceClient(conn).WorkspaceInfo(ctx, &supervisor.WorkspaceInfoRequest{})
if err != nil {
return nil, fmt.Errorf("failed to retrieve workspace info: %w", err)
}

return &infoData{
WorkspaceId: wsInfo.WorkspaceId,
InstanceId: wsInfo.InstanceId,
WorkspaceClass: wsInfo.WorkspaceClass,
WorkspaceUrl: wsInfo.WorkspaceUrl,
ClusterHost: wsInfo.WorkspaceClusterHost,
}, nil
type infoData struct {
WorkspaceId string `json:"workspace_id"`
InstanceId string `json:"instance_id"`
WorkspaceClass *supervisor.WorkspaceInfoResponse_WorkspaceClass `json:"workspace_class"`
WorkspaceUrl string `json:"workspace_url"`
ClusterHost string `json:"cluster_host"`
}

func outputInfo(info *infoData) {
Expand All @@ -80,14 +82,6 @@ func outputInfo(info *infoData) {
table.Render()
}

type infoData struct {
WorkspaceId string `json:"workspace_id"`
InstanceId string `json:"instance_id"`
WorkspaceClass *supervisor.WorkspaceInfoResponse_WorkspaceClass `json:"workspace_class"`
WorkspaceUrl string `json:"workspace_url"`
ClusterHost string `json:"cluster_host"`
}

func init() {
infoCmd.Flags().BoolVarP(&infoCmdOpts.Json, "json", "j", false, "Output in JSON format")
rootCmd.AddCommand(infoCmd)
Expand Down
3 changes: 1 addition & 2 deletions components/gitpod-cli/cmd/ports-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper"
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

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

if portsListError != nil {
log.WithError(portsListError).Error("Could not get the ports list.")
utils.LogError(ctx, portsListError, "Could not get the ports list.")
return
}

Expand Down
7 changes: 5 additions & 2 deletions components/gitpod-cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ require (
github.com/creack/pty v1.1.17
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
github.com/gitpod-io/gitpod/gitpod-protocol v0.0.0-00010101000000-000000000000
github.com/gitpod-io/gitpod/ide-metrics-api v0.0.0-00010101000000-000000000000
github.com/gitpod-io/gitpod/supervisor/api v0.0.0-00010101000000-000000000000
github.com/go-errors/errors v1.4.2
github.com/golang/mock v1.6.0
github.com/google/go-cmp v0.5.8
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf
github.com/google/tcpproxy v0.0.0-20180808230851-dfa16c61dad2
github.com/gorilla/handlers v1.5.1
github.com/manifoldco/promptui v0.9.0
github.com/olekukonko/tablewriter v0.0.5
github.com/pkg/errors v0.9.1
github.com/prometheus/procfs v0.8.0
github.com/sirupsen/logrus v1.8.1
github.com/sourcegraph/jsonrpc2 v0.0.0-20200429184054-15c2290dcb37
github.com/spf13/cobra v1.1.3
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f
google.golang.org/grpc v1.49.0
gopkg.in/yaml.v2 v2.4.0
)
Expand All @@ -46,4 +47,6 @@ replace github.com/gitpod-io/gitpod/gitpod-protocol => ../gitpod-protocol/go //

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

replace github.com/gitpod-io/gitpod/ide-metrics-api => ../ide-metrics-api/go // leeway

replace github.com/gitpod-io/gitpod/common-go => ../common-go // leeway
7 changes: 4 additions & 3 deletions components/gitpod-cli/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions components/gitpod-cli/pkg/supervisor-helper/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package supervisor_helper

import (
"context"
"fmt"

"github.com/gitpod-io/gitpod/supervisor/api"
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
"google.golang.org/grpc"
)

func FetchInfo(ctx context.Context, conn *grpc.ClientConn) (*api.WorkspaceInfoResponse, error) {
wsInfo, err := supervisor.NewInfoServiceClient(conn).WorkspaceInfo(ctx, &supervisor.WorkspaceInfoRequest{})
if err != nil {
return nil, fmt.Errorf("failed to retrieve workspace info: %w", err)
}

return wsInfo, nil
}
87 changes: 87 additions & 0 deletions components/gitpod-cli/pkg/utils/logError.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package utils

import (
"bytes"
"context"
"encoding/json"
"fmt"
"google.golang.org/grpc"
"net/http"
"net/url"
"os"

gitpod "github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"
supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper"
ide_metrics "github.com/gitpod-io/gitpod/ide-metrics-api"
"github.com/go-errors/errors"
log "github.com/sirupsen/logrus"
)

func LogError(ctx context.Context, errToReport error, errorMessage string) {
log.WithError(errToReport).Error(errorMessage)

file, err := os.OpenFile(os.TempDir()+"/gitpod-cli-errors.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err == nil {
log.SetOutput(file)
} else {
log.SetLevel(log.FatalLevel)
}

conn, err := supervisor_helper.Dial(ctx)
if err != nil {
log.WithError(err).Error(err)
}
defer func(conn *grpc.ClientConn) {
err := conn.Close()
if err != nil {
log.WithError(err).Error(err)
}
}(conn)

wsInfo, err := supervisor_helper.FetchInfo(ctx, conn)
if err != nil {
log.WithError(err).Error("failed to retrieve workspace info")
return
}

parsedUrl, err := url.Parse(wsInfo.GitpodHost)
if err != nil {
log.WithError(err).Error("cannot parse GitpodHost")
return
}

ideMetricsUrl := fmt.Sprintf("https://ide.%s/metrics-api/reportError", parsedUrl.Host)

reportErrorRequest := &ide_metrics.ReportErrorRequest{
ErrorStack: errors.New(errToReport).ErrorStack(),
Component: "gitpod-cli",
Version: gitpod.Version,
UserId: "", // todo: retrieve this from server
WorkspaceId: wsInfo.WorkspaceId,
InstanceId: wsInfo.InstanceId,
Properties: map[string]string{},
}

payload, err := json.Marshal(reportErrorRequest)
if err != nil {
log.WithError(err).Error("failed to marshal json while attempting to report error")
return
}

req, err := http.NewRequest("POST", ideMetricsUrl, bytes.NewBuffer(payload))
if err != nil {
log.WithError(err).Error("failed to init request for ide-metrics-api")
return
}

client := &http.Client{}
_, err = client.Do(req)
if err != nil {
log.WithError(err).Error("cannot report error to ide-metrics-api")
return
}
}
2 changes: 1 addition & 1 deletion components/ide-metrics-api/go/idemetrics.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading