Documentation
¶
Overview ¶
Package posthog evaluates feature flags against PostHog, by way of OpenFeature.
Choosing it commits a deployment to two PostHog credentials, not one. The project API key identifies the project; the personal API key is what the SDK requires before it will evaluate a flag at all, and a config carrying only the project key is refused at validation rather than building an object that fails every evaluation. Endpoint selects EU Cloud or a self-hosted instance; empty means PostHog US Cloud.
A personal API key is a user-scoped credential with broad access, which is a different thing to hand a deployment than a project key. That is part of the cost of this provider, and it is the reason the launchdarkly sibling needs only one secret.
A missing boolean flag is indistinguishable from a disabled one ¶
PostHog's API answers false for a boolean flag it has never heard of, and there is nothing in that answer to separate it from a flag that exists and is off. So CanUseFeature returns (false, nil) for a flag nobody has created — it never reports featureflags.ErrFlagNotFound. The four typed getters do, because a string, number, or object flag has a value to be missing.
Callers relying on the three-way distinction featureflags documents should read that as: with this provider, the boolean path collapses two of the three answers into the inert one. It is the same outcome by a shorter route, not an error being swallowed.
Evaluation ¶
The five evaluation methods and their circuit-breaker protocol come from featureflags/internal/openfeatureflags, embedded, and are the same code the launchdarkly sibling runs. WithConfigModifiers reaches posthog.Config before the client is built, for anything this package's Config does not name.
Close is mandatory ¶
Each construction registers a uniquely-named provider in OpenFeature's process-global registry, and that registry has no removal API. Close detaches the registration by replacing it with the no-op provider and then closes the PostHog client. Skipping it leaks the client — and a service that rebuilds its flag manager on config reload leaks one per cycle until the process exits.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrNilConfig = platformerrors.New("missing posthog config") ErrMissingCredentials = platformerrors.New("missing PostHog credentials") )
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
ProjectAPIKey string `env:"PROJECT_API_KEY" json:"projectAPIKey,omitempty" yaml:"projectAPIKey,omitempty"`
PersonalAPIKey string `env:"PERSONAL_API_KEY" json:"personalAPIKey,omitempty" yaml:"personalAPIKey,omitempty"`
// Endpoint is the PostHog host. Leave empty for PostHog US Cloud (the SDK
// default); set it for EU Cloud (https://eu.posthog.com) or self-hosted.
Endpoint string `env:"ENDPOINT" json:"endpoint,omitempty" yaml:"endpoint,omitempty"`
}
func (*Config) ValidateWithContext ¶
ValidateWithContext validates the Config.
Both keys are required, and Endpoint is not — it defaults to PostHog US Cloud. PersonalAPIKey used to be documented here as optional "since it is only needed for the local-evaluation API", which is true of the SDK in general and false of this package: the thing being built is a feature flag manager, and the SDK answers every flag evaluation with "specifying a PersonalApiKey is required for using feature flags" without one. It was already required by NewFeatureFlagManager; the disagreement was that a config naming only the project key validated clean and then failed to build.
type FeatureFlagManager ¶
type FeatureFlagManager struct {
// Evaluator is the flag evaluation every OpenFeature-backed provider
// here does; see featureflags/internal/openfeatureflags. Embedded, so
// this type still presents the whole featureflags.FeatureFlagManager
// surface.
openfeatureflags.Evaluator
// contains filtered or unexported fields
}
FeatureFlagManager is the PostHog featureflags.FeatureFlagManager implementation, by way of OpenFeature. It is exported, and returned by NewFeatureFlagManager, so a caller who has chosen PostHog can depend on that choice rather than on the interface every flag backend shares.
func NewFeatureFlagManager ¶
func NewFeatureFlagManager(cfg *Config, circuitBreaker circuitbreaking.CircuitBreaker, opts ...Option) (*FeatureFlagManager, error)
NewFeatureFlagManager constructs a PostHog FeatureFlagManager backed by OpenFeature.
func (*FeatureFlagManager) Close ¶
func (f *FeatureFlagManager) Close() error
Close closes the PostHog client and detaches it from OpenFeature's process-global provider registry.
Each construction registers a uniquely-named provider in that registry, which has no removal API — so without the swap below, every reload cycle left another registration holding a reference to a client that had just been closed, and the process accumulated them until it exited. Replacing the registration with the no-op provider releases the client; the (small, clientless) map entry itself is not removable and is left behind.
type Option ¶
type Option func(*options)
Option configures the FeatureFlagManager this package constructs. The zero configuration works: an absent logger logs nowhere, an absent tracer provider traces nowhere, and an absent metrics provider records nothing.
func WithConfigModifiers ¶
WithConfigModifiers appends functions that mutate the PostHog client config before the client is built. Modifiers accumulate across repeated uses of this option and run in the order provided.
func WithMetricsProvider ¶
WithMetricsProvider attaches a metrics provider.
func WithTracerProvider ¶
WithTracerProvider attaches a tracer provider, enabling spans on every evaluation.