diff options
author | Stan Hu <stanhu@gmail.com> | 2020-09-19 03:34:49 -0700 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2020-09-19 14:00:45 -0700 |
commit | 0590d9198f653ff2170e0f26790056bef4f056fe (patch) | |
tree | dc0d68866ea16ba4f74d441c3aa2048b12fb9e95 /internal/command/personalaccesstoken | |
parent | f100e7e83943b3bb5db232f5bf79a616fdba88f1 (diff) | |
download | gitlab-shell-sh-extract-context-from-env.tar.gz |
Make it possible to propagate correlation ID across processessh-extract-context-from-env
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
Diffstat (limited to 'internal/command/personalaccesstoken')
-rw-r--r-- | internal/command/personalaccesstoken/personalaccesstoken.go | 9 | ||||
-rw-r--r-- | internal/command/personalaccesstoken/personalaccesstoken_test.go | 3 |
2 files changed, 7 insertions, 5 deletions
diff --git a/internal/command/personalaccesstoken/personalaccesstoken.go b/internal/command/personalaccesstoken/personalaccesstoken.go index b283890..6f3d03e 100644 --- a/internal/command/personalaccesstoken/personalaccesstoken.go +++ b/internal/command/personalaccesstoken/personalaccesstoken.go @@ -1,6 +1,7 @@ package personalaccesstoken import ( + "context" "errors" "fmt" "strconv" @@ -31,13 +32,13 @@ type tokenArgs struct { ExpiresDate string // Calculated, a TTL is passed from command-line. } -func (c *Command) Execute() error { +func (c *Command) Execute(ctx context.Context) error { err := c.parseTokenArgs() if err != nil { return err } - response, err := c.getPersonalAccessToken() + response, err := c.getPersonalAccessToken(ctx) if err != nil { return err } @@ -76,11 +77,11 @@ func (c *Command) parseTokenArgs() error { return nil } -func (c *Command) getPersonalAccessToken() (*personalaccesstoken.Response, error) { +func (c *Command) getPersonalAccessToken(ctx context.Context) (*personalaccesstoken.Response, error) { client, err := personalaccesstoken.NewClient(c.Config) if err != nil { return nil, err } - return client.GetPersonalAccessToken(c.Args, c.TokenArgs.Name, &c.TokenArgs.Scopes, c.TokenArgs.ExpiresDate) + return client.GetPersonalAccessToken(ctx, c.Args, c.TokenArgs.Name, &c.TokenArgs.Scopes, c.TokenArgs.ExpiresDate) } diff --git a/internal/command/personalaccesstoken/personalaccesstoken_test.go b/internal/command/personalaccesstoken/personalaccesstoken_test.go index bc748ab..5970142 100644 --- a/internal/command/personalaccesstoken/personalaccesstoken_test.go +++ b/internal/command/personalaccesstoken/personalaccesstoken_test.go @@ -2,6 +2,7 @@ package personalaccesstoken import ( "bytes" + "context" "encoding/json" "io/ioutil" "net/http" @@ -170,7 +171,7 @@ func TestExecute(t *testing.T) { ReadWriter: &readwriter.ReadWriter{Out: output, In: input}, } - err := cmd.Execute() + err := cmd.Execute(context.Background()) if tc.expectedError == "" { assert.NoError(t, err) |