databricks-cli/libs/telemetry/context.go

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

26 lines
541 B
Go
Raw Normal View History

2025-01-20 16:28:02 +00:00
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
2025-01-22 21:03:13 +00:00
func WithNewLogger(ctx context.Context) context.Context {
return context.WithValue(ctx, telemetryLoggerKey, &logger{})
2025-01-20 16:28:02 +00:00
}
2025-01-22 21:03:13 +00:00
func fromContext(ctx context.Context) *logger {
2025-01-20 16:28:02 +00:00
v := ctx.Value(telemetryLoggerKey)
if v == nil {
panic(fmt.Errorf("telemetry logger not found in the context"))
}
2025-01-22 21:03:13 +00:00
return v.(*logger)
2025-01-20 16:28:02 +00:00
}