Skip to content

Commit

Permalink
feat: Telemetry logged in analytics.testkube.io debug server, Telemet…
Browse files Browse the repository at this point in the history
…ry instead of analytics (rename) (#1674)

* feat: analytics upadte

* analytics improvement

* feat: analytics are telemetry now

* feat: analytics are telemetry now

* rename

* config route

* feat: config

* CR comments

* CR comments

* private methods

* fix: error are logged

* fix: go vet warnings

* rename files

* fix: ioutil package to io
  • Loading branch information
exu authored Jun 27, 2022
1 parent d4ae7bf commit a2dcdd7
Show file tree
Hide file tree
Showing 97 changed files with 711 additions and 524 deletions.
4 changes: 2 additions & 2 deletions .builds-darwin.goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ builds:
- -X main.commit={{.Commit}}
- -X main.date={{.Date}}
- -X main.builtBy=goreleaser
- -X github.com/kubeshop/testkube/pkg/analytics.TestkubeMeasurementID={{.Env.ANALYTICS_TRACKING_ID}}
- -X github.com/kubeshop/testkube/pkg/analytics.TestkubeMeasurementSecret={{.Env.ANALYTICS_API_KEY}}
- -X github.com/kubeshop/testkube/pkg/telemetry.TestkubeMeasurementID={{.Env.ANALYTICS_TRACKING_ID}}
- -X github.com/kubeshop/testkube/pkg/telemetry.TestkubeMeasurementSecret={{.Env.ANALYTICS_API_KEY}}
archives:
- format: binary
4 changes: 2 additions & 2 deletions .builds-linux.goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ builds:
- -X main.commit={{.Commit}}
- -X main.date={{.Date}}
- -X main.builtBy=goreleaser
- -X github.com/kubeshop/testkube/pkg/analytics.TestkubeMeasurementID={{.Env.ANALYTICS_TRACKING_ID}}
- -X github.com/kubeshop/testkube/pkg/analytics.TestkubeMeasurementSecret={{.Env.ANALYTICS_API_KEY}}
- -X github.com/kubeshop/testkube/pkg/telemetry.TestkubeMeasurementID={{.Env.ANALYTICS_TRACKING_ID}}
- -X github.com/kubeshop/testkube/pkg/telemetry.TestkubeMeasurementSecret={{.Env.ANALYTICS_API_KEY}}
archives:
- format: binary
4 changes: 2 additions & 2 deletions .builds-windows.goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ builds:
- -X main.commit={{.Commit}}
- -X main.date={{.Date}}
- -X main.builtBy=goreleaser
- -X github.com/kubeshop/testkube/pkg/analytics.TestkubeMeasurementID={{.Env.ANALYTICS_TRACKING_ID}}
- -X github.com/kubeshop/testkube/pkg/analytics.TestkubeMeasurementSecret={{.Env.ANALYTICS_API_KEY}}
- -X github.com/kubeshop/testkube/pkg/telemetry.TestkubeMeasurementID={{.Env.ANALYTICS_TRACKING_ID}}
- -X github.com/kubeshop/testkube/pkg/telemetry.TestkubeMeasurementSecret={{.Env.ANALYTICS_API_KEY}}
archives:
- format: binary
17 changes: 10 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ NAMESPACE ?= "testkube"
DATE ?= $(shell date -u --iso-8601=seconds)
COMMIT ?= $(shell git log -1 --pretty=format:"%h")
VERSION ?= 999.0.0-$(shell git log -1 --pretty=format:"%h")
DEBUG ?= $(DEBUG)
ANALYTICS_TRACKING_ID ?= $(ANALYTICS_TRACKING_ID)
ANALYTICS_API_KEY ?= $(ANALYTICS_API_KEY)"
LD_FLAGS += -X github.com/kubeshop/testkube/internal/app/api/v1.SlackBotClientID=$(SLACK_BOT_CLIENT_ID)
LD_FLAGS += -X github.com/kubeshop/testkube/internal/app/api/v1.SlackBotClientSecret=$(SLACK_BOT_CLIENT_SECRET)
LD_FLAGS += -X github.com/kubeshop/testkube/pkg/analytics.TestkubeMeasurementID=$(ANALYTICS_TRACKING_ID)
LD_FLAGS += -X github.com/kubeshop/testkube/pkg/analytics.TestkubeMeasurementSecret=$(ANALYTICS_API_KEY)
LD_FLAGS += -X github.com/kubeshop/testkube/pkg/telemetry.TestkubeMeasurementID=$(ANALYTICS_TRACKING_ID)
LD_FLAGS += -X github.com/kubeshop/testkube/pkg/telemetry.TestkubeMeasurementSecret=$(ANALYTICS_API_KEY)
LD_FLAGS += -X github.com/kubeshop/testkube/internal/pkg/api.Version=$(VERSION)
LD_FLAGS += -X github.com/kubeshop/testkube/internal/pkg/api.Commit=$(COMMIT)

Expand All @@ -23,7 +26,7 @@ use-env-file:
$(call setup_env)

run-api: use-env-file
TESTKUBE_NAMESPACE=$(NAMESPACE) SCRAPPERENABLED=true STORAGE_SSL=true DEBUG=$(DEBUG) APISERVER_PORT=8088 go run -ldflags='$(LD_FLAGS)' cmd/api-server/main.go
TESTKUBE_ANALYTICS_ENABLED=$(TESTKUBE_ANALYTICS_ENABLED) TESTKUBE_NAMESPACE=$(NAMESPACE) SCRAPPERENABLED=true STORAGE_SSL=true DEBUG=$(DEBUG) APISERVER_PORT=8088 go run -ldflags='$(LD_FLAGS)' cmd/api-server/main.go

run-api-race-detector: use-env-file
TESTKUBE_NAMESPACE=$(NAMESPACE) DEBUG=1 APISERVER_PORT=8088 go run -race -ldflags='$(LD_FLAGS)' cmd/api-server/main.go
Expand All @@ -46,10 +49,10 @@ build-testkube-bin:
-X main.commit=$(COMMIT) \
-X main.date=$(DATE) \
-X main.builtBy=$(USER) \
-X github.com/kubeshop/testkube/pkg/analytics.TestkubeMeasurementID=$(ANALYTICS_TRACKING_ID) \
-X github.com/kubeshop/testkube/pkg/analytics.TestkubeMeasurementSecret=$(ANALYTICS_API_KEY) \
-X github.com/kubeshop/testkube/internal/app/api/v1.SlackBotClientID=$(SLACK_BOT_CLIENT_ID) \
-X github.com/kubeshop/testkube/internal/app/api/v1.SlackBotClientSecret=$(SLACK_BOT_CLIENT_SECRET)" \
-X github.com/kubeshop/testkube/pkg/telemetry.TestkubeMeasurementID=$(ANALYTICS_TRACKING_ID) \
-X github.com/kubeshop/testkube/pkg/telemetry.TestkubeMeasurementSecret=$(ANALYTICS_API_KEY) \
-o "$(BIN_DIR)/kubectl-testkube" \
cmd/kubectl-testkube/main.go

Expand All @@ -60,10 +63,10 @@ build-testkube-bin-intel:
-X main.commit=$(COMMIT) \
-X main.date=$(DATE) \
-X main.builtBy=$(USER) \
-X github.com/kubeshop/testkube/pkg/analytics.TestkubeMeasurementID=$(ANALYTICS_TRACKING_ID) \
-X github.com/kubeshop/testkube/pkg/analytics.TestkubeMeasurementSecret=$(ANALYTICS_API_KEY) \
-X github.com/kubeshop/testkube/internal/app/api/v1.SlackBotClientID=$(SLACK_BOT_CLIENT_ID) \
-X github.com/kubeshop/testkube/internal/app/api/v1.SlackBotClientSecret=$(SLACK_BOT_CLIENT_SECRET)" \
-X github.com/kubeshop/testkube/pkg/telemetry.TestkubeMeasurementID=$(ANALYTICS_TRACKING_ID) \
-X github.com/kubeshop/testkube/pkg/telemetry.TestkubeMeasurementSecret=$(ANALYTICS_API_KEY) \
-o "$(BIN_DIR)/kubectl-testkube" \
cmd/kubectl-testkube/main.go

Expand Down
21 changes: 21 additions & 0 deletions api/v1/testkube.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,25 @@ paths:
items:
$ref: "#/components/schemas/Problem"

/config/{key}:
patch:
tags:
- api
summary: "update config key"
description: "Updates config key in cluster storage state"
operationId: updateConfigKey
responses:
200:
description: update successful
502:
description: "problem with updating key in cluster storage"
content:
application/problem+json:
schema:
type: array
items:
$ref: "#/components/schemas/Problem"

components:
schemas:
Variables:
Expand Down Expand Up @@ -2454,6 +2473,8 @@ components:
type: string
clusterId:
type: string
enableTelemetry:
type: boolean

#
# Errors
Expand Down
4 changes: 2 additions & 2 deletions build/api-server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ RUN cd cmd/api-server; \
GOOS=$TARGETOS GOARCH=$TARGETARCH 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/analytics.TestkubeMeasurementID=$ANALYTICS_TRACKING_ID \
-X github.com/kubeshop/testkube/pkg/analytics.TestkubeMeasurementSecret=$ANALYTICS_API_KEY \
-X github.com/kubeshop/testkube/internal/app/api/v1.SlackBotClientID=$SLACK_BOT_CLIENT_ID \
-X github.com/kubeshop/testkube/internal/app/api/v1.SlackBotClientSecret=$SLACK_BOT_CLIENT_SECRET" \
-X github.com/kubeshop/testkube/pkg/telemetry.TestkubeMeasurementID=$ANALYTICS_TRACKING_ID \
-X github.com/kubeshop/testkube/pkg/telemetry.TestkubeMeasurementSecret=$ANALYTICS_API_KEY \
-o /app -mod mod -a .

FROM alpine:3.15
Expand Down
22 changes: 13 additions & 9 deletions cmd/api-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/kubeshop/testkube/internal/pkg/api/repository/result"
"github.com/kubeshop/testkube/internal/pkg/api/repository/storage"
"github.com/kubeshop/testkube/internal/pkg/api/repository/testresult"
"github.com/kubeshop/testkube/pkg/analytics"
"github.com/kubeshop/testkube/pkg/envs"
"github.com/kubeshop/testkube/pkg/migrator"
"github.com/kubeshop/testkube/pkg/secret"
"github.com/kubeshop/testkube/pkg/ui"
Expand Down Expand Up @@ -61,12 +61,6 @@ func runMigrations() (err error) {

func main() {

out, err := analytics.SendServerStartAnonymousInfo()
if err != nil {
ui.Debug("analytics send error", "error", err.Error())
}
ui.Debug(out)

port := os.Getenv("APISERVER_PORT")
namespace := "testkube"
if ns, ok := os.LookupEnv("TESTKUBE_NAMESPACE"); ok {
Expand Down Expand Up @@ -99,6 +93,13 @@ func main() {
testResultsRepository := testresult.NewMongoRespository(db)
configRepository := config.NewMongoRespository(db)

// try to load from mongo based config first
telemetryEnabled, err := configRepository.GetTelemetryEnabled(context.Background())
if err != nil {
// fallback to envs in case of failure (no record yet, or other error)
telemetryEnabled = envs.IsTrue("TESTKUBE_ANALYTICS_ENABLED")
}

clusterId, err := configRepository.GetUniqueClusterId(context.Background())
ui.WarnOnError("Getting uniqe clusterId", err)

Expand All @@ -107,7 +108,7 @@ func main() {
ui.ExitOnError("Running server migrations", err)
}

err = apiv1.NewTestkubeAPI(
api := apiv1.NewTestkubeAPI(
namespace,
resultsRepository,
testResultsRepository,
Expand All @@ -117,7 +118,10 @@ func main() {
secretClient,
webhooksClient,
clusterId,
).Run()
)

api.WithTelemetry(telemetryEnabled)

err = api.Run()
ui.ExitOnError("Running API Server", err)
}
4 changes: 2 additions & 2 deletions cmd/kubectl-testkube/commands/disable.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package commands

import (
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/analytics"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/oauth"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/telemetry"
"github.com/kubeshop/testkube/pkg/ui"
"github.com/spf13/cobra"
)
Expand All @@ -18,7 +18,7 @@ func NewDisableCmd() *cobra.Command {
},
}

cmd.AddCommand(analytics.NewDisableAnalyticsCmd())
cmd.AddCommand(telemetry.NewDisableTelemetryCmd())
cmd.AddCommand(oauth.NewDisableOAuthCmd())

return cmd
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubectl-testkube/commands/enable.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package commands

import (
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/analytics"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/oauth"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/telemetry"
"github.com/kubeshop/testkube/pkg/ui"
"github.com/spf13/cobra"
)
Expand All @@ -18,7 +18,7 @@ func NewEnableCmd() *cobra.Command {
},
}

cmd.AddCommand(analytics.NewEnableAnalyticsCmd())
cmd.AddCommand(telemetry.NewEnableTelemetryCmd())
cmd.AddCommand(oauth.NewEnableOAuthCmd())

return cmd
Expand Down
11 changes: 11 additions & 0 deletions cmd/kubectl-testkube/commands/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,19 @@ func NewInstallCmd() *cobra.Command {
Use: "install",
Short: "Install Helm chart registry in current kubectl context and update dependencies",
Run: func(cmd *cobra.Command, args []string) {
ui.Info("WELCOME TO")
ui.Logo()

err := HelmUpgradeOrInstalTestkube(options)
ui.ExitOnError("Installing testkube", err)

ui.Info(`To help improve the quality of Testkube, we collect anonymous basic telemetry data. Head out to https://kubeshop.github.io/testkube/telemetry/ to read our policy or feel free to:`)

ui.NL()
ui.ShellCommand("disable telemetry by typing", "testkube disable telemetry")
ui.Info(" Happy Testing! 🚀")
ui.NL()

},
}

Expand Down
18 changes: 9 additions & 9 deletions cmd/kubectl-testkube/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"

"github.com/kubeshop/testkube/cmd/kubectl-testkube/config"
"github.com/kubeshop/testkube/pkg/analytics"
"github.com/kubeshop/testkube/pkg/telemetry"
"github.com/kubeshop/testkube/pkg/ui"
)

Expand All @@ -17,7 +17,7 @@ var (
BuiltBy string
Date string

analyticsEnabled bool
telemetryEnabled bool
client string
verbose bool
namespace string
Expand Down Expand Up @@ -60,13 +60,13 @@ var RootCmd = &cobra.Command{
PersistentPostRun: func(cmd *cobra.Command, args []string) {
ui.SetVerbose(verbose)

if analyticsEnabled {
ui.Debug("collecting anonymous analytics data, you can disable it by calling `kubectl testkube disable analytics`")
out, err := analytics.SendAnonymousCmdInfo(cmd, Version)
if telemetryEnabled {
ui.Debug("collecting anonymous telemetry data, you can disable it by calling `kubectl testkube disable telemetry`")
out, err := telemetry.SendCmdEvent(cmd, Version)
if ui.Verbose && err != nil {
ui.Err(err)
}
ui.Debug("analytics send event response", out)
ui.Debug("telemetry send event response", out)

// trigger init event only for first run
cfg, err := config.Load()
Expand All @@ -79,11 +79,11 @@ var RootCmd = &cobra.Command{

ui.Debug("sending 'init' event")

out, err := analytics.SendCmdInit(cmd, Version)
out, err := telemetry.SendCmdInitEvent(cmd, Version)
if ui.Verbose && err != nil {
ui.Err(err)
}
ui.Debug("analytics init event response", out)
ui.Debug("telemetry init event response", out)
}

}
Expand Down Expand Up @@ -111,7 +111,7 @@ func Execute() {
apiURI = os.Getenv("TESTKUBE_API_URI")
}

RootCmd.PersistentFlags().BoolVarP(&analyticsEnabled, "analytics-enabled", "", cfg.AnalyticsEnabled, "enable analytics")
RootCmd.PersistentFlags().BoolVarP(&telemetryEnabled, "telemetry-enabled", "", cfg.TelemetryEnabled, "enable collection of anonumous telemetry data")
RootCmd.PersistentFlags().StringVarP(&client, "client", "c", "proxy", "client used for connecting to Testkube API one of proxy|direct")
RootCmd.PersistentFlags().StringVarP(&namespace, "namespace", "", defaultNamespace, "Kubernetes namespace, default value read from config if set")
RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "", false, "show additional debug messages")
Expand Down
23 changes: 18 additions & 5 deletions cmd/kubectl-testkube/commands/status.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
package commands

import (
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/analytics"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/oauth"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/commands/telemetry"
"github.com/kubeshop/testkube/cmd/kubectl-testkube/config"
"github.com/kubeshop/testkube/pkg/ui"
"github.com/spf13/cobra"
)

func NewStatusCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "status <feature|resource>",
Use: "status [feature|resource]",
Short: "Show status of feature or resource",
Run: func(cmd *cobra.Command, args []string) {
err := cmd.Help()
ui.PrintOnError("Displaying help", err)
cfg, err := config.Load()
ui.ExitOnError("loading config file", err)

if cfg.TelemetryEnabled {
ui.Success("Telemetry", "enabled")
} else {
ui.Success("Telemetry", "disabled")
}

if cfg.OAuth2Data.Enabled {
ui.Success("OAuth", "enabled")
} else {
ui.Success("OAuth", "disabled")
}
},
}

cmd.AddCommand(analytics.NewStatusAnalyticsCmd())
cmd.AddCommand(telemetry.NewStatusTelemetryCmd())
cmd.AddCommand(oauth.NewStatusOAuthCmd())

return cmd
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package analytics
package telemetry

import (
"github.com/kubeshop/testkube/cmd/kubectl-testkube/config"
"github.com/kubeshop/testkube/pkg/ui"
"github.com/spf13/cobra"
)

func NewDisableAnalyticsCmd() *cobra.Command {
func NewDisableTelemetryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "analytics",
Short: "disable collecting of anonymous analytics",
Use: "telemetry",
Short: "disable collecting of anonymous telemetry data",
Run: func(cmd *cobra.Command, args []string) {
cfg, err := config.Load()
ui.ExitOnError("loading config file", err)
Expand All @@ -19,7 +19,7 @@ func NewDisableAnalyticsCmd() *cobra.Command {
err = config.Save(cfg)
ui.ExitOnError("saving config file", err)

ui.Success("Analytics", "disabled")
ui.Success("Telemetry", "disabled")
},
}

Expand Down
Loading

0 comments on commit a2dcdd7

Please sign in to comment.