Skip to content

Commit afe00b5

Browse files
feat: rename config flag to observe-config, use default otel command for our start command (#146)
### Description OB-38651 Rename `--config` flag to `--observe-config` and use the default OTel collector command for our start command. This adds all `otelcol` flags to our `start` command and gives us full functionality for the new `--config` flag (which specifies OTel configs). ### Checklist - [ ] Created tests which fail without the change (if possible) - [ ] Extended the README / documentation, if necessary
1 parent efdb121 commit afe00b5

File tree

7 files changed

+68
-80
lines changed

7 files changed

+68
-80
lines changed

internal/commands/diagnose/otelconfigcheck.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/observeinc/observe-agent/internal/commands/start"
88
logger "github.com/observeinc/observe-agent/internal/commands/util"
9+
"github.com/observeinc/observe-agent/observecol"
910
"github.com/spf13/viper"
1011
"go.opentelemetry.io/collector/otelcol"
1112
)
@@ -16,13 +17,14 @@ type OtelConfigTestResult struct {
1617
}
1718

1819
func checkOtelConfig(_ *viper.Viper) (bool, any, error) {
19-
colSettings, cleanup, err := start.SetupAndGenerateCollectorSettings(logger.WithCtx(context.Background(), logger.GetNop()))
20-
if err != nil {
21-
return false, nil, err
22-
}
20+
configFilePaths, cleanup, err := start.SetupAndGetConfigFiles(logger.WithCtx(context.Background(), logger.GetNop()))
2321
if cleanup != nil {
2422
defer cleanup()
2523
}
24+
if err != nil {
25+
return false, nil, err
26+
}
27+
colSettings := observecol.GenerateCollectorSettingsWithConfigFiles(configFilePaths)
2628
// These are the same checks as the `otelcol validate` command:
2729
// https://github.com/open-telemetry/opentelemetry-collector/blob/main/otelcol/command_validate.go
2830
col, err := otelcol.NewCollector(*colSettings)

internal/commands/start/start.go

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"github.com/observeinc/observe-agent/internal/root"
1313
"github.com/observeinc/observe-agent/observecol"
1414
"github.com/spf13/cobra"
15-
"github.com/spf13/viper"
16-
collector "go.opentelemetry.io/collector/otelcol"
1715
)
1816

