goforit

package module
v0.0.0-...-fea34bd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2023 License: MIT Imports: 13 Imported by: 0

README

This project is deprecated and is no longer being actively maintained.

Build Status GoDoc

goforit is an experimental, quick-and-dirty client library for feature flags in Go.

Backends

Feature flags can be stored in any desired backend. goforit provides a several flatfile implementations out-of-the-box, so feature flags can be defined in a JSON or CSV file. See below for details.

Alternatively, flags can be stored in a key-value store like Consul or Redis.

Usage

Create a CSV file that defines the flag names and sampling rates:

go.sun.money,0
go.moon.mercury,1
go.stars.money,.5
func main() {
	ctx := context.Background()

	// flags.csv contains comma-separated flag names and sample rates.
	// See: testdata/flags_example.csv
	backend := goforit.BackendFromFile("flags.csv")
	goforit.Init(30*time.Second, backend)

	if goforit.Enabled(ctx, "go.sun.mercury", nil) {
		fmt.Println("The go.sun.mercury feature is enabled for 100% of requests")
	}

	if goforit.Enabled(ctx, "go.stars.money", nil) {
		fmt.Println("The go.stars.money feature is enabled for 50% of requests")
	}
}

Backends

Included flatfile backends are:

CSV

This is a very simple backend, where every row defines a flag name and a rate at which it should be enabled, between zero and one. Initialize this backend with BackendFromFile. See an example.

JSON v1

This backend allows each flag to have multiple rules, like a series of if-statements. Each call to .Enabled() takes a map of properties, which rules can match against. Each rule's matching or non-matching can cause the overall flag to be on or off, or can fallthrough to the next rule. See the proposal for this system or an example JSON file. It's a bit confusing to understand.

JSON v2

In this format, each flag can have a number of rules, and each rule can contain a number of predicates for matching properties. When a flag is evaluated, it uses the first rule whose predicates match the given properties. See an example JSON file, that also includes test cases.

Status

goforit is in an experimental state and may introduce breaking changes without notice.

License

goforit is available under the MIT license.

Documentation

Overview

Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/stripe/goforit"
)

func main() {
	ctx := context.Background()

	// flags.csv contains comma-separated flag names and sample rates.
	// See: testdata/flags2_example.json
	backend := goforit.BackendFromJSONFile2("testdata/flags2_example.json")
	flags := goforit.New(30*time.Second, backend, goforit.WithOwnedStats(true))
	defer func() { _ = flags.Close() }()

	if flags.Enabled(ctx, "go.sun.mercury", nil) {
		fmt.Println("The go.sun.mercury feature is enabled for 100% of requests")
	}
	// Same thing.
	if flags.Enabled(nil, "go.sun.mercury", nil) {
		fmt.Println("The go.sun.mercury feature is enabled for 100% of requests")
	}

	if flags.Enabled(ctx, "go.stars.money", nil) {
		fmt.Println("The go.stars.money feature is enabled for 50% of requests")
	}
}
Output:

Index

Examples

Constants

View Source
const DefaultInterval = 30 * time.Second
View Source
const DefaultStatsdAddr = "127.0.0.1:8200"

DefaultStatsdAddr is the address we will emit metrics to if not overridden.

Variables

This section is empty.

Functions

func Override

func Override(ctx context.Context, name string, value bool) context.Context

Override allows overriding the value of a goforit flag within a context. This is mainly useful for tests.

Types

type Backend

type Backend interface {
	// Refresh returns a new set of flags.
	// It also returns the age of these flags, or an empty time if no age is known.
	Refresh() ([]*flags2.Flag2, time.Time, error)
}

func BackendFromJSONFile2

func BackendFromJSONFile2(filename string) Backend

BackendFromJSONFile2 creates a v2 backend powered by a JSON file

type Goforit

type Goforit interface {
	Enabled(ctx context.Context, name string, props map[string]string) (enabled bool)
	RefreshFlags(backend Backend)
	TryRefreshFlags(backend Backend) error
	SetStalenessThreshold(threshold time.Duration)
	AddDefaultTags(tags map[string]string)
	ReportCounts(callback func(name string, total, enabled uint64, isDeleted bool))
	Close() error
}

Goforit is the main interface for the library to check if flags enabled, refresh flags customizing behavior or mocking.

func New

func New(interval time.Duration, backend Backend, opts ...Option) Goforit

New creates a new goforit

type MetricsClient

type MetricsClient interface {
	Histogram(string, float64, []string, float64) error
	TimeInMilliseconds(name string, milli float64, tags []string, rate float64) error
	Gauge(string, float64, []string, float64) error
	Count(string, int64, []string, float64) error
	io.Closer
}

MetricsClient is the set of methods required to emit metrics to statsd, for customizing behavior or mocking.

type Option

type Option interface {
	// contains filtered or unexported methods
}

func DeletedCallback

func DeletedCallback(cb evaluationCallback) Option

DeletedCallback registers a callback to execute for each flag that is scheduled for deletion

func EvaluationCallback

func EvaluationCallback(cb evaluationCallback) Option

EvaluationCallback registers a callback to execute for each evaluated flag

func Logger

func Logger(printf func(msg string, args ...interface{})) Option

Logger uses the supplied function to log errors. By default, errors are written to os.Stderr.

func Statsd

func Statsd(stats MetricsClient) Option

Statsd uses the supplied client to emit metrics to. By default, a client is created and configured to emit metrics to DefaultStatsdAddr.

func WithContextOverrideDisabled

func WithContextOverrideDisabled(disabled bool) Option

func WithOwnedStats

func WithOwnedStats(isOwned bool) Option

WithOwnedStats instructs the returned Goforit instance to call Close() on its stats client when Goforit is closed.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL