mirror of https://github.com/databricks/cli.git
address comments
This commit is contained in:
parent
11dad47dd6
commit
941958cad9
|
@ -76,6 +76,7 @@ func New(ctx context.Context) *cobra.Command {
|
||||||
// Configure our user agent with the command that's about to be executed.
|
// Configure our user agent with the command that's about to be executed.
|
||||||
ctx = withCommandInUserAgent(ctx, cmd)
|
ctx = withCommandInUserAgent(ctx, cmd)
|
||||||
ctx = withUpstreamInUserAgent(ctx)
|
ctx = withUpstreamInUserAgent(ctx)
|
||||||
|
ctx = withCommandExecIdInUserAgent(ctx)
|
||||||
cmd.SetContext(ctx)
|
cmd.SetContext(ctx)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -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())
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
|
@ -1,13 +1,15 @@
|
||||||
package root
|
package root
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/databricks/databricks-sdk-go/useragent"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCommandString(t *testing.T) {
|
func TestWithCommandInUserAgent(t *testing.T) {
|
||||||
root := &cobra.Command{
|
root := &cobra.Command{
|
||||||
Use: "root",
|
Use: "root",
|
||||||
}
|
}
|
||||||
|
@ -26,4 +28,9 @@ func TestCommandString(t *testing.T) {
|
||||||
assert.Equal(t, "root", commandString(root))
|
assert.Equal(t, "root", commandString(root))
|
||||||
assert.Equal(t, "hello", commandString(hello))
|
assert.Equal(t, "hello", commandString(hello))
|
||||||
assert.Equal(t, "hello_world", commandString(world))
|
assert.Equal(t, "hello_world", commandString(world))
|
||||||
|
|
||||||
|
ctx := withCommandInUserAgent(context.Background(), world)
|
||||||
|
|
||||||
|
ua := useragent.FromContext(ctx)
|
||||||
|
assert.Contains(t, ua, "cmd/hello_world")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,14 +2,10 @@ package root
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"regexp"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/databricks/databricks-sdk-go/useragent"
|
"github.com/databricks/databricks-sdk-go/useragent"
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUpstreamSet(t *testing.T) {
|
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/")
|
||||||
assert.NotContains(t, useragent.FromContext(ctx), "upstream-version/")
|
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)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue