Skip to content

Commit c181a17

Browse files
feat: rework otel config package to avoid globals
1 parent 4684f43 commit c181a17

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

internal/pkg/agent/application/application.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,16 @@ func New(
123123
override(cfg)
124124
}
125125

126-
otelconfig.SetExecutionModeFromConfig(log, rawConfig)
127-
otelExecMode := otelconfig.GetExecutionMode()
128-
isOtelSubprocessExecution := otelExecMode == otelmanager.SubprocessExecutionMode
126+
otelExecMode := otelconfig.GetExecutionModeFromConfig(log, rawConfig)
127+
isOtelExecModeSubprocess := otelExecMode == otelmanager.SubprocessExecutionMode
129128

130129
// monitoring is not supported in bootstrap mode https://github.com/elastic/elastic-agent/issues/1761
131130
isMonitoringSupported := !disableMonitoring && cfg.Settings.V1MonitoringEnabled
132131
upgrader, err := upgrade.NewUpgrader(log, cfg.Settings.DownloadConfig, cfg.Settings.Upgrade, agentInfo, new(upgrade.AgentWatcherHelper))
133132
if err != nil {
134133
return nil, nil, nil, fmt.Errorf("failed to create upgrader: %w", err)
135134
}
136-
monitor := monitoring.New(isMonitoringSupported, cfg.Settings.DownloadConfig.OS(), cfg.Settings.MonitoringConfig, agentInfo, isOtelSubprocessExecution)
135+
monitor := monitoring.New(isMonitoringSupported, cfg.Settings.DownloadConfig.OS(), cfg.Settings.MonitoringConfig, agentInfo, isOtelExecModeSubprocess)
137136

138137
runtime, err := runtime.NewManager(
139138
log,

internal/pkg/agent/cmd/inspect.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/elastic/elastic-agent/internal/pkg/config/operations"
3131
"github.com/elastic/elastic-agent/internal/pkg/diagnostics"
3232
otelconfig "github.com/elastic/elastic-agent/internal/pkg/otel/config"
33+
"github.com/elastic/elastic-agent/internal/pkg/otel/manager"
3334
"github.com/elastic/elastic-agent/pkg/component"
3435
"github.com/elastic/elastic-agent/pkg/core/logger"
3536
"github.com/elastic/elastic-agent/pkg/utils"
@@ -409,8 +410,9 @@ func getMonitoringFn(ctx context.Context, logger *logger.Logger, cfg map[string]
409410
if err != nil {
410411
return nil, fmt.Errorf("could not load agent info: %w", err)
411412
}
412-
otelconfig.SetExecutionModeFromConfig(logger, config)
413-
monitor := monitoring.New(agentCfg.Settings.V1MonitoringEnabled, agentCfg.Settings.DownloadConfig.OS(), agentCfg.Settings.MonitoringConfig, agentInfo, otelconfig.IsSubprocessExecution())
413+
otelExecMode := otelconfig.GetExecutionModeFromConfig(logger, config)
414+
isOtelExecModeSubprocess := otelExecMode == manager.SubprocessExecutionMode
415+
monitor := monitoring.New(agentCfg.Settings.V1MonitoringEnabled, agentCfg.Settings.DownloadConfig.OS(), agentCfg.Settings.MonitoringConfig, agentInfo, isOtelExecModeSubprocess)
414416
return monitor.MonitoringConfig, nil
415417
}
416418

internal/pkg/otel/config/config.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,37 @@ import (
1010
"github.com/elastic/elastic-agent/internal/pkg/otel/manager"
1111
)
1212

13-
var executionMode = manager.EmbeddedExecutionMode
13+
const defaultExecMode = manager.EmbeddedExecutionMode
1414

1515
type execModeConfig struct {
1616
Agent struct {
1717
Features struct {
1818
Otel *struct {
19-
SubprocessExecution bool `json:"subprocess_execution" yaml:"subprocess_execution" config:"subprocess_execution"`
19+
SubprocessExecution *bool `json:"subprocess_execution,omitempty" yaml:"subprocess_execution,omitempty" config:"subprocess_execution,omitempty"`
2020
} `json:"otel,omitempty" yaml:"otel,omitempty" config:"otel,omitempty"`
2121
} `json:"features" yaml:"features" config:"features"`
2222
} `json:"agent" yaml:"agent" config:"agent"`
2323
}
2424

25-
// SetExecutionModeFromConfig sets the execution mode of the OTel runtime based on the config.
26-
func SetExecutionModeFromConfig(log *logp.Logger, conf *config.Config) {
25+
// GetExecutionModeFromConfig returns the execution mode of the OTel runtime based on the config.
26+
func GetExecutionModeFromConfig(log *logp.Logger, conf *config.Config) manager.ExecutionMode {
2727
var c execModeConfig
2828
if err := conf.UnpackTo(&c); err != nil {
29-
log.Warnf("failed to unpack config in otel init execution mode: %v", err)
30-
return
29+
log.Warnf("failed to unpack config when getting otel runtime execution mode: %v", err)
30+
return defaultExecMode
3131
}
3232

33-
if c.Agent.Features.Otel != nil && c.Agent.Features.Otel.SubprocessExecution {
34-
executionMode = manager.SubprocessExecutionMode
35-
} else {
36-
executionMode = manager.EmbeddedExecutionMode
33+
if c.Agent.Features.Otel == nil {
34+
return defaultExecMode
3735
}
38-
}
3936

40-
// GetExecutionMode returns the execution mode of the OTel runtime.
41-
func GetExecutionMode() manager.ExecutionMode {
42-
return executionMode
43-
}
37+
if c.Agent.Features.Otel.SubprocessExecution == nil {
38+
return defaultExecMode
39+
}
4440

45-
// IsSubprocessExecution returns true if the OTel runtime is running in subprocess mode.
46-
func IsSubprocessExecution() bool {
47-
return executionMode == manager.SubprocessExecutionMode
41+
if *c.Agent.Features.Otel.SubprocessExecution {
42+
return manager.SubprocessExecutionMode
43+
} else {
44+
return manager.EmbeddedExecutionMode
45+
}
4846
}

0 commit comments

Comments
 (0)