Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/fuzz_panic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ func isAutoGenerated(leaf leafCommand) bool {
// "bundle", "auth", "sync", "fs", etc. The heuristic: anything whose
// root isn't in this block list is auto-generated.
manualRoots := map[string]bool{
"aitools": true,
"bundle": true,
"auth": true,
"sync": true,
Expand Down
8 changes: 8 additions & 0 deletions libs/telemetry/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ func fromContext(ctx context.Context) *logger {

return v.(*logger)
}

// loggerFromContext returns the telemetry logger, or false if none was
// installed. Unlike fromContext it does not panic, so callers on paths that may
// run without telemetry setup can drop events instead of crashing.
func loggerFromContext(ctx context.Context) (*logger, bool) {
v, ok := ctx.Value(telemetryLoggerKey).(*logger)
return v, ok
}
11 changes: 10 additions & 1 deletion libs/telemetry/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ const (
)

func Log(ctx context.Context, event protos.DatabricksCliLog) {
fromContext(ctx).log(event)
// A missing logger means telemetry was never initialized on this context
// (e.g. a command invoked outside the normal cmd/root setup). Dropping the
// event is the right call: telemetry is best-effort and must never crash a
// command.
l, ok := loggerFromContext(ctx)
if !ok {
log.Debugf(ctx, "telemetry logger not found in the context; dropping event")
return
}
l.log(event)
}

type logger struct {
Expand Down
11 changes: 11 additions & 0 deletions libs/telemetry/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,14 @@ func TestTelemetryUploadMaxRetries(t *testing.T) {
assert.EqualError(t, err, "failed to upload telemetry logs after three attempts")
assert.Equal(t, 3, count)
}

func TestLogWithoutLoggerDropsEvent(t *testing.T) {
// A context without a telemetry logger must not panic; the event is dropped.
assert.NotPanics(t, func() {
Log(t.Context(), protos.DatabricksCliLog{
CliTestEvent: &protos.CliTestEvent{
Name: protos.DummyCliEnumValue1,
},
})
})
}
Loading