Skip to content

Commit cf2d85f

Browse files
committed
add wrapper to libbeat instrumentation to propagate missing tracer
config via env
1 parent ab4b728 commit cf2d85f

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

internal/beater/beater.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func (s *Runner) Run(ctx context.Context) error {
292292
}
293293
}
294294

295-
instrumentation, err := instrumentation.New(s.rawConfig, "apm-server", version.Version)
295+
instrumentation, err := newInstrumentation(s.rawConfig)
296296
if err != nil {
297297
return err
298298
}
@@ -537,6 +537,50 @@ func (s *Runner) Run(ctx context.Context) error {
537537
return errors.Join(result, closeErr)
538538
}
539539

540+
// newInstrumentation is a thin wrapper around libbeat instrumentation that
541+
// sets missing tracer configuration from elastic-agent.
542+
func newInstrumentation(rawConfig *agentconfig.C) (instrumentation.Instrumentation, error) {
543+
var apmCfg struct {
544+
GlobalLabels string `config:"globallabels"`
545+
TLS struct {
546+
SkipVerify bool `config:"skipverify"`
547+
ServerCertificate string `config:"servercert"`
548+
ServerCA string `config:"serverca"`
549+
} `config:"tls"`
550+
}
551+
cfg, err := rawConfig.Child("instrumentation", -1)
552+
if err != nil {
553+
// Fallback to instrumentation.New if the configs are not present.
554+
return instrumentation.New(rawConfig, "apm-server", version.Version)
555+
}
556+
if err := cfg.Unpack(&apmCfg); err != nil {
557+
return nil, err
558+
}
559+
const (
560+
envVerifyServerCert = "ELASTIC_APM_VERIFY_SERVER_CERT"
561+
envServerCert = "ELASTIC_APM_SERVER_CERT"
562+
envCACert = "ELASTIC_APM_SERVER_CA_CERT_FILE"
563+
envGlobalLabels = "ELASTIC_APM_GLOBAL_LABELS"
564+
)
565+
if apmCfg.TLS.SkipVerify {
566+
os.Setenv(envVerifyServerCert, "false")
567+
defer os.Unsetenv(envVerifyServerCert)
568+
}
569+
if apmCfg.TLS.ServerCertificate != "" {
570+
os.Setenv(envServerCert, apmCfg.TLS.ServerCertificate)
571+
defer os.Unsetenv(envServerCert)
572+
}
573+
if apmCfg.TLS.ServerCA != "" {
574+
os.Setenv(envCACert, apmCfg.TLS.ServerCA)
575+
defer os.Unsetenv(envCACert)
576+
}
577+
if len(apmCfg.GlobalLabels) > 0 {
578+
os.Setenv(envGlobalLabels, apmCfg.GlobalLabels)
579+
defer os.Unsetenv(envGlobalLabels)
580+
}
581+
return instrumentation.New(rawConfig, "apm-server", version.Version)
582+
}
583+
540584
func maxConcurrentDecoders(memLimitGB float64) uint {
541585
// Allow 128 concurrent decoders for each 1GB memory, limited to at most 2048.
542586
const max = 2048

0 commit comments

Comments
 (0)