OpenFeature Provider (Go)
Overview
The Mixpanel OpenFeature provider allows you to use Mixpanel Feature Flags through the standardized OpenFeature API. This provider wraps the Mixpanel Go SDK’s feature flags, letting you use the OpenFeature Go SDK with Mixpanel as the backend.
For the core Feature Flags SDK guide, see Feature Flags (Go).
Prerequisites
- You are on an Enterprise subscription plan
- You have the Mixpanel Go SDK installed (minimum version
v2.0.0) - You have your Project Token from your Mixpanel Project Settings
Installation
go get github.com/mixpanel/mixpanel-go/v2/openfeature
go get github.com/open-feature/go-sdkUsage
The provider wraps either a LocalFeatureFlagsProvider or RemoteFeatureFlagsProvider from the Mixpanel SDK.
With Local Evaluation
package main
import (
"context"
"fmt"
"time"
"github.com/mixpanel/mixpanel-go/v2/flags"
mpof "github.com/mixpanel/mixpanel-go/v2/openfeature"
"github.com/open-feature/go-sdk/openfeature"
)
func main() {
config := flags.DefaultLocalFlagsConfig()
config.PollingInterval = 60 * time.Second
// Create a provider with local evaluation (automatically starts polling)
provider, err := mpof.NewProviderWithLocalConfig("YOUR_PROJECT_TOKEN", config)
if err != nil {
panic(err)
}
defer provider.Shutdown()
openfeature.SetProvider(provider)
client := openfeature.NewClient("my-app")
showNewUI, _ := client.BooleanValue(context.Background(), "new-ui", false, openfeature.EvaluationContext{})
fmt.Println("Show new UI:", showNewUI)
}With Remote Evaluation
config := flags.DefaultRemoteFlagsConfig()
provider, err := mpof.NewProviderWithRemoteConfig("YOUR_PROJECT_TOKEN", config)
if err != nil {
panic(err)
}
openfeature.SetProvider(provider)
client := openfeature.NewClient("my-app")
variant, _ := client.StringValue(context.Background(), "checkout-flow", "control", openfeature.EvaluationContext{})With an Existing Flags Provider
If you already have a configured LocalFeatureFlagsProvider or RemoteFeatureFlagsProvider, you can wrap it directly:
import (
mixpanel "github.com/mixpanel/mixpanel-go/v2"
mpof "github.com/mixpanel/mixpanel-go/v2/openfeature"
)
mp := mixpanel.NewClient("YOUR_PROJECT_TOKEN",
mixpanel.WithLocalFlagsConfig(mixpanel.LocalFlagsConfig{
EnablePolling: true,
PollingInterval: 60 * time.Second,
}),
)
mp.LocalFlags().StartPollingForDefinitions()
defer mp.LocalFlags().StopPollingForDefinitions()
provider := mpof.NewProvider(mp.LocalFlags())
openfeature.SetProvider(provider)Supported Flag Types
| OpenFeature Method | Go Type |
|---|---|
BooleanValue / BooleanValueDetails | bool |
StringValue / StringValueDetails | string |
IntValue / IntValueDetails | int64 |
FloatValue / FloatValueDetails | float64 |
ObjectValue / ObjectValueDetails | interface{} |
Context and Identity
The evaluation context passed to each flag evaluation is forwarded to the Mixpanel flags provider.
targetingKeyhas no special meaning in this provider. It is treated as a regular context property.- Identity should be managed through the Mixpanel SDK (e.g., setting
distinct_idin the context).
Error Handling
The provider returns the default value on all errors, with a resolution error indicating the cause:
| Error Code | Condition |
|---|---|
PROVIDER_NOT_READY | The provider has not been initialized |
FLAG_NOT_FOUND | The requested flag does not exist |
TYPE_MISMATCH | The flag value does not match the requested type |
Use ValueDetails methods to inspect error codes:
details, err := client.BooleanValueDetails(ctx, "my-flag", false, openfeature.EvaluationContext{})
if details.ErrorCode != "" {
fmt.Printf("Flag error: %s - %s\n", details.ErrorCode, details.ErrorMessage)
}Lifecycle
Shutdown()stops background polling for local evaluation providers. Call it when your application exits.- The reason code for successful evaluations is
STATIC.
Was this page useful?