Creating a Hook for the go-sdk
A Hook taps into one or more of the flag evaluation's lifecycle events (before/after/error/finally) to perform the same action at that point for every evaluation. In this blog post, we'll look into what it takes to create an OpenFeature hook in Go that verifies that the return value is a valid hex color.
Objective
Create and integrate a spec compliant hook that validates that the result of a flag evaluation is a hex color.Prerequisites
- Golang 1.17+
Repository setup
Hooks
written for the go-sdk
are all maintained in the
go-sdk-contrib
repository, containing both hooks
and providers
.
The following commands can be used to setup the go-sdk-contrib
repository,
this clones the repository and sets up your hook specific go module under /hooks/MY-NEW-HOOK-NAME
adding a go.mod
and README.md
file. The module will then be referenced in the top level go.work
file.
git clone https://github.com/open-feature/go-sdk-contrib.git
cd go-sdk-contrib
make HOOK=MY-NEW-HOOK-NAME new-hook
make workspace-init
Creating the hook
In order for the Hook
to be compatible with the OpenFeature go-sdk
, it will need to comply to the OpenFeature spec.
For the go-sdk
this means that it will need to conform to the following interface:
type Hook interface {
Before(ctx context.Context, hookContext HookContext, hookHints HookHints) (*EvaluationContext, error)
After(ctx context.Context, hookContext HookContext, flagEvaluationDetails InterfaceEvaluationDetails, hookHints HookHints) error
Error(ctx context.Context, hookContext HookContext, err error, hookHints HookHints)
Finally(ctx context.Context, hookContext HookContext, hookHints HookHints)
}
In order to avoid implementing all the required functions to conform to the interface, embed the of.UnimplementedHook
struct in the Hook struct.
Then define the After
lifecycle event, overriding of.UnimplementedHook
's null implementation.
// Hook validates the flag evaluation details After flag resolution
type Hook struct {
of.UnimplementedHook
Validator validator
}
func (h Hook) After(ctx context.Context, hookContext of.HookContext, flagEvaluationDetails of.InterfaceEvaluationDetails, hookHints of.HookHints) error {
err := h.Validator.IsValid(flagEvaluationDetails)
if err != nil {
return err
}
return nil
}
Notice the Validator
field of type validator
in the Hook
struct. This is defined as such:
type validator interface {
IsValid(flagEvaluationDetails of.InterfaceEvaluationDetails) error
}
This allows us to supply any validator that implements this function signature, you can either create your own validator or use one of the existing validators. This tutorial uses the existing hex regex validator.
Integrating the hook
-
Install dependencies
go get github.com/open-feature/go-sdk
go get github.com/open-feature/go-sdk-contrib/hooks/validator -
Import the dependencies
package main
import (
"context"
"fmt"
"github.com/open-feature/go-sdk-contrib/hooks/validator/pkg/regex"
"github.com/open-feature/go-sdk-contrib/hooks/validator/pkg/validator"
"github.com/open-feature/go-sdk/pkg/openfeature"
"log"
) -
Create an instance of the
validator hook
struct using the regex hex validatorfunc main() {
hexValidator, err := regex.Hex()
if err != nil {
log.Fatal(err)
}
v := validator.Hook{Validator: hexValidator}
} -
Register the
NoopProvider
, this simply returns the given default value on flag evaluation.This step is optional, the sdk uses the
NoopProvider
by default but we're explicitly setting it for completenessopenfeature.SetProvider(openfeature.NoopProvider{})
-
Create the client, call the flag evaluation using the
validator hook
at the point of invocationclient := openfeature.NewClient("foo")
result, err := client.
StringValueDetails(
context.Background(),
"blue",
"invalidhex",
openfeature.EvaluationContext{},
openfeature.WithHooks(v),
)
if err != nil {
fmt.Println("err:", err)
}
fmt.Println("result:", result) -
Check that the flag evaluation returns an error as
invalidhex
is not a valid hex colorgo run main.go
err: execute after hook: regex doesn't match on flag value
result {blue 1 {invalidhex }}Note that despite getting an error we still get a result.