Documentation
¶
Overview ¶
Package feature provides OpenFeature wiring and helpers for go-service.
This package integrates the OpenFeature Go SDK into go-service's DI/lifecycle model by:
- optionally registering an OpenFeature FeatureProvider at application start, and
- shutting down the OpenFeature SDK at application stop.
Provider registration and enablement ¶
Provider registration is intentionally optional. The Register function is wired via Module and is a no-op when no github.com/open-feature/go-sdk/openfeature.FeatureProvider is available in the DI graph (i.e. it is not provided by the consuming service).
This repository does not construct or connect a built-in OpenFeature provider from Config. Consuming services that need a remote or custom provider should use that config in their own provider constructor and provide the resulting github.com/open-feature/go-sdk/openfeature.FeatureProvider to the DI graph.
When a provider is present, Register installs any configured telemetry hooks immediately and appends lifecycle hooks that:
- call github.com/open-feature/go-sdk/openfeature.SetProviderWithContextAndWait during application start, and
- call github.com/open-feature/go-sdk/openfeature.ShutdownWithContext during application stop.
Register uses OpenFeature's package-level SDK APIs, so provider registration, hooks, and shutdown are process-global effects.
Telemetry hooks ¶
When both an OpenFeature provider and a metrics provider are available, Register installs OpenTelemetry hooks for OpenFeature so evaluations can emit metrics and traces. If no FeatureProvider is present, Register returns before installing hooks.
Trace event attributes are produced by the upstream OpenFeature OpenTelemetry hook. They may include OpenFeature semantic-convention fields such as the targeting key and evaluated flag value. Treat feature trace data as operational telemetry and protect exporter/back-end access accordingly.
Clients ¶
NewClient constructs an OpenFeature client named after the service. Callers can use this client to evaluate feature flags.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Module = di.Module( di.Constructor(NewClient), di.Register(Register), )
Module wires OpenFeature into go.uber.org/fx/go.uber.org/dig.
It provides:
- an OpenFeature client constructor (NewClient), and
- lifecycle registration (Register) that optionally installs a FeatureProvider.
Optional provider behavior ¶
Register is designed to be safe to include even when no OpenFeature provider is supplied by the consuming service. The FeatureProvider dependency is optional; if it is not present in the DI graph, Register becomes a no-op and OpenFeature uses its default provider semantics.
When a provider is present, Register appends lifecycle hooks to set the provider during startup and shut down the OpenFeature SDK during stop. If a metrics provider is also available, Register immediately installs OpenTelemetry hooks for metrics and traces.
Functions ¶
func NewClient ¶
func NewClient(name env.Name) *openfeature.Client
NewClient returns an OpenFeature client named after the service.
The returned client is created via openfeature.NewClient using the service name string. Callers use the client to evaluate feature flags/values against the currently configured provider.
func Register ¶
func Register(params ProviderParams)
Register registers an optional OpenFeature FeatureProvider with the application lifecycle.
Disabled behavior: if params.FeatureProvider is nil (not provided), Register is a no-op.
Enabled behavior:
- If a MetricProvider is also available, Register immediately installs OpenTelemetry hooks so evaluations emit metrics and traces.
- Register appends lifecycle hooks that set the OpenFeature provider during application start and shut down the OpenFeature SDK during application stop.
Register panics if OpenFeature metrics hook construction fails when both MetricProvider and FeatureProvider are provided.
Register uses OpenFeature's package-level SDK APIs, so provider registration, hooks, and shutdown are process-global effects.
Types ¶
type Config ¶
Config carries optional settings for service-owned OpenFeature integrations.
It embeds client.Config to reuse common client-side configuration fields that may be shared across feature-related integrations (for example address, timeout, TLS, retry/limiter, and key-value options).
The feature package does not consume Config directly or construct a provider from it. Services that need a remote/custom OpenFeature provider should consume this config in their own provider constructor and provide the resulting github.com/open-feature/go-sdk/openfeature.FeatureProvider to the DI graph.
Optional pointers and "enabled" semantics ¶
This config is intentionally optional. By convention across go-service configuration types, a nil *Config is treated as "feature disabled". The embedded *client.Config is also optional; Config.IsEnabled returns true only when both the outer *Config and the embedded *client.Config are non-nil/enabled.
Note: provider registration itself is controlled by the presence of an OpenFeature provider in the DI graph (see Register). A service may have feature config present without wiring a provider, in which case OpenFeature behaves with its default provider semantics.
type ProviderParams ¶
type ProviderParams struct {
di.In
// Lifecycle is used to register start/stop hooks that set the OpenFeature provider and shut down the SDK.
Lifecycle di.Lifecycle
// MetricProvider is an optional OpenTelemetry meter provider used to install OpenFeature telemetry hooks.
// Hooks are installed only when MetricProvider and FeatureProvider are both present.
MetricProvider metrics.MeterProvider
// FeatureProvider is the OpenFeature provider to register.
//
// It is optional: if not present in the DI graph, [Register] is a no-op and OpenFeature uses its
// default provider semantics.
FeatureProvider openfeature.FeatureProvider `optional:"true"`
// Name is the service name.
//
// Register does not currently use this field. [NewClient] consumes [env.Name] separately when constructing
// the OpenFeature client.
Name env.Name
}
ProviderParams defines dependencies used to register an OpenFeature provider.
It is intended for dependency injection (go.uber.org/fx/go.uber.org/dig). The FeatureProvider dependency is marked as optional so services may include feature wiring without necessarily providing a concrete provider.