evaluation

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2022 License: Apache-2.0 Imports: 10 Imported by: 2

README

LaunchDarkly Go SDK Evaluation Engine

Circle CI Documentation

Overview

This repository contains the internal feature flag evaluation logic and data model used by the LaunchDarkly Go SDK. It is packaged separately because it is also used by internal LaunchDarkly components. Applications using the LaunchDarkly Go SDK should not need to reference this package directly.

Note that the base import path is gopkg.in/launchdarkly/go-server-sdk-evaluation.v1, not github.com/launchdarkly/go-server-sdk-evaluation. This ensures that the package can be referenced not only as a Go module, but also by projects that use older tools like dep and govendor, because the 5.x release of the Go SDK supports either module or non-module usage. Future releases of this package, and of the Go SDK, may drop support for non-module usage.

Supported Go versions

This version of the project has been tested with Go 1.14 and higher.

Integration with easyjson

By default, go-server-sdk-evaluation-private uses LaunchDarkly's open-source JSON library go-jsonstream to convert its data model types like FeatureFlag to and from JSON; this is considerably faster than Go's built-in encoding/json and does not depend on any third-party code. However, it can optionally integrate with the third-party library easyjson, which may be even faster in some cases, without requiring any changes in your code. To enable this, set the build tag launchdarkly_easyjson when you run go build, which both switches go-jsonstream to use easyjson internally and also generates MarshalEasyJSON/UnmarshalEasyJSON methods for each JSON-serializable type. The easyjson library is still under development and has some potential compatibility issues; see its documentation for more details.

If you do not set the launchdarkly_easyjson build tag, go-server-sdk-evaluation does not use any code from easyjson.

Learn more

Check out our documentation for in-depth instructions on configuring and using LaunchDarkly. You can also head straight to the complete reference guide for the Go SDK, or the generated API documentation for this project.

Contributing

We encourage pull requests and other contributions from the community. Check out our contributing guidelines for instructions on how to contribute to this SDK.

About LaunchDarkly

  • LaunchDarkly is a continuous delivery platform that provides feature flags as a service and allows developers to iterate quickly and safely. We allow you to easily flag your features and manage them from the LaunchDarkly dashboard. With LaunchDarkly, you can:
    • Roll out a new feature to a subset of your users (like a group of users who opt-in to a beta tester group), gathering feedback and bug reports from real-world use cases.
    • Gradually roll out a feature to an increasing percentage of users, and track the effect that the feature has on key metrics (for instance, how likely is a user to complete a purchase if they have feature A versus feature B?).
    • Turn off a feature that you realize is causing performance problems in production, without needing to re-deploy, or even restart the application with a changed configuration file.
    • Grant access to certain features based on user attributes, like payment plan (eg: users on the ‘gold’ plan get access to more features than users in the ‘silver’ plan). Disable parts of your application to facilitate maintenance, without taking everything offline.
  • LaunchDarkly provides feature flag SDKs for a wide variety of languages and technologies. Check out our documentation for a complete list.
  • Explore LaunchDarkly

Documentation

Overview

Package evaluation contains the LaunchDarkly Go SDK feature flag evaluation engine.

Normal use of the Go SDK does not require referencing this package directly. It is used internally by the SDK, but is published and versioned separately so it can be used in other LaunchDarkly components without making the SDK versioning dependent on these internal APIs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BigSegmentMembership added in v1.4.0

type BigSegmentMembership interface {
	// CheckMembership tests whether the user is explicitly included or explicitly excluded in the
	// specified segment, or neither. The segment is identified by a segmentRef which is not the
	// same as the segment key-- it includes the key but also versioning information that the SDK
	// will provide. The store implementation should not be concerned with the format of this.
	//
	// If the user is explicitly included (regardless of whether the user is also explicitly
	// excluded or not-- that is, inclusion takes priority over exclusion), the method returns an
	// OptionalBool with a true value.
	//
	// If the user is explicitly excluded, and is not explicitly included, the method returns an
	// OptionalBool with a false value.
	//
	// If the user's status in the segment is undefined, the method returns OptionalBool{} with no
	// value (so calling IsDefined() on it will return false).
	CheckMembership(segmentRef string) ldvalue.OptionalBool
}

