Skip to content

Commit 3626fd8

Browse files
committed
improve the logging
1 parent 6e4257f commit 3626fd8

File tree

2 files changed

+67
-27
lines changed

2 files changed

+67
-27
lines changed

cns/cni-telemetry-sidecar/main.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,46 @@ import (
77
"os/signal"
88
"syscall"
99

10-
"github.com/Azure/azure-container-networking/cns/logger"
10+
"github.com/Azure/azure-container-networking/cns/logger/v2"
11+
cores "github.com/Azure/azure-container-networking/cns/logger/v2/cores"
12+
"go.uber.org/zap"
1113
)
1214

1315
var (
1416
version = "unknown"
1517
configPath = flag.String("config", "/etc/cns/cns-config.json", "Path to CNS configuration file")
18+
logLevel = flag.String("log-level", "info", "Log level (debug, info, warn, error)")
1619
)
1720

1821
func main() {
1922
flag.Parse()
2023

21-
// Initialize logging for the CNI telemetry sidecar
22-
logger.InitLogger("azure-cns-cni-telemetry-sidecar", 1, 1, "/var/log/azure-cns-telemetry")
23-
defer logger.Close()
24+
// Initialize main logger with correct path for shared volume
25+
zapLogger, cleanup, err := logger.New(&logger.Config{
26+
Level: *logLevel,
27+
File: &cores.FileConfig{
28+
Filepath: "/var/log/azure-cni-telemetry-sidecar.log", // This will write to host's /var/log/azure-cns/
29+
},
30+
})
31+
if err != nil {
32+
panic("Failed to initialize logger: " + err.Error())
33+
}
34+
defer cleanup()
2435

25-
logger.Printf("Starting Azure CNI Telemetry Sidecar v%s", version)
36+
zapLogger.Info("Starting Azure CNI Telemetry Sidecar",
37+
zap.String("version", version),
38+
zap.String("configPath", *configPath),
39+
zap.String("logLevel", *logLevel))
2640

27-
// Create telemetry sidecar service
41+
// Create telemetry sidecar service and pass the logger
2842
sidecar := NewTelemetrySidecar(*configPath)
2943

44+
// Set the logger for the sidecar to avoid nil pointer
45+
if err := sidecar.SetLogger(zapLogger); err != nil {
46+
zapLogger.Error("Failed to set logger for sidecar", zap.Error(err))
47+
os.Exit(1)
48+
}
49+
3050
// Setup graceful shutdown context
3151
ctx, cancel := context.WithCancel(context.Background())
3252
defer cancel()
@@ -37,15 +57,17 @@ func main() {
3757

3858
go func() {
3959
sig := <-sigCh
40-
logger.Printf("Received shutdown signal %v, initiating graceful shutdown", sig)
60+
zapLogger.Info("Received shutdown signal, initiating graceful shutdown",
61+
zap.String("signal", sig.String()))
4162
cancel()
4263
}()
4364

44-
// Run the telemetry sidecar
65+
// Run the telemetry sidecar (using the Run method from sidecar.go)
4566
if err := sidecar.Run(ctx); err != nil {
46-
logger.Errorf("Azure CNI Telemetry Sidecar failed: %v", err)
67+
zapLogger.Error("Azure CNI Telemetry Sidecar failed",
68+
zap.Error(err))
4769
os.Exit(1)
4870
}
4971

50-
logger.Printf("Azure CNI Telemetry Sidecar stopped gracefully")
72+
zapLogger.Info("Azure CNI Telemetry Sidecar stopped gracefully")
5173
}

cns/cni-telemetry-sidecar/sidecar.go

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
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

1414
const (
@@ -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
3747
func (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 {
151169
func (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
176194
func (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

Comments
 (0)