@@ -3,49 +3,129 @@ package main
33import (
44 "context"
55 "flag"
6+ "fmt"
67 "os"
78 "os/signal"
89 "syscall"
910
10- "github.com/Azure/azure-container-networking/cns/logger"
11+ "github.com/Azure/azure-container-networking/telemetry"
12+ "go.uber.org/zap"
13+ "go.uber.org/zap/zapcore"
1114)
1215
1316var (
1417 version = "unknown"
15- configPath = flag . String ( "config" , "/etc/cns/cns-config.json" , "Path to CNS configuration file" )
18+ aiMetadata = "unknown"
1619)
1720
1821func main () {
22+ var (
23+ configPath = flag .String ("config" , "/etc/azure-cns/cns_config.json" , "Path to CNS configuration file" )
24+ logLevel = flag .String ("log-level" , "info" , "Log level (debug, info, warn, error)" )
25+ showVersion = flag .Bool ("version" , false , "Show version information" )
26+ )
1927 flag .Parse ()
2028
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 ()
29+ // Show version and exit if requested
30+ if * showVersion {
31+ fmt .Printf ("Azure CNI Telemetry Sidecar version: %s\n " , version )
32+ fmt .Printf ("AI Metadata: %s\n " , aiMetadata )
33+ return
34+ }
35+
36+ // Create a zap logger early for structured logging
37+ zapLogger := createZapLogger (* logLevel )
38+ defer zapLogger .Sync ()
39+
40+ // Use structured logging with zap instead of log.Printf
41+ zapLogger .Info ("Starting Azure CNI Telemetry Sidecar" ,
42+ zap .String ("version" , version ),
43+ zap .String ("configPath" , * configPath ),
44+ zap .String ("logLevel" , * logLevel ))
2445
25- logger .Printf ("Starting Azure CNI Telemetry Sidecar v%s" , version )
46+ // Initialize telemetry buffer with zap logger
47+ tb := telemetry .NewTelemetryBuffer (zapLogger )
48+ if tb == nil {
49+ zapLogger .Error ("Failed to create telemetry buffer" )
50+ os .Exit (1 )
51+ }
52+ defer tb .Close ()
2653
27- // Create telemetry sidecar service
28- sidecar := NewTelemetrySidecar (* configPath )
54+ zapLogger .Info ("Telemetry buffer initialized successfully" )
2955
30- // Setup graceful shutdown context
56+ // Set up signal handling for graceful shutdown
3157 ctx , cancel := context .WithCancel (context .Background ())
3258 defer cancel ()
3359
34- // Handle OS signals for graceful shutdown
35- sigCh := make (chan os.Signal , 1 )
36- signal .Notify (sigCh , os .Interrupt , syscall .SIGTERM )
60+ sigChan := make (chan os.Signal , 1 )
61+ signal .Notify (sigChan , syscall .SIGINT , syscall .SIGTERM )
3762
3863 go func () {
39- sig := <- sigCh
40- logger . Printf ("Received shutdown signal %v, initiating graceful shutdown" , sig )
64+ sig := <- sigChan
65+ zapLogger . Info ("Received shutdown signal" , zap . String ( "signal" , sig . String ()) )
4166 cancel ()
4267 }()
4368
44- // Run the telemetry sidecar
45- if err := sidecar . Run (ctx ); err != nil {
46- logger . Errorf ( "Azure CNI Telemetry Sidecar failed: %v " , err )
69+ // Implement the main telemetry service
70+ if err := runTelemetryService (ctx , zapLogger , * configPath ); err != nil {
71+ zapLogger . Error ( " Telemetry service failed" , zap . Error ( err ) )
4772 os .Exit (1 )
4873 }
4974
50- logger .Printf ("Azure CNI Telemetry Sidecar stopped gracefully" )
75+ zapLogger .Info ("Azure CNI Telemetry Sidecar stopped" )
76+ }
77+
78+ // createZapLogger creates a zap logger with the specified log level
79+ func createZapLogger (logLevel string ) * zap.Logger {
80+ var level zapcore.Level
81+ switch logLevel {
82+ case "debug" :
83+ level = zapcore .DebugLevel
84+ case "info" :
85+ level = zapcore .InfoLevel
86+ case "warn" :
87+ level = zapcore .WarnLevel
88+ case "error" :
89+ level = zapcore .ErrorLevel
90+ default :
91+ level = zapcore .InfoLevel
92+ }
93+
94+ config := zap.Config {
95+ Level : zap .NewAtomicLevelAt (level ),
96+ Development : false ,
97+ Sampling : & zap.SamplingConfig {
98+ Initial : 100 ,
99+ Thereafter : 100 ,
100+ },
101+ Encoding : "json" ,
102+ EncoderConfig : zap .NewProductionEncoderConfig (),
103+ OutputPaths : []string {"stdout" },
104+ ErrorOutputPaths : []string {"stderr" },
105+ }
106+
107+ logger , err := config .Build ()
108+ if err != nil {
109+ // Fallback to a basic logger if configuration fails
110+ return zap .NewNop ()
111+ }
112+
113+ return logger
114+ }
115+
116+ // runTelemetryService implements the main telemetry collection logic
117+ func runTelemetryService (ctx context.Context , logger * zap.Logger , configPath string ) error {
118+ logger .Info ("CNI Telemetry Sidecar is running" )
119+
120+ // TODO: Implement your telemetry collection logic here
121+ // Example implementation structure:
122+ // 1. Read configuration from configPath
123+ // 2. Set up telemetry collectors for CNI metrics
124+ // 3. Start HTTP server for health checks and metrics endpoints
125+ // 4. Process and forward telemetry data
126+
127+ // For now, just wait for shutdown
128+ <- ctx .Done ()
129+ logger .Info ("Telemetry service shutting down" )
130+ return nil
51131}
0 commit comments