BigSegmentMembership is the return type of BigSegmentProvider.GetUserMembership(). It is associated with a single user, and provides the ability to check whether that user is included in or excluded from any number of big segments.

This is an immutable snapshot of the state for this user at the time GetBigSegmentMembership was called. Calling CheckMembership should not cause the state to be queried again. The object should be safe for concurrent access by multiple goroutines.

This interface also exists in go-server-sdk because it is exposed as part of the public SDK API; users can write their own implementations of SDK components, but we do not want application code to reference go-server-sdk-evaluation symbols directly as part of that, because this library is versioned separately from the SDK. Currently the two interfaces are identical, but it might be that the go-server-sdk-evaluation version would diverge from the go-server-sdk version due to some internal requirements that aren't relevant to users, in which case go-server-sdk would be responsible for bridging the difference.

type BigSegmentProvider added in v1.4.0

type BigSegmentProvider interface {
	// GetUserMembership queries a snapshot of the current segment state for a specific user.
	//
	// The underlying big segment store implementation will use a hash of the user key, rather
	// than the raw key. But computing the hash is the responsibility of the BigSegmentProvider
	// implementation rather than the evaluator, because there may already have a cached result for
	// that user, and we don't want to have to compute a hash repeatedly just to query a cache.
	//
	// If the returned BigSegmentMembership is nil, it is treated the same as an implementation
	// whose IsUserIncluded and IsUserExcluded methods always return false.
	GetUserMembership(
		userKey string,
	) (BigSegmentMembership, ldreason.BigSegmentsStatus)
}

BigSegmentProvider is an abstraction for querying user membership in big segments. The caller provides an implementation of this interface to NewEvaluatorWithBigSegments.

type DataProvider

type DataProvider interface {
	// GetFeatureFlag attempts to retrieve a feature flag from the data store by key.
	//
	// The evaluator calls this method if a flag contains a prerequisite condition referencing
	// another flag.
	//
	// The method returns nil if the flag was not found. The DataProvider should treat any deleted
	// flag as "not found" even if the data store contains a deleted flag placeholder for it.
	GetFeatureFlag(key string) *ldmodel.FeatureFlag
	// GetSegment attempts to retrieve a user segment from the data store by key.
	//
	// The evaluator calls this method if a clause in a flag rule uses the OperatorSegmentMatch
	// test.
	//
	// The method returns nil if the segment was not found. The DataProvider should treat any deleted
	// segment as "not found" even if the data store contains a deleted segment placeholder for it.
	GetSegment(key string) *ldmodel.Segment
}

DataProvider is an abstraction for querying feature flags and user segments from a data store. The caller provides an implementation of this interface to NewEvaluator.

Flags and segments are returned by reference for efficiency only (on the assumption that the caller already has these objects in memory); the evaluator will never modify their properties.

type Evaluator

type Evaluator interface {
	// Evaluate evaluates a feature flag for the specified user.
	//
	// The flag is passed by reference only for efficiency; the evaluator will never modify any flag
	// properties. Passing a nil flag will result in a panic.
	//
	// The evaluator does not know anything about analytics events; generating any appropriate analytics
	// events is the responsibility of the caller, who can also provide a callback in prerequisiteFlagEventRecorder
	// to be notified if any additional evaluations were done due to prerequisites. The prerequisiteFlagEventRecorder
	// parameter can be nil if you do not need to track prerequisite evaluations.
	Evaluate(
		flag *ldmodel.FeatureFlag,
		user lduser.User,
		prerequisiteFlagEventRecorder PrerequisiteFlagEventRecorder,
	) ldreason.EvaluationDetail
}

