databricks-cli/libs/telemetry/logger.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

54 lines
1.3 KiB
Go
Raw Normal View History

2025-01-20 16:28:02 +00:00
package telemetry
import (
"context"
2025-01-22 21:03:13 +00:00
"github.com/databricks/cli/libs/telemetry/protos"
2025-01-20 16:28:02 +00:00
"github.com/google/uuid"
)
2025-01-22 21:03:13 +00:00
const SkipEnvVar = "DATABRICKS_CLI_SKIP_TELEMETRY"
// DATABRICKS_CLI_BLOCK_ON_TELEMETRY_UPLOAD is an environment variable that can be set
// to make the CLI process block until the telemetry logs are uploaded.
// Only used for testing.
const BlockOnUploadEnvVar = "DATABRICKS_CLI_BLOCK_ON_TELEMETRY_UPLOAD"
2025-01-20 16:28:02 +00:00
2025-01-22 21:03:13 +00:00
func Log(ctx context.Context, event protos.DatabricksCliLog) {
fromContext(ctx).log(event)
}
2025-01-20 16:28:02 +00:00
2025-01-22 21:03:13 +00:00
func GetLogs(ctx context.Context) []protos.FrontendLog {
return fromContext(ctx).getLogs()
}
2025-01-20 16:28:02 +00:00
2025-01-22 21:03:13 +00:00
func SetExecutionContext(ctx context.Context, ec protos.ExecutionContext) {
fromContext(ctx).setExecutionContext(ec)
2025-01-20 16:28:02 +00:00
}
2025-01-22 21:03:13 +00:00
type logger struct {
logs []protos.FrontendLog
2025-01-20 16:28:02 +00:00
}
2025-01-22 21:03:13 +00:00
func (l *logger) log(event protos.DatabricksCliLog) {
2025-01-20 16:28:02 +00:00
if l.logs == nil {
2025-01-22 21:03:13 +00:00
l.logs = make([]protos.FrontendLog, 0)
2025-01-20 16:28:02 +00:00
}
2025-01-22 21:03:13 +00:00
l.logs = append(l.logs, protos.FrontendLog{
2025-01-20 16:28:02 +00:00
FrontendLogEventID: uuid.New().String(),
2025-01-22 21:03:13 +00:00
Entry: protos.FrontendLogEntry{
2025-01-20 16:28:02 +00:00
DatabricksCliLog: event,
},
})
}
2025-01-22 21:03:13 +00:00
func (l *logger) getLogs() []protos.FrontendLog {
return l.logs
2025-01-20 16:28:02 +00:00
}
2025-01-22 21:03:13 +00:00
func (l *logger) setExecutionContext(ec protos.ExecutionContext) {
for i := range l.logs {
l.logs[i].Entry.DatabricksCliLog.ExecutionContext = &ec
}
2025-01-20 16:28:02 +00:00
}