IBM Cloud App Configuration SDK is used to perform feature flag and property evaluation based on the configuration on IBM Cloud App Configuration service.
IBM Cloud App Configuration is a centralized feature management and configuration service on IBM Cloud for use with web and mobile applications, microservices, and distributed environments.
Instrument your applications with App Configuration Go SDK, and use the App Configuration dashboard, API or CLI to define feature flags or properties, organized into collections and targeted to segments. Change feature flag states in the cloud to activate or deactivate features in your application or environment, when required. You can also manage the properties for distributed applications centrally.
There are a few different ways to download and install the IBM App Configuration Go SDK project for use by your Go application:
Use this command to download and install the SDK (along with its dependencies) to allow your Go application to use it:
go get -u github.com/IBM/appconfiguration-go-sdk
If your application is using Go modules, you can add a suitable import to your Go application, like this:
import (
AppConfiguration "github.com/IBM/appconfiguration-go-sdk/lib"
)
then run go mod tidy
to download and install the new dependency and update your Go application's go.mod file.
Initialize the sdk to connect with your App Configuration service instance.
appConfiguration := AppConfiguration.GetInstance()
appConfiguration.Init("region", "guid", "apikey")
appConfiguration.SetContext("collectionId", "environmentId")
- region : Region name where the App Configuration service instance is created. Use
AppConfiguration.REGION_US_SOUTH
for DallasAppConfiguration.REGION_EU_GB
for LondonAppConfiguration.REGION_AU_SYD
for Sydney
- guid : Instance Id of the App Configuration service. Obtain it from the service credentials section of the App Configuration dashboard.
- apikey : ApiKey of the App Configuration service. Obtain it from the service credentials section of the App Configuration dashboard.
- collectionId: Id of the collection created in App Configuration service instance under the Collections section.
- environmentId: Id of the environment created in App Configuration service instance under the Environments section.
Here, by default live update from the server is enabled. To turn off this mode see the below section
You can also work offline with local configuration file and perform feature and property related operations.
After appConfiguration.Init("region", "guid", "apikey")
, follow the below steps
appConfiguration.SetContext("collectionId", "environmentId", AppConfiguration.ContextOptions{
ConfigurationFile: "path/to/configuration/file.json",
LiveConfigUpdateEnabled: false,
})
- ConfigurationFile: Path to the JSON file, which contains configuration details.
- LiveConfigUpdateEnabled: Set this value to
false
if the new configuration values shouldn't be fetched from the server. Make sure to provide a proper JSON file in the path. By default, this value is enabled.
feature := appConfiguration.GetFeature("featureId")
if (feature.IsEnabled()) {
// feature flag is enabled
} else {
// feature flag is disabled
}
fmt.Println("Feature Name", feature.GetFeatureName())
fmt.Println("Feature Id", feature.GetFeatureId())
fmt.Println("Feature Type", feature.GetFeatureDataType())
fmt.Println("Feature is enabled", feature.IsEnabled())
features := appConfiguration.GetFeatures()
feature := features["featureId"]
fmt.Println("Feature Name", feature.GetFeatureName())
fmt.Println("Feature Id", feature.GetFeatureId())
fmt.Println("Feature Type", feature.GetFeatureDataType())
fmt.Println("Feature is enabled", feature.IsEnabled())
You can use the feature.GetCurrentValue(identityId, identityAttributes)
method to evaluate the value of the feature
flag. You should pass an unique identityId as the parameter to perform the feature flag evaluation. If the feature flag
is configured with segments in the App Configuration service, you can set the attributes values as a map.
identityId := "identityId"
identityAttributes := make(map[string]interface{})
identityAttributes["email"] = "ibm.com"
identityAttributes["city"] = "Bangalore"
featureVal := feature.GetCurrentValue(identityId, identityAttributes)
property := appConfiguration.GetProperty("propertyId")
fmt.Println("Property Name", property.GetPropertyName())
fmt.Println("Property Id", property.GetPropertyId())
fmt.Println("Property Type", property.GetPropertyDataType())
properties := appConfiguration.GetProperties()
property := properties["propertyId"]
fmt.Println("Property Name", property.GetPropertyName())
fmt.Println("Property Id", property.GetPropertyId())
fmt.Println("Property Type", property.GetPropertyDataType())
You can use the property.GetCurrentValue(identityId, identityAttributes)
method to evaluate the value of the
property. You should pass an unique identityId as the parameter to perform the property evaluation. If the property is
configured with segments in the App Configuration service, you can set the attributes values as a map.
identityId := "identityId"
identityAttributes := make(map[string]interface{})
identityAttributes["email"] = "ibm.com"
identityAttributes["city"] = "Bengaluru"
propertyVal := property.GetCurrentValue(identityId, identityAttributes)
To listen to the data changes add the following code in your application
appConfiguration.RegisterConfigurationUpdateListener(func () {
fmt.Println("Get your feature/property value now ")
})
appConfiguration.FetchConfigurations()
appConfiguration.EnableDebug(true)
Try this sample application in the examples folder to learn more about feature and property evaluation.
This project is released under the Apache 2.0 license. The license's full text can be found in LICENSE