Evaluator is the engine for evaluating feature flags.

func NewEvaluator

func NewEvaluator(dataProvider DataProvider) Evaluator

NewEvaluator creates an Evaluator, specifying a DataProvider that it will use if it needs to query additional feature flags or user segments during an evaluation.

To support big segments, you must use NewEvaluatorWithOptions and EvaluatorOptionBigSegmentProvider.

func NewEvaluatorWithBigSegments deprecated added in v1.4.0

func NewEvaluatorWithBigSegments(
	dataProvider DataProvider,
	bigSegmentProvider BigSegmentProvider,
) Evaluator

NewEvaluatorWithBigSegments is a deprecated way to specify a BigSegmentProvider.

Deprecated: Use NewEvaluatorWithOptions instead.

func NewEvaluatorWithOptions added in v1.5.0

func NewEvaluatorWithOptions(dataProvider DataProvider, options ...EvaluatorOption) Evaluator

NewEvaluatorWithOptions creates an Evaluator, specifying a DataProvider that it will use if it needs to query additional feature flags or user segments during an evaluation, and also any number of EvaluatorOption modifiers.

type EvaluatorOption added in v1.5.0

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

EvaluatorOption is an optional parameter for NewEvaluator.

func EvaluatorOptionBigSegmentProvider added in v1.5.0

func EvaluatorOptionBigSegmentProvider(bigSegmentProvider BigSegmentProvider) EvaluatorOption

EvaluatorOptionBigSegmentProvider is an option for NewEvaluator that specifies a BigSegmentProvider for evaluating big segment membership. If the parameter is nil, it will be treated the same as a BigSegmentProvider that always returns a "store not configured" status.

func EvaluatorOptionErrorLogger added in v1.5.0

func EvaluatorOptionErrorLogger(errorLogger ldlog.BaseLogger) EvaluatorOption

EvaluatorOptionErrorLogger is an option for NewEvaluator that specifies a logger for error reporting. The Evaluator will only log errors for conditions that should not be possible and require investigation, such as a malformed flag or a code path that should not have been reached. If the parameter is nil, no logging is done.

type PrerequisiteFlagEvent

type PrerequisiteFlagEvent struct {
	// TargetFlagKey is the key of the feature flag that had a prerequisite.
	TargetFlagKey string
	// User is the user that the flag was evaluated for. We pass this back to the caller, even though the caller
	// already passed it to us in the Evaluate parameters, so that the caller can provide a stateless function for
	// PrerequisiteFlagEventRecorder rather than a closure (since closures are less efficient).
	User lduser.User
	// PrerequisiteFlag is the full configuration of the prerequisite flag. We need to pass the full flag here rather
	// than just the key because the flag's properties (such as TrackEvents) can affect how events are generated.
	// This is passed by reference for efficiency only, and will never be nil; the PrerequisiteFlagEventRecorder
	// must not modify the flag's properties.
	PrerequisiteFlag *ldmodel.FeatureFlag
	// PrerequisiteResult is the result of evaluating the prerequisite flag.
	PrerequisiteResult ldreason.EvaluationDetail
}

PrerequisiteFlagEvent is the parameter data passed to PrerequisiteFlagEventRecorder.

type PrerequisiteFlagEventRecorder

type PrerequisiteFlagEventRecorder func(PrerequisiteFlagEvent)

PrerequisiteFlagEventRecorder is a function that Evaluator.Evaluate() will call to record the result of a prerequisite flag evaluation.

Directories

Path Synopsis
Package ldbuilders contains helpers for constructing the data model objects defined by ldmodel.
Package ldbuilders contains helpers for constructing the data model objects defined by ldmodel.
Package ldmodel contains the LaunchDarkly Go SDK feature flag data model.
Package ldmodel contains the LaunchDarkly Go SDK feature flag data model.

Jump to

Keyboard shortcuts

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