-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add logger for CLI telemetry #2083
Open
shreyas-goenka
wants to merge
1
commit into
main
Choose a base branch
from
add-telemetry-logger
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+603
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package telemetry_test | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/databricks/cli/integration/internal/acc" | ||
"github.com/databricks/cli/libs/telemetry" | ||
"github.com/databricks/cli/libs/telemetry/protos" | ||
"github.com/databricks/databricks-sdk-go/client" | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// Wrapper to capture the response from the API client since that's not directly | ||
// accessible from the logger. | ||
type apiClientWrapper struct { | ||
response *telemetry.ResponseBody | ||
apiClient *client.DatabricksClient | ||
} | ||
|
||
func (wrapper *apiClientWrapper) Do(ctx context.Context, method, path string, | ||
headers map[string]string, request, response any, | ||
visitors ...func(*http.Request) error, | ||
) error { | ||
err := wrapper.apiClient.Do(ctx, method, path, headers, request, response, visitors...) | ||
wrapper.response = response.(*telemetry.ResponseBody) | ||
return err | ||
} | ||
|
||
func TestTelemetryLogger(t *testing.T) { | ||
events := []telemetry.DatabricksCliLog{ | ||
{ | ||
CliTestEvent: &protos.CliTestEvent{ | ||
Name: protos.DummyCliEnumValue1, | ||
}, | ||
}, | ||
{ | ||
BundleInitEvent: &protos.BundleInitEvent{ | ||
Uuid: uuid.New().String(), | ||
TemplateName: "abc", | ||
TemplateEnumArgs: []protos.BundleInitTemplateEnumArg{ | ||
{ | ||
Key: "a", | ||
Value: "b", | ||
}, | ||
{ | ||
Key: "c", | ||
Value: "d", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
assert.Equal(t, len(events), reflect.TypeOf(telemetry.DatabricksCliLog{}).NumField(), | ||
"Number of events should match the number of fields in DatabricksCliLog. Please add a new event to this test.") | ||
|
||
ctx, w := acc.WorkspaceTest(t) | ||
ctx = telemetry.WithDefaultLogger(ctx) | ||
|
||
// Extend the maximum wait time for the telemetry flush just for this test. | ||
oldV := telemetry.MaxAdditionalWaitTime | ||
telemetry.MaxAdditionalWaitTime = 1 * time.Hour | ||
t.Cleanup(func() { | ||
telemetry.MaxAdditionalWaitTime = oldV | ||
}) | ||
|
||
for _, event := range events { | ||
telemetry.Log(ctx, event) | ||
} | ||
|
||
apiClient, err := client.New(w.W.Config) | ||
require.NoError(t, err) | ||
|
||
// Flush the events. | ||
wrapper := &apiClientWrapper{ | ||
apiClient: apiClient, | ||
} | ||
telemetry.Flush(ctx, wrapper) | ||
|
||
// Assert that the events were logged. | ||
assert.Equal(t, telemetry.ResponseBody{ | ||
NumProtoSuccess: int64(len(events)), | ||
Errors: []telemetry.LogError{}, | ||
}, *wrapper.response) | ||
} |
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,19 @@ | ||
package telemetry | ||
|
||
// RequestBody is the request body type bindings for the /telemetry-ext API endpoint. | ||
type RequestBody struct { | ||
UploadTime int64 `json:"uploadTime"` | ||
Items []string `json:"items"` | ||
ProtoLogs []string `json:"protoLogs"` | ||
} | ||
|
||
// ResponseBody is the response body type bindings for the /telemetry-ext API endpoint. | ||
type ResponseBody struct { | ||
Errors []LogError `json:"errors"` | ||
NumProtoSuccess int64 `json:"numProtoSuccess"` | ||
} | ||
|
||
type LogError struct { | ||
Message string `json:"message"` | ||
ErrorType string `json:"ErrorType"` | ||
} |
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,62 @@ | ||
package telemetry | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
// Private type to store the telemetry logger in the context | ||
type telemetryLogger int | ||
|
||
// Key to store the telemetry logger in the context | ||
var telemetryLoggerKey telemetryLogger | ||
|
||
func WithDefaultLogger(ctx context.Context) context.Context { | ||
v := ctx.Value(telemetryLoggerKey) | ||
|
||
// If no logger is set in the context, set the default logger. | ||
if v == nil { | ||
nctx := context.WithValue(ctx, telemetryLoggerKey, &defaultLogger{}) | ||
return nctx | ||
} | ||
|
||
switch v.(type) { | ||
case *defaultLogger: | ||
panic(fmt.Errorf("default telemetry logger already set in the context: %T", v)) | ||
case *mockLogger: | ||
// Do nothing. Unit and integration tests set the mock logger in the context | ||
// to avoid making actual API calls. Thus WithDefaultLogger should silently | ||
// ignore the mock logger. | ||
default: | ||
panic(fmt.Errorf("unexpected telemetry logger type: %T", v)) | ||
} | ||
|
||
return ctx | ||
} | ||
|
||
// WithMockLogger sets a mock telemetry logger in the context. It overrides the | ||
// default logger if it is already set in the context. | ||
func WithMockLogger(ctx context.Context) context.Context { | ||
v := ctx.Value(telemetryLoggerKey) | ||
if v != nil { | ||
panic(fmt.Errorf("telemetry logger already set in the context: %T", v)) | ||
} | ||
|
||
return context.WithValue(ctx, telemetryLoggerKey, &mockLogger{}) | ||
} | ||
|
||
func fromContext(ctx context.Context) Logger { | ||
v := ctx.Value(telemetryLoggerKey) | ||
if v == nil { | ||
panic(fmt.Errorf("telemetry logger not found in the context")) | ||
} | ||
|
||
switch vv := v.(type) { | ||
case *defaultLogger: | ||
return vv | ||
case *mockLogger: | ||
return vv | ||
default: | ||
panic(fmt.Errorf("unexpected telemetry logger type: %T", v)) | ||
} | ||
} |
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,77 @@ | ||
package telemetry | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWithDefaultLogger(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
// No default logger set | ||
ctx1 := WithDefaultLogger(ctx) | ||
assert.Equal(t, &defaultLogger{}, ctx1.Value(telemetryLoggerKey)) | ||
|
||
// Default logger already set | ||
assert.PanicsWithError(t, "default telemetry logger already set in the context: *telemetry.defaultLogger", func() { | ||
WithDefaultLogger(ctx1) | ||
}) | ||
|
||
// Mock logger already set | ||
ctx2 := WithMockLogger(ctx) | ||
assert.NotPanics(t, func() { | ||
WithDefaultLogger(ctx2) | ||
}) | ||
|
||
// Unexpected logger type | ||
type foobar struct{} | ||
ctx3 := context.WithValue(ctx, telemetryLoggerKey, &foobar{}) | ||
assert.PanicsWithError(t, "unexpected telemetry logger type: *telemetry.foobar", func() { | ||
WithDefaultLogger(ctx3) | ||
}) | ||
} | ||
|
||
func TestWithMockLogger(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
// No logger set | ||
ctx1 := WithMockLogger(ctx) | ||
assert.Equal(t, &mockLogger{}, ctx1.Value(telemetryLoggerKey)) | ||
|
||
// Logger already set | ||
assert.PanicsWithError(t, "telemetry logger already set in the context: *telemetry.mockLogger", func() { | ||
WithMockLogger(ctx1) | ||
}) | ||
|
||
// Default logger already set | ||
ctx2 := WithDefaultLogger(ctx) | ||
assert.PanicsWithError(t, "telemetry logger already set in the context: *telemetry.defaultLogger", func() { | ||
WithMockLogger(ctx2) | ||
}) | ||
} | ||
|
||
func TestFromContext(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
// No logger set | ||
assert.PanicsWithError(t, "telemetry logger not found in the context", func() { | ||
fromContext(ctx) | ||
}) | ||
|
||
// Default logger set | ||
ctx1 := WithDefaultLogger(ctx) | ||
assert.Equal(t, &defaultLogger{}, fromContext(ctx1)) | ||
|
||
// Mock logger set | ||
ctx2 := WithMockLogger(ctx) | ||
assert.Equal(t, &mockLogger{}, fromContext(ctx2)) | ||
|
||
// Unexpected logger type | ||
type foobar struct{} | ||
ctx3 := context.WithValue(ctx, telemetryLoggerKey, &foobar{}) | ||
assert.PanicsWithError(t, "unexpected telemetry logger type: *telemetry.foobar", func() { | ||
fromContext(ctx3) | ||
}) | ||
} |
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,22 @@ | ||
package telemetry | ||
|
||
import "github.com/databricks/cli/libs/telemetry/protos" | ||
|
||
// This corresponds to the FrontendLog lumberjack proto in universe. | ||
// FrontendLog is the top-level struct for any client-side logs at Databricks | ||
// regardless of whether they are generated from the CLI or the web UI. | ||
type FrontendLog struct { | ||
// A unique identifier for the log event generated from the CLI. | ||
FrontendLogEventID string `json:"frontend_log_event_id,omitempty"` | ||
|
||
Entry FrontendLogEntry `json:"entry,omitempty"` | ||
} | ||
|
||
type FrontendLogEntry struct { | ||
DatabricksCliLog DatabricksCliLog `json:"databricks_cli_log,omitempty"` | ||
} | ||
|
||
type DatabricksCliLog struct { | ||
CliTestEvent *protos.CliTestEvent `json:"cli_test_event,omitempty"` | ||
BundleInitEvent *protos.BundleInitEvent `json:"bundle_init_event,omitempty"` | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the purpose of this typecheck here? Why panic (as opposed to logging a message).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reaching here typically is a developer error. Panicking informs the developer there's a problem without having to return and propagate the error. For example, we also panic here: dbr.RunsOnRuntime(ctx)
We do the type check because we do not want to error or override if a mock logger is configured.