• Home
  • Integrations
  • SDKs
  • Guides
  • API docs
    No results for ""
    EXPAND ALL

    EDIT ON GITHUB

    Monitoring SDK status

    Read time: 5 minutes
    Last edited: Mar 16, 2023

    Overview

    This topic explains how various SDKs monitor the connection to LaunchDarkly to determine the most recent flag values.

    Monitoring the connection to LaunchDarkly

    Some SDKs expose some of their internal statuses through the Connection Status API to allow your application to monitor the SDK's status. This is provided primarily as a mechanism for the application to determine how recently the internal flag cache has been updated with the most recent values, as well as diagnosing potential reasons for the flag cache to be out of date.

    • Client-side SDKs
    • Server-side SDKs

    Client-side SDKs

    This feature is available in the following client-side SDKs:

    .NET (client-side)

    Expand .NET (client-side) code sample
    Supported versions

    This feature is available in the client-side .NET SDK's versions 2.0.0 and later.

    The client-side .NET SDK defines "data source status" as the status of the SDK's communication with LaunchDarkly to get feature flag data. If the streaming connection to LaunchDarkly is interrupted, or, in polling mode, if a polling request fails, the SDK might not be able to receive flag updates. The data source status will indicate this, providing an overall state such as "valid" or "interrupted" and information about the last error that occurred, if any.

    To check the data source status:

    var dataSourceStatus = client.DataSourceStatusProvider.Status;

    Alternatively, you can register an event handler that will receive a new status value whenever the status changes.

    Here is an example:

    client.DataSourceStatusProvider.StatusChanged +=
    (sender, status) -> {
    Console.WriteLine("new status is: {0}", status);
    };

    To learn more, read IDataSourceStatusProvider.

    Android

    Expand Android code sample
    Supported versions

    This feature is available in the Android SDK's versions 2.8.0 and later.

    The Android SDK exposes some of its internal status through APIs to let your application monitor it. This allows the application to determine how recently the internal flag cache has been updated with the most recent values, as well as diagnosing potential reasons for the flag cache to be out of date.

    The SDK has seven connectivity states dependent on its configuration, application foreground state, network connectivity, and calls explicitly setting the client offline or online.

    This table describes the states:

    Connection modeDescription
    STREAMINGThe SDK is either connected to the flag stream, or is actively attempting to acquire a connection.
    POLLINGThe SDK is in foreground polling mode because it was configured with streaming disabled.
    BACKGROUND_POLLINGThe SDK has detected the application is in the background and has transitioned to battery-saving background polling.
    BACKGROUND_DISABLEDThe SDK was configured with background polling disabled. The SDK has detected the application is in the background and is not attempting to update the flag cache.
    OFFLINEThe SDK has detected that the mobile device does not have an active network connection. It has ceased flag update attempts until the network status changes.
    SET_OFFLINEThe SDK has been explicitly set offline, either in the initial configuration, by setOffline(), or as a result of failed authentication to LaunchDarkly. The SDK will stay offline unless setOnline() is called.
    SHUTDOWNThe shutdown state indicates the SDK has been permanently shutdown as a result of a call to close().

    The SDK also internally stores a timestamp of the most recent successful and failed connections to LaunchDarkly, as well as information related to the most recent failed connection. The LDClient method getConnectionInformation() returns a structure allowing retrieval of these fields.

    LDClient client = LDClient.get();
    ConnectionInformation connectionInfo = client.getConnectionInformation();
    // One of the seven modes described above
    ConnectionInformation.ConnectionMode connectionMode =
    connectionInfo.getConnectionMode();
    // Most recent successful flag cache update in millis from the epoch
    // Or null if flags have never been retrieved
    Long lastSuccess = connectionInfo.getLastSuccessfulConnection();
    // Most recent unsuccessful flag cache update attempt in millis from the epoch
    // Or null if flag update has never been attempted
    Long lastError = connectionInfo.getLastFailedConnection();
    // Most recent failure or null
    LDFailure ldFailure = connectionInfo.getLastFailure();

    LDFailure is a LaunchDarklyException with an associated FailureType. It may include a .cause(), which is propagated from an underlying exception associated with the update's failure. The cause itself should be considered unstable because it is dependent on internal implementation, though the mechanism to retrieve it will be maintained.

    The failure types are summarized below:

    FailureTypeDescription
    INVALID_RESPONSE_BODYA response body received either through polling or streaming was unable to be parsed.
    NETWORK_FAILUREA network request for polling, or the EventSource stream reported a failure.
    UNEXPECTED_STREAM_ELEMENT_TYPEAn event was received through the stream with an unknown event name. This could indicate a newer SDK is available if new event kinds have become available through the flag stream since the SDK's release.
    UNEXPECTED_RESPONSE_CODEThis indicates the LDFailure is an instance of LDInvalidResponseCodeFailure. Continue reading below for more details.
    UNKNOWN_ERRORSome other issue occurred.

    If matching on the FailureType, use a default case to handle any future cases provided. The UNEXPECTED_RESPONSE_CODE case indicates that you can cast the LDFailure to a LDInvalidResponseCodeFailure for more information. This more specific failure includes a response code and whether the failure is considered retryable.

    Here is an example:

    LDClient client = LDClient.get();
    ConnectionInformation connectionInfo = client.getConnectionInformation();
    LDFailure ldFailure = connectionInfo.getLastFailure();
    if (ldFailure != null) {
    Timber.d("Received failure with message %s", ldFailure.getMessage());
    // Retrieve the failure type
    LDFailure.FailureType failureType = ldFailure.getFailureType();
    switch (failureType) {
    case INVALID_RESPONSE_BODY:
    Timber.d("Received invalid response body");
    break;
    case NETWORK_FAILURE:
    Timber.d("Network failure, may have bad connection");
    break;
    case UNEXPECTED_STREAM_ELEMENT_TYPE:
    Timber.d("Unexpected stream element, may require update");
    break;
    case UNEXPECTED_RESPONSE_CODE:
    LDInvalidResponseCodeFailure responseCodeFailure =
    (LDInvalidResponseCodeFailure) ldFailure;
    int responseCode = responseCodeFailure.getResponseCode();
    if (responseCodeFailure.isRetryable()) {
    Timber.d("Received invalid response code %d", responseCode);
    } else {
    Timber.d("Received invalid response code %d, giving up", responseCode);
    }
    break;
    case UNKNOWN_ERROR:
    default:
    Timber.d("Unknown error");
    break;
    }
    Throwable cause = ldFailure.getCause();
    if (cause != null) {
    // Do something with underlying cause
    }
    }

    A callback-based interface is also provided to allow notifying the application when the ConnectionMode changes, as well as whenever the LDFailure in ConnectionStatus changes. The application must provide a class instance implementing LDStatusListener to the SDK client instance method registerStatusListener to register the listeners with the SDK.

    Listener weak reference

    The SDK maintains only a weak reference to the registered LDStatusListener, so the application maintains a reference for as long as the application desires the listener to be available. This helps prevent creating a long-term reference to an Activity by creating a static internal class instance for use as a listener. By using a weak reference to the listener, the Activity can still be garbage collected normally, even if it maintains a registered LDStatusListener. We recommend unregistering the listener when it's finished.

    Here is a brief example:

    class MainActivity extends Activity {
    private LDClient client;
    private LDStatusListener ldStatusListener;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ldStatusListener = new LDStatusListener() {
    @Override
    public void onConnectionModeChanged(ConnectionInformation connectionInfo) {
    // handle new connection info
    }
    @Override
    public void onInternalFailure(LDFailure ldFailure) {
    // handle failure
    }
    };
    client = LDClient.get();
    client.registerStatusListener(ldStatusListener);
    }
    @Override
    protected void onDestroy() {
    super.onDestroy();
    client.unregisterStatusListener(ldStatusListener);
    }
    }

    Electron

    Expand Electron code sample

    By default, the client requests feature flag values only once per user. This happens once at startup time, and then each time you call identify(). You can also use a persistent connection to receive flag updates whenever they occur.

    Enable this behavior by setting streaming to true in the client options or calling client.setStreaming(true). LaunchDarkly pushes new values to the SDK, which updates the current feature flag state in the background, ensuring that variation() always returns the latest values.

    If you want to be notified when a flag has changed, you can use an event listener for a specific flag.

    Here's how:

    client.on('change:flag-key-123abc', (newValue, oldValue) => {
    console.log('The flag was ' + oldValue + ' and now it is ' + newValue);
    });

    Alternatively, you can listen for all feature flag changes.

    Here's how:

    client.on('change', (allFlagChanges) => {
    Object.keys(allFlagChanges).forEach((key) => {
    console.log('Flag ' + key + ' is now ' + allFlagChanges[key]);
    });
    });

    Subscribing to change events automatically turns on streaming mode as well, unless you have explicitly set streaming to false.

    Flutter

    Expand Flutter code sample

    The Flutter SDK exposes some of its internal status through APIs to let your application monitor it. This allows the application to determine how recently the internal flag cache has been updated with the most recent values, as well as diagnosing potential reasons for the flag cache to be out of date.

    The SDK also stores a timestamp of the most recent successful and failed connections to LaunchDarkly, as well as information related to the most recent failed connection. The LDClient method getConnectionInformation() returns a structure that lets you retrieve these fields.

    LDConnectionInformation connectionInfo = await LDClient.getConnectionInformation();
    // The current connection state
    LDConnectionState connectionState = connectionInfo.connectionState;
    // Most recent successful flag cache update
    DateTime? lastSuccess = connectionInfo.lastSuccessfulConnection;
    // Most recent unsuccessful flag cache update attempt
    DateTime? lastfailure = connectionInfo.lastFailedConnection;
    // Most recent failure or null
    LDFailure? ldFailure = connectionInfo.lastFailure;

    To learn more about the connection status fields, read the API docs.

    iOS

    Expand iOS code sample

    The iOS SDK exposes some of its internal status through the Connection Status API to let your application monitor it. This allows the application to determine how recently the internal flag cache has been updated with the most recent values, as well as diagnosing potential reasons for the flag cache to be out of date.

    The SDK has four connectivity states dependent on its configuration, application foreground state, network connectivity, and calls explicitly setting the client offline or online.

    This table describes the states:

    Connection ModeDescription
    streamingThe SDK has an active streaming connection.
    pollingThe SDK has an active polling connection.
    offlineThe SDK is set offline or has no network connection.
    establishingStreamingConnectionThe SDK is attempting to connect to LaunchDarkly by streaming.

    The SDK also stores a timestamp of the most recent successful and failed connections to LaunchDarkly, as well as information related to the most recent failed connection. lastKnownFlagValidity is nil if either no connection has ever been made successfully or if the SDK has an active streaming connection. It has a value if it is in polling mode and at least one poll has completed successfully, or if it is in streaming mode whenever the streaming connection closes. The LDClient.shared method getConnectionInformation() returns a structure that lets you retrieve these fields.

    The ConnectionInformation class can return four different values for lastFailureReason.

    This table describes the values:

    lastFailureReason valueDescription
    noneThis returns when no error has been recorded.
    unknownErrorThis returns when there is an internal error in the stream request.
    unauthorizedThis returns when an incorrect mobile key is provided.
    httpErrorThis returns when an error with an HTTP error code is present.

    You can listen to changes in ConnectionInformation.ConnectionMode in a similar manner to flag observers.

    Here is an example of the API:

    // Get current connection information
    let connectionInformation = LDClient.get()!.getConnectionInformation()
    // Setting a connection mode update observer
    LDClient.get()!.observeCurrentConnectionMode(owner: self) { [weak self] connectionMode in
    // do something after ConnectionMode was updated.
    }

    React Native

    Expand React Native code sample

    The React Native SDK exposes some of its internal status through the Connection Status API to allow your application to monitor the SDK's status. It provides this primarily as a mechanism for the application to determine how recently the internal flag cache has been updated with the most recent values, as well as diagnose potential reasons for the flag cache to be out of date.

    const connectionMode = await client.getConnectionMode();
    const lastSuccessfulConnection = await client.getLastSuccessfulConnection();
    const lastFailedConnection = await client.getLastFailedConnection();
    const lastFailureReason = await client.getLastFailure();

    To learn more about the return types, read the API documentation about LDConnectionMode and LDFailureReason.

    Server-side SDKs

    The following server-side SDKs provide two kinds of SDK status monitoring:

    • Data source status
    • Data store status

    Data source status is the status of the SDK's communication with LaunchDarkly to get feature flag data. If the streaming connection to LaunchDarkly is interrupted, or, in polling mode, if a polling request fails, the SDK might not be able to receive flag updates. The data source status will indicate this, providing an overall state such as "valid" or "interrupted" and information about the last error that occurred, if any.

    Data store status is the status of a database, such as Redis or DynamoDB, if the SDK has been configured to use one. If the SDK tries to read from or write to a database and encounters an error, the data store status will indicate this.

    This feature is available in the following server-side SDKs:

    .NET (server-side)

    Expand .NET (server-side) code sample
    Supported versions

    This feature is available in the .NET SDK's versions 6.0.0 and later.

    To check the data source status:

    var dataSourceStatus = client.DataSourceStatusProvider.Status;

    Alternatively, you can register an event handler that will receive a new status value whenever the status changes.

    Here is an example:

    client.DataSourceStatusProvider.StatusChanged +=
    (sender, status) -> {
    Console.WriteLine("new status is: {0}", status);
    };

    Go

    Expand Go code sample

    To check the data source status:

    dataSourceStatus := client.GetDataSourceStatusProvider().GetStatus()

    Alternatively, you can monitor a channel that provides a new status value whenever the status changes:

    dataStoreStatusChannel := client.GetDataStoreStatusProvider().AddStatusListener()
    go func() {
    for status := range dataStoreStatusChannel {
    fmt.Println("new status is: ", status)
    }
    }()

    To learn more, read GetDataSourceStatusProvider and GetDataStoreStatusProvider.

    Java

    Expand Java code sample

    To check the data source status:

    DataSourceStatusProvider.Status dataSourceStatus =
    client.getDataSourceStatusProvider().getStatus()

    Alternatively, you can register an event listener that receives a new status value whenever the status changes:

    client.getDataSourceStatusProvider().addStatusListener(
    status -> {
    System.out.println("new status is: " + status);
    }
    );

    To learn more, read DataSourceStatusProvider and DataStoreStatusProvider.