databricks-cli/libs/telemetry/context.go

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

47 lines
1.1 KiB
Go
Raw Normal View History

2024-11-29 00:57:48 +00:00
package telemetry
import (
"context"
"fmt"
2024-11-29 00:57:48 +00:00
)
// 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 v != nil {
panic(fmt.Sprintf("telemetry logger already set in the context: %v", v))
2024-11-29 00:57:48 +00:00
}
return context.WithValue(ctx, telemetryLoggerKey, &defaultLogger{logs: []FrontendLog{}})
2024-11-29 00:57:48 +00:00
}
func WithMockLogger(ctx context.Context) context.Context {
v := ctx.Value(telemetryLoggerKey)
if v != nil {
panic(fmt.Sprintf("telemetry logger already set in the context: %v", v))
}
return context.WithValue(ctx, telemetryLoggerKey, &mockLogger{})
}
func fromContext(ctx context.Context) Logger {
v := ctx.Value(telemetryLoggerKey)
if v == nil {
2024-11-29 00:57:48 +00:00
panic("telemetry logger not found in the context")
}
switch v.(type) {
case *defaultLogger:
return v.(*defaultLogger)
case *mockLogger:
return v.(*mockLogger)
default:
panic(fmt.Sprintf("unexpected telemetry logger type: %T", v))
}
2024-11-29 00:57:48 +00:00
}