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
| Edit this page View SourceFlagValueChangeHandler(string, Context, EventHandler<FlagValueChangeEvent>)
Creates a handler for receiving notifications when a specific feature flag's value has changed for a specific evaluation context.
Declaration
EventHandler<FlagChangeEvent> FlagValueChangeHandler(string flagKey, Context context, EventHandler<FlagValueChangeEvent> handler)
Parameters
Type | Name | Description |
---|---|---|
string | flagKey | the flag key to be evaluated |
Context | context | the evaluation context |
EventHandler<FlagValueChangeEvent> | handler | a handler that will receive a FlagValueChangeEvent |
Returns
Type | Description |
---|---|
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 Context. If the
feature flag you are tracking does not have any targeting rules, you must still
pass a dummy context such as Context.New("for-global-flags")
. If you do not
want the user to appear on your dashboard, use the Anonymous
property:
Context.Builder("for-global-flags").Anonymous(true).Build()
.
Examples
var flagKey = "my-important-flag";
var contextForFlagEvaluation = Context.New("context-for-evaluation");
var listenForNewValue = client.FlagTracker.FlagValueChangeHandler(
flagKey,
contextForFlagEvaluation,
(sender, changeArgs) =>
{
System.Console.WriteLine("flag '" + changeArgs.Key
+ "' changed for " + contextForFlagEvaluation.Key
+ " from " + changeArgs.OldValue
+ " to " + changeArgs.NewValue);
});
client.FlagTracker.FlagChanged += listenForNewValue;
Events
| Edit this page View SourceFlagChanged
An event for receiving notifications of feature flag changes in general.
Declaration
event EventHandler<FlagChangeEvent> FlagChanged
Event Type
Type | Description |
---|---|
EventHandler<FlagChangeEvent> |
Remarks
This event is raised whenever the SDK receives any change to any feature flag's configuration, or to a 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 evaluation context, only that some part of the flag configuration was changed so that it may return a different value than it previously returned for some context. If you want to track flag value changes, use FlagValueChangeHandler(string, Context, 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); };