Documentation ΒΆ
Overview ΒΆ
Package ffclient aids adding instrumentation to have feature flags in your app without any backend server.
Summary ΒΆ
This package and its subpackages contain bits of code to have an easy feature flag solution with no complex installation to do on your infrastructure and without using 3rd party vendor for this.
The ffclient package provides the entry point - initialization and the basic method to get your flags value.
Before using the module you need to initialized it this way:
func main() { err := ffclient.Init(ffclient.Config{ PollingInterval: 3 * time.Second, HTTPRetriever: &ffClient.HTTPRetriever{ URL: "http://example.com/flag-config.yaml", }, }) defer ffclient.Close() ...
This example will load a file from an HTTP endpoint and will refresh the flags every 3 seconds.
Now you can evalute your flags anywhere in your code.
func main() { ... user := ffuser.NewUser("user-unique-key") hasFlag, _ := ffclient.BoolVariation("test-flag", user, false) if hasFlag { //flag "test-flag" is true for the user } else { // flag "test-flag" is false for the user } ...
Index ΒΆ
- func AllFlagsState(user ffuser.User) flagstate.AllFlags
- func BoolVariation(flagKey string, user ffuser.User, defaultValue bool) (bool, error)
- func Close()
- func Float64Variation(flagKey string, user ffuser.User, defaultValue float64) (float64, error)
- func GetCacheRefreshDate() time.Time
- func Init(config Config) error
- func IntVariation(flagKey string, user ffuser.User, defaultValue int) (int, error)
- func JSONArrayVariation(flagKey string, user ffuser.User, defaultValue []interface{}) ([]interface{}, error)
- func JSONVariation(flagKey string, user ffuser.User, defaultValue map[string]interface{}) (map[string]interface{}, error)
- func StringVariation(flagKey string, user ffuser.User, defaultValue string) (string, error)
- type Config
- type DataExporter
- type FileRetriever
- type GCStorageRetriever
- type GithubRetriever
- type GoFeatureFlag
- func (g *GoFeatureFlag) AllFlagsState(user ffuser.User) flagstate.AllFlags
- func (g *GoFeatureFlag) BoolVariation(flagKey string, user ffuser.User, defaultValue bool) (bool, error)
- func (g *GoFeatureFlag) Close()
- func (g *GoFeatureFlag) Float64Variation(flagKey string, user ffuser.User, defaultValue float64) (float64, error)
- func (g *GoFeatureFlag) GetCacheRefreshDate() time.Time
- func (g *GoFeatureFlag) IntVariation(flagKey string, user ffuser.User, defaultValue int) (int, error)
- func (g *GoFeatureFlag) JSONArrayVariation(flagKey string, user ffuser.User, defaultValue []interface{}) ([]interface{}, error)
- func (g *GoFeatureFlag) JSONVariation(flagKey string, user ffuser.User, defaultValue map[string]interface{}) (map[string]interface{}, error)
- func (g *GoFeatureFlag) RawVariation(flagKey string, user ffuser.User, sdkDefaultValue interface{}) (model.RawVarResult, error)
- func (g *GoFeatureFlag) StringVariation(flagKey string, user ffuser.User, defaultValue string) (string, error)
- type HTTPRetriever
- type KubernetesRetriever
- type NotifierConfig
- type Retriever
- type S3Retriever
- type SlackNotifier
- type WebhookConfig
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func AllFlagsState ΒΆ added in v0.17.0
AllFlagsState return the values of all the flags for a specific user. If valid field is false it means that we had an error when checking the flags.
func BoolVariation ΒΆ
BoolVariation return the value of the flag in boolean. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value.
func Close ΒΆ
func Close()
Close the component by stopping the background refresh and clean the cache.
func Float64Variation ΒΆ
Float64Variation return the value of the flag in float64. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value.
func GetCacheRefreshDate ΒΆ added in v0.22.0
GetCacheRefreshDate gives the date of the latest refresh of the cache
func Init ΒΆ
Init the feature flag component with the configuration of ffclient.Config
func main() { err := ffclient.Init(ffclient.Config{ PollingInterval: 3 * time.Second, Retriever: &ffClient.HTTPRetriever{ URL: "http://example.com/flag-config.yaml", }, }) defer ffclient.Close()
func IntVariation ΒΆ
IntVariation return the value of the flag in int. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value.
func JSONArrayVariation ΒΆ
func JSONArrayVariation(flagKey string, user ffuser.User, defaultValue []interface{}) ([]interface{}, error)
JSONArrayVariation return the value of the flag in []interface{}. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value.
func JSONVariation ΒΆ
func JSONVariation( flagKey string, user ffuser.User, defaultValue map[string]interface{}) (map[string]interface{}, error)
JSONVariation return the value of the flag in map[string]interface{}. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value.
func StringVariation ΒΆ
StringVariation return the value of the flag in string. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value.
Types ΒΆ
type Config ΒΆ
type Config struct { // PollingInterval (optional) Poll every X time // The minimum possible is 1 second // Default: 60 seconds PollingInterval time.Duration // Logger (optional) logger use by the library // Default: No log Logger *log.Logger // Context (optional) used to call other services (HTTP, S3 ...) // Default: context.Background() Context context.Context // Retriever is the component in charge to retrieve your flag file Retriever Retriever // Notifiers (optional) is the list of notifiers called when a flag change Notifiers []NotifierConfig // FileFormat (optional) is the format of the file to retrieve (available YAML, TOML and JSON) // Default: YAML FileFormat string // DataExporter (optional) is the configuration where we store how we should output the flags variations results DataExporter DataExporter // StartWithRetrieverError (optional) If true, the SDK will start even if we did not get any flags from the retriever. // It will serve only default values until the retriever returns the flags. // The init method will not return any error if the flag file is unreachable. // Default: false StartWithRetrieverError bool // Offline (optional) If true, the SDK will not try to retrieve the flag file and will not export any data. // No notification will be send neither. // Default: false Offline bool }
Config is the configuration of go-feature-flag. You should also have a retriever to specify where to read the flags file.
func (*Config) GetRetriever ΒΆ
GetRetriever returns a retriever.FlagRetriever configure with the retriever available in the config.
type DataExporter ΒΆ added in v0.10.0
type DataExporter struct { // FlushInterval is the interval we are waiting to export the data. // example: if you set your FlushInterval to 1 minutes, we will send // the data every minute unless we reach the max event in cache before. FlushInterval time.Duration // MaxEventInMemory is the maximum number of event you keep in the cache // before sending the data to the Exporter. // We will send the data when the MaxEventInMemory is reach or if we have // waited the FlushInterval. MaxEventInMemory int64 // Exporter is the configuration of your exporter. // You can see all available exporter in the ffexporter package. Exporter exporter.Exporter }
DataExporter is the configuration of your export target.
type FileRetriever ΒΆ added in v0.5.0
type FileRetriever struct {
Path string
}
FileRetriever is a configuration struct for a local flat file.
type GCStorageRetriever ΒΆ added in v0.19.0
type GCStorageRetriever struct { // Bucket is the name of your Google Cloud Storage Bucket. Bucket string // Object is the name of your file in your bucket. Object string // Options are Google Cloud Api options needed for downloading // your feature flag configuration file. Options []option.ClientOption // contains filtered or unexported fields }
GCStorageRetriever is a configuration struct for a Google Cloud Storage retriever.
type GithubRetriever ΒΆ added in v0.4.0
type GithubRetriever struct { RepositorySlug string Branch string // default is main FilePath string GithubToken string Timeout time.Duration // default is 10 seconds // contains filtered or unexported fields }
GithubRetriever is a configuration struct for a GitHub retriever.
func (*GithubRetriever) Retrieve ΒΆ added in v0.17.0
func (r *GithubRetriever) Retrieve(ctx context.Context) ([]byte, error)
func (*GithubRetriever) SetHTTPClient ΒΆ added in v0.17.0
func (r *GithubRetriever) SetHTTPClient(client internal.HTTPClient)
SetHTTPClient is here if you want to override the default http.Client we are using. It is also used for the tests.
type GoFeatureFlag ΒΆ added in v0.6.0
type GoFeatureFlag struct {
// contains filtered or unexported fields
}
GoFeatureFlag is the main object of the library it contains the cache, the config and the update.
func New ΒΆ added in v0.6.0
func New(config Config) (*GoFeatureFlag, error)
New creates a new go-feature-flag instance that retrieve the config from a YAML file and return everything you need to manage your flags.
func (*GoFeatureFlag) AllFlagsState ΒΆ added in v0.17.0
func (g *GoFeatureFlag) AllFlagsState(user ffuser.User) flagstate.AllFlags
AllFlagsState return a flagstate.AllFlags that contains all the flags for a specific user.
func (*GoFeatureFlag) BoolVariation ΒΆ added in v0.6.0
func (g *GoFeatureFlag) BoolVariation(flagKey string, user ffuser.User, defaultValue bool) (bool, error)
BoolVariation return the value of the flag in boolean. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value. Note: Use this function only if you are using multiple go-feature-flag instances.
func (*GoFeatureFlag) Close ΒΆ added in v0.6.0
func (g *GoFeatureFlag) Close()
Close wait until thread are done
func (*GoFeatureFlag) Float64Variation ΒΆ added in v0.6.0
func (g *GoFeatureFlag) Float64Variation(flagKey string, user ffuser.User, defaultValue float64) (float64, error)
Float64Variation return the value of the flag in float64. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value. Note: Use this function only if you are using multiple go-feature-flag instances.
func (*GoFeatureFlag) GetCacheRefreshDate ΒΆ added in v0.22.0
func (g *GoFeatureFlag) GetCacheRefreshDate() time.Time
GetCacheRefreshDate gives the date of the latest refresh of the cache
func (*GoFeatureFlag) IntVariation ΒΆ added in v0.6.0
func (g *GoFeatureFlag) IntVariation(flagKey string, user ffuser.User, defaultValue int) (int, error)
IntVariation return the value of the flag in int. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value. Note: Use this function only if you are using multiple go-feature-flag instances.
func (*GoFeatureFlag) JSONArrayVariation ΒΆ added in v0.6.0
func (g *GoFeatureFlag) JSONArrayVariation( flagKey string, user ffuser.User, defaultValue []interface{}) ([]interface{}, error)
JSONArrayVariation return the value of the flag in []interface{}. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value. Note: Use this function only if you are using multiple go-feature-flag instances.
func (*GoFeatureFlag) JSONVariation ΒΆ added in v0.6.0
func (g *GoFeatureFlag) JSONVariation( flagKey string, user ffuser.User, defaultValue map[string]interface{}) (map[string]interface{}, error)
JSONVariation return the value of the flag in map[string]interface{}. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value. Note: Use this function only if you are using multiple go-feature-flag instances.
func (*GoFeatureFlag) RawVariation ΒΆ added in v0.22.2
func (g *GoFeatureFlag) RawVariation(flagKey string, user ffuser.User, sdkDefaultValue interface{}, ) (model.RawVarResult, error)
RawVariation return the raw value of the flag (without any types). This raw result is mostly used by software built on top of go-feature-flag such as go-feature-flag relay proxy. If you are using directly the library you should avoid calling this function. Note: Use this function only if you are using multiple go-feature-flag instances.
func (*GoFeatureFlag) StringVariation ΒΆ added in v0.6.0
func (g *GoFeatureFlag) StringVariation(flagKey string, user ffuser.User, defaultValue string) (string, error)
StringVariation return the value of the flag in string. An error is return if you don't have init the library before calling the function. If the key does not exist we return the default value. Note: Use this function only if you are using multiple go-feature-flag instances.
type HTTPRetriever ΒΆ
type HTTPRetriever struct { // URL of your endpoint URL string // HTTP Method we should use (default: GET) Method string // Body of the request if needed (default: empty body) Body string // Header added to the request Header http.Header // Timeout we should wait before failing (default: 10 seconds) Timeout time.Duration // contains filtered or unexported fields }
HTTPRetriever is a configuration struct for an HTTP endpoint retriever.
func (*HTTPRetriever) Retrieve ΒΆ added in v0.17.0
func (r *HTTPRetriever) Retrieve(ctx context.Context) ([]byte, error)
func (*HTTPRetriever) SetHTTPClient ΒΆ added in v0.17.0
func (r *HTTPRetriever) SetHTTPClient(client internal.HTTPClient)
SetHTTPClient is here if you want to override the default http.Client we are using. It is also used for the tests.
type KubernetesRetriever ΒΆ added in v0.23.0
type KubernetesRetriever struct { Namespace string ConfigMapName string Key string ClientConfig restclient.Config // contains filtered or unexported fields }
KubernetesRetriever is a configuration struct for a Kubernetes retriever.
type NotifierConfig ΒΆ added in v0.9.0
type NotifierConfig interface {
GetNotifier(config Config) (ffnotifier.Notifier, error)
}
NotifierConfig is the interface for your notifiers. You can use as notifier a WebhookConfig
Notifiers: []ffclient.NotifierConfig{ &ffclient.WebhookConfig{ EndpointURL: " https://example.com/hook", Secret: "Secret", Meta: map[string]string{ "app.name": "my app", }, }, // ... }
type Retriever ΒΆ added in v0.5.0
type Retriever interface { // Retrieve function is supposed to load the file and to return a []byte of your flag configuration file. // If you want to specify the format of the file, you can use the ffclient.Config.FileFormat option to // specify if it is a YAML, JSON or TOML file. Retrieve(ctx context.Context) ([]byte, error) }
Retriever is the interface to create a Retriever to load you flags.
type S3Retriever ΒΆ
type S3Retriever struct { // Bucket is the name of your S3 Bucket. Bucket string // Item is the path to your flag file in your bucket. Item string // AwsConfig is the AWS SDK configuration object we will use to // download your feature flag configuration file. AwsConfig aws.Config // contains filtered or unexported fields }
S3Retriever is a configuration struct for a S3 retriever.
type SlackNotifier ΒΆ added in v0.9.0
type SlackNotifier struct {
SlackWebhookURL string
}
func (*SlackNotifier) GetNotifier ΒΆ added in v0.9.0
func (w *SlackNotifier) GetNotifier(config Config) (ffnotifier.Notifier, error)
GetNotifier convert the configuration in a Notifier struct
type WebhookConfig ΒΆ added in v0.7.0
type WebhookConfig struct { // Deprecated: use EndpointURL instead PayloadURL string // EndpointURL is the URL where we gonna do the POST Request. EndpointURL string Secret string // Secret used to sign your request body. Meta map[string]string // Meta information that you want to send to your webhook (not mandatory) }
WebhookConfig is the configuration of your webhook. we will call this URL with a POST request with the following format
{ "meta":{ "hostname": "server01" }, "flags":{ "deleted": { "test-flag": { "rule": "key eq \"random-key\"", "percentage": 100, "true": true, "false": false, "default": false } }, "added": { "test-flag3": { "percentage": 5, "true": "test", "false": "false", "default": "default" } }, "updated": { "test-flag2": { "old_value": { "rule": "key eq \"not-a-key\"", "percentage": 100, "true": true, "false": false, "default": false }, "new_value": { "disable": true, "rule": "key eq \"not-a-key\"", "percentage": 100, "true": true, "false": false, "default": false } } } } }
func (*WebhookConfig) GetNotifier ΒΆ added in v0.9.0
func (w *WebhookConfig) GetNotifier(config Config) (ffnotifier.Notifier, error)
GetNotifier convert the configuration in a Notifier struct
Source Files ΒΆ
Directories ΒΆ
Path | Synopsis |
---|---|
examples
|
|
Package ffexporter defines the data exporter of go-feature-flag Theses exporters are usable in your init configuration.
|
Package ffexporter defines the data exporter of go-feature-flag Theses exporters are usable in your init configuration. |
Package ffuser defines the go-feature-flag model for user properties.
|
Package ffuser defines the go-feature-flag model for user properties. |
Package testutils is a generated GoMock package.
|
Package testutils is a generated GoMock package. |