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
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-12-06 13:29:39 +00:00
|
|
|
"github.com/snyk/driftctl/build"
|
2022-05-03 09:15:26 +00:00
|
|
|
"github.com/snyk/driftctl/pkg/config"
|
2021-12-06 13:29:39 +00:00
|
|
|
"github.com/snyk/driftctl/pkg/memstore"
|
|
|
|
"github.com/snyk/driftctl/pkg/version"
|
2021-04-27 16:32:03 +00:00
|
|
|
)
|
|
|
|
|
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-10-08 14:01:03 +00:00
|
|
|
ProviderName string `json:"provider_name"`
|
2022-02-01 11:26:29 +00:00
|
|
|
IaCSourceCount uint `json:"iac_source_count"`
|
2022-05-03 09:15:26 +00:00
|
|
|
Client string `json:"client"`
|
2021-06-28 12:39:14 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 14:07:50 +00:00
|
|
|
type Telemetry struct {
|
|
|
|
build build.BuildInterface
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTelemetry(build build.BuildInterface) *Telemetry {
|
|
|
|
return &Telemetry{build: build}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (te Telemetry) SendTelemetry(store memstore.Bucket) {
|
|
|
|
|
|
|
|
if !te.build.IsUsageReportingEnabled() {
|
|
|
|
logrus.Debug("Usage reporting is disabled on this build, telemetry skipped")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-12 13:34:06 +00:00
|
|
|
t := &telemetry{
|
|
|
|
Version: version.Current(),
|
|
|
|
Os: runtime.GOOS,
|
|
|
|
Arch: runtime.GOARCH,
|
2022-05-03 09:15:26 +00:00
|
|
|
Client: "driftctl",
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.IsSnyk() {
|
|
|
|
t.Client = "snyk-cli"
|
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
|
|
|
|
}
|
|
|
|
|
2021-10-08 14:01:03 +00:00
|
|
|
if val, ok := store.Get("provider_name").(string); ok {
|
|
|
|
t.ProviderName = val
|
|
|
|
}
|
|
|
|
|
2022-02-01 11:26:29 +00:00
|
|
|
if val, ok := store.Get("iac_source_count").(uint); ok {
|
|
|
|
t.IaCSourceCount = val
|
|
|
|
}
|
|
|
|
|
2021-06-28 12:39:14 +00:00
|
|
|
body, err := json.Marshal(t)
|
2021-04-27 16:32:03 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Debug(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{}
|
2021-12-10 14:49:35 +00:00
|
|
|
req, _ := http.NewRequest("POST", "https://telemetry.driftctl.com/telemetry", bytes.NewReader(body))
|
2021-04-27 16:32:03 +00:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
_, err = client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("Unable to send telemetry data: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|