66 "os"
77
88 "github.com/Azure/azure-container-networking/cns/configuration"
9+ "github.com/Azure/azure-container-networking/telemetry"
910 "go.uber.org/zap"
1011)
1112
@@ -29,7 +30,6 @@ func (cm *ConfigManager) SetLogger(logger *zap.Logger) {
2930
3031// LoadConfig loads the CNS configuration from file
3132func (cm * ConfigManager ) LoadConfig () (* configuration.CNSConfig , error ) {
32- // Use zap logger if available, otherwise create a default config
3333 if cm .logger != nil {
3434 cm .logger .Debug ("Loading CNS configuration" , zap .String ("path" , cm .configPath ))
3535 }
@@ -40,22 +40,7 @@ func (cm *ConfigManager) LoadConfig() (*configuration.CNSConfig, error) {
4040 cm .logger .Info ("CNS config file not found, using default configuration" ,
4141 zap .String ("path" , cm .configPath ))
4242 }
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
43+ return cm .createDefaultConfig (), nil
5944 }
6045
6146 // Read the config file
@@ -80,22 +65,119 @@ func (cm *ConfigManager) LoadConfig() (*configuration.CNSConfig, error) {
8065 return nil , fmt .Errorf ("failed to parse config file: %w" , err )
8166 }
8267
68+ // Apply defaults and environment variable overrides
69+ cm .setConfigDefaults (& config )
70+
71+ // Check for AppInsights key from all sources (build-time, config, env)
72+ hasAppInsightsKey := cm .hasEffectiveAppInsightsKey (& config .TelemetrySettings )
73+
74+ if cm .logger != nil {
75+ cm .logger .Info ("Successfully loaded CNS configuration" ,
76+ zap .String ("path" , cm .configPath ),
77+ zap .Bool ("telemetryDisabled" , config .TelemetrySettings .DisableAll ),
78+ zap .Bool ("cniTelemetryEnabled" , config .TelemetrySettings .EnableCNITelemetry ),
79+ zap .String ("socketPath" , config .TelemetrySettings .CNITelemetrySocketPath ),
80+ zap .Bool ("hasAppInsightsKey" , hasAppInsightsKey ))
81+ }
82+
83+ return & config , nil
84+ }
85+
86+ // createDefaultConfig creates a default configuration
87+ func (cm * ConfigManager ) createDefaultConfig () * configuration.CNSConfig {
88+ config := & configuration.CNSConfig {
89+ TelemetrySettings : configuration.TelemetrySettings {
90+ DisableAll : false ,
91+ TelemetryBatchSizeBytes : defaultBatchSizeInBytes ,
92+ TelemetryBatchIntervalInSecs : defaultBatchIntervalInSecs ,
93+ RefreshIntervalInSecs : defaultRefreshTimeoutInSecs ,
94+ DisableMetadataRefreshThread : false ,
95+ DebugMode : false ,
96+ DisableTrace : false ,
97+ DisableMetric : false ,
98+ DisableEvent : false ,
99+ EnableCNITelemetry : false , // Default to false
100+ CNITelemetrySocketPath : "/var/run/azure-vnet-telemetry.sock" ,
101+ },
102+ }
103+
104+ // Set AppInsights key from environment variables (if any)
105+ cm .setAppInsightsKeyFromEnv (& config .TelemetrySettings )
106+
107+ return config
108+ }
109+
110+ // setConfigDefaults applies default values and environment variable overrides
111+ func (cm * ConfigManager ) setConfigDefaults (config * configuration.CNSConfig ) {
83112 // Set default values for telemetry settings if not specified
84113 if config .TelemetrySettings .TelemetryBatchSizeBytes == 0 {
85- config .TelemetrySettings .TelemetryBatchSizeBytes = 16384
114+ config .TelemetrySettings .TelemetryBatchSizeBytes = defaultBatchSizeInBytes
86115 }
87116 if config .TelemetrySettings .TelemetryBatchIntervalInSecs == 0 {
88- config .TelemetrySettings .TelemetryBatchIntervalInSecs = 15
117+ config .TelemetrySettings .TelemetryBatchIntervalInSecs = defaultBatchIntervalInSecs
89118 }
90119 if config .TelemetrySettings .RefreshIntervalInSecs == 0 {
91- config .TelemetrySettings .RefreshIntervalInSecs = 15
120+ config .TelemetrySettings .RefreshIntervalInSecs = defaultRefreshTimeoutInSecs
92121 }
93122
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 ))
123+ // Set default CNI telemetry socket path
124+ if config .TelemetrySettings .CNITelemetrySocketPath == "" {
125+ config .TelemetrySettings .CNITelemetrySocketPath = "/var/run/azure-vnet-telemetry.sock"
98126 }
99127
100- return & config , nil
128+ // Handle AppInsights instrumentation key from environment variables
129+ cm .setAppInsightsKeyFromEnv (& config .TelemetrySettings )
130+ }
131+
132+ // setAppInsightsKeyFromEnv sets the AppInsights instrumentation key from environment variables
133+ func (cm * ConfigManager ) setAppInsightsKeyFromEnv (ts * configuration.TelemetrySettings ) {
134+ // Try multiple environment variable names
135+ envKeys := []string {
136+ "APPINSIGHTS_INSTRUMENTATIONKEY" ,
137+ "APPLICATIONINSIGHTS_CONNECTION_STRING" ,
138+ "AI_INSTRUMENTATION_KEY" ,
139+ }
140+
141+ // If no key is set in config, try environment variables
142+ if ts .AppInsightsInstrumentationKey == "" {
143+ for _ , envKey := range envKeys {
144+ if key := os .Getenv (envKey ); key != "" {
145+ ts .AppInsightsInstrumentationKey = key
146+ if cm .logger != nil {
147+ cm .logger .Debug ("Found AppInsights key in environment variable" ,
148+ zap .String ("envVar" , envKey ))
149+ }
150+ break
151+ }
152+ }
153+ }
154+ }
155+
156+ // hasEffectiveAppInsightsKey checks if AppInsights key is available from any source
157+ // (build-time aiMetadata, config file, or environment variables)
158+ func (cm * ConfigManager ) hasEffectiveAppInsightsKey (ts * configuration.TelemetrySettings ) bool {
159+ // Priority 1: Build-time embedded key via telemetry.aiMetadata
160+ if buildTimeKey := telemetry .GetAIMetadata (); buildTimeKey != "" {
161+ return true
162+ }
163+
164+ // Priority 2: Config file
165+ if ts .AppInsightsInstrumentationKey != "" {
166+ return true
167+ }
168+
169+ // Priority 3: Environment variables
170+ envKeys := []string {
171+ "APPINSIGHTS_INSTRUMENTATIONKEY" ,
172+ "APPLICATIONINSIGHTS_CONNECTION_STRING" ,
173+ "AI_INSTRUMENTATION_KEY" ,
174+ }
175+
176+ for _ , envKey := range envKeys {
177+ if key := os .Getenv (envKey ); key != "" {
178+ return true
179+ }
180+ }
181+
182+ return false
101183}
0 commit comments