@@ -15,33 +15,55 @@ const (
1515 vmSizeStr = "VMSize"
1616 osVersionStr = "OSVersion"
1717 locationStr = "Region"
18- appVersionStr = "Appversion "
18+ appNameStr = "AppName "
1919 subscriptionIDStr = "SubscriptionID"
20+ vmNameStr = "VMName"
2021 defaultTimeout = 10
2122)
2223
24+ var debugMode bool
25+
2326func messageListener () appinsights.DiagnosticsMessageListener {
24- return appinsights .NewDiagnosticsMessageListener (func (msg string ) error {
25- log .Printf ("[AppInsights] [%s] %s\n " , time .Now ().Format (time .UnixDate ), msg )
26- return nil
27- })
27+ if debugMode {
28+ return appinsights .NewDiagnosticsMessageListener (func (msg string ) error {
29+ debuglog ("[AppInsights] [%s] %s\n " , time .Now ().Format (time .UnixDate ), msg )
30+ return nil
31+ })
32+ }
33+
34+ return nil
35+ }
36+
37+ func debuglog (format string , args ... interface {}) {
38+ if debugMode {
39+ log .Printf (format , args ... )
40+ }
2841}
2942
3043func getMetadata (th * telemetryHandle ) {
3144 var metadata common.Metadata
3245 var err error
3346
47+ if th .refreshTimeout < 4 {
48+ th .refreshTimeout = defaultTimeout
49+ }
50+
3451 // check if metadata in memory otherwise initiate wireserver request
3552 for {
3653 metadata , err = common .GetHostMetadata (metadataFile )
37- if err == nil || ! th .enableMetadataRefreshThread {
54+ if err == nil || th .disableMetadataRefreshThread {
3855 break
3956 }
4057
41- log . Printf ("[AppInsights] Error getting metadata %v. Sleep for %d" , err , th .refreshTimeout )
58+ debuglog ("[AppInsights] Error getting metadata %v. Sleep for %d" , err , th .refreshTimeout )
4259 time .Sleep (time .Duration (th .refreshTimeout ) * time .Second )
4360 }
4461
62+ if err != nil {
63+ debuglog ("[AppInsights] Error getting metadata %v" , err )
64+ return
65+ }
66+
4567 //acquire write lock before writing metadata to telemetry handle
4668 th .rwmutex .Lock ()
4769 th .metadata = metadata
@@ -50,46 +72,42 @@ func getMetadata(th *telemetryHandle) {
5072 // Save metadata retrieved from wireserver to a file
5173 kvs , err := store .NewJsonFileStore (metadataFile )
5274 if err != nil {
53- log . Printf ("[AppInsights] Error initializing kvs store: %v" , err )
75+ debuglog ("[AppInsights] Error initializing kvs store: %v" , err )
5476 return
5577 }
5678
5779 kvs .Lock (true )
5880 err = common .SaveHostMetadata (th .metadata , metadataFile )
5981 kvs .Unlock (true )
6082 if err != nil {
61- log . Printf ("[AppInsights] saving host metadata failed with :%v" , err )
83+ debuglog ("[AppInsights] saving host metadata failed with :%v" , err )
6284 }
6385}
6486
65- // NewAITelemetry creates telemetry handle with user specified appinsights key .
87+ // NewAITelemetry creates telemetry handle with user specified appinsights id .
6688func NewAITelemetry (
67- key string ,
68- appName string ,
69- appVersion string ,
70- batchSize int ,
71- batchInterval int ,
72- enableMetadataRefreshThread bool ,
73- refreshTimeout int ,
89+ id string ,
90+ aiConfig AIConfig ,
7491) TelemetryHandle {
7592
76- telemetryConfig := appinsights .NewTelemetryConfiguration (key )
77- telemetryConfig .MaxBatchSize = batchSize
78- telemetryConfig .MaxBatchInterval = time .Duration (batchInterval ) * time .Second
93+ telemetryConfig := appinsights .NewTelemetryConfiguration (id )
94+ telemetryConfig .MaxBatchSize = aiConfig .BatchSize
95+ telemetryConfig .MaxBatchInterval = time .Duration (aiConfig .BatchInterval ) * time .Second
96+ debugMode = aiConfig .DebugMode
7997
8098 th := & telemetryHandle {
81- client : appinsights .NewTelemetryClientFromConfig (telemetryConfig ),
82- appName : appName ,
83- appVersion : appVersion ,
84- diagListener : messageListener (),
85- enableMetadataRefreshThread : enableMetadataRefreshThread ,
86- refreshTimeout : refreshTimeout ,
99+ client : appinsights .NewTelemetryClientFromConfig (telemetryConfig ),
100+ appName : aiConfig . AppName ,
101+ appVersion : aiConfig . AppVersion ,
102+ diagListener : messageListener (),
103+ disableMetadataRefreshThread : aiConfig . DisableMetadataRefreshThread ,
104+ refreshTimeout : aiConfig . RefreshTimeout ,
87105 }
88106
89- if th .enableMetadataRefreshThread {
90- go getMetadata (th )
91- } else {
107+ if th .disableMetadataRefreshThread {
92108 getMetadata (th )
109+ } else {
110+ go getMetadata (th )
93111 }
94112
95113 return th
@@ -104,14 +122,14 @@ func (th *telemetryHandle) TrackLog(report Report) {
104122 //Override few of existing columns with metadata
105123 trace .Tags .User ().SetAuthUserId (runtime .GOOS )
106124 trace .Tags .Operation ().SetId (report .Context )
107- trace .Tags .Operation ().SetParentId (th .appName )
125+ trace .Tags .Operation ().SetParentId (th .appVersion )
108126
109127 // copy app specified custom dimension
110128 for key , value := range report .CustomDimensions {
111129 trace .Properties [key ] = value
112130 }
113131
114- trace .Properties [appVersionStr ] = th .appVersion
132+ trace .Properties [appNameStr ] = th .appName
115133
116134 // Acquire read lock to read metadata
117135 th .rwmutex .RLock ()
@@ -148,6 +166,7 @@ func (th *telemetryHandle) TrackMetric(metric Metric) {
148166 if metadata .SubscriptionID != "" {
149167 aimetric .Properties [locationStr ] = th .metadata .Location
150168 aimetric .Properties [subscriptionIDStr ] = th .metadata .SubscriptionID
169+ aimetric .Properties [vmNameStr ] = th .metadata .VMName
151170 }
152171
153172 // copy custom dimensions
0 commit comments