Documentation
¶
Overview ¶
Package engine evaluates schema_version 1 feature-flags documents in memory: kill switch, then ordered rules (first match wins), then the flag's default. Every answer carries the reason that produced it.
An Engine is built once per document version and never changes, so a provider can swap engines atomically while requests keep evaluating. Evaluation does no network or other I/O of its own; only the custom operators and resolvers a service registers run its code.
Index ¶
Constants ¶
const ( MetricOperatorDuration = "featureflags.operator.duration" MetricResolverDuration = "featureflags.resolver.duration" )
Histograms of custom code latency in seconds. Operator samples carry the operator and outcome (ok, error) attributes; resolver samples carry the attribute and outcome.
const MeterName = "github.com/luckylanggam/tools-go-featureflags"
MeterName is the OpenTelemetry meter the library records its metrics on.
Variables ¶
This section is empty.
Functions ¶
func WithAttributes ¶
func WithAttributes(ctx context.Context, attrs Attributes) context.Context
WithAttributes returns a context whose evaluations see attrs, typically set once per request by middleware. Attributes already in ctx are kept unless attrs sets them, and arguments passed to Evaluate win over both. The first call also starts the request's resolver cache, which later calls share, so set attributes that resolvers read before evaluating.
Types ¶
type Attributes ¶
Attributes describe the caller (site_id, network_id, player_id, ...). Keys are catalog attribute names; values are converted to the catalog type before comparison, so 101 and "101" are the same integer.
func AttributesFromContext ¶
func AttributesFromContext(ctx context.Context) Attributes
AttributesFromContext returns a copy of the attributes WithAttributes put in ctx, or nil when there are none.
type Decision ¶
type Decision struct {
Flag string
Enabled bool
Reason Reason
Rule string
RuleIndex int
MissingAttrs []string
InvalidAttrs []string
FailedOperators []string
FailedResolvers []string
UndecidedOperators []string
}
Decision is the result of evaluating one flag for one caller. MissingAttrs and InvalidAttrs name the attributes that stopped a rule from matching because the caller did not send them (and no resolver filled them) or sent a value that does not convert to the catalog type. FailedOperators and FailedResolvers name the custom operators and the resolvers' attributes that returned an error. All four are sorted and may be set whatever the Reason. UndecidedOperators is set only with ReasonDecidedByService.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine evaluates one feature-flags document. It is immutable and safe for concurrent use.
func New ¶
New compiles a feature-flags document against its catalog. It fails when the document cannot be evaluated: an attribute missing from the catalog, an operator that is neither built in nor declared in the catalog, a declared custom operator that is not registered (unless WithUnregisteredOperatorsUndecided), an unknown group or one over another attribute, a value of the wrong type, or an invalid regular expression. Run validate on the documents first for the full set of checks.
type OperatorFunc ¶
OperatorFunc decides a custom operator for one caller. attr is the caller's value converted to the attribute's catalog type; value is the condition's value converted to the operator's catalog value_type. An error counts as no match and is reported in Decision.FailedOperators.
type Option ¶
type Option func(*options)
Option configures New.
func WithMeterProvider ¶
func WithMeterProvider(mp metric.MeterProvider) Option
WithMeterProvider sets where custom operator and resolver latency is recorded. The default is the global OpenTelemetry meter provider.
func WithRegistry ¶
WithRegistry gives the engine the service's custom operators and resolvers.
func WithUnregisteredOperatorsUndecided ¶
func WithUnregisteredOperatorsUndecided() Option
WithUnregisteredOperatorsUndecided is for tools that evaluate a service's flags without its code, such as the Flag Console. A custom operator the catalog declares but the registry lacks no longer fails New; a rule that needs it is reported with ReasonDecidedByService. Services must not use it: they rely on New rejecting a document they cannot evaluate.
type Reason ¶
type Reason string
Reason says what decided a flag.
const ( // ReasonUnknownFlag: the flag is not in the document; it is off. ReasonUnknownFlag Reason = "unknown_flag" // ReasonKillSwitch: the flag has enabled=false; it is off for everyone. ReasonKillSwitch Reason = "kill_switch" // ReasonRule: the rule named in Decision.Rule matched. ReasonRule Reason = "rule" // ReasonDefault: no rule matched; the flag's default applies. ReasonDefault Reason = "default" // ReasonDecidedByService: every other check of the rule in Decision.Rule // passed, but it needs custom operators only the service implements // (see WithUnregisteredOperatorsUndecided). Enabled carries no meaning: // the flag is not known to be off. ReasonDecidedByService Reason = "decided_by_service" )
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds the custom operators and resolvers a service implements in code. Register them at startup and pass the registry to every New with WithRegistry; an engine copies what is registered when it is built. It is safe for concurrent use.
func (*Registry) RegisterOperator ¶
func (r *Registry) RegisterOperator(name string, fn OperatorFunc) error
RegisterOperator adds the custom operator name, which the catalog must also declare. Conditions negate it with the not_ prefix like a built-in, so name may not be built in or start with not_.
func (*Registry) RegisterResolver ¶
func (r *Registry) RegisterResolver(attr string, fn ResolverFunc) error
RegisterResolver adds the resolver for attr. It is used only when the catalog declares a resolver for attr, and it runs at most once per request and set of inputs (see WithAttributes).
type ResolverFunc ¶
type ResolverFunc func(ctx context.Context, inputs Attributes) (any, error)
ResolverFunc fetches the attribute it is registered for when a rule needs it and the caller did not send it. inputs holds the attributes the catalog lists in the resolver's requires, converted to their catalog types. A nil value leaves the attribute missing; so does an error, which is also reported in Decision.FailedResolvers.