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
544 B
Go
Raw Normal View History

2025-01-20 16:28:02 +00:00
package telemetry
import (
"context"
2025-02-03 10:42:17 +00:00
"errors"
2025-01-20 16:28:02 +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
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 {
2025-02-03 10:42:17 +00:00
panic(errors.New("telemetry logger not found in the context"))
2025-01-20 16:28:02 +00:00
}
2025-01-22 21:03:13 +00:00
return v.(*logger)
2025-01-20 16:28:02 +00:00
}