@@ -3,30 +3,145 @@ package main
33// Entry point of the telemetry service if started by CNI
44
55import (
6+ "fmt"
7+ "os"
8+ "runtime"
69 "time"
710
11+ acn "github.com/Azure/azure-container-networking/common"
12+ "github.com/Azure/azure-container-networking/log"
813 "github.com/Azure/azure-container-networking/telemetry"
914)
1015
1116const (
12- reportToHostIntervalInSeconds = 60 * time .Second
13- azurecnitelemetry = "azure-vnet-telemetry"
17+ reportToHostIntervalInSeconds = 30
18+ azureVnetTelemetry = "azure-vnet-telemetry"
19+ configExtension = ".config"
1420)
1521
22+ var version string
23+
24+ var args = acn.ArgumentList {
25+ {
26+ Name : acn .OptLogLevel ,
27+ Shorthand : acn .OptLogLevelAlias ,
28+ Description : "Set the logging level" ,
29+ Type : "int" ,
30+ DefaultValue : acn .OptLogLevelInfo ,
31+ ValueMap : map [string ]interface {}{
32+ acn .OptLogLevelInfo : log .LevelInfo ,
33+ acn .OptLogLevelDebug : log .LevelDebug ,
34+ },
35+ },
36+ {
37+ Name : acn .OptLogTarget ,
38+ Shorthand : acn .OptLogTargetAlias ,
39+ Description : "Set the logging target" ,
40+ Type : "int" ,
41+ DefaultValue : acn .OptLogTargetFile ,
42+ ValueMap : map [string ]interface {}{
43+ acn .OptLogTargetSyslog : log .TargetSyslog ,
44+ acn .OptLogTargetStderr : log .TargetStderr ,
45+ acn .OptLogTargetFile : log .TargetLogfile ,
46+ acn .OptLogStdout : log .TargetStdout ,
47+ acn .OptLogMultiWrite : log .TargetStdOutAndLogFile ,
48+ },
49+ },
50+ {
51+ Name : acn .OptLogLocation ,
52+ Shorthand : acn .OptLogLocationAlias ,
53+ Description : "Set the directory location where logs will be saved" ,
54+ Type : "string" ,
55+ DefaultValue : "" ,
56+ },
57+ {
58+ Name : acn .OptVersion ,
59+ Shorthand : acn .OptVersionAlias ,
60+ Description : "Print version information" ,
61+ Type : "bool" ,
62+ DefaultValue : false ,
63+ },
64+ {
65+ Name : acn .OptTelemetryConfigDir ,
66+ Shorthand : acn .OptTelemetryConfigDirAlias ,
67+ Description : "Set the telmetry config directory" ,
68+ Type : "string" ,
69+ DefaultValue : telemetry .CniInstallDir ,
70+ },
71+ }
72+
73+ // Prints description and version information.
74+ func printVersion () {
75+ fmt .Printf ("Azure Container Telemetry Service\n " )
76+ fmt .Printf ("Version %v\n " , version )
77+ }
78+
1679func main () {
1780 var tb * telemetry.TelemetryBuffer
81+ var config telemetry.TelemetryConfig
82+ var configPath string
1883 var err error
1984
85+ acn .ParseArgs (& args , printVersion )
86+ logTarget := acn .GetArg (acn .OptLogTarget ).(int )
87+ logDirectory := acn .GetArg (acn .OptLogLocation ).(string )
88+ logLevel := acn .GetArg (acn .OptLogLevel ).(int )
89+ configDirectory := acn .GetArg (acn .OptTelemetryConfigDir ).(string )
90+ vers := acn .GetArg (acn .OptVersion ).(bool )
91+
92+ if vers {
93+ printVersion ()
94+ os .Exit (0 )
95+ }
96+
97+ log .SetName (azureVnetTelemetry )
98+ log .SetLevel (logLevel )
99+ if logDirectory != "" {
100+ log .SetLogDirectory (logDirectory )
101+ }
102+
103+ err = log .SetTarget (logTarget )
104+ if err != nil {
105+ fmt .Printf ("Failed to configure logging: %v\n " , err )
106+ return
107+ }
108+
109+ log .Printf ("args %+v" , os .Args )
110+
111+ if runtime .GOOS == "linux" {
112+ configPath = fmt .Sprintf ("%s/%s%s" , configDirectory , azureVnetTelemetry , configExtension )
113+ } else {
114+ configPath = fmt .Sprintf ("%s\\ %s%s" , configDirectory , azureVnetTelemetry , configExtension )
115+ }
116+
117+ log .Printf ("[Telemetry] Config path: %s" , configPath )
118+
119+ config , err = telemetry .ReadConfigFile (configPath )
120+ if err != nil {
121+ log .Printf ("[Telemetry] Error reading telemetry config: %v" , err )
122+ }
123+
124+ log .Printf ("read config returned %+v" , config )
125+
20126 for {
21127 tb = telemetry .NewTelemetryBuffer ("" )
128+
129+ log .Printf ("[Telemetry] Starting telemetry server" )
22130 err = tb .StartServer ()
23131 if err == nil || tb .FdExists {
24132 break
25133 }
26134
135+ log .Printf ("[Telemetry] Telemetry service starting failed: %v" , err )
27136 tb .Cleanup (telemetry .FdName )
28137 time .Sleep (time .Millisecond * 200 )
29138 }
30139
31- tb .BufferAndPushData (reportToHostIntervalInSeconds )
140+ if config .ReportToHostIntervalInSeconds == 0 {
141+ config .ReportToHostIntervalInSeconds = reportToHostIntervalInSeconds
142+ }
143+
144+ log .Printf ("[Telemetry] Report to host for an interval of %d seconds" , config .ReportToHostIntervalInSeconds )
145+ tb .BufferAndPushData (config .ReportToHostIntervalInSeconds * time .Second )
146+ log .Close ()
32147}
0 commit comments