Skip to content

Dynamic Configuration and Dynamic Endpoint Updates

The examples in this document use FTSDKAgent and FTSDKConfig. Existing projects that use FTMobileAgent and FTMobileConfig can still integrate through the corresponding compatible interfaces.

Remote Configuration

Use remote configuration to obtain the environment variables delivered by the platform and adjust the SDK collection settings. You can register a unified callback when the SDK is initialized, or actively fetch the configuration when required by your business logic and perform custom processing before the configuration is applied.

Prerequisites

Before starting the SDK on the main thread, set FTSDKConfig.remoteConfiguration = YES and configure a valid reporting endpoint. Then initialize RUM and specify the application whose configuration is to be fetched through the App ID in FTRumConfig. For complete integration steps, see SDK Initialization and RUM Configuration.

Remote sampling configuration applies to the modules that have already been integrated; log and trace collection still require their own initialization.

Update Methods

Update Method Minimum Update Interval Callback
Automatically attempt an update on RUM initialization and when the app returns to the foreground remoteConfigMiniUpdateInterval, 12 hours by default Global callback registered at initialization
updateRemoteConfig without parameters The interval set at initialization Global callback
updateRemoteConfigWithMiniUpdateInterval:completion: (Swift: updateRemoteConfig(withMiniUpdateInterval:completion:)) The interval passed in this call, in seconds, as a non-negative integer; 0 skips the interval check You can specify a completion for this call

The minimum update interval limits the request frequency. The SDK does not refresh periodically every 12 hours, and restarting the app does not reset the saved request time. Active updates are asynchronous requests; passing 0 still requires the initialization conditions to be met. If a request is already being executed, this call is skipped and a failure result is returned to the callback passed in this call.

Callbacks and Custom Configuration

SDK >= 1.5.19 supports customizing the final configuration through the return value of FTRemoteConfigFetchCompletionBlock.

The callback executes before the configuration is applied. The parameters are described below:

Parameter Description
success The result of this fetch operation. YES does not guarantee that model or content is non-nil; NO means the request was not executed or the fetch failed.
error Failure details; nil on success.
model The modifiable configuration model. Check that it is non-nil before use.
content The configuration dictionary associated with this callback, from which custom fields can be read; when the remote content has not changed, it may come from the cached configuration.

When the operation succeeds and model is non-nil, return the modified model, which the SDK applies and caches; returning nil uses the model that the SDK passed in before the callback. On failure, return nil. The callback is not guaranteed to execute on the main thread, so switch to the main thread when updating the UI.

A non-nil completion passed in this call takes precedence over FTSDKConfig.remoteConfigFetchCompletionBlock, and the two are never executed at the same time. The updateRemoteConfig call without parameters uses the global callback; when nil is explicitly passed, the global callback is used after the request is actually initiated, and results that fail the pre-request checks may not be reported to the global callback.

When the actual request succeeds and the remote content has not changed, the callback is still executed with the existing model passed in. success = YES does not mean that the server-side configuration has changed.

Code Example

Starting from SDK 1.6.8, the following fractional percentage sampling fields are supported, with a value range of [0,100]. The legacy sampling properties are retained for compatibility; for the fields and their priorities, see Environment Variables.

The example omits the creation of config and rumConfig and stores the callback where it is accessible from both initialization and business refresh. The two scenarios run at different times: the callback is registered at startup, and active updates follow at business trigger points.

custom_userid is a custom field delivered remotely. When this field matches, the example sets all three sampling rates to 100%; to adjust the rates based on the currently logged-in user, you also need to use the user ID from your business side and handle configuration restoration after user switching.

// Core: define a reusable remote configuration callback.
FTRemoteConfigFetchCompletionBlock handler =
    ^FTRemoteConfigModel * _Nullable(
        BOOL success,
        NSError * _Nullable error,
        FTRemoteConfigModel * _Nullable model,
        NSDictionary<NSString *, id> * _Nullable content) {

    if (!success || !model) {
        return nil;
    }

    id userId = content[@"custom_userid"];
    if ([userId isKindOfClass:NSString.class] &&
        [userId isEqualToString:@"user_1"]) {
        model.sessionSamplingRate = @100; // RUM: 100%
        model.logSamplingRate = @100;     // Log: 100%
        model.traceSamplingRate = @100;   // Trace: 100%
    }

    return model; // The SDK uses the modified configuration.
};

