From 0590d9198f653ff2170e0f26790056bef4f056fe Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Sat, 19 Sep 2020 03:34:49 -0700 Subject: Make it possible to propagate correlation ID across processes Previously, gitlab-shell did not pass a context through the application. Correlation IDs were generated down the call stack, since we don't pass the context around from the start execution. This has several potential downsides: 1. It's easier for programming mistakes to be made in future which lead to multiple correlation IDs being generated for a single request. 2. Correlation IDs cannot be passed in from upstream requests 3. Other advantages of context passing, such as distributed tracing is not possible. This commit changes the behavior: 1. Extract the correlation ID from the environment at the start of the application. 2. If no correlation ID exists, generate a random one. 3. Pass the correlation ID to the GitLabNet API requests. This change also enables other clients of GitLabNet (e.g. Gitaly) to pass along the correlation ID in the internal API requests (https://gitlab.com/gitlab-org/gitaly/-/issues/2725). Fixes https://gitlab.com/gitlab-org/gitlab-shell/-/issues/474 --- cmd/check/main.go | 5 ++++- cmd/gitlab-shell-authorized-keys-check/main.go | 5 ++++- cmd/gitlab-shell-authorized-principals-check/main.go | 5 ++++- cmd/gitlab-shell/main.go | 5 ++++- 4 files changed, 16 insertions(+), 4 deletions(-) (limited to 'cmd') diff --git a/cmd/check/main.go b/cmd/check/main.go index e88b9fe..28634f4 100644 --- a/cmd/check/main.go +++ b/cmd/check/main.go @@ -38,7 +38,10 @@ func main() { os.Exit(1) } - if err = cmd.Execute(); err != nil { + ctx, finished := command.ContextWithCorrelationID() + defer finished() + + if err = cmd.Execute(ctx); err != nil { fmt.Fprintf(readWriter.ErrOut, "%v\n", err) os.Exit(1) } diff --git a/cmd/gitlab-shell-authorized-keys-check/main.go b/cmd/gitlab-shell-authorized-keys-check/main.go index 4b3949c..3a7dcbb 100644 --- a/cmd/gitlab-shell-authorized-keys-check/main.go +++ b/cmd/gitlab-shell-authorized-keys-check/main.go @@ -41,7 +41,10 @@ func main() { os.Exit(1) } - if err = cmd.Execute(); err != nil { + ctx, finished := command.ContextWithCorrelationID() + defer finished() + + if err = cmd.Execute(ctx); err != nil { console.DisplayWarningMessage(err.Error(), readWriter.ErrOut) os.Exit(1) } diff --git a/cmd/gitlab-shell-authorized-principals-check/main.go b/cmd/gitlab-shell-authorized-principals-check/main.go index fc46180..ea8d140 100644 --- a/cmd/gitlab-shell-authorized-principals-check/main.go +++ b/cmd/gitlab-shell-authorized-principals-check/main.go @@ -41,7 +41,10 @@ func main() { os.Exit(1) } - if err = cmd.Execute(); err != nil { + ctx, finished := command.ContextWithCorrelationID() + defer finished() + + if err = cmd.Execute(ctx); err != nil { console.DisplayWarningMessage(err.Error(), readWriter.ErrOut) os.Exit(1) } diff --git a/cmd/gitlab-shell/main.go b/cmd/gitlab-shell/main.go index 8df781c..763aa5e 100644 --- a/cmd/gitlab-shell/main.go +++ b/cmd/gitlab-shell/main.go @@ -41,7 +41,10 @@ func main() { os.Exit(1) } - if err = cmd.Execute(); err != nil { + ctx, finished := command.ContextWithCorrelationID() + defer finished() + + if err = cmd.Execute(ctx); err != nil { console.DisplayWarningMessage(err.Error(), readWriter.ErrOut) os.Exit(1) } -- cgit v1.2.1