2021-04-27 16:32:03 +00:00
|
|
|
package telemetry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-06-28 12:39:14 +00:00
|
|
|
"encoding/json"
|
2021-04-27 16:32:03 +00:00
|
|
|
"net/http"
|
2021-07-12 13:34:06 +00:00
|
|
|
"runtime"
|
2021-04-27 16:32:03 +00:00
|
|
|
|
2021-06-25 15:14:50 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/memstore"
|
2021-07-12 13:34:06 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/version"
|
2021-04-27 16:32:03 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2021-06-28 12:39:14 +00:00
|
|
|
type telemetry struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
Os string `json:"os"`
|
|
|
|
Arch string `json:"arch"`
|
|
|
|
TotalResources int `json:"total_resources"`
|
|
|
|
TotalManaged int `json:"total_managed"`
|
|
|
|
Duration uint `json:"duration"`
|
|
|
|
}
|
|
|
|
|
2021-07-12 13:34:06 +00:00
|
|
|
func SendTelemetry(store memstore.Bucket) {
|
|
|
|
t := &telemetry{
|
|
|
|
Version: version.Current(),
|
|
|
|
Os: runtime.GOOS,
|
|
|
|
Arch: runtime.GOARCH,
|
2021-06-28 12:39:14 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 13:34:06 +00:00
|
|
|
if val, ok := store.Get("total_resources").(int); ok {
|
2021-06-28 12:39:14 +00:00
|
|
|
t.TotalResources = val
|
|
|
|
}
|
|
|
|
|
2021-07-12 13:34:06 +00:00
|
|
|
if val, ok := store.Get("total_managed").(int); ok {
|
2021-06-28 12:39:14 +00:00
|
|
|
t.TotalManaged = val
|
|
|
|
}
|
|
|
|
|
2021-07-12 13:34:06 +00:00
|
|
|
if val, ok := store.Get("duration").(uint); ok {
|
2021-06-28 12:39:14 +00:00
|
|
|
t.Duration = val
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := json.Marshal(t)
|
2021-04-27 16:32:03 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Debug(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
req, _ := http.NewRequest("POST", "https://2lvzgmrf2e.execute-api.eu-west-3.amazonaws.com/telemetry", bytes.NewReader(body))
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
_, err = client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("Unable to send telemetry data: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|