-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
cmd-exec-id
to user agent (#1808)
## Changes This PR adds the `cmd-exec-id` field to the user agent. This allows us to correlate multiple HTTP requests made from the CLI. ### Why Not Use HTTP traceparent? We considered using the traceparent header in HTTP as an alternative, but it's not a good fit for our use case. Here's why: 1. Purpose of traceparent: It's designed to trace a single HTTP request across a distributed system as it moves through subsystems and proxies. 2. Our requirement: We need to trace multiple HTTP requests made during a single command execution in the CLI. For more details about how traceparent itself works and how it's used in the Go SDK, see databricks/databricks-sdk-go#914. ## Tests Unit test
- Loading branch information
1 parent
2bbdd04
commit f3bf33d
Showing
4 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package root | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/databricks/databricks-sdk-go/useragent" | ||
"github.com/google/uuid" | ||
) | ||
|
||
func withCommandExecIdInUserAgent(ctx context.Context) context.Context { | ||
// A UUID that will allow us to correlate multiple API requests made by | ||
// the same CLI invocation. | ||
return useragent.InContext(ctx, "cmd-exec-id", uuid.New().String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package root | ||
|
||
import ( | ||
"context" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/databricks/databricks-sdk-go/useragent" | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestWithCommandExecIdInUserAgent(t *testing.T) { | ||
ctx := withCommandExecIdInUserAgent(context.Background()) | ||
|
||
// Check that the command exec ID is in the user agent string. | ||
ua := useragent.FromContext(ctx) | ||
re := regexp.MustCompile(`cmd-exec-id/([a-f0-9-]+)`) | ||
matches := re.FindAllStringSubmatch(ua, -1) | ||
|
||
// Assert that we have exactly one match and that it's a valid UUID. | ||
require.Len(t, matches, 1) | ||
_, err := uuid.Parse(matches[0][1]) | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters