update go-checkpoint
This commit is contained in:
parent
f534b85ee9
commit
6ff34c8788
|
@ -27,6 +27,7 @@ import (
|
|||
|
||||
var magicBytes [4]byte = [4]byte{0x35, 0x77, 0x69, 0xFB}
|
||||
|
||||
// ReportParams are the parameters for configuring a telemetry report.
|
||||
type ReportParams struct {
|
||||
// Signature is some random signature that should be stored and used
|
||||
// as a cookie-like value. This ensures that alerts aren't repeated.
|
||||
|
@ -43,14 +44,14 @@ type ReportParams struct {
|
|||
|
||||
StartTime time.Time `json:"start_time"`
|
||||
EndTime time.Time `json:"end_time"`
|
||||
Version string `json:"version"`
|
||||
Product string `json:"product"`
|
||||
Payload interface{} `json:"payload,omitempty"`
|
||||
RunID string `json:"run_id"`
|
||||
OS string `json:"os"`
|
||||
Arch string `json:"arch"`
|
||||
Args []string `json:"args"`
|
||||
OS string `json:"os"`
|
||||
Payload interface{} `json:"payload,omitempty"`
|
||||
Product string `json:"product"`
|
||||
RunID string `json:"run_id"`
|
||||
SchemaVersion string `json:"schema_version"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
func (i *ReportParams) signature() string {
|
||||
|
@ -65,15 +66,36 @@ func (i *ReportParams) signature() string {
|
|||
return signature
|
||||
}
|
||||
|
||||
// Report sends telemetry information to checkpoint
|
||||
func Report(ctx context.Context, r *ReportParams) error {
|
||||
if disabled := os.Getenv("CHECKPOINT_DISABLE"); disabled != "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
req, err := ReportRequest(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client := cleanhttp.DefaultClient()
|
||||
resp, err := client.Do(req.WithContext(ctx))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.StatusCode != 201 {
|
||||
return fmt.Errorf("Unknown status: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReportRequest creates a request object for making a report
|
||||
func ReportRequest(r *ReportParams) (*http.Request, error) {
|
||||
// Populate some fields automatically if we can
|
||||
if r.RunID == "" {
|
||||
uuid, err := uuid.GenerateUUID()
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
r.RunID = uuid
|
||||
}
|
||||
|
@ -92,18 +114,9 @@ func Report(ctx context.Context, r *ReportParams) error {
|
|||
|
||||
b, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// file logging while debugging
|
||||
file, err := os.OpenFile("telemetry.log", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
file.Write(b)
|
||||
file.WriteString("\n")
|
||||
|
||||
u := &url.URL{
|
||||
Scheme: "https",
|
||||
Host: "checkpoint-api.hashicorp.com",
|
||||
|
@ -112,21 +125,12 @@ func Report(ctx context.Context, r *ReportParams) error {
|
|||
|
||||
req, err := http.NewRequest("POST", u.String(), bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("User-Agent", "HashiCorp/go-checkpoint")
|
||||
|
||||
client := cleanhttp.DefaultClient()
|
||||
resp, err := client.Do(req.WithContext(ctx))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.StatusCode != 201 {
|
||||
return fmt.Errorf("Unknown status: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
return req, nil
|
||||
}
|
||||
|
||||
// CheckParams are the parameters for configuring a check request.
|
||||
|
@ -221,7 +225,6 @@ func Check(p *CheckParams) (*CheckResponse, error) {
|
|||
p.OS = runtime.GOOS
|
||||
}
|
||||
|
||||
// TODO: race here if we try to write this file twice
|
||||
// If we're given a SignatureFile, then attempt to read that.
|
||||
signature := p.Signature
|
||||
if p.Signature == "" && p.SignatureFile != "" {
|
||||
|
|
|
@ -546,10 +546,10 @@
|
|||
"revision": "7554cd9344cec97297fa6649b055a8c98c2a1e55"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "kBuCrFoNYcM0PcdbrOJQwec3Heg=",
|
||||
"checksumSHA1": "EPwsEGG/9t4sCexmFYnlZpE548A=",
|
||||
"path": "github.com/hashicorp/go-checkpoint",
|
||||
"revision": "194925eac2c1f69fcac1693d3f02f1337c341763",
|
||||
"revisionTime": "2017-06-15T06:56:40Z"
|
||||
"revision": "04fd58160a0619a814172a795aa173fa64be731c",
|
||||
"revisionTime": "2017-06-17T00:44:57Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "fSe5y1UgTDeYlnFfUcDA1zzcw+U=",
|
||||
|
|
Loading…
Reference in New Issue