From f533e97b8ef265e877209f1a0df8a8eaf2045cc5 Mon Sep 17 00:00:00 2001 From: Tamilmani Manoharan Date: Mon, 21 Oct 2019 16:06:14 -0700 Subject: [PATCH 01/11] addressed concerns discussed during brownbag session --- aitelemetry/api.go | 14 +++++ aitelemetry/telemetrywrapper.go | 77 ++++++++++++++++++---------- aitelemetry/telemetrywrapper_test.go | 32 +++++++++++- 3 files changed, 94 insertions(+), 29 deletions(-) diff --git a/aitelemetry/api.go b/aitelemetry/api.go index 49bf864b4d..a60d43d889 100644 --- a/aitelemetry/api.go +++ b/aitelemetry/api.go @@ -21,6 +21,18 @@ type Metric struct { CustomDimensions map[string]string } +type AIConfig struct { + AppName string + AppVersion string + BatchSize int + BatchInterval int + EnableMetadataRefreshThread bool + RefreshTimeout int + EnableLogging bool + EnableTrace bool + EnableMetric bool +} + // TelmetryHandle holds appinsight handles and metadata type telemetryHandle struct { telemetryConfig *appinsights.TelemetryConfiguration @@ -31,6 +43,8 @@ type telemetryHandle struct { client appinsights.TelemetryClient enableMetadataRefreshThread bool refreshTimeout int + enableTrace bool + enableMetric bool rwmutex sync.RWMutex } diff --git a/aitelemetry/telemetrywrapper.go b/aitelemetry/telemetrywrapper.go index e8702f2432..3232007a26 100644 --- a/aitelemetry/telemetrywrapper.go +++ b/aitelemetry/telemetrywrapper.go @@ -20,11 +20,23 @@ const ( defaultTimeout = 10 ) -func messageListener() appinsights.DiagnosticsMessageListener { - return appinsights.NewDiagnosticsMessageListener(func(msg string) error { - log.Printf("[AppInsights] [%s] %s\n", time.Now().Format(time.UnixDate), msg) - return nil - }) +var enableLogging bool + +func messageListener(enableLogging bool) appinsights.DiagnosticsMessageListener { + if enableLogging { + return appinsights.NewDiagnosticsMessageListener(func(msg string) error { + debuglog("[AppInsights] [%s] %s\n", time.Now().Format(time.UnixDate), msg) + return nil + }) + } + + return nil +} + +func debuglog(format string, args ...interface{}) { + if enableLogging { + log.Printf(format, args...) + } } func getMetadata(th *telemetryHandle) { @@ -38,7 +50,7 @@ func getMetadata(th *telemetryHandle) { break } - log.Printf("[AppInsights] Error getting metadata %v. Sleep for %d", err, th.refreshTimeout) + debuglog("[AppInsights] Error getting metadata %v. Sleep for %d", err, th.refreshTimeout) time.Sleep(time.Duration(th.refreshTimeout) * time.Second) } @@ -50,7 +62,7 @@ func getMetadata(th *telemetryHandle) { // Save metadata retrieved from wireserver to a file kvs, err := store.NewJsonFileStore(metadataFile) if err != nil { - log.Printf("[AppInsights] Error initializing kvs store: %v", err) + debuglog("[AppInsights] Error initializing kvs store: %v", err) return } @@ -58,38 +70,39 @@ func getMetadata(th *telemetryHandle) { err = common.SaveHostMetadata(th.metadata, metadataFile) kvs.Unlock(true) if err != nil { - log.Printf("[AppInsights] saving host metadata failed with :%v", err) + debuglog("[AppInsights] saving host metadata failed with :%v", err) } } -// NewAITelemetry creates telemetry handle with user specified appinsights key. +// NewAITelemetry creates telemetry handle with user specified appinsights id. func NewAITelemetry( - key string, - appName string, - appVersion string, - batchSize int, - batchInterval int, - enableMetadataRefreshThread bool, - refreshTimeout int, + id string, + aiConfig AIConfig, ) TelemetryHandle { - telemetryConfig := appinsights.NewTelemetryConfiguration(key) - telemetryConfig.MaxBatchSize = batchSize - telemetryConfig.MaxBatchInterval = time.Duration(batchInterval) * time.Second + telemetryConfig := appinsights.NewTelemetryConfiguration(id) + telemetryConfig.MaxBatchSize = aiConfig.BatchSize + telemetryConfig.MaxBatchInterval = time.Duration(aiConfig.BatchInterval) * time.Second th := &telemetryHandle{ client: appinsights.NewTelemetryClientFromConfig(telemetryConfig), - appName: appName, - appVersion: appVersion, - diagListener: messageListener(), - enableMetadataRefreshThread: enableMetadataRefreshThread, - refreshTimeout: refreshTimeout, + appName: aiConfig.AppName, + appVersion: aiConfig.AppVersion, + diagListener: messageListener(aiConfig.EnableLogging), + enableMetadataRefreshThread: aiConfig.EnableMetadataRefreshThread, + refreshTimeout: aiConfig.RefreshTimeout, + enableTrace: aiConfig.EnableTrace, + enableMetric: aiConfig.EnableMetric, } - if th.enableMetadataRefreshThread { - go getMetadata(th) - } else { - getMetadata(th) + enableLogging = aiConfig.EnableLogging + + if !aiConfig.EnableTrace && !aiConfig.EnableMetric { + if th.enableMetadataRefreshThread { + go getMetadata(th) + } else { + getMetadata(th) + } } return th @@ -98,6 +111,10 @@ func NewAITelemetry( // TrackLog function sends report (trace) to appinsights resource. It overrides few of the existing columns with app information // and for rest it uses custom dimesion func (th *telemetryHandle) TrackLog(report Report) { + if !th.enableTrace { + return + } + // Initialize new trace message trace := appinsights.NewTraceTelemetry(report.Message, appinsights.Warning) @@ -136,6 +153,10 @@ func (th *telemetryHandle) TrackLog(report Report) { // TrackMetric function sends metric to appinsights resource. It overrides few of the existing columns with app information // and for rest it uses custom dimesion func (th *telemetryHandle) TrackMetric(metric Metric) { + if !th.enableMetric { + return + } + // Initialize new metric aimetric := appinsights.NewMetricTelemetry(metric.Name, metric.Value) diff --git a/aitelemetry/telemetrywrapper_test.go b/aitelemetry/telemetrywrapper_test.go index 55d65bd037..d08053e8f4 100644 --- a/aitelemetry/telemetrywrapper_test.go +++ b/aitelemetry/telemetrywrapper_test.go @@ -30,7 +30,18 @@ func TestMain(m *testing.M) { } func TestNewAITelemetry(t *testing.T) { - th = NewAITelemetry("00ca2a73-c8d6-4929-a0c2-cf84545ec225", "testapp", "v1.0.26", 4096, 2, false, 10) + aiConfig := AIConfig{ + AppName: "testapp", + AppVersion: "v1.0.26", + BatchSize: 4096, + BatchInterval: 2, + EnableMetadataRefreshThread: false, + RefreshTimeout: 10, + EnableLogging: true, + EnableMetric: true, + EnableTrace: true, + } + th = NewAITelemetry("00ca2a73-c8d6-4929-a0c2-cf84545ec225", aiConfig) if th == nil { t.Errorf("Error intializing AI telemetry") } @@ -61,3 +72,22 @@ func TestTrackLog(t *testing.T) { func TestClose(t *testing.T) { th.Close(10) } + +func TestClosewithoutSend(t *testing.T) { + aiConfig := AIConfig{ + AppName: "testapp", + AppVersion: "v1.0.26", + BatchSize: 4096, + BatchInterval: 2, + EnableMetadataRefreshThread: false, + RefreshTimeout: 10, + EnableLogging: true, + } + + thtest := NewAITelemetry("00ca2a73-c8d6-4929-a0c2-cf84545ec225", aiConfig) + if thtest == nil { + t.Errorf("Error intializing AI telemetry") + } + + thtest.Close(10) +} From 136f2de48c2a8b583b85933894447f837640b03f Mon Sep 17 00:00:00 2001 From: Tamilmani Manoharan Date: Mon, 21 Oct 2019 17:50:26 -0700 Subject: [PATCH 02/11] updated fields in aitelemetry --- aitelemetry/api.go | 10 +++++----- aitelemetry/telemetrywrapper.go | 28 ++++++++++++++-------------- aitelemetry/telemetrywrapper_test.go | 5 +---- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/aitelemetry/api.go b/aitelemetry/api.go index a60d43d889..df48c78919 100644 --- a/aitelemetry/api.go +++ b/aitelemetry/api.go @@ -28,9 +28,9 @@ type AIConfig struct { BatchInterval int EnableMetadataRefreshThread bool RefreshTimeout int - EnableLogging bool - EnableTrace bool - EnableMetric bool + DisableLogging bool + DisableTrace bool + DisableMetric bool } // TelmetryHandle holds appinsight handles and metadata @@ -43,8 +43,8 @@ type telemetryHandle struct { client appinsights.TelemetryClient enableMetadataRefreshThread bool refreshTimeout int - enableTrace bool - enableMetric bool + disableTrace bool + disableMetric bool rwmutex sync.RWMutex } diff --git a/aitelemetry/telemetrywrapper.go b/aitelemetry/telemetrywrapper.go index 3232007a26..fbbaabc2a1 100644 --- a/aitelemetry/telemetrywrapper.go +++ b/aitelemetry/telemetrywrapper.go @@ -15,15 +15,15 @@ const ( vmSizeStr = "VMSize" osVersionStr = "OSVersion" locationStr = "Region" - appVersionStr = "Appversion" + appNameStr = "AppName" subscriptionIDStr = "SubscriptionID" defaultTimeout = 10 ) -var enableLogging bool +var disableLogging bool -func messageListener(enableLogging bool) appinsights.DiagnosticsMessageListener { - if enableLogging { +func messageListener(disableLogging bool) appinsights.DiagnosticsMessageListener { + if !disableLogging { return appinsights.NewDiagnosticsMessageListener(func(msg string) error { debuglog("[AppInsights] [%s] %s\n", time.Now().Format(time.UnixDate), msg) return nil @@ -34,7 +34,7 @@ func messageListener(enableLogging bool) appinsights.DiagnosticsMessageListener } func debuglog(format string, args ...interface{}) { - if enableLogging { + if !disableLogging { log.Printf(format, args...) } } @@ -88,16 +88,16 @@ func NewAITelemetry( client: appinsights.NewTelemetryClientFromConfig(telemetryConfig), appName: aiConfig.AppName, appVersion: aiConfig.AppVersion, - diagListener: messageListener(aiConfig.EnableLogging), + diagListener: messageListener(aiConfig.DisableLogging), enableMetadataRefreshThread: aiConfig.EnableMetadataRefreshThread, refreshTimeout: aiConfig.RefreshTimeout, - enableTrace: aiConfig.EnableTrace, - enableMetric: aiConfig.EnableMetric, + disableTrace: aiConfig.DisableTrace, + disableMetric: aiConfig.DisableMetric, } - enableLogging = aiConfig.EnableLogging + disableLogging = aiConfig.DisableLogging - if !aiConfig.EnableTrace && !aiConfig.EnableMetric { + if !th.disableTrace || !th.disableMetric { if th.enableMetadataRefreshThread { go getMetadata(th) } else { @@ -111,7 +111,7 @@ func NewAITelemetry( // TrackLog function sends report (trace) to appinsights resource. It overrides few of the existing columns with app information // and for rest it uses custom dimesion func (th *telemetryHandle) TrackLog(report Report) { - if !th.enableTrace { + if th.disableTrace { return } @@ -121,14 +121,14 @@ func (th *telemetryHandle) TrackLog(report Report) { //Override few of existing columns with metadata trace.Tags.User().SetAuthUserId(runtime.GOOS) trace.Tags.Operation().SetId(report.Context) - trace.Tags.Operation().SetParentId(th.appName) + trace.Tags.Operation().SetParentId(th.appVersion) // copy app specified custom dimension for key, value := range report.CustomDimensions { trace.Properties[key] = value } - trace.Properties[appVersionStr] = th.appVersion + trace.Properties[appNameStr] = th.appName // Acquire read lock to read metadata th.rwmutex.RLock() @@ -153,7 +153,7 @@ func (th *telemetryHandle) TrackLog(report Report) { // TrackMetric function sends metric to appinsights resource. It overrides few of the existing columns with app information // and for rest it uses custom dimesion func (th *telemetryHandle) TrackMetric(metric Metric) { - if !th.enableMetric { + if th.disableMetric { return } diff --git a/aitelemetry/telemetrywrapper_test.go b/aitelemetry/telemetrywrapper_test.go index d08053e8f4..d2a141058b 100644 --- a/aitelemetry/telemetrywrapper_test.go +++ b/aitelemetry/telemetrywrapper_test.go @@ -37,9 +37,6 @@ func TestNewAITelemetry(t *testing.T) { BatchInterval: 2, EnableMetadataRefreshThread: false, RefreshTimeout: 10, - EnableLogging: true, - EnableMetric: true, - EnableTrace: true, } th = NewAITelemetry("00ca2a73-c8d6-4929-a0c2-cf84545ec225", aiConfig) if th == nil { @@ -81,7 +78,7 @@ func TestClosewithoutSend(t *testing.T) { BatchInterval: 2, EnableMetadataRefreshThread: false, RefreshTimeout: 10, - EnableLogging: true, + DisableLogging: true, } thtest := NewAITelemetry("00ca2a73-c8d6-4929-a0c2-cf84545ec225", aiConfig) From f185717fe352b35d1b4849dd3e72a91b9391471f Mon Sep 17 00:00:00 2001 From: Tamilmani Manoharan Date: Tue, 22 Oct 2019 11:23:21 -0700 Subject: [PATCH 03/11] addressed review comments --- aitelemetry/api.go | 2 +- aitelemetry/telemetrywrapper.go | 13 ++++++------- aitelemetry/telemetrywrapper_test.go | 12 +++++++++--- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/aitelemetry/api.go b/aitelemetry/api.go index df48c78919..d89234e05e 100644 --- a/aitelemetry/api.go +++ b/aitelemetry/api.go @@ -28,7 +28,7 @@ type AIConfig struct { BatchInterval int EnableMetadataRefreshThread bool RefreshTimeout int - DisableLogging bool + DebugMode bool DisableTrace bool DisableMetric bool } diff --git a/aitelemetry/telemetrywrapper.go b/aitelemetry/telemetrywrapper.go index fbbaabc2a1..026dc37d2e 100644 --- a/aitelemetry/telemetrywrapper.go +++ b/aitelemetry/telemetrywrapper.go @@ -20,10 +20,10 @@ const ( defaultTimeout = 10 ) -var disableLogging bool +var debugMode bool -func messageListener(disableLogging bool) appinsights.DiagnosticsMessageListener { - if !disableLogging { +func messageListener() appinsights.DiagnosticsMessageListener { + if debugMode { return appinsights.NewDiagnosticsMessageListener(func(msg string) error { debuglog("[AppInsights] [%s] %s\n", time.Now().Format(time.UnixDate), msg) return nil @@ -34,7 +34,7 @@ func messageListener(disableLogging bool) appinsights.DiagnosticsMessageListener } func debuglog(format string, args ...interface{}) { - if !disableLogging { + if debugMode { log.Printf(format, args...) } } @@ -83,20 +83,19 @@ func NewAITelemetry( telemetryConfig := appinsights.NewTelemetryConfiguration(id) telemetryConfig.MaxBatchSize = aiConfig.BatchSize telemetryConfig.MaxBatchInterval = time.Duration(aiConfig.BatchInterval) * time.Second + debugMode = aiConfig.DebugMode th := &telemetryHandle{ client: appinsights.NewTelemetryClientFromConfig(telemetryConfig), appName: aiConfig.AppName, appVersion: aiConfig.AppVersion, - diagListener: messageListener(aiConfig.DisableLogging), + diagListener: messageListener(), enableMetadataRefreshThread: aiConfig.EnableMetadataRefreshThread, refreshTimeout: aiConfig.RefreshTimeout, disableTrace: aiConfig.DisableTrace, disableMetric: aiConfig.DisableMetric, } - disableLogging = aiConfig.DisableLogging - if !th.disableTrace || !th.disableMetric { if th.enableMetadataRefreshThread { go getMetadata(th) diff --git a/aitelemetry/telemetrywrapper_test.go b/aitelemetry/telemetrywrapper_test.go index d2a141058b..ecc1f98161 100644 --- a/aitelemetry/telemetrywrapper_test.go +++ b/aitelemetry/telemetrywrapper_test.go @@ -1,7 +1,9 @@ package aitelemetry import ( + "fmt" "os" + "path/filepath" "runtime" "testing" @@ -15,7 +17,9 @@ func TestMain(m *testing.M) { if runtime.GOOS == "linux" { platform.ExecuteCommand("cp metadata_test.json /tmp/azuremetadata.json") } else { - platform.ExecuteCommand("copy metadata_test.json azuremetadata.json") + metadataFile := filepath.FromSlash(os.Getenv("TEMP")) + "\\azuremetadata.json" + cmd := fmt.Sprintf("copy metadata_test.json %s", metadataFile) + platform.ExecuteCommand(cmd) } exitCode := m.Run() @@ -23,7 +27,9 @@ func TestMain(m *testing.M) { if runtime.GOOS == "linux" { platform.ExecuteCommand("rm /tmp/azuremetadata.json") } else { - platform.ExecuteCommand("del azuremetadata.json") + metadataFile := filepath.FromSlash(os.Getenv("TEMP")) + "\\azuremetadata.json" + cmd := fmt.Sprintf("del %s", metadataFile) + platform.ExecuteCommand(cmd) } os.Exit(exitCode) @@ -37,6 +43,7 @@ func TestNewAITelemetry(t *testing.T) { BatchInterval: 2, EnableMetadataRefreshThread: false, RefreshTimeout: 10, + DebugMode: true, } th = NewAITelemetry("00ca2a73-c8d6-4929-a0c2-cf84545ec225", aiConfig) if th == nil { @@ -78,7 +85,6 @@ func TestClosewithoutSend(t *testing.T) { BatchInterval: 2, EnableMetadataRefreshThread: false, RefreshTimeout: 10, - DisableLogging: true, } thtest := NewAITelemetry("00ca2a73-c8d6-4929-a0c2-cf84545ec225", aiConfig) From 64b1481977cece6dd341c833afa29f56850b50a2 Mon Sep 17 00:00:00 2001 From: Tamilmani Manoharan Date: Tue, 22 Oct 2019 11:32:50 -0700 Subject: [PATCH 04/11] flipped the metadata thread field --- aitelemetry/api.go | 40 ++++++++++++++++----------------- aitelemetry/telemetrywrapper.go | 22 +++++++++--------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/aitelemetry/api.go b/aitelemetry/api.go index d89234e05e..bc1f255f3e 100644 --- a/aitelemetry/api.go +++ b/aitelemetry/api.go @@ -22,30 +22,30 @@ type Metric struct { } type AIConfig struct { - AppName string - AppVersion string - BatchSize int - BatchInterval int - EnableMetadataRefreshThread bool - RefreshTimeout int - DebugMode bool - DisableTrace bool - DisableMetric bool + AppName string + AppVersion string + BatchSize int + BatchInterval int + DisableMetadataRefreshThread bool + RefreshTimeout int + DebugMode bool + DisableTrace bool + DisableMetric bool } // TelmetryHandle holds appinsight handles and metadata type telemetryHandle struct { - telemetryConfig *appinsights.TelemetryConfiguration - appName string - appVersion string - metadata common.Metadata - diagListener appinsights.DiagnosticsMessageListener - client appinsights.TelemetryClient - enableMetadataRefreshThread bool - refreshTimeout int - disableTrace bool - disableMetric bool - rwmutex sync.RWMutex + telemetryConfig *appinsights.TelemetryConfiguration + appName string + appVersion string + metadata common.Metadata + diagListener appinsights.DiagnosticsMessageListener + client appinsights.TelemetryClient + disableMetadataRefreshThread bool + refreshTimeout int + disableTrace bool + disableMetric bool + rwmutex sync.RWMutex } // Telemetry Interface to send metrics/Logs to appinsights diff --git a/aitelemetry/telemetrywrapper.go b/aitelemetry/telemetrywrapper.go index 026dc37d2e..0ff449a412 100644 --- a/aitelemetry/telemetrywrapper.go +++ b/aitelemetry/telemetrywrapper.go @@ -86,21 +86,21 @@ func NewAITelemetry( debugMode = aiConfig.DebugMode th := &telemetryHandle{ - client: appinsights.NewTelemetryClientFromConfig(telemetryConfig), - appName: aiConfig.AppName, - appVersion: aiConfig.AppVersion, - diagListener: messageListener(), - enableMetadataRefreshThread: aiConfig.EnableMetadataRefreshThread, - refreshTimeout: aiConfig.RefreshTimeout, - disableTrace: aiConfig.DisableTrace, - disableMetric: aiConfig.DisableMetric, + client: appinsights.NewTelemetryClientFromConfig(telemetryConfig), + appName: aiConfig.AppName, + appVersion: aiConfig.AppVersion, + diagListener: messageListener(), + disableMetadataRefreshThread: aiConfig.DisableMetadataRefreshThread, + refreshTimeout: aiConfig.RefreshTimeout, + disableTrace: aiConfig.DisableTrace, + disableMetric: aiConfig.DisableMetric, } if !th.disableTrace || !th.disableMetric { - if th.enableMetadataRefreshThread { - go getMetadata(th) - } else { + if th.disableMetadataRefreshThread { getMetadata(th) + } else { + go getMetadata(th) } } From 95f9e99d366e3ee1747729884f987e6521d751a1 Mon Sep 17 00:00:00 2001 From: Tamilmani Manoharan Date: Tue, 22 Oct 2019 12:07:32 -0700 Subject: [PATCH 05/11] fixed compilation errors --- aitelemetry/telemetrywrapper.go | 2 +- aitelemetry/telemetrywrapper_test.go | 26 +++++++++++++------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/aitelemetry/telemetrywrapper.go b/aitelemetry/telemetrywrapper.go index 0ff449a412..f132bdf59f 100644 --- a/aitelemetry/telemetrywrapper.go +++ b/aitelemetry/telemetrywrapper.go @@ -46,7 +46,7 @@ func getMetadata(th *telemetryHandle) { // check if metadata in memory otherwise initiate wireserver request for { metadata, err = common.GetHostMetadata(metadataFile) - if err == nil || !th.enableMetadataRefreshThread { + if err == nil || th.disableMetadataRefreshThread { break } diff --git a/aitelemetry/telemetrywrapper_test.go b/aitelemetry/telemetrywrapper_test.go index ecc1f98161..0d25706407 100644 --- a/aitelemetry/telemetrywrapper_test.go +++ b/aitelemetry/telemetrywrapper_test.go @@ -37,13 +37,13 @@ func TestMain(m *testing.M) { func TestNewAITelemetry(t *testing.T) { aiConfig := AIConfig{ - AppName: "testapp", - AppVersion: "v1.0.26", - BatchSize: 4096, - BatchInterval: 2, - EnableMetadataRefreshThread: false, - RefreshTimeout: 10, - DebugMode: true, + AppName: "testapp", + AppVersion: "v1.0.26", + BatchSize: 4096, + BatchInterval: 2, + RefreshTimeout: 10, + DebugMode: true, + DisableMetadataRefreshThread: true, } th = NewAITelemetry("00ca2a73-c8d6-4929-a0c2-cf84545ec225", aiConfig) if th == nil { @@ -79,12 +79,12 @@ func TestClose(t *testing.T) { func TestClosewithoutSend(t *testing.T) { aiConfig := AIConfig{ - AppName: "testapp", - AppVersion: "v1.0.26", - BatchSize: 4096, - BatchInterval: 2, - EnableMetadataRefreshThread: false, - RefreshTimeout: 10, + AppName: "testapp", + AppVersion: "v1.0.26", + BatchSize: 4096, + BatchInterval: 2, + DisableMetadataRefreshThread: true, + RefreshTimeout: 10, } thtest := NewAITelemetry("00ca2a73-c8d6-4929-a0c2-cf84545ec225", aiConfig) From c7f88d9d614c82586a95d35488b07b8ede6867e3 Mon Sep 17 00:00:00 2001 From: Tamilmani Manoharan Date: Tue, 22 Oct 2019 12:09:56 -0700 Subject: [PATCH 06/11] added default refresh timeout --- aitelemetry/telemetrywrapper.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/aitelemetry/telemetrywrapper.go b/aitelemetry/telemetrywrapper.go index f132bdf59f..ec5c39ac9b 100644 --- a/aitelemetry/telemetrywrapper.go +++ b/aitelemetry/telemetrywrapper.go @@ -50,6 +50,10 @@ func getMetadata(th *telemetryHandle) { break } + if th.refreshTimeout < 4 { + th.refreshTimeout = defaultTimeout + } + debuglog("[AppInsights] Error getting metadata %v. Sleep for %d", err, th.refreshTimeout) time.Sleep(time.Duration(th.refreshTimeout) * time.Second) } From e61b74a205c460ac9d202bd6da044cd6b2970524 Mon Sep 17 00:00:00 2001 From: Tamilmani Manoharan Date: Tue, 22 Oct 2019 12:11:15 -0700 Subject: [PATCH 07/11] moved out of for loop --- aitelemetry/telemetrywrapper.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aitelemetry/telemetrywrapper.go b/aitelemetry/telemetrywrapper.go index ec5c39ac9b..02e070036f 100644 --- a/aitelemetry/telemetrywrapper.go +++ b/aitelemetry/telemetrywrapper.go @@ -43,6 +43,10 @@ func getMetadata(th *telemetryHandle) { var metadata common.Metadata var err error + if th.refreshTimeout < 4 { + th.refreshTimeout = defaultTimeout + } + // check if metadata in memory otherwise initiate wireserver request for { metadata, err = common.GetHostMetadata(metadataFile) @@ -50,10 +54,6 @@ func getMetadata(th *telemetryHandle) { break } - if th.refreshTimeout < 4 { - th.refreshTimeout = defaultTimeout - } - debuglog("[AppInsights] Error getting metadata %v. Sleep for %d", err, th.refreshTimeout) time.Sleep(time.Duration(th.refreshTimeout) * time.Second) } From ade3f8e8cb8a112d47515c61a5f90f8c465b790e Mon Sep 17 00:00:00 2001 From: Tamilmani Manoharan Date: Tue, 22 Oct 2019 12:20:42 -0700 Subject: [PATCH 08/11] added unittest --- aitelemetry/telemetrywrapper_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/aitelemetry/telemetrywrapper_test.go b/aitelemetry/telemetrywrapper_test.go index 0d25706407..baa16faa31 100644 --- a/aitelemetry/telemetrywrapper_test.go +++ b/aitelemetry/telemetrywrapper_test.go @@ -35,6 +35,23 @@ func TestMain(m *testing.M) { os.Exit(exitCode) } +func TestEmptyAIKey(t *testing.T) { + aiConfig := AIConfig{ + AppName: "testapp", + AppVersion: "v1.0.26", + BatchSize: 4096, + BatchInterval: 2, + RefreshTimeout: 10, + DebugMode: true, + DisableMetadataRefreshThread: true, + } + th := NewAITelemetry("", aiConfig) + if th == nil { + t.Errorf("Error intializing AI telemetry") + } + th.Close(10) +} + func TestNewAITelemetry(t *testing.T) { aiConfig := AIConfig{ AppName: "testapp", From c7158ce27e50472e12de7caa605f0b11e117ad14 Mon Sep 17 00:00:00 2001 From: Tamilmani Manoharan Date: Tue, 22 Oct 2019 12:38:03 -0700 Subject: [PATCH 09/11] added disableall field which disables everything(trace,metric) --- aitelemetry/api.go | 2 ++ aitelemetry/telemetrywrapper.go | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/aitelemetry/api.go b/aitelemetry/api.go index bc1f255f3e..8d9ebbc79f 100644 --- a/aitelemetry/api.go +++ b/aitelemetry/api.go @@ -31,6 +31,7 @@ type AIConfig struct { DebugMode bool DisableTrace bool DisableMetric bool + DisableAll bool } // TelmetryHandle holds appinsight handles and metadata @@ -45,6 +46,7 @@ type telemetryHandle struct { refreshTimeout int disableTrace bool disableMetric bool + disableAll bool rwmutex sync.RWMutex } diff --git a/aitelemetry/telemetrywrapper.go b/aitelemetry/telemetrywrapper.go index 02e070036f..ca823b0a81 100644 --- a/aitelemetry/telemetrywrapper.go +++ b/aitelemetry/telemetrywrapper.go @@ -98,6 +98,7 @@ func NewAITelemetry( refreshTimeout: aiConfig.RefreshTimeout, disableTrace: aiConfig.DisableTrace, disableMetric: aiConfig.DisableMetric, + disableAll: aiConfig.DisableAll, } if !th.disableTrace || !th.disableMetric { @@ -114,7 +115,7 @@ func NewAITelemetry( // TrackLog function sends report (trace) to appinsights resource. It overrides few of the existing columns with app information // and for rest it uses custom dimesion func (th *telemetryHandle) TrackLog(report Report) { - if th.disableTrace { + if th.disableTrace || th.disableAll { return } @@ -156,7 +157,7 @@ func (th *telemetryHandle) TrackLog(report Report) { // TrackMetric function sends metric to appinsights resource. It overrides few of the existing columns with app information // and for rest it uses custom dimesion func (th *telemetryHandle) TrackMetric(metric Metric) { - if th.disableMetric { + if th.disableMetric || th.disableAll { return } From 266b4cae00fa7844076d75091771c80f1a04be7a Mon Sep 17 00:00:00 2001 From: Tamilmani Manoharan Date: Tue, 22 Oct 2019 18:21:29 -0700 Subject: [PATCH 10/11] removed disable logic from ai package --- aitelemetry/api.go | 6 ------ aitelemetry/telemetrywrapper.go | 23 ++++++----------------- 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/aitelemetry/api.go b/aitelemetry/api.go index 8d9ebbc79f..d3c76cf99e 100644 --- a/aitelemetry/api.go +++ b/aitelemetry/api.go @@ -29,9 +29,6 @@ type AIConfig struct { DisableMetadataRefreshThread bool RefreshTimeout int DebugMode bool - DisableTrace bool - DisableMetric bool - DisableAll bool } // TelmetryHandle holds appinsight handles and metadata @@ -44,9 +41,6 @@ type telemetryHandle struct { client appinsights.TelemetryClient disableMetadataRefreshThread bool refreshTimeout int - disableTrace bool - disableMetric bool - disableAll bool rwmutex sync.RWMutex } diff --git a/aitelemetry/telemetrywrapper.go b/aitelemetry/telemetrywrapper.go index ca823b0a81..11657d60b7 100644 --- a/aitelemetry/telemetrywrapper.go +++ b/aitelemetry/telemetrywrapper.go @@ -17,6 +17,7 @@ const ( locationStr = "Region" appNameStr = "AppName" subscriptionIDStr = "SubscriptionID" + vmNameStr = "VMName" defaultTimeout = 10 ) @@ -96,17 +97,12 @@ func NewAITelemetry( diagListener: messageListener(), disableMetadataRefreshThread: aiConfig.DisableMetadataRefreshThread, refreshTimeout: aiConfig.RefreshTimeout, - disableTrace: aiConfig.DisableTrace, - disableMetric: aiConfig.DisableMetric, - disableAll: aiConfig.DisableAll, } - if !th.disableTrace || !th.disableMetric { - if th.disableMetadataRefreshThread { - getMetadata(th) - } else { - go getMetadata(th) - } + if th.disableMetadataRefreshThread { + getMetadata(th) + } else { + go getMetadata(th) } return th @@ -115,10 +111,6 @@ func NewAITelemetry( // TrackLog function sends report (trace) to appinsights resource. It overrides few of the existing columns with app information // and for rest it uses custom dimesion func (th *telemetryHandle) TrackLog(report Report) { - if th.disableTrace || th.disableAll { - return - } - // Initialize new trace message trace := appinsights.NewTraceTelemetry(report.Message, appinsights.Warning) @@ -157,10 +149,6 @@ func (th *telemetryHandle) TrackLog(report Report) { // TrackMetric function sends metric to appinsights resource. It overrides few of the existing columns with app information // and for rest it uses custom dimesion func (th *telemetryHandle) TrackMetric(metric Metric) { - if th.disableMetric || th.disableAll { - return - } - // Initialize new metric aimetric := appinsights.NewMetricTelemetry(metric.Name, metric.Value) @@ -173,6 +161,7 @@ func (th *telemetryHandle) TrackMetric(metric Metric) { if metadata.SubscriptionID != "" { aimetric.Properties[locationStr] = th.metadata.Location aimetric.Properties[subscriptionIDStr] = th.metadata.SubscriptionID + aimetric.Properties[vmNameStr] = th.metadata.VMName } // copy custom dimensions From 5bff193f407d26d7fdba25dd0bb6891e02e8f7f0 Mon Sep 17 00:00:00 2001 From: Tamilmani Manoharan Date: Wed, 23 Oct 2019 14:54:51 -0700 Subject: [PATCH 11/11] added an error check for getting metadata --- aitelemetry/telemetrywrapper.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/aitelemetry/telemetrywrapper.go b/aitelemetry/telemetrywrapper.go index 11657d60b7..b7892aeac2 100644 --- a/aitelemetry/telemetrywrapper.go +++ b/aitelemetry/telemetrywrapper.go @@ -59,6 +59,11 @@ func getMetadata(th *telemetryHandle) { time.Sleep(time.Duration(th.refreshTimeout) * time.Second) } + if err != nil { + debuglog("[AppInsights] Error getting metadata %v", err) + return + } + //acquire write lock before writing metadata to telemetry handle th.rwmutex.Lock() th.metadata = metadata