Skip to content

Commit

Permalink
Kubernetes tests: Let the benchmark metric recorder be overridden via…
Browse files Browse the repository at this point in the history
… context

PiperOrigin-RevId: 700842559
  • Loading branch information
EtiennePerot authored and gvisor-bot committed Nov 28, 2024
1 parent 28c50dd commit 7458283
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion test/kubernetes/benchmetric/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ load("//tools:defs.bzl", "go_library")

package(
default_applicable_licenses = ["//:license"],
default_visibility = ["//test/kubernetes:__subpackages__"],
default_visibility = ["//:sandbox"],
licenses = ["notice"],
)

Expand Down
12 changes: 12 additions & 0 deletions test/kubernetes/benchmetric/benchmetric.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,20 @@ var (
recorderOnce sync.Once
)

type recorderContextKeyType int

const recorderContextKey recorderContextKeyType = iota

// WithRecorder returns a context with the given `Recorder`.
func WithRecorder(ctx context.Context, recorder Recorder) context.Context {
return context.WithValue(ctx, recorderContextKey, recorder)
}

// GetRecorder returns the benchmark's `Recorder` singleton.
func GetRecorder(ctx context.Context) (Recorder, error) {
if ctx.Value(recorderContextKey) != nil {
return ctx.Value(recorderContextKey).(Recorder), nil
}
recorderOnce.Do(func() {
recorder, recorderErr = recorderFn(ctx)
})
Expand Down

0 comments on commit 7458283

Please sign in to comment.