Skip to content

spotify/confidence-sdk-go

Confidence OpenFeature Go Provider

This repo contains the OpenFeature Go flag provider for Confidence.

OpenFeature

Before starting to use the provider, it can be helpful to read through the general OpenFeature docs and get familiar with the concepts.

Adding the dependency

require (
	github.com/spotify/confidence-sdk-go v0.2.2
)

It's also important to add the underlying OpenFeature SDK dependency:

require (
	github.com/open-feature/go-sdk v1.7.0
)

Creating and using the flag provider

Below is an example for how to create a OpenFeature client using the Confidence flag provider, and then resolve a flag with a boolean attribute. The provider is configured with an api key and a region, which will determine where it will send the resolving requests.

The flag will be applied immediately, meaning that Confidence will count the targeted user as having received the treatment.

You can retrieve attributes on the flag variant using property dot notation, meaning test-flag.boolean-key will retrieve the attribute boolean-key on the flag test-flag.

You can also use only the flag name test-flag and retrieve all values as a map with client.ObjectValue().

The flag's schema is validated against the requested data type, and if it doesn't match it will fall back to the default value.

import (
    o "github.com/open-feature/go-sdk/openfeature"
    c "github.com/spotify/confidence-sdk-go/pkg/confidence"
    p "github.com/spotify/confidence-sdk-go/pkg/provider"
)

confidenceSdk := c.NewConfidenceBuilder().SetAPIConfig(c.APIConfig{APIKey: "clientSecret"}).Build()
confidenceProvider := p.NewFlagProvider(confidenceSdk)


o.SetProvider(confidenceProvider)
client := o.NewClient("testApp")
	
attributes := make(map[string]interface{})
attributes["country"] = "SE"
attributes["plan"] = "premium"
attributes["user_id"] = "user1"

boolValue, error := client.BooleanValue(context.Background(), "test-flag.boolean-key", false, 
	o.NewEvaluationContext("", attributes))

Demo app

To run the demo app, replace the CLIENT_SECRET with client secret setup in the Confidence console, the flags with existing flags and execute the app with cd demo && go run GoDemoApp.go.