Skip to content

Trace Configuration

Trace Initialization Configuration

FTTraceConfig *traceConfig = [[FTTraceConfig alloc] init];
traceConfig.enableLinkRumData = YES;
traceConfig.enableAutoTrace = YES;
traceConfig.networkTraceType = FTNetworkTraceTypeDDtrace;
[[FTSDKAgent sharedInstance] startTraceWithConfigOptions:traceConfig];
let traceConfig = FTTraceConfig()
traceConfig.enableLinkRumData = true
traceConfig.enableAutoTrace = true
FTSDKAgent.sharedInstance().startTrace(withConfigOptions: traceConfig)
Property Type Required Description
sampleRate int No Sampling rate. Value range [0,100], 0 means no collection, 100 means full collection, default value 100
networkTraceType NS_ENUM No Sets the trace type, default is DDTrace. Currently supports Zipkin, Jaeger, DDTrace, Skywalking (8.0+), TraceParent (W3C)
enableLinkRumData BOOL No Whether to associate with RUM data, default NO
enableAutoTrace BOOL No Sets whether to enable automatic HTTP Trace, default NO, currently only supports NSURLSession

Trace Network Tracing

Automatic mode can be enabled via FTTraceConfig configuration, or added manually. Trace-related data is passed through the FTTraceManager singleton, with related APIs as follows.

Usage

// FTTraceManager.h

/// Gets trace request header parameters
/// - Parameters:
///   - key: A unique identifier that can determine a specific request
///   - url: Request URL
/// - Returns: Dictionary of trace request header parameters
- (NSDictionary *)getTraceHeaderWithKey:(NSString *)key url:(NSURL *)url;
/// Gets trace request header parameters
/// - Parameters:
///   - key: A unique identifier that can determine a specific request
///   - url: Request URL
/// - Returns: Dictionary of trace request header parameters
open func getTraceHeader(withKey key: String, url: URL) -> [AnyHashable : Any]

Code Example

NSString *key = [[NSUUID UUID] UUIDString];
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
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(string: "https://www.baidu.com")! as URL
let traceHeader = FTTraceManager.sharedInstance().getTraceHeader(withKey: NSUUID().uuidString, url: url)
let request = NSMutableURLRequest(url: url)
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()