Documentation ¶
Overview ¶
Package splitio provides integration with https://www.split.io/.
Basics ¶
Using the client directly:
client := splitio.MustNewClient(config) attributes := splitIO.Attributes{ "age": 37, } if client.IsFeatureEnabled("MY_FEATURE_FLAG", attributes) { // do stuff }
Using the middleware ¶
In the service:
MY_FF := splitio.Feature("MY_FEATURE_FLAG") Features := []splitio.Feature{ MY_FF, }
In the transport layer:
func GetUserFromRequest(ctx context.Context, request interface{}) map[User]Attributes { // Extract user and attributes from the endpoint request }
In the main package:
client := splitio.MustNewClient(config) middlewareConfig := splitio.DefaultFFMidlewareConfig() middlewareConfig.MultiUserDecodeFn = myapi.GetUserFromRequest middlewareConfig.Features = myservice.Features middleware := NewFeatureFlagMiddleware(client, middlewareConfig) myEndpoint := endpoint.Chain( // ... middleware, // ... )(myEndpoint)
In your code:
if splitio.IsFeatureEnabled(ctx, MY_FF) { // do stuff }
Although the initial setup is more complex, it has the advantage of setting up everything only once, and then integrating seamlessly with new feature flags.
Each feature is checked once per request, for the users and attributes specified in the "MultiUserDecodeFn". The behavior is stored in the context, so that it is possible to check anywhere in the service if the feature is enabled without caring about which user or attributes should be used.
Index ¶
- Variables
- func EnrichLogWithEnabledFeatures(ctx context.Context, zctx zerolog.Context) zerolog.Context
- func IsFeatureEnabled(ctx context.Context, feature Feature) bool
- func MockFeatureListToContext(ctx context.Context, features []Feature) context.Context
- func MockFeaturesToContext(ctx context.Context, features map[Feature]bool) context.Context
- func NewFeatureFlagMiddleware(client Client, config FFMidlewareConfig) endpoint.Middleware
- func NewZerologLogger() io.Writer
- type Attributes
- type Client
- type Config
- type FFMidlewareConfig
- type Feature
- type MultiUserDecoder
- type User
Constants ¶
This section is empty.
Variables ¶
var NoUser = User("nouser")
NoUser will always have the same behavior
Functions ¶
func EnrichLogWithEnabledFeatures ¶
EnrichLogWithEnabledFeatures takes a zerolog context and adds the key enabled_features with a list with all enabled features. If there isn't any feature enabled, it returns the original context.
func IsFeatureEnabled ¶
IsFeatureEnabled checks if the @feature stored in the context @ctx was previously selected in the FeatureFlagMiddleware
func MockFeatureListToContext ¶
MockFeatureListToContext writes the mocked behaviours in @features to the context
func MockFeaturesToContext ¶
MockFeaturesToContext writes the mocked behaviours in @features to the context
func NewFeatureFlagMiddleware ¶
func NewFeatureFlagMiddleware(client Client, config FFMidlewareConfig) endpoint.Middleware
NewFeatureFlagMiddleware returns a middleware that checks if a feature is enabled. The chosen behaviour is stored in the context, and it is possible to check its value with splitio.IsFeatureEnabled(ctx, feature) It integrates with foudationkit's trace, and stores the behaviour in the labels.
func NewZerologLogger ¶
NewZerologLogger returns a new logger for Split IO
Types ¶
type Attributes ¶
type Attributes map[string]interface{}
Attributes represents the Split IO Attributes
type Client ¶
type Client interface { IsFeatureEnabled(Feature, Attributes) bool IsFeatureWithUserEnabled(User, Feature, Attributes) bool Close() }
Client wraps a splitio client
func MustNewClient ¶
MustNewClient returns a new Client based on the config
type Config ¶
type Config struct { Provider string `default:"splitio"` SplitID string `secret:"true"` Mode string `default:"memory"` Redis struct { Database int Host string Port int } Stub struct { Active string `default:""` } }
Config represents the informations to create a Split Client
type FFMidlewareConfig ¶
type FFMidlewareConfig struct { // MultiUserDecodeFn is a function that extract a multiple users from the // endpoint request. The feature flag will be checked against all of the // users, and will only be active if all of the users are in the split. MultiUserDecodeFn MultiUserDecoder // Features are the list of FF that should be checked by the middleware Features []Feature }
FFMidlewareConfig contains the parameters for the feature flag middleware
func DefaultFFMidlewareConfig ¶
func DefaultFFMidlewareConfig() FFMidlewareConfig
DefaultFFMidlewareConfig returns the default config for the FF Midleware
type MultiUserDecoder ¶
type MultiUserDecoder func(ctx context.Context, request interface{}) map[User]Attributes
MultiUserDecoder is a function that is able to extract multiple users from an endpoint's request, with each user being associated with a set of attributes. If no user can be extracted from the request, the fuction should return a fixed user called "nouser". It's an error to return an empty map, which will cause the features to be evaluated as "off"