// Scenario 1: set at SDK / RUM initialization (creation of config and rumConfig is omitted).
config.remoteConfiguration = YES;
config.remoteConfigFetchCompletionBlock = handler;

[FTSDKAgent startWithConfigOptions:config];
[[FTSDKAgent sharedInstance] startRumWithConfigOptions:rumConfig];

// Scenario 2: actively refresh on demand later in your business logic, reusing the same callback.
// 0 means the minimum update interval is ignored.
[FTSDKAgent updateRemoteConfigWithMiniUpdateInterval:0
                                        completion:handler];
// Core: define a reusable remote configuration callback.
let handler: FTRemoteConfigFetchCompletionBlock = { success, error, model, content in
    guard success, let model = model else {
        return nil
    }

    let userId = content?["custom_userid"] as? String
    if userId == "user_1" {
        model.sessionSamplingRate = 100 // RUM: 100%
        model.logSamplingRate = 100     // Log: 100%
        model.traceSamplingRate = 100   // Trace: 100%
    }

    return model // The SDK uses the modified configuration.
}

// Scenario 1: set at SDK / RUM initialization (creation of config and rumConfig is omitted).
config.remoteConfiguration = true
config.remoteConfigFetchCompletionBlock = handler

FTSDKAgent.start(withConfigOptions: config)
FTSDKAgent.sharedInstance().startRum(withConfigOptions: rumConfig)

// Scenario 2: actively refresh on demand later in your business logic, reusing the same callback.
// 0 means the minimum update interval is ignored.
FTSDKAgent.updateRemoteConfig(withMiniUpdateInterval: 0, completion: handler)

Dynamically Update the Reporting Endpoint

Use FTSDKAgent to dynamically switch the data reporting endpoint while the SDK is running. After the setting succeeds, subsequent data continues to be uploaded to the new endpoint.

SDK >= 1.5.21 supports setDatakitURL and setDatawayURL:clientToken:. Use either setDatakitURL or setDatawayURL:clientToken:. When using setDatawayURL:clientToken:, you must also pass the new clientToken.

Use Cases

FTSDKConfig supports not passing datakitUrl or datawayUrl during initialization. In this case, the SDK performs data collection first but does not upload the data.

After you later call [FTSDKAgent setDatakitURL:] or [FTSDKAgent setDatawayURL:clientToken:] to dynamically set the reporting endpoint, the SDK starts consuming the local cache and reporting data.

Note that while no upload endpoint is set, the collected data is still subject to the local cache limits. The cache limits are mainly affected by the following configurations:

  • FTLoggerConfig.logCacheLimitCount
  • FTRumConfig.rumCacheLimitCount
  • The database cache limit corresponding to FTSDKConfig.enableLimitWithDbSize

If the cache reaches its limit, the excess data may be discarded. Therefore, in this mode, add the upload endpoint as soon as possible, or set the cache limits appropriately for your business scenario.

[FTSDKAgent setDatakitURL:@"http://10.0.0.1:9529"];
[FTSDKAgent setDatawayURL:@"https://open.dataway.url" clientToken:@"your-client-token"];
FTSDKAgent.setDatakitURL("http://10.0.0.1:9529")
FTSDKAgent.setDatawayURL("https://open.dataway.url", clientToken: "your-client-token")
Method Type Required Description
setDatakitURL NSString Yes Dynamically sets the DataKit reporting endpoint. After the setting succeeds, the SDK continues to upload data to the new DataKit endpoint.
setDatawayURL:clientToken: NSString, NSString Yes Dynamically sets the Dataway reporting endpoint and clientToken. After the setting succeeds, the SDK continues to upload data to the new Dataway endpoint.