From dc455bf71c26ae7de142968f358cb2b2e9a85035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Denoix?= Date: Wed, 20 Jul 2022 13:48:01 +0200 Subject: [PATCH] [DEX-100] fix(pkg) Pass custom logger for telemetry (#14) --- pkg/cmd/root/root.go | 2 +- pkg/telemetry/telemetry.go | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/root/root.go b/pkg/cmd/root/root.go index 8b73601f..e36ef69d 100644 --- a/pkg/cmd/root/root.go +++ b/pkg/cmd/root/root.go @@ -206,7 +206,7 @@ func createContext(cmd *cobra.Command, stderr io.Writer, hasDebug bool, hasTelem var telemetryClient telemetry.TelemetryClient var err error if hasTelemetry { - telemetryClient, err = telemetry.NewAnalyticsTelemetryClient() + telemetryClient, err = telemetry.NewAnalyticsTelemetryClient(hasDebug) // Fail silently if telemetry is not available unless in debug mode. if err != nil && hasDebug { fmt.Fprintf(stderr, "Error creating telemetry client: %s\n", err) diff --git a/pkg/telemetry/telemetry.go b/pkg/telemetry/telemetry.go index 5db917c6..ca64f189 100644 --- a/pkg/telemetry/telemetry.go +++ b/pkg/telemetry/telemetry.go @@ -2,6 +2,7 @@ package telemetry import ( "context" + "log" "net" "runtime" @@ -29,9 +30,32 @@ type AnalyticsTelemetryClient struct { client analytics.Client } -func NewAnalyticsTelemetryClient() (TelemetryClient, error) { +type AnalyticsTelemetryLogger struct { + debug bool + logger *log.Logger +} + +func (l AnalyticsTelemetryLogger) Logf(format string, args ...interface{}) { + if l.debug { + l.logger.Printf("INFO: "+format, args...) + } +} + +func (l AnalyticsTelemetryLogger) Errorf(format string, args ...interface{}) { + // The telemetry should always fail silently, unless in debug mode + if l.debug { + l.logger.Printf("ERROR: "+format, args...) + } +} + +func newTelemetryLogger(debug bool) AnalyticsTelemetryLogger { + return AnalyticsTelemetryLogger{debug, log.New(nil, "telemetry ", log.LstdFlags)} +} + +func NewAnalyticsTelemetryClient(debug bool) (TelemetryClient, error) { client, err := analytics.NewWithConfig("", analytics.Config{ Endpoint: telemetryAnalyticsURL, + Logger: newTelemetryLogger(debug), }) if err != nil { return nil, err