Skip to content

Commit

Permalink
Refactor scarf impl
Browse files Browse the repository at this point in the history
  • Loading branch information
sourishkrout committed Sep 20, 2024
1 parent 44dda5a commit 040b765
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
41 changes: 29 additions & 12 deletions internal/telemetry/scarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"

Expand All @@ -18,24 +19,31 @@ const (
client = "Kernel"
)

type LookupEnv func(key string) (string, bool)
type reporterFunc func() error
type lookupEnvFunc func(key string) (string, bool)

var reporter reporterFunc

func init() {
reporter = liveReporter
}

// Returns true if telemetry reporting is enabled, false otherwise.
func ReportUnlessNoTracking(logger *zap.Logger) bool {
if v := os.Getenv("DO_NOT_TRACK"); v != "" && v != "0" && v != "false" {
logger.Info("Telemetry reporting is disabled with DO_NOT_TRACK")
return false
}
disablers := []string{"DO_NOT_TRACK", "SCARF_NO_ANALYTICS"}

if v := os.Getenv("SCARF_NO_ANALYTICS"); v != "" && v != "0" && v != "false" {
logger.Info("Telemetry reporting is disabled with SCARF_NO_ANALYTICS")
return false
for _, key := range disablers {
disabled, err := trackingDisabledForEnv(key)
if err == nil && disabled {
logger.Info(fmt.Sprintf("Telemetry reporting is disabled with %s", key))
return false
}
}

logger.Info("Telemetry reporting is enabled")

go func() {
err := report()
err := reporter()
if err != nil {
logger.Warn("Error reporting telemetry", zap.Error(err))
}
Expand All @@ -44,7 +52,16 @@ func ReportUnlessNoTracking(logger *zap.Logger) bool {
return true
}

func report() error {
func trackingDisabledForEnv(key string) (bool, error) {
val, err := strconv.ParseBool(os.Getenv(key))
if err != nil {
return false, err
}

return val, nil
}

func liveReporter() error {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

Expand All @@ -71,7 +88,7 @@ func report() error {
return nil
}

func buildURL(lookup LookupEnv, client string) (*url.URL, error) {
func buildURL(lookup lookupEnvFunc, client string) (*url.URL, error) {
baseAndClient := base + client

props := []string{
Expand Down Expand Up @@ -103,7 +120,7 @@ func buildURL(lookup LookupEnv, client string) (*url.URL, error) {
return dst, nil
}

func addValue(lookup LookupEnv, params *url.Values, prop string) {
func addValue(lookup lookupEnvFunc, params *url.Values, prop string) {
if v, ok := lookup(fmt.Sprintf("TELEMETRY_%s", strings.ToUpper(prop))); ok {
params.Add(strings.ToLower(prop), v)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/telemetry/scarf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
)

func TestReportUnlessNoTracking(t *testing.T) {
// don't send telemetry in tests
reporter = func() error {
return nil
}

t.Run("Track", func(t *testing.T) {
logger := zap.NewNop()
require.True(t, ReportUnlessNoTracking(logger))
Expand Down

0 comments on commit 040b765

Please sign in to comment.