Skip to content

Commit

Permalink
[DEX-100] fix(pkg) Pass custom logger for telemetry (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
clemfromspace authored Jul 20, 2022
1 parent 5634495 commit dc455bf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 25 additions & 1 deletion pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package telemetry

import (
"context"
"log"
"net"
"runtime"

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dc455bf

Please # to comment.