-
Notifications
You must be signed in to change notification settings - Fork 1
feat: rename config flag to observe-config, use default otel command for our start command #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,8 +12,6 @@ import ( | |
"github.com/observeinc/observe-agent/internal/root" | ||
"github.com/observeinc/observe-agent/observecol" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
collector "go.opentelemetry.io/collector/otelcol" | ||
) | ||
|
||
func SetupAndGetConfigFiles(ctx context.Context) ([]string, func(), error) { | ||
|
@@ -42,46 +40,38 @@ func DefaultLoggerCtx() context.Context { | |
return logger.WithCtx(context.Background(), logger.Get()) | ||
} | ||
|
||
func SetupAndGenerateCollectorSettings(ctx context.Context) (*collector.CollectorSettings, func(), error) { | ||
configFilePaths, cleanup, err := SetupAndGetConfigFiles(ctx) | ||
if err != nil { | ||
return nil, cleanup, err | ||
} | ||
// Generate collector settings with all config files | ||
colSettings := observecol.GenerateCollectorSettings(configFilePaths) | ||
return colSettings, cleanup, nil | ||
} | ||
func MakeStartCommand() *cobra.Command { | ||
// Create the start command from the otel collector command | ||
settings := observecol.GenerateCollectorSettings() | ||
otleCmd := observecol.GetOtelCollectorCommand(settings) | ||
otleCmd.Use = "start" | ||
otleCmd.Short = "Start the Observe agent process." | ||
otleCmd.Long = `The Observe agent is based on the OpenTelemetry Collector. | ||
This command reads in the local config and env vars and starts the | ||
collector on the current host.` | ||
// Drop the sub commands | ||
otleCmd.ResetCommands() | ||
|
||
var startCmd = &cobra.Command{ | ||
Use: "start", | ||
Short: "Start the Observe agent process.", | ||
Long: `The Observe agent is based on the OpenTelemetry Collector. | ||
This command reads in the local config and env vars and starts the | ||
collector on the current host.`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
colSettings, cleanup, err := SetupAndGenerateCollectorSettings(DefaultLoggerCtx()) | ||
// Modify the run function so we can pass in our packaged config files. | ||
originalRunE := otleCmd.RunE | ||
otleCmd.RunE = func(cmd *cobra.Command, args []string) error { | ||
configFilePaths, cleanup, err := SetupAndGetConfigFiles(DefaultLoggerCtx()) | ||
if cleanup != nil { | ||
defer cleanup() | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
if cleanup != nil { | ||
defer cleanup() | ||
configFlag := otleCmd.Flags().Lookup("config") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The flags for the otel collector are hidden behind private implementations of interfaces, args passed by value, and function scope locals. It's really not possible to access and set them directly if you aren't passing args to the cmd flags. Because of this, I think using the otelcol command itself for our |
||
for _, path := range configFilePaths { | ||
configFlag.Value.Set(path) | ||
} | ||
otelCmd := observecol.GetOtelCollectorCommand(colSettings) | ||
return otelCmd.RunE(cmd, args) | ||
}, | ||
return originalRunE(cmd, args) | ||
} | ||
return otleCmd | ||
} | ||
|
||
func init() { | ||
startCmd.PersistentFlags().String("otel-config", "", "Path to additional otel configuration file") | ||
viper.BindPFlag("otelConfigFile", startCmd.PersistentFlags().Lookup("otel-config")) | ||
startCmd := MakeStartCommand() | ||
root.RootCmd.AddCommand(startCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
// startCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,41 +10,44 @@ import ( | |
|
||
"github.com/observeinc/observe-agent/internal/commands/start" | ||
"github.com/observeinc/observe-agent/internal/root" | ||
"github.com/observeinc/observe-agent/observecol" | ||
"go.opentelemetry.io/collector/otelcol" | ||
"golang.org/x/sys/windows" | ||
"golang.org/x/sys/windows/svc" | ||
) | ||
|
||
func run() error { | ||
inService, err := svc.IsWindowsService() | ||
if err != nil { | ||
// If we're not running as a service, we run normally. | ||
if inService, err := svc.IsWindowsService(); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I reorganized this method so it now looks more like the default OCB method. The diff for this condition is just code motion. |
||
log.Fatalf("failed to determine if we are running in service: %v", err) | ||
} else if !inService { | ||
return runInteractive() | ||
} | ||
|
||
if inService { | ||
if len(os.Args) != 2 { | ||
log.Fatal("Expected to run svc as: observe-agent.exe <path to observe-agent.yaml>") | ||
} | ||
root.CfgFile = os.Args[1] | ||
root.InitConfig() | ||
colSettings, cleanup, err := start.SetupAndGenerateCollectorSettings(start.DefaultLoggerCtx()) | ||
if err != nil { | ||
return err | ||
} | ||
if cleanup != nil { | ||
defer cleanup() | ||
} | ||
if err := svc.Run("", otelcol.NewSvcHandler(*colSettings)); err != nil { | ||
if errors.Is(err, windows.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) { | ||
// Per https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-startservicectrldispatchera#return-value | ||
// this means that the process is not running as a service, so run interactively. | ||
return runInteractive() | ||
} | ||
|
||
return fmt.Errorf("failed to start collector server: %w", err) | ||
if len(os.Args) != 2 { | ||
log.Fatal("Expected to run svc as: observe-agent.exe <path to observe-agent.yaml>") | ||
} | ||
root.CfgFile = os.Args[1] | ||
root.InitConfig() | ||
|
||
// Get the collector settings along with our bundled config files. | ||
configFilePaths, cleanup, err := start.SetupAndGetConfigFiles(start.DefaultLoggerCtx()) | ||
if cleanup != nil { | ||
defer cleanup() | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
colSettings := observecol.GenerateCollectorSettingsWithConfigFiles(configFilePaths) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NB: Generating the settings this way instead of using the otel command means it won't work with |
||
|
||
if err := svc.Run("", otelcol.NewSvcHandler(*colSettings)); err != nil { | ||
if errors.Is(err, windows.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) { | ||
// Per https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-startservicectrldispatchera#return-value | ||
// this means that the process is not running as a service, so run interactively. | ||
return runInteractive() | ||
} | ||
} else { | ||
return runInteractive() | ||
|
||
return fmt.Errorf("failed to start collector server: %w", err) | ||
} | ||
|
||
return nil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,15 +14,7 @@ import ( | |
"go.opentelemetry.io/collector/otelcol" | ||
) | ||
|
||
func makeMapProvidersMap(providers ...confmap.Provider) map[string]confmap.Provider { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was unused. |
||
ret := make(map[string]confmap.Provider, len(providers)) | ||
for _, provider := range providers { | ||
ret[provider.Scheme()] = provider | ||
} | ||
return ret | ||
} | ||
|
||
func GenerateCollectorSettings(URIs []string) *otelcol.CollectorSettings { | ||
func GenerateCollectorSettings() *otelcol.CollectorSettings { | ||
buildInfo := component.BuildInfo{ | ||
Command: "observe-agent", | ||
Description: "Observe Distribution of Opentelemetry Collector", | ||
|
@@ -33,7 +25,6 @@ func GenerateCollectorSettings(URIs []string) *otelcol.CollectorSettings { | |
Factories: components, | ||
ConfigProviderSettings: otelcol.ConfigProviderSettings{ | ||
ResolverSettings: confmap.ResolverSettings{ | ||
URIs: URIs, | ||
ProviderFactories: []confmap.ProviderFactory{ | ||
fileprovider.NewFactory(), | ||
envprovider.NewFactory(), | ||
|
@@ -47,6 +38,12 @@ func GenerateCollectorSettings(URIs []string) *otelcol.CollectorSettings { | |
return set | ||
} | ||
|
||
func GenerateCollectorSettingsWithConfigFiles(configFiles []string) *otelcol.CollectorSettings { | ||
set := GenerateCollectorSettings() | ||
set.ConfigProviderSettings.ResolverSettings.URIs = configFiles | ||
return set | ||
} | ||
|
||
func GetOtelCollectorCommand(otelconfig *otelcol.CollectorSettings) *cobra.Command { | ||
cmd := otelcol.NewCommand(*otelconfig) | ||
return cmd | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Diagnose won't run against otel config overrides, but we never supported this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you make a ticket to support the new
--observe-config
flag indiagnose
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created https://observe.atlassian.net/browse/OB-40550