77
88 "github.com/Azure/azure-container-networking/aitelemetry"
99 "github.com/Azure/azure-container-networking/cns/configuration"
10- "github.com/Azure/azure-container-networking/cns/logger"
1110 "github.com/Azure/azure-container-networking/telemetry"
11+ "go.uber.org/zap"
1212)
1313
1414const (
@@ -23,6 +23,7 @@ type TelemetrySidecar struct {
2323 configPath string
2424 configManager * ConfigManager
2525 telemetryBuffer * telemetry.TelemetryBuffer
26+ logger * zap.Logger
2627}
2728
2829// NewTelemetrySidecar creates a new telemetry sidecar instance
@@ -33,9 +34,22 @@ func NewTelemetrySidecar(configPath string) *TelemetrySidecar {
3334 }
3435}
3536
37+ // SetLogger sets the zap logger for the sidecar
38+ func (s * TelemetrySidecar ) SetLogger (logger * zap.Logger ) error {
39+ if logger == nil {
40+ return fmt .Errorf ("logger cannot be nil" )
41+ }
42+ s .logger = logger
43+ return nil
44+ }
45+
3646// Run starts the telemetry sidecar and manages its lifecycle
3747func (s * TelemetrySidecar ) Run (ctx context.Context ) error {
38- logger .Printf ("Initializing Azure CNI Telemetry Sidecar for azure-vnet-telemetry" )
48+ if s .logger == nil {
49+ return fmt .Errorf ("logger not initialized - call SetLogger() first" )
50+ }
51+
52+ s .logger .Info ("Initializing Azure CNI Telemetry Sidecar for azure-vnet-telemetry" )
3953
4054 // Load CNS configuration from shared mount
4155 config , err := s .configManager .LoadConfig ()
@@ -45,7 +59,7 @@ func (s *TelemetrySidecar) Run(ctx context.Context) error {
4559
4660 // Determine if telemetry should run based on configuration and environment
4761 if ! s .shouldRunTelemetry (config ) {
48- logger .Printf ("CNI Telemetry disabled, entering sleep mode" )
62+ s . logger .Info ("CNI Telemetry disabled, entering sleep mode" )
4963 return s .sleepUntilShutdown (ctx )
5064 }
5165
@@ -55,7 +69,7 @@ func (s *TelemetrySidecar) Run(ctx context.Context) error {
5569 }
5670
5771 // Start telemetry service mimicking azure-vnet-telemetry behavior
58- logger .Printf ("Starting Azure VNet Telemetry service (CNI mode)" )
72+ s . logger .Info ("Starting Azure VNet Telemetry service (CNI mode)" )
5973 return s .runCNITelemetryService (ctx )
6074}
6175
@@ -93,8 +107,10 @@ func (s *TelemetrySidecar) initializeCNITelemetry(config *configuration.CNSConfi
93107 return fmt .Errorf ("failed to create AI telemetry handle for CNI: %w" , err )
94108 }
95109
96- logger .Printf ("CNI Telemetry initialized with Application Insights (App: %s, Version: %s, BatchSize: %d)" ,
97- cniTelemetryAppName , cniTelemetryVersion , ts .TelemetryBatchSizeBytes )
110+ s .logger .Info ("CNI Telemetry initialized with Application Insights" ,
111+ zap .String ("app" , cniTelemetryAppName ),
112+ zap .String ("version" , cniTelemetryVersion ),
113+ zap .Int ("batchSize" , ts .TelemetryBatchSizeBytes ))
98114 return nil
99115}
100116
@@ -108,19 +124,20 @@ func (s *TelemetrySidecar) runCNITelemetryService(ctx context.Context) error {
108124 return fmt .Errorf ("failed to start CNI telemetry server: %w" , err )
109125 }
110126
111- logger .Printf ("Azure VNet Telemetry server started successfully on socket: %s" , telemetrySocket )
127+ s .logger .Info ("Azure VNet Telemetry server started successfully" ,
128+ zap .String ("socket" , telemetrySocket ))
112129
113130 // Start telemetry data collection in background (non-blocking)
114131 go s .telemetryBuffer .PushData (ctx )
115132
116133 // Log readiness for CNI network event collection
117- logger .Printf ("CNI Telemetry sidecar ready to collect Azure network interface events" )
134+ s . logger .Info ("CNI Telemetry sidecar ready to collect Azure network interface events" )
118135
119136 // Wait for context cancellation (graceful shutdown signal)
120137 <- ctx .Done ()
121138
122139 // Perform cleanup and graceful shutdown
123- logger .Printf ("Shutting down Azure CNI Telemetry service" )
140+ s . logger .Info ("Shutting down Azure CNI Telemetry service" )
124141 return s .shutdownTelemetry ()
125142}
126143
@@ -131,9 +148,10 @@ func (s *TelemetrySidecar) cleanupExistingInstances() {
131148
132149 // Use the same FdName that azure-vnet-telemetry uses for consistency
133150 if err := tempBuffer .Cleanup (telemetry .FdName ); err != nil {
134- logger .Printf ("Warning: Failed to cleanup existing CNI telemetry instances: %v" , err )
151+ s .logger .Warn ("Failed to cleanup existing CNI telemetry instances" ,
152+ zap .Error (err ))
135153 } else {
136- logger .Printf ("Successfully cleaned up existing CNI telemetry instances" )
154+ s . logger .Info ("Successfully cleaned up existing CNI telemetry instances" )
137155 }
138156}
139157
@@ -142,7 +160,7 @@ func (s *TelemetrySidecar) shutdownTelemetry() error {
142160 if s .telemetryBuffer != nil {
143161 // Close telemetry buffer (ensures data is flushed to Azure)
144162 s .telemetryBuffer .Close ()
145- logger .Printf ("CNI Telemetry buffer closed and remaining data flushed to Azure" )
163+ s . logger .Info ("CNI Telemetry buffer closed and remaining data flushed to Azure" )
146164 }
147165 return nil
148166}
@@ -151,30 +169,30 @@ func (s *TelemetrySidecar) shutdownTelemetry() error {
151169func (s * TelemetrySidecar ) shouldRunTelemetry (config * configuration.CNSConfig ) bool {
152170 // Check global telemetry disable flag in CNS configuration
153171 if config .TelemetrySettings .DisableAll {
154- logger .Printf ("CNI Telemetry disabled globally in CNS configuration" )
172+ s . logger .Info ("CNI Telemetry disabled globally in CNS configuration" )
155173 return false
156174 }
157175
158176 // Check CNI telemetry specific enable flag (replaces old "ts" option)
159177 cniTelemetryEnabled := os .Getenv ("CNI_TELEMETRY_ENABLED" )
160178 if cniTelemetryEnabled != "true" {
161- logger .Printf ("CNI Telemetry not enabled via CNI_TELEMETRY_ENABLED environment variable" )
179+ s . logger .Info ("CNI Telemetry not enabled via CNI_TELEMETRY_ENABLED environment variable" )
162180 return false
163181 }
164182
165183 // Validate Application Insights key availability
166184 if config .TelemetrySettings .AppInsightsInstrumentationKey == "" {
167- logger .Printf ("No Application Insights instrumentation key configured for CNI telemetry" )
185+ s . logger .Info ("No Application Insights instrumentation key configured for CNI telemetry" )
168186 return false
169187 }
170188
171- logger .Printf ("CNI Telemetry enabled - will collect Azure network interface events" )
189+ s . logger .Info ("CNI Telemetry enabled - will collect Azure network interface events" )
172190 return true
173191}
174192
175193// sleepUntilShutdown keeps the container running when telemetry is disabled
176194func (s * TelemetrySidecar ) sleepUntilShutdown (ctx context.Context ) error {
177- logger .Printf ("CNI Telemetry sidecar sleeping until shutdown signal received" )
195+ s . logger .Info ("CNI Telemetry sidecar sleeping until shutdown signal received" )
178196 <- ctx .Done ()
179197 return ctx .Err ()
180198}
0 commit comments