@@ -38,6 +38,7 @@ var cfgFile string
3838type Config struct {
3939 Server ServerConfig `yaml:"server"`
4040 Metrics MetricsConfig `yaml:"metrics"`
41+ Logs LogsConfig `yaml:"logs"`
4142 Integrations map [string ]interface {} `yaml:"integrations"`
4243
4344}
@@ -93,9 +94,30 @@ type RemoteWrite struct {
9394
9495}
9596
97+ type LogsConfig struct {
98+ Configs []LogConfig `yaml:"configs"`
99+ }
100+
101+ type LogConfig struct {
102+ Name string `yaml:"name"`
103+ Clients []LogClient `yaml:"clients"`
104+ Positions Positions `yaml:"positions"`
105+ }
106+
107+ type Positions struct {
108+ Filename string `yaml:"filename"`
109+ }
110+
111+ type LogClient struct {
112+ URL string `yaml:"url"`
113+ BasicAuth BasicAuth `yaml:"basic_auth"`
114+ ExternalLabels map [string ]string `yaml:"external_labels"`
115+ }
116+
96117
97118type TelescopeConfig struct {
98119 Metrics bool
120+ Logs bool
99121 Network string
100122 ProjectId string
101123 ProjectName string
@@ -175,7 +197,6 @@ var cmd = &cobra.Command{
175197}
176198
177199
178-
179200func generateNetworkConfig () Config {
180201 // var telescope_config TelescopeConfig
181202 // cMetrics := viper.GetBool("metrics")
@@ -185,6 +206,8 @@ func generateNetworkConfig() Config {
185206 cTelescopeUsername := viper .GetString ("telescope-username" )
186207 cTelescopePassword := viper .GetString ("telescope-password" )
187208 cRemoteWriteUrl := viper .GetString ("remote-write-url" )
209+ isEnableLogs := viper .GetString ("enable-logs" )
210+ cLogSinkURL := viper .GetString ("logs-sink-url" )
188211
189212 ports , err := networkDiscovery (cNetwork )
190213
@@ -218,7 +241,7 @@ func generateNetworkConfig() Config {
218241 idx ++
219242 }
220243
221- return Config {
244+ config := Config {
222245 Server : ServerConfig {
223246 LogLevel : "info" ,
224247 },
@@ -227,7 +250,7 @@ func generateNetworkConfig() Config {
227250 Global : GlobalConfig {
228251 ScrapeInterval : "15s" ,
229252 ExternalLabels : map [string ]string {
230- "project_id" : cProjectId ,
253+ "project_id" : cProjectId ,
231254 "project_name" : cProjectName ,
232255 },
233256 RemoteWrite : []RemoteWrite {
@@ -238,13 +261,12 @@ func generateNetworkConfig() Config {
238261 "password" : cTelescopePassword ,
239262 },
240263 },
241-
242264 },
243265 },
244266 Configs : []MetricConfig {
245267 {
246- Name : toLowerAndEscape (cProjectName + cNetwork + "_metrics" ),
247- HostFilter : false ,
268+ Name : toLowerAndEscape (cProjectName + cNetwork + "_metrics" ),
269+ HostFilter : false ,
248270 ScrapeConfigs : scrapeConfigs ,
249271 },
250272 },
@@ -257,8 +279,36 @@ func generateNetworkConfig() Config {
257279 Enabled : true ,
258280 },
259281 },
260-
261282 }
283+
284+ if isEnableLogs == "true" {
285+ config .Logs = LogsConfig {
286+ Configs : []LogConfig {
287+ {
288+ Name : "telescope_logs" ,
289+ Clients : []LogClient {
290+ {
291+ URL : cLogSinkURL ,
292+ BasicAuth : BasicAuth {
293+ Username : cTelescopeUsername ,
294+ Password : cTelescopePassword ,
295+ },
296+ ExternalLabels : map [string ]string {
297+ "project_id" : cProjectId ,
298+ "project_name" : cProjectName ,
299+ },
300+ },
301+ },
302+ Positions : Positions {
303+ Filename : "/tmp/telescope_logs" ,
304+ },
305+ },
306+ },
307+ }
308+ }
309+
310+ return config
311+
262312}
263313
264314func LoadNetworkConfig () string {
@@ -290,11 +340,13 @@ var helperFunction = func(cmd *cobra.Command, args []string) {
290340 fmt .Println (" --telescope-username\t Specify the telescope username" )
291341 fmt .Println (" --telescope-password\t Specify the telescope password" )
292342 fmt .Println (" --remote-write-url\t Specify the remote write URL" )
343+ fmt .Println (" --config-file\t \t Specify the config file" )
344+ fmt .Println (" --logs\t \t Enable logs" )
293345}
294346
295347
296348func checkRequiredFlags () error {
297- requiredFlags := []string {"metrics" , "network" , "project-id" , "project-name" , "telescope-username" , "telescope-password" , "remote-write-url" }
349+ requiredFlags := []string {"metrics" , "enable-logs" , " network" , "project-id" , "project-name" , "telescope-username" , "telescope-password" , "remote-write-url" }
298350 missingFlags := []string {}
299351
300352 for _ , flag := range requiredFlags {
@@ -303,7 +355,7 @@ func checkRequiredFlags() error {
303355 }
304356 }
305357
306- if len (missingFlags ) > 0 {
358+ if len (missingFlags ) > 0 && viper . GetString ( "config-file" ) == "" {
307359 return fmt .Errorf ("Error: Missing required flags: %s" , strings .Join (missingFlags , ", " ))
308360 }
309361
@@ -314,22 +366,28 @@ func init() {
314366 prometheus .MustRegister (build .NewCollector ("agent" ))
315367 cobra .OnInitialize (initConfig )
316368 // cmd.SetHelpFunc(helperFunction)
317- cmd .Flags ().Bool ("metrics" , false , "Enable metrics" )
369+ cmd .Flags ().StringVar (& cfgFile , "config-file" , "" , "Specify the config file" )
370+ cmd .Flags ().Bool ("metrics" , true , "Enable metrics" )
371+ cmd .Flags ().Bool ("enable-logs" , false , "Enable logs" )
318372 cmd .Flags ().String ("network" , "" , "Specify the network" )
319373 cmd .Flags ().String ("project-id" , "" , "Specify the project ID" )
320374 cmd .Flags ().String ("project-name" , "" , "Specify the project name" )
321375 cmd .Flags ().String ("telescope-username" , "" , "Specify the telescope username" )
322376 cmd .Flags ().String ("telescope-password" , "" , "Specify the telescope password" )
323377 cmd .Flags ().String ("remote-write-url" , "" , "Specify the remote write URL" )
378+ cmd .Flags ().String ("logs-sink-url" , "" , "Specify the Log Sink URL" )
324379
325380 // Bind flags with viper
381+ viper .BindPFlag ("config-file" , cmd .Flags ().Lookup ("config-file" ))
326382 viper .BindPFlag ("metrics" , cmd .Flags ().Lookup ("metrics" ))
327383 viper .BindPFlag ("network" , cmd .Flags ().Lookup ("network" ))
328384 viper .BindPFlag ("project-id" , cmd .Flags ().Lookup ("project-id" ))
329385 viper .BindPFlag ("project-name" , cmd .Flags ().Lookup ("project-name" ))
330386 viper .BindPFlag ("telescope-username" , cmd .Flags ().Lookup ("telescope-username" ))
331387 viper .BindPFlag ("telescope-password" , cmd .Flags ().Lookup ("telescope-password" ))
332388 viper .BindPFlag ("remote-write-url" , cmd .Flags ().Lookup ("remote-write-url" ))
389+ viper .BindPFlag ("logs-sink-url" , cmd .Flags ().Lookup ("logs-sink-url" ))
390+ viper .BindPFlag ("enable-logs" , cmd .Flags ().Lookup ("enable-logs" ))
333391
334392
335393 // cmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cmd.yaml)")
@@ -340,38 +398,33 @@ func initConfig() {
340398 if cfgFile != "" {
341399 // Use config file from the flag.
342400 viper .SetConfigFile (cfgFile )
401+ if err := viper .ReadInConfig (); err != nil {
402+ log .Fatalf ("Error reading config file: %v" , err )
403+ }
343404 } else {
344- // Find home directory.
345- home , err := os .UserHomeDir ()
346- cobra .CheckErr (err )
347-
348- // Search config in home directory with name ".cmd" (without extension).
349- viper .AddConfigPath (home )
350- viper .SetConfigType ("yaml" )
351- viper .SetConfigName (".cmd" )
405+ viper .AutomaticEnv () // read in environment variables that match
352406 }
407+ }
353408
354- viper . AutomaticEnv () // read in environment variables that match
409+ func ( c * TelescopeConfig ) loadConfig () error {
355410
356- // If a config file is found, read it in.
357- if err := viper .ReadInConfig (); err == nil {
358- fmt .Fprintln (os .Stderr , "Using config file:" , viper .ConfigFileUsed ())
411+ // Check if config-file is provided
412+ if viper .GetString ("config-file" ) != "" {
413+ cfgFile = viper .GetString ("config-file" )
414+ viper .SetConfigFile (cfgFile )
415+ if err := viper .ReadInConfig (); err != nil {
416+ return fmt .Errorf ("Error reading config file: %s" , err )
417+ }
418+ } else {
419+ // generateNetworkConfig
420+ LoadNetworkConfig ()
421+ }
422+
423+ // Check for required fields only if config-file is not provided
424+ if err := checkRequiredFlags (); err != nil {
425+ return err
359426 }
360- }
361427
362- func (c * TelescopeConfig ) loadConfig () error {
363- c .Metrics = viper .GetBool ("metrics" )
364- c .Network = viper .GetString ("network" )
365- c .ProjectId = viper .GetString ("project-id" )
366- c .ProjectName = viper .GetString ("project-name" )
367- c .TelescopeUsername = viper .GetString ("telescope-username" )
368- c .TelescopePassword = viper .GetString ("telescope-password" )
369- c .RemoteWriteUrl = viper .GetString ("remote-write-url" )
370-
371- // Check for required fields
372- if err := checkRequiredFlags (); err != nil {
373- return err
374- }
375428
376429 return nil
377430
@@ -423,10 +476,16 @@ func agent(configPath string) {
423476func main () {
424477
425478 err := cmd .Execute ()
426-
427- configPath := LoadNetworkConfig ()
428- agent (configPath )
429479 if err != nil {
430480 os .Exit (1 )
431481 }
482+
483+ var configPath string
484+ if cfgFile != "" {
485+ configPath = cfgFile
486+ } else {
487+ configPath = LoadNetworkConfig ()
488+ }
489+
490+ agent (configPath )
432491}
0 commit comments