Skip to content

Commit 1f55116

Browse files
committed
Add client to telemetry
1 parent 43b9475 commit 1f55116

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/snyk/driftctl/pkg/config"
1818
"github.com/snyk/driftctl/pkg/version"
1919
"github.com/snyk/driftctl/sentry"
20-
"github.com/spf13/viper"
2120
)
2221

2322
func init() {
@@ -35,7 +34,7 @@ func run() int {
3534
logger.Init()
3635
build := build.Build{}
3736
// Check whether driftCTL is run under Snyk CLI
38-
isSnyk := viper.GetBool("IS_SNYK")
37+
isSnyk := config.IsSnyk()
3938
logrus.WithFields(logrus.Fields{
4039
"isRelease": fmt.Sprintf("%t", build.IsRelease()),
4140
"isUsageReportingEnabled": fmt.Sprintf("%t", build.IsUsageReportingEnabled()),

pkg/config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ func Init() {
77
viper.AutomaticEnv()
88
viper.SetEnvPrefix("dctl")
99
}
10+
11+
func IsSnyk() bool {
12+
return viper.GetBool("IS_SNYK")
13+
}

pkg/telemetry/telemetry.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/sirupsen/logrus"
1010
"github.com/snyk/driftctl/build"
11+
"github.com/snyk/driftctl/pkg/config"
1112
"github.com/snyk/driftctl/pkg/memstore"
1213
"github.com/snyk/driftctl/pkg/version"
1314
)
@@ -21,6 +22,7 @@ type telemetry struct {
2122
Duration uint `json:"duration"`
2223
ProviderName string `json:"provider_name"`
2324
IaCSourceCount uint `json:"iac_source_count"`
25+
Client string `json:"client"`
2426
}
2527

2628
type Telemetry struct {
@@ -42,6 +44,11 @@ func (te Telemetry) SendTelemetry(store memstore.Bucket) {
4244
Version: version.Current(),
4345
Os: runtime.GOOS,
4446
Arch: runtime.GOARCH,
47+
Client: "driftctl",
48+
}
49+
50+
if config.IsSnyk() {
51+
t.Client = "snyk-cli"
4552
}
4653

4754
if val, ok := store.Get("total_resources").(int); ok {

pkg/telemetry/telemetry_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/snyk/driftctl/pkg/resource"
1414
"github.com/snyk/driftctl/pkg/version"
1515
"github.com/snyk/driftctl/test/mocks"
16+
"github.com/spf13/viper"
1617
"github.com/stretchr/testify/assert"
1718
)
1819

@@ -45,6 +46,7 @@ func TestSendTelemetry(t *testing.T) {
4546
Duration: 123,
4647
ProviderName: "aws",
4748
IaCSourceCount: 2,
49+
Client: "driftctl",
4850
},
4951
setStoreValues: func(s memstore.Bucket, a *analyser.Analysis) {
5052
s.Set("total_resources", a.Summary().TotalResources)
@@ -67,6 +69,7 @@ func TestSendTelemetry(t *testing.T) {
6769
Arch: runtime.GOARCH,
6870
Duration: 124,
6971
ProviderName: "aws",
72+
Client: "driftctl",
7073
},
7174
setStoreValues: func(s memstore.Bucket, a *analyser.Analysis) {
7275
s.Set("total_resources", a.Summary().TotalResources)
@@ -90,6 +93,7 @@ func TestSendTelemetry(t *testing.T) {
9093
Version: version.Current(),
9194
Os: runtime.GOOS,
9295
Arch: runtime.GOARCH,
96+
Client: "driftctl",
9397
},
9498
setStoreValues: func(s memstore.Bucket, a *analyser.Analysis) {},
9599
},
@@ -150,3 +154,34 @@ func TestTelemetryNotSend(t *testing.T) {
150154

151155
assert.Zero(t, httpmock.GetTotalCallCount())
152156
}
157+
158+
func TestTelemetrySetProperClient(t *testing.T) {
159+
httpmock.Activate()
160+
defer httpmock.DeactivateAndReset()
161+
httpmock.RegisterResponder(
162+
"POST",
163+
"https://telemetry.driftctl.com/telemetry",
164+
func(req *http.Request) (*http.Response, error) {
165+
requestTelemetry := &telemetry{}
166+
requestBody, err := ioutil.ReadAll(req.Body)
167+
if err != nil {
168+
t.Fatal(err)
169+
}
170+
171+
err = json.Unmarshal(requestBody, requestTelemetry)
172+
if err != nil {
173+
t.Fatal(err)
174+
}
175+
176+
assert.Equal(t, "snyk-cli", requestTelemetry.Client)
177+
178+
return httpmock.NewBytesResponse(202, []byte{}), nil
179+
},
180+
)
181+
182+
viper.Set("IS_SNYK", true)
183+
store := memstore.New().Bucket(memstore.TelemetryBucket)
184+
tl := NewTelemetry(mocks.MockBuild{UsageReporting: true})
185+
tl.SendTelemetry(store)
186+
assert.Equal(t, 1, httpmock.GetTotalCallCount())
187+
}

0 commit comments

Comments
 (0)