address comments

This commit is contained in:
Shreyas Goenka 2024-10-31 11:16:14 +01:00
parent 11dad47dd6
commit 941958cad9
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
5 changed files with 51 additions and 22 deletions

View File

@ -76,6 +76,7 @@ func New(ctx context.Context) *cobra.Command {
// Configure our user agent with the command that's about to be executed.
ctx = withCommandInUserAgent(ctx, cmd)
ctx = withUpstreamInUserAgent(ctx)
ctx = withCommandExecIdInUserAgent(ctx)
cmd.SetContext(ctx)
return nil
}

View File

@ -0,0 +1,16 @@
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'll will allow use to correlate multiple API requests made by
// the same command invocation.
// When we add telemetry to the CLI, this exec ID will allow allow us to
// correlate logs in HTTP access logs with logs in Frontend logs.
return useragent.InContext(ctx, "cmd-exec-id", uuid.New().String())
}

View File

@ -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 trace 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)
}

View File

@ -1,13 +1,15 @@
package root
import (
"context"
"testing"
"github.com/databricks/databricks-sdk-go/useragent"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)
func TestCommandString(t *testing.T) {
func TestWithCommandInUserAgent(t *testing.T) {
root := &cobra.Command{
Use: "root",
}
@ -26,4 +28,9 @@ func TestCommandString(t *testing.T) {
assert.Equal(t, "root", commandString(root))
assert.Equal(t, "hello", commandString(hello))
assert.Equal(t, "hello_world", commandString(world))
ctx := withCommandInUserAgent(context.Background(), world)
ua := useragent.FromContext(ctx)
assert.Contains(t, ua, "cmd/hello_world")
}

View File

@ -2,14 +2,10 @@ package root
import (
"context"
"regexp"
"testing"
"github.com/databricks/databricks-sdk-go/useragent"
"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestUpstreamSet(t *testing.T) {
@ -47,20 +43,3 @@ func TestUpstreamVersionSetUpstreamNotSet(t *testing.T) {
assert.NotContains(t, useragent.FromContext(ctx), "upstream/")
assert.NotContains(t, useragent.FromContext(ctx), "upstream-version/")
}
func TestWithCommandInUserAgent(t *testing.T) {
ctx := withCommandInUserAgent(context.Background(), &cobra.Command{Use: "foo"})
// Check that the command name is in the user agent string.
ua := useragent.FromContext(ctx)
assert.Contains(t, ua, "cmd/foo")
// Check that the command trace ID is in the user agent string.
re := regexp.MustCompile(`command-trace-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)
}