databricks-cli/libs/telemetry/context.go

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

31 lines
731 B
Go
Raw Normal View History

2024-11-29 00:57:48 +00:00
package telemetry
import (
"context"
)
// 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
2024-12-27 07:57:24 +00:00
func ContextWithLogger(ctx context.Context) context.Context {
2024-11-29 00:57:48 +00:00
_, ok := ctx.Value(telemetryLoggerKey).(*logger)
if ok {
2024-12-27 07:57:24 +00:00
// If a logger is already configured in the context, do not set a new one.
// This is useful for testing.
return ctx
2024-11-29 00:57:48 +00:00
}
2024-12-30 06:59:18 +00:00
return context.WithValue(ctx, telemetryLoggerKey, &logger{logs: []FrontendLog{}})
2024-11-29 00:57:48 +00:00
}
func fromContext(ctx context.Context) *logger {
l, ok := ctx.Value(telemetryLoggerKey).(*logger)
if !ok {
panic("telemetry logger not found in the context")
}
return l
}