Documentation
¶
Overview ¶
Example ¶
The README's usage section is a copy of this example. Keep the two in step.
package main
import (
"context"
"fmt"
"log"
"os"
octopus "github.com/OctopusDeploy/openfeature-provider-go"
"github.com/open-feature/go-sdk/openfeature"
)
func main() {
productMetadata, err := octopus.NewProductMetadataWithVersion("YourProductName", "1.0.0")
if err != nil {
log.Fatal(err)
}
configuration := octopus.NewFeatureConfiguration(os.Getenv("OCTOPUS_FEATURE_FLAGS_CLIENT_IDENTIFIER"), productMetadata)
provider, err := octopus.NewFeatureProvider(configuration)
if err != nil {
log.Fatal(err)
}
// Wait for the flags at startup, so your first evaluations don't get the default value.
if err := openfeature.SetProviderAndWait(provider); err != nil {
log.Fatal(err)
}
defer openfeature.Shutdown()
client := openfeature.NewDefaultClient()
enabled, _ := client.BooleanValue(context.Background(), "to-the-moon-feature", false, openfeature.EvaluationContext{})
if enabled {
fmt.Println("🚀🚀🚀")
}
}
Output:
Index ¶
- type FeatureConfiguration
- type FeatureProvider
- func (p *FeatureProvider) BooleanEvaluation(_ context.Context, flag string, defaultValue bool, ...) openfeature.BoolResolutionDetail
- func (p *FeatureProvider) FloatEvaluation(_ context.Context, flag string, defaultValue float64, ...) openfeature.FloatResolutionDetail
- func (p *FeatureProvider) Hooks() []openfeature.Hook
- func (p *FeatureProvider) Init(evaluationContext openfeature.EvaluationContext) error
- func (p *FeatureProvider) InitWithContext(ctx context.Context, _ openfeature.EvaluationContext) error
- func (p *FeatureProvider) IntEvaluation(_ context.Context, flag string, defaultValue int64, ...) openfeature.IntResolutionDetail
- func (p *FeatureProvider) Metadata() openfeature.Metadata
- func (p *FeatureProvider) ObjectEvaluation(_ context.Context, flag string, defaultValue any, ...) openfeature.InterfaceResolutionDetail
- func (p *FeatureProvider) Shutdown()
- func (p *FeatureProvider) ShutdownWithContext(ctx context.Context) error
- func (p *FeatureProvider) StringEvaluation(_ context.Context, flag string, defaultValue string, ...) openfeature.StringResolutionDetail
- type ProductMetadata
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FeatureConfiguration ¶
type FeatureConfiguration struct {
// ClientIdentifier is the value of the Octopus variable
// Octopus.FeatureFlags.ClientIdentifier.
ClientIdentifier string
// ProductMetadata describes the product using the provider.
ProductMetadata ProductMetadata
// CacheDuration is how often the provider polls the feature flags service
// for new feature flag evaluations. Defaults to 1 minute.
CacheDuration time.Duration
// RequestTimeout is how long a request to the feature flags service may
// take before it is abandoned. It covers the whole request, connecting
// included, and should be kept well under CacheDuration so a stalled
// request can't hold up the next poll. Defaults to 10 seconds.
RequestTimeout time.Duration
// ReleaseVersionOverride overrides the application release version
// embedded in ClientIdentifier. It is sent to the feature flags service
// only when not empty.
ReleaseVersionOverride string
// Logger receives the provider's log output. Nil means the provider logs
// nothing.
Logger *slog.Logger
// HTTPClient is used for requests to the feature flags service, for
// example to configure a proxy, TLS or instrumentation. Nil means the
// provider creates and owns its own client. The provider never closes a
// client set here.
HTTPClient *http.Client
// ServerURI is the base URI of the feature flags service, for testing
// against a non-production service. Leave it unset in production. It must
// be an absolute http or https URI. Defaults to https://features.octopus.com.
ServerURI string
}
FeatureConfiguration configures a FeatureProvider. Create one with NewFeatureConfiguration, then assign any optional fields before passing it to NewFeatureProvider.
func NewFeatureConfiguration ¶
func NewFeatureConfiguration(clientIdentifier string, productMetadata ProductMetadata) *FeatureConfiguration
NewFeatureConfiguration returns a configuration with the required values set and every optional field at its default.
type FeatureProvider ¶
type FeatureProvider struct {
// contains filtered or unexported fields
}
FeatureProvider is an OpenFeature provider for Octopus feature flags. Create one with NewFeatureProvider and register it with the OpenFeature SDK.
func NewFeatureProvider ¶
func NewFeatureProvider(configuration *FeatureConfiguration) (*FeatureProvider, error)
NewFeatureProvider returns a provider for the given configuration. It returns an error if ClientIdentifier is empty, ClientIdentifier or ReleaseVersionOverride contains a line break or other control character, ProductMetadata wasn't created with one of its constructors, CacheDuration or RequestTimeout isn't positive, or ServerURI is set but isn't an absolute http or https URI. The provider keeps its own copy, so changes to configuration afterwards have no effect on it. It makes no requests until the SDK initializes it.
func (*FeatureProvider) BooleanEvaluation ¶
func (p *FeatureProvider) BooleanEvaluation(_ context.Context, flag string, defaultValue bool, flatCtx openfeature.FlattenedContext) openfeature.BoolResolutionDetail
BooleanEvaluation implements openfeature.FeatureProvider.
func (*FeatureProvider) FloatEvaluation ¶
func (p *FeatureProvider) FloatEvaluation(_ context.Context, flag string, defaultValue float64, _ openfeature.FlattenedContext) openfeature.FloatResolutionDetail
FloatEvaluation implements openfeature.FeatureProvider. Octopus only supports boolean flags, so it always returns the default.
func (*FeatureProvider) Hooks ¶
func (p *FeatureProvider) Hooks() []openfeature.Hook
Hooks implements openfeature.FeatureProvider.
func (*FeatureProvider) Init ¶
func (p *FeatureProvider) Init(evaluationContext openfeature.EvaluationContext) error
Init implements openfeature.StateHandler.
func (*FeatureProvider) InitWithContext ¶
func (p *FeatureProvider) InitWithContext(ctx context.Context, _ openfeature.EvaluationContext) error
InitWithContext implements openfeature.ContextAwareStateHandler. It fetches the feature flags, stopping early if ctx is done, and then checks for changes every CacheDuration until shutdown. A failed fetch isn't an error: the provider is still ready, and every evaluation returns the default with FLAG_NOT_FOUND until a later check succeeds. Calling it again while the provider is running waits for the first fetch and starts nothing new. After shutdown it returns an error, because a provider can't be initialized again.
func (*FeatureProvider) IntEvaluation ¶
func (p *FeatureProvider) IntEvaluation(_ context.Context, flag string, defaultValue int64, _ openfeature.FlattenedContext) openfeature.IntResolutionDetail
IntEvaluation implements openfeature.FeatureProvider. Octopus only supports boolean flags, so it always returns the default.
func (*FeatureProvider) Metadata ¶
func (p *FeatureProvider) Metadata() openfeature.Metadata
Metadata implements openfeature.FeatureProvider.
func (*FeatureProvider) ObjectEvaluation ¶
func (p *FeatureProvider) ObjectEvaluation(_ context.Context, flag string, defaultValue any, _ openfeature.FlattenedContext) openfeature.InterfaceResolutionDetail
ObjectEvaluation implements openfeature.FeatureProvider. Octopus only supports boolean flags, so it always returns the default.
func (*FeatureProvider) Shutdown ¶
func (p *FeatureProvider) Shutdown()
Shutdown implements openfeature.StateHandler.
func (*FeatureProvider) ShutdownWithContext ¶
func (p *FeatureProvider) ShutdownWithContext(ctx context.Context) error
ShutdownWithContext implements openfeature.ContextAwareStateHandler. It stops checking for changes and cancels any request in flight, even if ctx is already done, then waits for that to finish, returning ctx's error if ctx ends first. Evaluations keep using the last feature flags fetched. It can be called more than once, and the provider can't be initialized again afterwards.
func (*FeatureProvider) StringEvaluation ¶
func (p *FeatureProvider) StringEvaluation(_ context.Context, flag string, defaultValue string, _ openfeature.FlattenedContext) openfeature.StringResolutionDetail
StringEvaluation implements openfeature.FeatureProvider. Octopus only supports boolean flags, so it always returns the default.
type ProductMetadata ¶
type ProductMetadata struct {
// contains filtered or unexported fields
}
ProductMetadata describes the product using the provider. The feature flags service receives it with each request, for telemetry.
func NewProductMetadata ¶
func NewProductMetadata(name string) (ProductMetadata, error)
NewProductMetadata returns metadata with the product's name only. Characters that aren't valid in an HTTP token are removed from name, and an error is returned if none remain.
func NewProductMetadataWithVersion ¶
func NewProductMetadataWithVersion(name, version string) (ProductMetadata, error)
NewProductMetadataWithVersion returns metadata with the product's name and version. Characters that aren't valid in an HTTP token are removed from both, and an error is returned if none remain in either.
func (ProductMetadata) Version ¶
func (m ProductMetadata) Version() string
Version returns the product version, or "" if none was given.