feature

package
v2.303.2 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2026 License: MIT Imports: 8 Imported by: 0

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 `openfeature.FeatureProvider` is available in the DI graph (i.e. it is not provided by the consuming service).

When a provider is present, `Register` appends lifecycle hooks that:

  • call `openfeature.SetProviderAndWait` during application start, and
  • call `openfeature.Shutdown` during application stop.

Telemetry hooks

When a metrics provider is available, `Register` installs OpenTelemetry hooks for OpenFeature so evaluations can emit metrics and traces.

Clients

`NewClient` constructs an OpenFeature client named after the service (via env.Name). Callers can use this client to evaluate feature flags.

Start with `Module`, `Register`, and `NewClient`.

Index

Constants

This section is empty.

Variables

Module wires OpenFeature into Fx/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 available, it also 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 available, Register installs OpenTelemetry hooks so evaluations emit metrics and traces.
  • Register appends lifecycle hooks that:
  • set the OpenFeature provider during application start (openfeature.SetProviderAndWait), and
  • shut down the OpenFeature SDK during application stop (openfeature.Shutdown).

Types

type Config

type Config struct {
	*client.Config `yaml:",inline" json:",inline" toml:",inline"`
}

Config configures OpenFeature client behavior for go-service.

It embeds `config/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).

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; 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 in this package). A service may have feature config present without wiring a provider, in which case OpenFeature behaves with its default provider semantics.

func (*Config) IsEnabled added in v2.115.0

func (c *Config) IsEnabled() bool

IsEnabled reports whether feature configuration is present and enabled.

It returns true only when both the feature wrapper config and the embedded client config are non-nil and enabled.

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.
	// When nil, telemetry hooks are not installed.
	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. It is typically used when constructing an OpenFeature client (see NewClient).
	Name env.Name
}

ProviderParams defines dependencies used to register an OpenFeature provider.

It is intended for dependency injection (Fx/Dig). The FeatureProvider dependency is marked as optional so services may include feature wiring without necessarily providing a concrete provider.

Jump to

Keyboard shortcuts

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