C++ (server-side) SDK 2.x to 3.0 migration guide
C++ (server-side) SDK 2.x to 3.0 migration guide
C++ (server-side) SDK 2.x to 3.0 migration guide
This topic explains the changes in the C++ SDK 3.0 release and how to adapt code that uses a 2.x version of the C++ (server-side) SDK to use version 3.0 or later.
Version 3.0 includes several breaking changes. Additionally, if you use the Relay Proxy, you must update your Relay Proxy to at least version 7.0 before you update your SDK to version 3.0. To learn more, read the Relay Proxy 7.0 release notes. To upgrade to the latest Relay Proxy version, visit Relay Proxy releases on GitHub.
In the 2.x versions of the C (server-side) SDK, LaunchDarkly provided a C SDK with a C++ wrapper. To work in C++, you needed to build and install the C SDK and then use the provided C++ bindings.
Starting with the 3.0 version of the C++ (server-side) SDK, LaunchDarkly is providing a C++ SDK and a C wrapper. You can build the SDK from source or incorporate prebuilt artifacts. To work in C, you need to use the provided C bindings. The C wrapper is included in the release binaries.
How to make the transition between the 2.x C SDK and the 3.0 C++ SDK depends on whether your application is in C or C++.
If you are currently working in C, you’ll need to upgrade to the C++ v3.0 SDK and then work with the new C bindings.
Here’s how:
Decide whether to incorporate the SDK by building from source or by using prebuilt artifacts. The repository location has changed from version 2.x.
To build from source, read Incorporating the SDK using cmake.
To incorporate the SDK using prebuilt artifacts, read Incorporating the SDK using prebuilt artifacts.
Then, include the LaunchDarkly SDK headers for version 3:
The C wrapper is included in the release binaries.
Update your configuration code that specifies your SDK key. In version 2.x, you specified configuration details using the LDConfig struct. In version 3.0, you must use the LDClientConfigBuilder instead. Here’s how:
Update your code that creates the client. In version 2.x, you created a client using the LDClient struct. In version 3.0, you must use the LDServerSDK object instead. Here’s how:
The third parameter to LDServerSDK_Start is new. If you pass NULL, the call will block for maxwait milliseconds for the client to initialize. If you pass a pointer to a bool, you can also determine whether initialization succeeded.
Here’s how:
If you are currently working in C++, you’ll need to upgrade to the C++ v3.0 SDK. You’ll be able to work in version 3.0 of the SDK natively. You will no longer need a C++ binding.
Here’s how:
To build from source, read Incorporating the SDK using cmake.
To incorporate the SDK using prebuilt artifacts, read Incorporating the SDK using prebuilt artifacts.
Then, include the LaunchDarkly SDK headers for version 3:
Update your configuration code that specifies your SDK key. In version 2.x, you specified configuration details using the LDConfig struct. In version 3.0, you must use the ConfigBuilder instead. Here’s how:
Update your code that creates a single shared instance of the client. In version 2.x, you created a client using the LDClient struct. In version 3.0, you must use the Client object instead. Here’s how:
You can also examine the result to determine if initialization succeeded. Here’s how:
For a complete example of setting up the version 3.0 SDK, read Get started.
Many LaunchDarkly customers create targeting rules for feature flags based on a variety of different information, including attributes pertaining to users, organizations, devices, and more. In previous versions of the LaunchDarkly SDK, you could define this information in a user object, using a combination of built-in and custom attributes. Now you can define this information in a more structured way, using contexts.
Each context has a required attribute called kind that you can use to categorize context instances for targeting and Experimentation. You can also add other attributes. Attributes can be strings, booleans, numbers, arrays, or JSON objects.
When you evaluate a feature flag within your application, the flag’s targeting rules use information from one or more kinds of contexts. For example, you may know:
kind of “user”kind of “organization”kind of “device”This new version of the LaunchDarkly SDK requires you to evaluate feature flags using an evaluation context, which is an object containing one or more contexts.
To learn more about contexts, read Contexts and segments.
The 3.0 version of this SDK lets you use contexts. When you migrate from version 2.x, you must replace every instance of a user with a context. For the 3.0 version of the C++ SDK, this means changing each use of LDUser with the ContextBuilder.
LaunchDarkly assumes older versions of the SDK use user contexts
A context always has a kind attribute. When older versions of the C++ SDK send events to LaunchDarkly, LaunchDarkly will convert the users in those events to contexts with a kind of user.
If a flag configuration specifies any context kinds other than user, older versions of the C++ SDK will not evaluate the flag correctly. You must upgrade your SDK if you are going to use context kinds other than user in your flag configurations.
The primary differences between working with users and working with contexts include the following:
The methods for evaluating flags and determining flag evaluation reasons have changed slightly. The 3.0 version of the SDK includes the following changes:
*Variation methods now take a context, rather than a user, as a parameter. To learn more, read Evaluating flags and Flag evaluation reasons.USER_NOT_SPECIFIED evaluation error code was previously defined as, the user object or user key was not provided. It has been redefined to mean that the context was not provided or was invalid.Here’s how to evaluate a flag using a context:
Where you previously created users, now you can create contexts.
Here’s how to construct a basic context, as compared with constructing a user:
Here’s how to construct a basic context, with a context kind of something other than “user”:
Here’s how to construct a multi-context, which includes multiple context kinds:
This section describes the changes to built-in and custom attributes in the 3.0 version of the SDK.
In previous SDK versions, the user object included several built-in attributes for describing the user. It also included optional custom attributes, which you could add to a custom object within the user object and then populate.
In version 3.0, the only built-in attributes are kind, key, name, and anonymous. Kind, key, and name are strings, and anonymous is a boolean.
You can define additional attributes for a context by passing in a name and value for each. Additional attributes can be any JSON type, including boolean, number, string, array, or object.
Here’s how to construct a context with additional attributes, as compared with constructing a similar user:
In previous versions of the SDK, if you set the value of a user’s custom attribute to an object, you could not reference that object in evaluations. In version 3.0, if a context attribute’s value is a JSON object, you can reference properties of that object as the attribute in the targeting rules for a flag or segment.
Here’s how to add object attributes to a context:
In your flag or segment targeting, use / as the delimiter to refer to specific object fields. For example, you can use /address/city in your targeting. To learn more, read Targeting with flags.
In previous versions of the SDK, you could set the value of a user’s secondary attribute, as an optional secondary key for a user, using LDUserSetSecondary. The SDK would incorporate this attribute into the variation bucket assignment hash.
In version 3.0, the secondary attribute has been removed. If you were previously using this attribute as part of distinguishing percentage rollouts, that will no longer work for your users.
As in previous versions of the SDK, you can mark specific attributes of a context as private. This restricts the context data your application sends to LaunchDarkly, while still using that data for flag targeting.
In version 3.0, there are two scopes for which you can mark attributes as private:
In the first example, all attributes are marked private for all contexts. Only the context key and kind are sent to LaunchDarkly. In the second example, the “email” and “address” attributes are private for all contexts:
To learn more, read ConfigBuilder.
Here’s how to mark an attribute as private for a particular context:
For attributes that are objects, you can mark specific fields private, using the / delimiter followed by the attribute name, then the / delimiter followed by the JSON property within the value.
For example, for the attribute "address": { "street": "Main St", "city": "Springfield" }, you could set just the /address/street as private:
In previous versions of the SDK, multiple user objects could represent one person. For example, this could happen the first time a person logged in to your application. The person might be represented by an anonymous user before they logged in, and a different user after they logged in. You could associate these two LaunchDarkly users by sending an alias event in the SDK.
With the introduction of contexts, the person in this scenario is represented by two different context kinds. For example, before they log in, they might be represented by a device context. After they log in, they might be represented by a multi-context, for example, by one context kind based on their device and simultaneously by another context kind based on their user information.
The 3.0 version of the SDK removes the ability to send an alias event. If you currently alias users, you will need to remove this code when you migrate to version 3.0.
If you want to continue associating two contexts with each other, you can use two different context kinds, and then identify a multi-context that includes both individual contexts when you want the association to occur. Unlike the aliasing method, the association doesn’t persist between calls. You must send the contexts you want to associate in each variation or identify call and each track call.
Here’s how:
To learn more, read Identify() in Client.
If you use a persistent feature store without connecting to LaunchDarkly, you must update how you configure the SDK for this situation. In the 3.0 version of the SDK, this configuration is now set through a LazyLoadBuilder. The functionality remains the same: the SDK reads flag data from the persistent store lazily, in the background.
Here’s how:
If you currently use a persistent feature store and also have the SDK connect to LaunchDarkly, you can no longer do so in version 3.0. This is an uncommon configuration.
You could consider either using the Relay Proxy to populate a persistent feature store, or populating the store with another SDK that is capable of this. Then, configure the C++ SDK to use lazy loading.
To learn more, read Storing data.
In the 3.0 version of the SDK, several configuration options have changed:
PrivateAttributes and AllAttributesPrivate configuration options both now apply to all contexts of any context kind. To learn more, read Understanding changes to private attributes.PrivateAttributeNames is now called PrivateAttributes. To learn more, read Understanding changes to private attributes.inlineUsersInEvents option has been removed.LDConfigSetUserKeysCapacity and LDConfigSetUserKeysFlushInterval options are now deprecated. They have been replaced in this version with contextKeysCapacity.To learn more, read ConfigBuilder