@@ -310,20 +310,19 @@ func runAgent(ctx context.Context,
310
310
}
311
311
}
312
312
313
- isOnlyDefaultConfigPath := len (fOtelConfigs ) == 1 && fOtelConfigs [0 ] == paths .YamlConfigPath
314
-
315
313
if len (c .Inputs ) != 0 && len (c .Outputs ) != 0 {
316
314
log .Println ("creating new logs agent" )
317
315
logAgent := logs .NewLogAgent (c )
318
316
// Always run logAgent as goroutine regardless of whether starting OTEL or Telegraf.
319
317
go logAgent .Run (ctx )
320
318
321
- // If only the default CWAgent config yaml is provided and does not exist, then ASSUME
319
+ // If only a single YAML is provided and does not exist, then ASSUME the agent is
322
320
// just monitoring logs since this is the default when no OTEL config flag is provided.
323
321
// So just start Telegraf.
324
- if isOnlyDefaultConfigPath {
322
+ if len ( fOtelConfigs ) == 1 {
325
323
_ , err = os .Stat (fOtelConfigs [0 ])
326
324
if errors .Is (err , os .ErrNotExist ) {
325
+ log .Println ("I! running in logs-only mode" )
327
326
useragent .Get ().SetComponents (& otelcol.Config {}, c )
328
327
return ag .Run (ctx )
329
328
}
@@ -333,20 +332,20 @@ func runAgent(ctx context.Context,
333
332
level := cwaLogger .ConvertToAtomicLevel (wlog .LogLevel ())
334
333
logger , loggerOptions := cwaLogger .NewLogger (writer , level )
335
334
336
- uris := fOtelConfigs
337
- // merge configs together
338
- if len ( uris ) > 1 {
339
- result , err := mergeConfigs ( uris , isOnlyDefaultConfigPath )
340
- if err != nil {
341
- return err
342
- }
343
- _ = os .Setenv (envconfig .CWAgentMergedOtelConfig , toyamlconfig .ToYamlConfig (result .ToStringMap ()))
344
- uris = []string {"env:" + envconfig .CWAgentMergedOtelConfig }
335
+ otelConfigs := fOtelConfigs
336
+ // try merging configs together, will return nil if nothing to merge
337
+ merged , err := mergeConfigs ( otelConfigs )
338
+ if err != nil {
339
+ return err
340
+ }
341
+ if merged != nil {
342
+ _ = os .Setenv (envconfig .CWAgentMergedOtelConfig , toyamlconfig .ToYamlConfig (merged .ToStringMap ()))
343
+ otelConfigs = []string {"env:" + envconfig .CWAgentMergedOtelConfig }
345
344
} else {
346
345
_ = os .Unsetenv (envconfig .CWAgentMergedOtelConfig )
347
346
}
348
347
349
- providerSettings := configprovider .GetSettings (uris , logger )
348
+ providerSettings := configprovider .GetSettings (otelConfigs , logger )
350
349
provider , err := otelcol .NewConfigProvider (providerSettings )
351
350
if err != nil {
352
351
return fmt .Errorf ("error while initializing config provider: %v" , err )
@@ -382,7 +381,7 @@ func runAgent(ctx context.Context,
382
381
// docs: https://github.com/open-telemetry/opentelemetry-collector/blob/93cbae436ae61b832279dbbb18a0d99214b7d305/otelcol/command.go#L63
383
382
// *************************************************************************************************
384
383
var e []string
385
- for _ , uri := range uris {
384
+ for _ , uri := range otelConfigs {
386
385
e = append (e , "--config=" + uri )
387
386
}
388
387
cmd .SetArgs (e )
@@ -405,32 +404,35 @@ func getCollectorParams(factories otelcol.Factories, providerSettings otelcol.Co
405
404
}
406
405
}
407
406
408
- func mergeConfigs (configPaths []string , isOnlyDefaultConfigPath bool ) (* confmap.Conf , error ) {
409
- result := confmap .New ()
410
- content , ok := os .LookupEnv (envconfig .CWOtelConfigContent )
411
- // Similar to translator, for containerized agent, try to use env variable if no other supplemental config paths
412
- // are provided.
413
- if ok && len (content ) > 0 && isOnlyDefaultConfigPath && envconfig .IsRunningInContainer () {
414
- log .Printf ("D! Merging OTEL configuration from: %s" , envconfig .CWOtelConfigContent )
415
- conf , err := confmap .LoadFromBytes ([]byte (content ))
416
- if err != nil {
417
- return nil , fmt .Errorf ("failed to load OTEL configs: %w" , err )
418
- }
419
- if err = result .Merge (conf ); err != nil {
420
- return nil , fmt .Errorf ("failed to merge OTEL configs: %w" , err )
407
+ // mergeConfigs tries to merge configurations together. If nothing to merge, returns nil without an error.
408
+ func mergeConfigs (configPaths []string ) (* confmap.Conf , error ) {
409
+ var loaders []confmap.Loader
410
+ if envconfig .IsRunningInContainer () {
411
+ content , ok := os .LookupEnv (envconfig .CWOtelConfigContent )
412
+ if ok && len (content ) > 0 {
413
+ log .Printf ("D! Merging OTEL configuration from: %s" , envconfig .CWOtelConfigContent )
414
+ loaders = append (loaders , confmap .NewByteLoader (envconfig .CWOtelConfigContent , []byte (content )))
421
415
}
422
416
}
423
- log . Printf ( "D! Merging OTEL configurations from: %s" , configPaths )
424
- for _ , configPath := range configPaths {
425
- conf , err := confmap . LoadFromFile ( configPath )
426
- if err != nil {
427
- return nil , fmt . Errorf ( "failed to load OTEL configs: %w" , err )
417
+ // If using environment variable or passing in more than one config
418
+ if len ( loaders ) > 0 || len ( configPaths ) > 1 {
419
+ log . Printf ( "D! Merging OTEL configurations from: %s" , configPaths )
420
+ for _ , configPath := range configPaths {
421
+ loaders = append ( loaders , confmap . NewFileLoader ( configPath ) )
428
422
}
429
- if err = result .Merge (conf ); err != nil {
430
- return nil , fmt .Errorf ("failed to merge OTEL configs: %w" , err )
423
+ result := confmap .New ()
424
+ for _ , loader := range loaders {
425
+ conf , err := loader .Load ()
426
+ if err != nil {
427
+ return nil , fmt .Errorf ("failed to load OTEL configs: %w" , err )
428
+ }
429
+ if err = result .Merge (conf ); err != nil {
430
+ return nil , fmt .Errorf ("failed to merge OTEL configs: %w" , err )
431
+ }
431
432
}
433
+ return result , nil
432
434
}
433
- return result , nil
435
+ return nil , nil
434
436
}
435
437
436
438
func components (telegrafConfig * config.Config ) (otelcol.Factories , error ) {
0 commit comments