package feature

import "github.com/influxdata/flux/internal/pkg/feature"

Package feature provides feature flagging capabilities for a Go application.

Flags are defined using the `MakeXXXFlag` functions. These can either be defined in code or generated using the feature generator included in this module.

Flags are configured in `flags.yml` at the top of this repository. Running `make flags` generates Go code based on this configuration to programmatically test flag values in a given request context. Boolean flags are the most common case, but integers, floats and strings are supported for more complicated experiments.

The `Flagger` interface is the crux of this package. It computes a map of feature flag values for a given request context. The implementation is application dependent. The result can be attached to the context using `Annotate` and then the results will be used when checking feature flags.

Flags can be checked using the context after it has been annotated. For example, to check a boolean flag that was generated by the `feature` command, you might do it this way:

if feature.MyFeature().Enabled(ctx) {
    // my feature is enabled
} else {
    // my feature is disabled
}

Index

Functions

func Inject

func Inject(ctx context.Context, flagger Flagger) context.Context

Inject will inject the Flagger into the context.

func SetMetrics

func SetMetrics(m Metrics)

SetMetrics sets the metric store for feature flags.

Types

type Base

type Base struct {
	// contains filtered or unexported fields
}

Base implements the base of the Flag type.

func MakeBase

func MakeBase(name, key, owner string, defaultValue interface{}) Base

MakeBase constructs a flag.

func (Base) Default

func (f Base) Default() interface{}

Default returns the type-agnostic zero value for the flag.

func (Base) Key

func (f Base) Key() string

Key returns the programmatic backend identifier for the flag.

type BoolFlag

type BoolFlag struct {
	Base
	// contains filtered or unexported fields
}

BoolFlag implements Flag for boolean values.

func MakeBoolFlag

func MakeBoolFlag(name, key, owner string, defaultValue bool) BoolFlag

MakeBoolFlag returns a string flag with the given Base and default.

func (BoolFlag) Enabled

func (f BoolFlag) Enabled(ctx context.Context) (v bool)

Enabled indicates whether flag is true or false on the request context.

type Flag

type Flag interface {
	// Key returns the programmatic backend identifier for the flag.
	Key() string
	// Default returns the type-agnostic zero value for the flag.
	// Type-specific flag implementations may expose a typed default
	// (e.g. BoolFlag includes a boolean Default field).
	Default() interface{}
}

Flag represents a generic feature flag with a key and a default.

func MakeFlag

func MakeFlag(name, key, owner string, defaultValue interface{}) Flag

MakeFlag constructs a Flag. The concrete implementation is inferred from the provided default.

type Flagger

type Flagger interface {
	// FlagValue returns the value for a given flag.
	FlagValue(ctx context.Context, flag Flag) interface{}
}

Flagger returns flag values.

func GetFlagger

func GetFlagger(ctx context.Context) Flagger

GetFlagger returns the Flagger for this context or the default flagger.

type FloatFlag

type FloatFlag struct {
	Base
	// contains filtered or unexported fields
}

FloatFlag implements Flag for float values.

func MakeFloatFlag

func MakeFloatFlag(name, key, owner string, defaultValue float64) FloatFlag

MakeFloatFlag returns a string flag with the given Base and default.

func (FloatFlag) Float

func (f FloatFlag) Float(ctx context.Context) (v float64)

Float value of the flag on the request context.

type IntFlag

type IntFlag struct {
	Base
	// contains filtered or unexported fields
}

IntFlag implements Flag for integer values.

func MakeIntFlag

func MakeIntFlag(name, key, owner string, defaultValue int) IntFlag

MakeIntFlag returns a string flag with the given Base and default.

func (IntFlag) Int

func (f IntFlag) Int(ctx context.Context) (v int)

Int value of the flag on the request context.

type Metrics

type Metrics interface {
	Inc(key string, value interface{})
}

type StringFlag

type StringFlag struct {
	Base
	// contains filtered or unexported fields
}

StringFlag implements Flag for string values.

func MakeStringFlag

func MakeStringFlag(name, key, owner string, defaultValue string) StringFlag

MakeStringFlag returns a string flag with the given Base and default.

func (StringFlag) String

func (f StringFlag) String(ctx context.Context) (v string)

String value of the flag on the request context.

Source Files

doc.go feature.go flag.go metrics.go

Directories

PathSynopsis
internal/pkg/feature/cmd
internal/pkg/feature/cmd/featureSprig Copyright (C) 2013 Masterminds
Version
v0.196.1 (latest)
Published
Feb 19, 2025
Platform
linux/amd64
Imports
2 packages
Last checked
1 day ago

Tools for package owners.