Trace Configuration¶
Trace Initialization Configuration¶
| Method | Type | Required | Description |
|---|---|---|---|
| setTraceSamplingRate | Float | No | Sampling percentage, range [0,100], default 100; decimals are supported, e.g. 0.1 means 0.1%. 0 means no sampling, 100 means full sampling. When explicitly set, the new field takes precedence over the old field regardless of setting order. Supported with ft-sdk >= 1.7.6. |
| setSamplingRate | Float | No | Deprecated since ft-sdk 1.7.6 and kept for compatibility. Sets the sampling rate, valid range [0,1], 0 means no sampling, 1 means full sampling, default value is 1 |
| setTraceType | TraceType | No | Sets the tracing type, default DDTrace. Currently supports Zipkin, Jaeger, DDTrace, Skywalking (8.0+), and TraceParent (W3C). If connecting to OpenTelemetry, when selecting the corresponding trace type, please refer to the supported types and agent-related configuration |
| setEnableLinkRUMData | Boolean | No | Whether to associate with RUM data, default false |
| setEnableAutoTrace | Boolean | No | Sets whether to enable automatic HTTP trace, default false. Automatic instrumentation depends on ft-plugin and supports OkHttp; with ft-sdk >= 1.7.6-alpha03 and ft-plugin >= 1.3.9-alpha01, and with URLConnection Resource collection enabled, HttpURLConnection / HttpsURLConnection are also supported. See URLConnection Trace |
| setOkHttpTraceHeaderHandler | Callback | No | ASM sets the global FTTraceInterceptor.HeaderHandler, not set by default, supported with ft-sdk 1.6.8 or later. Since ft-sdk 1.6.17, overriding getTraceContext(Request) is preferred; it can return headers, traceId, and spanId at once. See Custom Trace |
HttpURLConnection / HttpsURLConnection Trace¶
With ft-sdk >= 1.7.6-alpha03 and ft-plugin >= 1.3.9-alpha01, first enable setEnableTraceUserResource(true) and setEnableHttpURLConnectionResource(true) as described in URLConnection automatic collection, then enable Trace Header injection via FTTraceConfig.setEnableAutoTrace(true). Enabling only the trace switch does not enable URLConnection wrapping; Resource URL filtering also excludes trace injection for that path.
The propagation format and sampling decisions follow the Trace type and sampling rate configured in FTTraceConfig. After enabling setEnableLinkRUMData(true), successfully injected trace_id / span_id values are associated with the corresponding Resource. The Trace initialization example above can be used together with RUM configuration.
URLConnection replaces existing trace headers of the selected propagation format. If the request already has trace context injected by other SDKs or the JS layer, avoid enabling this repeatedly to prevent the Resource trace information from being inconsistent with the headers received by the server. setOkHttpTraceHeaderHandler only applies to OkHttp and is not used to customize URLConnection Trace Headers.
Tracer Network Tracing¶
With FTTraceConfig configured with enableAutoTrace enabled, trace data is added automatically, or you can manually use FTTraceManager to add a Propagation Header to HTTP requests, as shown below.
String url = "https://request.url";
String uuid = "uuid";
// Get trace header parameters
Map<String, String> headers = FTTraceManager.get().getTraceHeader(uuid, url);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(chain -> {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder();
// Add trace header parameters to the request
for (String key : headers.keySet()) {
requestBuilder.header(key, headers.get(key));
}
Request request = requestBuilder.build();
Response response = chain.proceed(request);
if (response != null) {
Map<String, String> requestHeaderMap = new HashMap<>();
Map<String, String> responseHeaderMap = new HashMap<>();
for (Pair<String, String> header : response.request().headers()) {
requestHeaderMap.put(header.first, header.second);
}
for (Pair<String, String> header : response.headers()) {
responseHeaderMap.put(header.first, header.second);
}
}
return response;
}).build();
Request.Builder builder = new Request.Builder().url(url).method(RequestMethod.GET.name(), null);
client.newCall(builder.build()).execute();
val url = "https://request.url"
val uuid = "uuid"
// Get trace header parameters
val headers = FTTraceManager.get().getTraceHeader(uuid, url)
val client: OkHttpClient = OkHttpClient.Builder().addInterceptor { chain ->
val original = chain.request()
val requestBuilder = original.newBuilder()
// Add trace header parameters to the request
for (key in headers.keys) {
requestBuilder.header(key!!, headers[key]!!)
}
val request = requestBuilder.build()
val response = chain.proceed(request)
if (response != null) {
val requestHeaderMap = HashMap<String, String>()
val responseHeaderMap = HashMap<String, String>()
request.headers.forEach {
requestHeaderMap[it.first] = it.second
}
response.headers.forEach {
responseHeaderMap[it.first] = it.second
}
}
response
}.build()
val builder: Request.Builder = Request.Builder().url(url).method(RequestMethod.GET.name, null)
client.newCall(builder.build()).execute()
Customize Resource and TraceHeader with OkHttp Interceptor¶
Resource¶
When FTRUMConfig.setEnableTraceUserResource(true) or FTRUMConfig.setOkHttpResourceContentHandler(...) is enabled, the custom Interceptor in OkHttp takes priority.
new OkHttpClient.Builder()
.addInterceptor(new FTTraceInterceptor())
.addInterceptor(new FTResourceInterceptor(new FTResourceInterceptor.ContentHandlerHelper() {
@Override
public void onRequest(Request request, HashMap<String, Object> extraData) {
String contentType = request.header("Content-Type");
extraData.put("df_request_header", request.headers().toString());
if ("application/json".equals(contentType) ||
"application/x-www-form-urlencoded".equals(contentType) ||
"application/xml".equals(contentType)) {
extraData.put("df_request_body", request.body());
}
}
@Override
public void onResponse(Response response, HashMap<String, Object> extraData) throws IOException {
String contentType = response.header("Content-Type");
extraData.put("df_response_header", response.headers().toString());
if ("application/json".equals(contentType) ||
"application/xml".equals(contentType)) {
// Copy a portion of the body to avoid the cost of processing large data
ResponseBody body = response.peekBody(33554432);
extraData.put("df_response_body", body.string());
}
}
@Override
public void onException(Exception e, HashMap<String, Object> extraData) {
}
}))
.eventListenerFactory(new FTResourceEventListener.FTFactory())
.build();
OkHttpClient.Builder()
.addInterceptor(FTTraceInterceptor())
.addInterceptor(FTResourceInterceptor(object : FTResourceInterceptor.ContentHandlerHelper {
override fun onRequest(request: Request, extraData: HashMap<String, Any>) {
val contentType = request.header("Content-Type")
extraData["df_request_header"] = request.headers().toString()
if ("application/json" == contentType ||
"application/x-www-form-urlencoded" == contentType ||
"application/xml" == contentType) {
extraData["df_request_body"] = request.body()
}
}
override fun onResponse(response: Response, extraData: HashMap<String, Any>) {
val contentType = response.header("Content-Type")
extraData["df_response_header"] = response.headers().toString()
if ("application/json" == contentType ||
"application/xml" == contentType) {
// Copy part of the response body to avoid large data consumption
val body = response.peekBody(33554432)
extraData["df_response_body"] = body.string()
}
}
override fun onException(e: Exception, extraData: HashMap<String, Any>) {
// Handle exceptions
}
}))
.eventListenerFactory(FTResourceEventListener.FTFactory())
.build()
TraceHeader¶
When FTTraceConfig.setEnableAutoTrace(true) or FTTraceConfig.setOkHttpTraceHeaderHandler(...) is enabled, the custom Interceptor in OkHttp takes priority. The following uses W3C Trace Context as an example.
With ft-sdk < 1.4.1, disable
FTTraceConfig.setEnableAutoTrace(false). ft-sdk > 1.6.7 supports associating custom Trace Headers with RUM data. ft-sdk >= 1.6.17 addsgetTraceContext(Request), which returns request headers,traceId, andspanIdat once. Using this method is recommended.
Recommended: Use getTraceContext(Request)¶
new OkHttpClient.Builder()
.addInterceptor(new FTTraceInterceptor(new FTTraceInterceptor.HeaderHandler() {
@Override
public TraceContext getTraceContext(Request request) {
HashMap<String, String> headers = FTTraceManager.get()
.getTraceHeader(request.url().toString());
return TraceContext.Simple.fromTraceType(headers);
}
}))
.addInterceptor(new FTResourceInterceptor())
.eventListenerFactory(new FTResourceEventListener.FTFactory())
.build();
OkHttpClient.Builder()
.addInterceptor(
FTTraceInterceptor(object : FTTraceInterceptor.HeaderHandler() {
override fun getTraceContext(request: Request): TraceContext? {
val headers = FTTraceManager.get().getTraceHeader(request.url.toString())
return TraceContext.Simple.fromTraceType(headers)
}
})
)
.addInterceptor(FTResourceInterceptor())
.eventListenerFactory(FTResourceEventListener.FTFactory())
.build()
Backward-Compatible Approach: Implement getTraceHeader / getTraceID / getSpanID Separately¶
new OkHttpClient.Builder()
.addInterceptor(new FTTraceInterceptor(new FTTraceInterceptor.HeaderHandler() {
private String[] splits;
@Override
public HashMap<String, String> getTraceHeader(Request request) {
HashMap<String, String> map = new HashMap<>();
String headerString = FTTraceManager.get()
.getTraceHeader(request.url().toString())
// Get the propagation header from the headers
.get(FTTraceHandler.W3C_TRACEPARENT_KEY);
splits = headerString.split("-");
map.put(W3C_TRACEPARENT_KEY, headerString);
return map;
}
@Override
public String getSpanID() {
if (splits != null) {
return splits[2];
}
return null;
}
@Override
public String getTraceID() {
if (splits != null) {
return splits[1];
}
return null;
}
}))
.addInterceptor(new FTResourceInterceptor())
.eventListenerFactory(new FTResourceEventListener.FTFactory())
.build();
OkHttpClient.Builder()
.addInterceptor(
FTTraceInterceptor(object : FTTraceInterceptor.HeaderHandler {
private var splits: Array<String>? = null
override fun getTraceHeader(request: Request): HashMap<String, String> {
val map = HashMap<String, String>()
val headerString = FTTraceManager.get()
.getTraceHeader(request.url.toString())
// Get the propagation header from the headers
.get(FTTraceHandler.W3C_TRACEPARENT_KEY)
splits = headerString?.split("-")?.toTypedArray()
headerString?.let {
map[W3C_TRACEPARENT_KEY] = it
}
return map
}
override fun getSpanID(): String? {
return splits?.getOrNull(2)
}
override fun getTraceID(): String? {
return splits?.getOrNull(1)
}
})
)
.addInterceptor(FTResourceInterceptor())
.eventListenerFactory(FTResourceEventListener.FTFactory())
.build()
Add ResourceID to OkHttp¶
Add a uuid to OkHttp Request. Enable this when the same request is frequently sent concurrently. With ft-plugin 1.3.5 or later and ft-sdk 1.6.10 or later, after enabling FTSDKConfig.setEnableOkhttpRequestTag(true), a ResourceID can be automatically added to the Request.