1917
func SetupAndGetConfigFiles(ctx context.Context) ([]string, func(), error) {
@@ -42,46 +40,38 @@ func DefaultLoggerCtx() context.Context {
4240
return logger.WithCtx(context.Background(), logger.Get())
4341
}
4442

45-
func SetupAndGenerateCollectorSettings(ctx context.Context) (*collector.CollectorSettings, func(), error) {
46-
configFilePaths, cleanup, err := SetupAndGetConfigFiles(ctx)
47-
if err != nil {
48-
return nil, cleanup, err
49-
}
50-
// Generate collector settings with all config files
51-
colSettings := observecol.GenerateCollectorSettings(configFilePaths)
52-
return colSettings, cleanup, nil
53-
}
43+
func MakeStartCommand() *cobra.Command {
44+
// Create the start command from the otel collector command
45+
settings := observecol.GenerateCollectorSettings()
46+
otleCmd := observecol.GetOtelCollectorCommand(settings)
47+
otleCmd.Use = "start"
48+
otleCmd.Short = "Start the Observe agent process."
49+
otleCmd.Long = `The Observe agent is based on the OpenTelemetry Collector.
50+
This command reads in the local config and env vars and starts the
51+
collector on the current host.`
52+
// Drop the sub commands
53+
otleCmd.ResetCommands()
5454

55-
var startCmd = &cobra.Command{
56-
Use: "start",
57-
Short: "Start the Observe agent process.",
58-
Long: `The Observe agent is based on the OpenTelemetry Collector.
59-
This command reads in the local config and env vars and starts the
60-
collector on the current host.`,
61-
RunE: func(cmd *cobra.Command, args []string) error {
62-
colSettings, cleanup, err := SetupAndGenerateCollectorSettings(DefaultLoggerCtx())
55+
// Modify the run function so we can pass in our packaged config files.
56+
originalRunE := otleCmd.RunE
57+
otleCmd.RunE = func(cmd *cobra.Command, args []string) error {
58+
configFilePaths, cleanup, err := SetupAndGetConfigFiles(DefaultLoggerCtx())
59+
if cleanup != nil {
60+
defer cleanup()
61+
}
6362
if err != nil {
6463
return err
6564
}
66-
if cleanup != nil {
67-
defer cleanup()
65+
configFlag := otleCmd.Flags().Lookup("config")
66+
for _, path := range configFilePaths {
67+
configFlag.Value.Set(path)
6868
}
69-
otelCmd := observecol.GetOtelCollectorCommand(colSettings)
70-
return otelCmd.RunE(cmd, args)
71-
},
69+
return originalRunE(cmd, args)
70+
}
71+
return otleCmd
7272
}
7373

7474
func init() {
75-
startCmd.PersistentFlags().String("otel-config", "", "Path to additional otel configuration file")
76-
viper.BindPFlag("otelConfigFile", startCmd.PersistentFlags().Lookup("otel-config"))
75+
startCmd := MakeStartCommand()
7776
root.RootCmd.AddCommand(startCmd)
78-
79-
// Here you will define your flags and configuration settings.
80-
81-
// Cobra supports Persistent Flags which will work for this command
82-
// and all subcommands, e.g.:
83-
84-
// Cobra supports local flags which will only run when this command
85-
// is called directly, e.g.:
86-
// startCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
8777
}

internal/connections/confighandler.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ func GetAllOtelConfigFilePaths(ctx context.Context, tmpDir string) ([]string, er
4343
configFilePaths = append(configFilePaths, connectionPaths...)
4444
}
4545
}
46-
// Read in otel-config flag and add to paths if set
47-
if viper.IsSet("otelConfigFile") {
48-
configFilePaths = append(configFilePaths, viper.GetString("otelConfigFile"))
49-
}
5046
// Generate override file and include path if overrides provided
5147
if viper.IsSet(OTEL_OVERRIDE_YAML_KEY) {
5248
// GetStringMap is more lenient with respect to conversions than Sub, which only handles maps.

internal/root/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func Execute() {
3434
func init() {
3535
cobra.OnInitialize(InitConfig)
3636

37-
RootCmd.PersistentFlags().StringVar(&CfgFile, "config", "", "config file path")
37+
RootCmd.PersistentFlags().StringVar(&CfgFile, "observe-config", "", "observe-agent config file path")
3838
}
3939

4040
// InitConfig reads in config file and ENV variables if set.

main_windows.go

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,44 @@ import (
1010

1111
"github.com/observeinc/observe-agent/internal/commands/start"
1212
"github.com/observeinc/observe-agent/internal/root"
13+
"github.com/observeinc/observe-agent/observecol"
1314
"go.opentelemetry.io/collector/otelcol"
1415
"golang.org/x/sys/windows"
1516
"golang.org/x/sys/windows/svc"
1617
)
1718

1819
func run() error {
19-
inService, err := svc.IsWindowsService()
20-
if err != nil {
20+
// If we're not running as a service, we run normally.
21+
if inService, err := svc.IsWindowsService(); err != nil {
2122
log.Fatalf("failed to determine if we are running in service: %v", err)
23+
} else if !inService {
24+
return runInteractive()
2225
}
2326

24-
if inService {
25-
if len(os.Args) != 2 {
26-
log.Fatal("Expected to run svc as: observe-agent.exe <path to observe-agent.yaml>")
27-
}
28-
root.CfgFile = os.Args[1]
29-
root.InitConfig()
30-
colSettings, cleanup, err := start.SetupAndGenerateCollectorSettings(start.DefaultLoggerCtx())
31-
if err != nil {
32-
return err
33-
}
34-
if cleanup != nil {
35-
defer cleanup()
36-
}
37-
if err := svc.Run("", otelcol.NewSvcHandler(*colSettings)); err != nil {
38-
if errors.Is(err, windows.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
39-
// Per https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-startservicectrldispatchera#return-value
40-
// this means that the process is not running as a service, so run interactively.
41-
return runInteractive()
42-
}
43-
44-
return fmt.Errorf("failed to start collector server: %w", err)
27+
if len(os.Args) != 2 {
28+
log.Fatal("Expected to run svc as: observe-agent.exe <path to observe-agent.yaml>")
29+
}
30+
root.CfgFile = os.Args[1]
31+
root.InitConfig()
32+
33+
// Get the collector settings along with our bundled config files.
34+
configFilePaths, cleanup, err := start.SetupAndGetConfigFiles(start.DefaultLoggerCtx())
35+
if cleanup != nil {
36+
defer cleanup()
37+
}
38+
if err != nil {
39+
return err
40+
}
41+
colSettings := observecol.GenerateCollectorSettingsWithConfigFiles(configFilePaths)
42+
43+
if err := svc.Run("", otelcol.NewSvcHandler(*colSettings)); err != nil {
44+
if errors.Is(err, windows.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
45+
// Per https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-startservicectrldispatchera#return-value
46+
// this means that the process is not running as a service, so run interactively.
47+
return runInteractive()
4548
}
46-
} else {
47-
return runInteractive()
49+
50+
return fmt.Errorf("failed to start collector server: %w", err)
4851
}
4952

5053
return nil

observecol/otelcollector.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,7 @@ import (
1414
"go.opentelemetry.io/collector/otelcol"
1515
)
1616

17-
func makeMapProvidersMap(providers ...confmap.Provider) map[string]confmap.Provider {
18-
ret := make(map[string]confmap.Provider, len(providers))
19-
for _, provider := range providers {
20-
ret[provider.Scheme()] = provider
21-
}
22-
return ret
23-
}
24-
25-
func GenerateCollectorSettings(URIs []string) *otelcol.CollectorSettings {
17+
func GenerateCollectorSettings() *otelcol.CollectorSettings {
2618
buildInfo := component.BuildInfo{
2719
Command: "observe-agent",
2820
Description: "Observe Distribution of Opentelemetry Collector",
@@ -33,7 +25,6 @@ func GenerateCollectorSettings(URIs []string) *otelcol.CollectorSettings {
3325
Factories: components,
3426
ConfigProviderSettings: otelcol.ConfigProviderSettings{
3527
ResolverSettings: confmap.ResolverSettings{
36-
URIs: URIs,
3728
ProviderFactories: []confmap.ProviderFactory{
3829
fileprovider.NewFactory(),
3930
envprovider.NewFactory(),
@@ -47,6 +38,12 @@ func GenerateCollectorSettings(URIs []string) *otelcol.CollectorSettings {
4738
return set
4839
}
4940

41+
func GenerateCollectorSettingsWithConfigFiles(configFiles []string) *otelcol.CollectorSettings {
42+
set := GenerateCollectorSettings()
43+
set.ConfigProviderSettings.ResolverSettings.URIs = configFiles
44+
return set
45+
}
46+
5047
func GetOtelCollectorCommand(otelconfig *otelcol.CollectorSettings) *cobra.Command {
5148
cmd := otelcol.NewCommand(*otelconfig)
5249
return cmd

packaging/linux/config/observe-agent.service

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Description=Observe Agent
33
After=network.target
44

55
[Service]
6-
ExecStart=/usr/bin/observe-agent start --config /etc/observe-agent/observe-agent.yaml
6+
ExecStart=/usr/bin/observe-agent start --observe-config /etc/observe-agent/observe-agent.yaml
77
KillMode=mixed
88
Restart=on-failure
99
Type=simple

0 commit comments

Comments
 (0)