Skip to content

Trace Configuration

Trace Initialization Configuration

   // Enable trace
   FTTraceConfig *traceConfig = [[FTTraceConfig alloc]init];
   traceConfig.enableLinkRumData = YES;
   traceConfig.enableAutoTrace = YES;
   traceConfig.networkTraceType = FTNetworkTraceTypeDDtrace;
   [[FTMobileAgent sharedInstance] startTraceWithConfigOptions:traceConfig];
   let traceConfig = FTTraceConfig.init()
   traceConfig.enableLinkRumData = true
   traceConfig.enableAutoTrace = true
   FTMobileAgent.sharedInstance().startTrace(withConfigOptions: traceConfig)
Property Type Required Description
traceSamplingRate float No Trace sampling rate, range [0,100], supports decimals, default 100. 0.1 means 0.1%, 1 means 1%. Supported since SDK 1.6.8.
sampleRate int No Deprecated since SDK 1.6.8; retained for compatibility. Integer percentage sampling rate, range [0,100], default 100. Use traceSamplingRate instead.
networkTraceType FTNetworkTraceType No Set the distributed tracing type. Default is DDTrace. Currently supports Zipkin, Jaeger, DDTrace, Skywalking (8.0+), and TraceParent (W3C). If you integrate OpenTelemetry and select a corresponding trace type, refer to the supported types and agent configuration.
enableLinkRumData BOOL No Whether to associate with RUM data. Default NO.
enableAutoTrace BOOL No Set whether to enable automatic HTTP trace. Default NO; currently only NSURLSession is supported.
enableAutoTraceURLConnection BOOL No SDK 1.6.8+: Automatically inject Trace request headers for NSURLConnection. Default NO. Independent of enableAutoTrace, which controls URLSession. See NSURLConnection Trace.
traceInterceptor FTTraceInterceptor No Supports determining via URLRequest whether to perform custom distributed tracing. If intercepted, returns TraceContext; if not intercepted, returns nil. Supported in SDK 1.5.10 and later. Lower priority than Custom URLSession Collection.
Sampling rate parameter naming

For SDK 1.6.8 and later, use traceSamplingRate, a percentage from 0 to 100 that supports decimals. Invalid values (out of range, NaN, infinity) do not override the original configuration. Legacy sampleRate / samplerate support only integer percentages and are marked as deprecated; they may be removed in a future version. When reading a fractional sampling rate through the legacy getter, only the integer part is returned.

For the units and priority of remote sampling fields, see Remote Configuration Fields.

Trace Network Distributed Tracing

You can enable automatic mode through FTTraceConfig, and you can also manually add Trace-related data. The APIs for manual addition are as follows:

NSString *key = [[NSUUID UUID]UUIDString];
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
// Manual step: obtain traceHeader before the request and add it to the request headers
NSDictionary *traceHeader = [[FTTraceManager sharedInstance] getTraceHeaderWithKey:key url:url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
if (traceHeader && traceHeader.allKeys.count>0) {
    [traceHeader enumerateKeysAndObjectsUsingBlock:^(id field, id value, BOOL * __unused stop) {
        [request setValue:value forHTTPHeaderField:field];
    }];
}
NSURLSession *session=[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];

NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
   // Your code
}];

[task resume];
let url:URL = NSURL.init(string: "https://www.baidu.com")! as URL
if let traceHeader = FTExternalDataManager.shared().getTraceHeader(withKey: NSUUID().uuidString, url: url) {
     let request = NSMutableURLRequest(url: url)
     // Manual step: obtain traceHeader before the request and add it to the request headers
     for (a,b) in traceHeader {
         request.setValue(b as? String, forHTTPHeaderField: a as! String)
     }
     let task = URLSession.shared.dataTask(with: request as URLRequest) {  data,  response,  error in
        // Your code
     }
     task.resume()
}

Advanced URLSession Collection

For more granular Resource collection, Trace injection, and error filtering for a URLSession instance, see Custom URLSession Network Collection.

NSURLConnection Trace

Supported in SDK 1.6.8+

enableAutoTraceURLConnection controls automatic Trace request header injection for NSURLConnection and defaults to NO. It is independent of enableAutoTrace, which controls URLSession, and of enableTraceURLConnectionResource for RUM.

FTTraceConfig *traceConfig = [[FTTraceConfig alloc] init];
traceConfig.enableAutoTraceURLConnection = YES;
traceConfig.traceSamplingRate = 100;
[[FTSDKAgent sharedInstance] startTraceWithConfigOptions:traceConfig];
let traceConfig = FTTraceConfig()
traceConfig.enableAutoTraceURLConnection = true
traceConfig.traceSamplingRate = 100
FTSDKAgent.sharedInstance().startTrace(withConfigOptions: traceConfig)

The existing networkTraceType, traceInterceptor, and sampling configurations continue to take effect. To generate RUM Resources and their Trace associations at the same time, enable NSURLConnection Resource Collection separately, and configure enableLinkRumData and the Trace type according to the existing Trace association rules.

Feedback

Is this page helpful?