Skip to content
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

Make configurable skipping pid check in span background command #161

Merged
merged 1 commit into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 40 additions & 31 deletions otelcli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
}
}

Expand Down Expand Up @@ -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:""`
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
28 changes: 15 additions & 13 deletions otelcli/span_background.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down