@@ -20,6 +20,7 @@ import (
20
20
"github.com/rs/zerolog"
21
21
22
22
"github.com/elastic/elastic-agent-client/v7/pkg/client"
23
+ "github.com/elastic/elastic-agent-client/v7/pkg/proto"
23
24
"github.com/elastic/go-ucfg"
24
25
"gopkg.in/yaml.v3"
25
26
)
@@ -285,7 +286,7 @@ func (a *Agent) start(ctx context.Context) error {
285
286
return a .reconfigure (ctx )
286
287
}
287
288
288
- cfg , err := a .configFromUnits ()
289
+ cfg , err := a .configFromUnits (ctx )
289
290
if err != nil {
290
291
return err
291
292
}
@@ -331,7 +332,7 @@ func (a *Agent) reconfigure(ctx context.Context) error {
331
332
return a .start (ctx )
332
333
}
333
334
334
- cfg , err := a .configFromUnits ()
335
+ cfg , err := a .configFromUnits (ctx )
335
336
if err != nil {
336
337
return err
337
338
}
@@ -373,7 +374,7 @@ func (a *Agent) stop() {
373
374
374
375
// configFromUnits takes both inputUnit and outputUnit and creates a single configuration just like fleet server was
375
376
// being started from a configuration file.
376
- func (a * Agent ) configFromUnits () (* config.Config , error ) {
377
+ func (a * Agent ) configFromUnits (ctx context. Context ) (* config.Config , error ) {
377
378
agentID := ""
378
379
agentVersion := ""
379
380
agentInfo := a .agent .AgentInfo ()
@@ -420,10 +421,54 @@ func (a *Agent) configFromUnits() (*config.Config, error) {
420
421
return nil , err
421
422
}
422
423
424
+ if expAPMCFG := expInput .APMConfig ; expAPMCFG != nil {
425
+ instrumentationCfg , err := apmConfigToInstrumentation (expAPMCFG )
426
+ if err != nil {
427
+ zerolog .Ctx (ctx ).Warn ().Err (err ).Msg ("Unable to parse expected APM config as instrumentation config" )
428
+ } else {
429
+ obj := map [string ]interface {}{
430
+ "inputs" : []interface {}{map [string ]interface {}{
431
+ "server" : map [string ]interface {}{
432
+ "instrumentation" : instrumentationCfg ,
433
+ },
434
+ },
435
+ }}
436
+ err = cfgData .Merge (obj , config .DefaultOptions ... )
437
+ if err != nil {
438
+ zerolog .Ctx (ctx ).Warn ().Err (err ).Msg ("Failed to merge APM config into cfgData" )
439
+ }
440
+ }
441
+
442
+ }
443
+
423
444
cliCfg := ucfg .MustNewFrom (a .cliCfg , config .DefaultOptions ... )
424
445
err = cliCfg .Merge (cfgData , config .DefaultOptions ... )
425
446
if err != nil {
426
447
return nil , err
427
448
}
428
449
return config .FromConfig (cliCfg )
429
450
}
451
+
452
+ // apmConfigToInstrumentation transforms the passed APMConfig into the Instrumentation config that is used by fleet-server.
453
+ func apmConfigToInstrumentation (src * proto.APMConfig ) (config.Instrumentation , error ) {
454
+ if apmest := src .GetElastic (); apmest != nil {
455
+ apmTLS := apmest .GetTls ()
456
+ iTLS := config.InstrumentationTLS {
457
+ SkipVerify : apmTLS .GetSkipVerify (),
458
+ ServerCertificate : apmTLS .GetServerCert (),
459
+ ServerCA : apmTLS .GetServerCa (),
460
+ }
461
+
462
+ cfg := config.Instrumentation {
463
+ Enabled : true ,
464
+ TLS : iTLS ,
465
+ Environment : apmest .GetEnvironment (),
466
+ APIKey : apmest .GetApiKey (),
467
+ SecretToken : apmest .GetSecretToken (),
468
+ Hosts : apmest .GetHosts (),
469
+ GlobalLabels : apmest .GetGlobalLabels (),
470
+ }
471
+ return cfg , nil
472
+ }
473
+ return config.Instrumentation {}, fmt .Errorf ("unable to transform APMConfig to instrumentation" )
474
+ }
0 commit comments