Interface IFlagTracker
An interface for tracking changes in feature flag configurations.
Namespace: LaunchDarkly.Sdk.Server.Interfaces
Assembly: LaunchDarkly.ServerSdk.dll
Syntax
public interface IFlagTracker
Remarks
An implementation of this interface is returned by FlagTracker. Application code never needs to implement this interface.
Methods
FlagValueChangeHandler(String, User, EventHandler<FlagValueChangeEvent>)
Creates a handler for receiving notifications when a specific feature flag's value has changed for a specific set of user properties.
Declaration
EventHandler<FlagChangeEvent> FlagValueChangeHandler(string flagKey, User user, EventHandler<FlagValueChangeEvent> handler)
Parameters
Type | Name | Description |
---|---|---|
System.String | flagKey | the flag key to be evaluated |
User | user | the user properties for evaluation |
System.EventHandler<FlagValueChangeEvent> | handler | a handler that will receive a FlagValueChangeEvent |
Returns
Type | Description |
---|---|
System.EventHandler<FlagChangeEvent> | a handler to be added to FlagChanged |
Remarks
When you call this method, it first immediately evaluates the feature flag. It then
returns a new event handler which you can add to the FlagChanged event.
Whenever the specified feature flag changes, it re-evaluates the flag for the same
user, and calls your handler
if and only if the resulting value has
changed. In other words, this method filters the more general FlagChangeEvent
events to produce more specific FlagValueChangeEvent events.
All feature flag evaluations require an instance of User. If the
feature flag you are tracking does not have any user targeting rules, you must still
pass a dummy user such as User.WithKey("for-global-flags")
. If you do not
want the user to appear on your dashboard, use the Anonymous
property:
User.Builder("for-global-flags").Anonymous(true).Build()
.
Examples
var flagKey = "my-important-flag";
var userForFlagEvaluation = User.WithKey("user-for-evaluation");
var listenForNewValue = client.FlagTracker.FlagValueChangeHandler(
flagKey,
userForFlagEvaluation,
(sender, changeArgs) =>
{
System.Console.WriteLine("flag '" + changeArgs.Key
+ "' changed for " + userForFlagEvaluation.Key
+ " from " + changeArgs.OldValue
+ " to " + changeArgs.NewValue);
});
client.FlagTracker.FlagChanged += listenForNewValue;
Events
FlagChanged
An event for receiving notifications of feature flag changes in general.
Declaration
event EventHandler<FlagChangeEvent> FlagChanged
Event Type
Type | Description |
---|---|
System.EventHandler<FlagChangeEvent> |
Remarks
This event is raised whenever the SDK receives any change to any feature flag's configuration, or to a user segment that is referenced by a feature flag. If the updated flag is used as a prerequisite for other flags, the SDK assumes that those flags may now behave differently and sends flag change events for them as well.
Note that this does not necessarily mean the flag's value has changed for any particular user, only that some part of the flag configuration was changed so that it may return a different value than it previously returned for some user. If you want to track flag value changes, use FlagValueChangeHandler(String, User, EventHandler<FlagValueChangeEvent>).
Change events only work if the SDK is actually connecting to LaunchDarkly (or using the file data source). If the SDK is only reading flags from a database (ExternalUpdatesOnly) then it cannot know when there is a change, because flags are read on an as-needed basis.
Notifications will be dispatched on a background task. It is the listener's responsibility to return as soon as possible so as not to block subsequent notifications.
Examples
client.FlagTracker.FlagChanged += (sender, eventArgs) => { System.Console.WriteLine("a flag has changed: " + eventArgs.Key); };