Skip to content

Commit

Permalink
added telemetry data collection for CLI commands (#553)
Browse files Browse the repository at this point in the history
* added telemetry data collection for CLI commands

Signed-off-by: Jasmin Gacic <jasmin.gacic@gmail.com>

* dockerfile fix

Co-authored-by: Taras <klum_tz@ukr.net>
  • Loading branch information
jasmingacic and fog1985 authored Nov 19, 2021
1 parent 8bbc41b commit 3c52a6f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ build-api-server:
go build -o $(BIN_DIR)/api-server -ldflags='$(LD_FLAGS)' cmd/api-server/main.go

build-testkube-bin:
go build -ldflags="-s -w -X main.version=0.0.0-$(COMMIT) -X main.commit=$(COMMIT) -X main.date=$(DATE) -X main.builtBy=$(USER)" -o "$(BIN_DIR)/kubectl-testkube" cmd/kubectl-testkube/main.go
go build -ldflags="-s -w -X main.version=0.0.0-$(COMMIT) -X main.commit=$(COMMIT) -X main.date=$(DATE) -X main.builtBy=$(USER) -X github.com/kubeshop/testkube/pkg/telemetry.telemetryToken=$(TELEMETRY_TOKEN)" -o "$(BIN_DIR)/kubectl-testkube" cmd/kubectl-testkube/main.go

docker-build-api:
docker build -t api-server -f build/api-server/Dockerfile .
Expand Down
2 changes: 1 addition & 1 deletion build/api-server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ENV GOOS=linux
ARG VERSION
ARG COMMIT

RUN cd cmd/api-server;go build -ldflags "-X github.com/kubeshop/testkube/internal/pkg/api.Version=$VERSION -X github.com/kubeshop/testkube/internal/pkg/api.Commit=$COMMIT" -X github.com/kubeshop/testkube/pkg/telemetry.telemetryToken=$({ secrets.TELEMETRY_TOKEN }) -o /app -mod mod -a .
RUN cd cmd/api-server;go build -ldflags "-X github.com/kubeshop/testkube/internal/pkg/api.Version=$VERSION -X github.com/kubeshop/testkube/internal/pkg/api.Commit=$COMMIT" -X github.com/kubeshop/testkube/pkg/telemetry.telemetryToken=${{ secrets.TELEMETRY_TOKEN }} -o /app -mod mod -a .

FROM alpine:latest
RUN apk --no-cache add ca-certificates libssl1.1
Expand Down
4 changes: 3 additions & 1 deletion cmd/kubectl-testkube/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package commands
import (
"fmt"
"os"
"strings"

"github.com/Masterminds/semver"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/scripts"
apiclient "github.com/kubeshop/testkube/pkg/api/v1/client"
"github.com/kubeshop/testkube/pkg/telemetry"
"github.com/kubeshop/testkube/pkg/ui"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -41,7 +43,7 @@ var RootCmd = &cobra.Command{

PersistentPreRun: func(cmd *cobra.Command, args []string) {
ui.Verbose = verbose

telemetry.CollectAnonymousCmdInfo(strings.Join(args, " "))
// version validation
// if client version is less than server version show warning
client, _ := scripts.GetClient(cmd)
Expand Down
4 changes: 3 additions & 1 deletion cmd/kubectl-testkube/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import "github.com/kubeshop/testkube/cmd/kubectl-testkube/commands"
import (
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands"
)

var (
commit string
Expand Down
21 changes: 21 additions & 0 deletions pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/md5"
"encoding/hex"
"os"
"path/filepath"
"strconv"
"time"

Expand Down Expand Up @@ -35,6 +36,26 @@ func CollectAnonymousInfo() {
}
}

func CollectAnonymousCmdInfo(command string) {

var isDisabled bool

if val, ok := os.LookupEnv("TESTKUBE_TELEMETRY_DISABLED"); ok {
isDisabled, _ = strconv.ParseBool(val)
}

if !isDisabled {
client := analytics.New(telemetryToken)
client.Enqueue(analytics.Track{
AnonymousId: machineID(),
Event: filepath.Join(heartbeatEvent, command),
Timestamp: time.Now(),
})

client.Close()
}
}

func machineID() string {
id, _ := generate()
return id
Expand Down

0 comments on commit 3c52a6f

Please sign in to comment.