Skip to content

Trace Configuration

await FTTracer().setConfig(
  enableLinkRUMData: true,
  enableAutoTrace: true,
);
Field Type Required Description
traceSamplingRate double No Trace sampling rate, range [0,100], decimals supported, default 100. 0.1 means 0.1%, 1 means 1%. Supported since SDK 0.5.8.
sampleRate double No Legacy probability configuration, retained for compatibility. Trace sampling rate, range [0,1], default 1. 0.1 means 10%, 1 means 100%. Use traceSamplingRate instead.
traceType enum TraceType No Trace type, default TraceType.ddTrace, supports ddTrace, zipkinMulti, zipkinSingle, traceparent, skywalking, jaeger
enableLinkRUMData bool No Whether to associate with RUM data, default false
enableAutoTrace bool No Whether to automatically add Trace Header to http requests, default false. Implemented by modifying HttpOverrides.global. If the project has customization requirements, inherit from FTHttpOverrides
enableNativeAutoTrace bool No Whether to enable automatic native network tracing for iOS NSURLSession and Android OKHttp, default false

Percentage Sampling Migration

Supported since SDK 0.5.8.

Sampling Rate Parameter Naming

Since SDK 0.5.8, traceSamplingRate is recommended. Its value is a percentage in the range 0~100, and decimals are supported. 0 means no sampling, and 100 means full sampling. Invalid values such as out-of-range values, NaN, or infinity throw ArgumentError.

The legacy sampleRate remains supported for compatibility and takes a probability in the range 0~1. For example, 0.1 in the legacy field means 10%, while 0.1 in the new field means 0.1%. When both the new and legacy fields are set, the new percentage field takes precedence, including an explicit 0.

For the units and precedence of remote sampling fields, see Flutter remote configuration fields.

For example, to migrate Trace sampling at 12.5%, use:

 await FTTracer().setConfig(
   enableAutoTrace: true,
-  sampleRate: 0.125,
+  traceSamplingRate: 12.5,
 );

Tracer Network Distributed Tracing

Automatic Collection

Enable it by turning on enableAutoTrace through FTTracer().setConfig.

Custom Tracer

Usage

/// Get trace HTTP request header data
/// [key] Unique ID
/// [url] Request URL
Future<Map<String, String>> getTraceHeader(String url, {String? key})

Code Example

void httpClientGetHttp() async {
  var url = 'http://reqeust.url.cn';
  var httpClient = HttpClient();
  String key = DateTime.now().millisecondsSinceEpoch.toString() + url;
  var errorMessage = "";
  HttpClientRequest request = await httpClient.getUrl(Uri.parse(url));
  HttpClientResponse? response;
  try {
    final traceHeaders =
        await FTTracer().getTraceHeader(request.uri.toString(), key: key);
    traceHeaders.forEach((key, value) {
      request.headers.add(key, value);
    });
    response = await request.close();
  } catch (exception) {
    errorMessage = exception.toString();
  } finally {
    Map<String, dynamic> requestHeader = {};
    Map<String, dynamic> responseHeader = {};

    request.headers.forEach((name, values) {
      requestHeader[name] = values;
    });
    if (response != null) {
      response.headers.forEach((name, values) {
        responseHeader[name] = values;
      });
    }
  }
}

For using the http and dio libraries, see example.

Feedback

Is this page helpful?