-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5119c52
commit 2537c4f
Showing
6 changed files
with
128 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package telemetry | ||
|
||
// classifyCmdErr maps errors to fixed strings to avoid leaking sensitive data inside error messages. | ||
func classifyCmdErr(e error) string { | ||
_ = e | ||
switch { | ||
default: | ||
return "unknown error" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package telemetry | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"runtime" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
// TODO(davidweisse): switch to "telemetry.confidential.cloud". | ||
apiHost = "telemetry-dot-constellation-license-server.ew.r.appspot.com" | ||
telemetryPath = "api/contrast/telemetry/v1" | ||
) | ||
|
||
// RequestV1 holds the information to be sent to the telemetry server. | ||
type RequestV1 struct { | ||
Version string `json:"version"` | ||
GOOS string `json:"goos"` | ||
GOARCH string `json:"goarch"` | ||
Cmd string `json:"cmd"` | ||
CmdErrString string `json:"cmderrstring"` | ||
Test bool `json:"test" gorm:"-"` | ||
} | ||
|
||
// IsTest checks if the request is used for testing. | ||
func (r *RequestV1) IsTest() bool { | ||
return r.Test | ||
} | ||
|
||
// Client sends the telemetry. | ||
type Client struct { | ||
httpClient *http.Client | ||
} | ||
|
||
// NewClient creates a new Client. | ||
func NewClient() *Client { | ||
return &Client{ | ||
httpClient: &http.Client{}, | ||
} | ||
} | ||
|
||
// SendTelemetry sends telemetry data to the telemetry server. | ||
func (c *Client) SendTelemetry(ctx context.Context, cmd *cobra.Command, cmdErr error) error { | ||
cmdErrString := classifyCmdErr(cmdErr) | ||
|
||
if cmd.Root().Version == "" { | ||
return fmt.Errorf("no cli version found") | ||
} | ||
|
||
telemetryRequest := RequestV1{ | ||
Version: cmd.Root().Version, | ||
GOOS: runtime.GOOS, | ||
GOARCH: runtime.GOARCH, | ||
Cmd: cmd.Name(), | ||
CmdErrString: cmdErrString, | ||
Test: false, | ||
} | ||
|
||
reqBody, err := json.Marshal(telemetryRequest) | ||
if err != nil { | ||
return fmt.Errorf("marshalling input: %w", err) | ||
} | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, telemetryURL().String(), bytes.NewReader(reqBody)) | ||
if err != nil { | ||
return fmt.Errorf("creating request: %w", err) | ||
} | ||
req.Header.Add("Content-Type", "application/json") | ||
resp, err := c.httpClient.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("doing request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("http error %d", resp.StatusCode) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func telemetryURL() *url.URL { | ||
return &url.URL{ | ||
Scheme: "https", | ||
Host: apiHost, | ||
Path: telemetryPath, | ||
} | ||
} |