diff --git a/otelcli/config.go b/otelcli/config.go index 7d75af1..daffe79 100644 --- a/otelcli/config.go +++ b/otelcli/config.go @@ -23,34 +23,35 @@ const spanBgSockfilename = "otel-cli-background.sock" // DefaultConfig returns a Config with all defaults set. func DefaultConfig() Config { return Config{ - Endpoint: "", - Protocol: "", - Timeout: "1s", - Headers: map[string]string{}, - Insecure: false, - Blocking: false, - NoTlsVerify: false, - ServiceName: "otel-cli", - SpanName: "todo-generate-default-span-names", - Kind: "client", - Attributes: map[string]string{}, - TraceparentCarrierFile: "", - TraceparentIgnoreEnv: false, - TraceparentPrint: false, - TraceparentPrintExport: false, - TraceparentRequired: false, - BackgroundParentPollMs: 10, - BackgroundSockdir: "", - BackgroundWait: false, - SpanStartTime: "now", - SpanEndTime: "now", - EventName: "todo-generate-default-event-names", - EventTime: "now", - CfgFile: "", - Verbose: false, - Fail: false, - StatusCode: "unset", - StatusDescription: "", + Endpoint: "", + Protocol: "", + Timeout: "1s", + Headers: map[string]string{}, + Insecure: false, + Blocking: false, + NoTlsVerify: false, + ServiceName: "otel-cli", + SpanName: "todo-generate-default-span-names", + Kind: "client", + Attributes: map[string]string{}, + TraceparentCarrierFile: "", + TraceparentIgnoreEnv: false, + TraceparentPrint: false, + TraceparentPrintExport: false, + TraceparentRequired: false, + BackgroundParentPollMs: 10, + BackgroundSockdir: "", + BackgroundWait: false, + BackgroundSkipParentPidCheck: false, + SpanStartTime: "now", + SpanEndTime: "now", + EventName: "todo-generate-default-event-names", + EventTime: "now", + CfgFile: "", + Verbose: false, + Fail: false, + StatusCode: "unset", + StatusDescription: "", } } @@ -79,9 +80,10 @@ type Config struct { TraceparentPrintExport bool `json:"traceparent_print_export" env:"OTEL_CLI_EXPORT_TRACEPARENT"` TraceparentRequired bool `json:"traceparent_required" env:"OTEL_CLI_TRACEPARENT_REQUIRED"` - BackgroundParentPollMs int `json:"background_parent_poll_ms" env:""` - BackgroundSockdir string `json:"background_socket_directory" env:""` - BackgroundWait bool `json:"background_wait" env:""` + BackgroundParentPollMs int `json:"background_parent_poll_ms" env:""` + BackgroundSockdir string `json:"background_socket_directory" env:""` + BackgroundWait bool `json:"background_wait" env:""` + BackgroundSkipParentPidCheck bool `json:"background_skip_parent_pid_check"` SpanStartTime string `json:"span_start_time" env:""` SpanEndTime string `json:"span_end_time" env:""` @@ -196,6 +198,7 @@ func (c Config) ToStringMap() map[string]string { "background_parent_poll_ms": strconv.Itoa(c.BackgroundParentPollMs), "background_socket_directory": c.BackgroundSockdir, "background_wait": strconv.FormatBool(c.BackgroundWait), + "background_skip_pid_check": strconv.FormatBool(c.BackgroundSkipParentPidCheck), "span_start_time": c.SpanStartTime, "span_end_time": c.SpanEndTime, "event_name": c.EventName, @@ -331,6 +334,12 @@ func (c Config) WithBackgroundWait(with bool) Config { return c } +// WithBackgroundSkipParentPidCheck returns the config with BackgroundSkipParentPidCheck set to the provided value. +func (c Config) WithBackgroundSkipParentPidCheck(with bool) Config { + c.BackgroundSkipParentPidCheck = with + return c +} + // WithSpanStartTime returns the config with SpanStartTime set to the provided value. func (c Config) WithSpanStartTime(with string) Config { c.SpanStartTime = with diff --git a/otelcli/span_background.go b/otelcli/span_background.go index 6fb0057..6c8886c 100644 --- a/otelcli/span_background.go +++ b/otelcli/span_background.go @@ -50,6 +50,7 @@ func init() { spanBgCmd.Flags().StringVar(&config.BackgroundSockdir, "sockdir", defaults.BackgroundSockdir, "a directory where a socket can be placed safely") spanBgCmd.Flags().BoolVar(&config.BackgroundWait, "wait", defaults.BackgroundWait, "wait for background to be fully started and then return") + spanBgCmd.Flags().BoolVar(&config.BackgroundSkipParentPidCheck, "skip-pid-check", defaults.BackgroundSkipParentPidCheck, "disable checking parent pid") addCommonParams(spanBgCmd) addSpanParams(spanBgCmd) @@ -99,20 +100,21 @@ func doSpanBackground(cmd *cobra.Command, args []string) { // when the parent is gone. the most straightforward approach that should // be fine on most Unix-ish operating systems is to poll getppid and exit // when the parent process pid changes - // TODO: make this configurable? - ppid := os.Getppid() - go func() { - for { - time.Sleep(time.Duration(config.BackgroundParentPollMs) * time.Millisecond) - - // check if the parent process has changed, exit when it does - cppid := os.Getppid() - if cppid != ppid { - spanBgEndEvent("parent_exited", span) - bgs.Shutdown() + if !config.BackgroundSkipParentPidCheck { + ppid := os.Getppid() + go func() { + for { + time.Sleep(time.Duration(config.BackgroundParentPollMs) * time.Millisecond) + + // check if the parent process has changed, exit when it does + cppid := os.Getppid() + if cppid != ppid { + spanBgEndEvent("parent_exited", span) + bgs.Shutdown() + } } - } - }() + }() + } // start the timeout goroutine, this is a little late but the server // has to be up for this to make much sense