@@ -29,7 +29,6 @@ func (cm *ConfigManager) SetLogger(logger *zap.Logger) {
2929
3030// LoadConfig loads the CNS configuration from file
3131func (cm * ConfigManager ) LoadConfig () (* configuration.CNSConfig , error ) {
32- // Use zap logger if available, otherwise create a default config
3332if cm .logger != nil {
3433cm .logger .Debug ("Loading CNS configuration" , zap .String ("path" , cm .configPath ))
3534}
@@ -40,22 +39,7 @@ func (cm *ConfigManager) LoadConfig() (*configuration.CNSConfig, error) {
4039cm .logger .Info ("CNS config file not found, using default configuration" ,
4140zap .String ("path" , cm .configPath ))
4241}
43-
44- // Return default configuration
45- return & configuration.CNSConfig {
46- TelemetrySettings : configuration.TelemetrySettings {
47- DisableAll : false ,
48- TelemetryBatchSizeBytes : 16384 ,
49- TelemetryBatchIntervalInSecs : 15 ,
50- RefreshIntervalInSecs : 15 ,
51- DisableMetadataRefreshThread : false ,
52- DebugMode : false ,
53- DisableTrace : false ,
54- DisableMetric : false ,
55- DisableEvent : false ,
56- AppInsightsInstrumentationKey : "" , // Will be set by environment or config
57- },
58- }, nil
42+ return cm .createDefaultConfig (), nil
5943}
6044
6145// Read the config file
@@ -80,22 +64,96 @@ func (cm *ConfigManager) LoadConfig() (*configuration.CNSConfig, error) {
8064return nil , fmt .Errorf ("failed to parse config file: %w" , err )
8165}
8266
67+ // Apply defaults and environment variable overrides
68+ cm .setConfigDefaults (& config )
69+
70+ if cm .logger != nil {
71+ cm .logger .Info ("Successfully loaded CNS configuration" ,
72+ zap .String ("path" , cm .configPath ),
73+ zap .Bool ("telemetryDisabled" , config .TelemetrySettings .DisableAll ),
74+ zap .Bool ("cniTelemetryEnabled" , config .TelemetrySettings .EnableCNITelemetry ),
75+ zap .String ("socketPath" , config .TelemetrySettings .CNITelemetrySocketPath ),
76+ zap .Bool ("hasAppInsightsKey" , config .TelemetrySettings .AppInsightsInstrumentationKey != "" ))
77+ }
78+
79+ return & config , nil
80+ }
81+
82+ // createDefaultConfig creates a default configuration
83+ func (cm * ConfigManager ) createDefaultConfig () * configuration.CNSConfig {
84+ config := & configuration.CNSConfig {
85+ TelemetrySettings : configuration.TelemetrySettings {
86+ DisableAll : false ,
87+ TelemetryBatchSizeBytes : defaultBatchSizeInBytes ,
88+ TelemetryBatchIntervalInSecs : defaultBatchIntervalInSecs ,
89+ RefreshIntervalInSecs : defaultRefreshTimeoutInSecs ,
90+ DisableMetadataRefreshThread : false ,
91+ DebugMode : false ,
92+ DisableTrace : false ,
93+ DisableMetric : false ,
94+ DisableEvent : false ,
95+ EnableCNITelemetry : false , // Default to false
96+ CNITelemetrySocketPath : "/var/run/azure-vnet-telemetry.sock" ,
97+ },
98+ }
99+
100+ // Set AppInsights key from environment variables
101+ cm .setAppInsightsKey (& config .TelemetrySettings )
102+
103+ return config
104+ }
105+
106+ // setConfigDefaults applies default values and environment variable overrides
107+ func (cm * ConfigManager ) setConfigDefaults (config * configuration.CNSConfig ) {
83108// Set default values for telemetry settings if not specified
84109if config .TelemetrySettings .TelemetryBatchSizeBytes == 0 {
85- config .TelemetrySettings .TelemetryBatchSizeBytes = 16384
110+ config .TelemetrySettings .TelemetryBatchSizeBytes = defaultBatchSizeInBytes
86111}
87112if config .TelemetrySettings .TelemetryBatchIntervalInSecs == 0 {
88- config .TelemetrySettings .TelemetryBatchIntervalInSecs = 15
113+ config .TelemetrySettings .TelemetryBatchIntervalInSecs = defaultBatchIntervalInSecs
89114}
90115if config .TelemetrySettings .RefreshIntervalInSecs == 0 {
91- config .TelemetrySettings .RefreshIntervalInSecs = 15
116+ config .TelemetrySettings .RefreshIntervalInSecs = defaultRefreshTimeoutInSecs
92117}
93118
94- if cm .logger != nil {
95- cm .logger .Info ("Successfully loaded CNS configuration" ,
96- zap .String ("path" , cm .configPath ),
97- zap .Bool ("telemetryDisabled" , config .TelemetrySettings .DisableAll ))
119+ // Set default CNI telemetry socket path
120+ if config .TelemetrySettings .CNITelemetrySocketPath == "" {
121+ config .TelemetrySettings .CNITelemetrySocketPath = "/var/run/azure-vnet-telemetry.sock"
98122}
99123
100- return & config , nil
124+ // Handle AppInsights instrumentation key from environment variables
125+ cm .setAppInsightsKey (& config .TelemetrySettings )
126+ }
127+
128+ // setAppInsightsKey sets the AppInsights instrumentation key from environment variables
129+ func (cm * ConfigManager ) setAppInsightsKey (ts * configuration.TelemetrySettings ) {
130+ // Try multiple environment variable names
131+ envKeys := []string {
132+ "APPINSIGHTS_INSTRUMENTATIONKEY" ,
133+ "APPLICATIONINSIGHTS_CONNECTION_STRING" ,
134+ "AI_INSTRUMENTATION_KEY" ,
135+ }
136+
137+ // If no key is set in config, try environment variables
138+ if ts .AppInsightsInstrumentationKey == "" {
139+ for _ , envKey := range envKeys {
140+ if key := os .Getenv (envKey ); key != "" {
141+ ts .AppInsightsInstrumentationKey = key
142+ if cm .logger != nil {
143+ cm .logger .Info ("Using AppInsights key from environment variable" ,
144+ zap .String ("envVar" , envKey ))
145+ }
146+ break
147+ }
148+ }
149+ }
150+
151+ // Log the status
152+ if cm .logger != nil {
153+ if ts .AppInsightsInstrumentationKey != "" {
154+ cm .logger .Info ("AppInsights instrumentation key configured for CNI telemetry" )
155+ } else {
156+ cm .logger .Warn ("No AppInsights instrumentation key found - telemetry will work locally but not send to Azure" )
157+ }
158+ }
101159}
0 commit comments