Trace Configuration¶
This document describes HarmonyOS Trace initialization configuration and distributed tracing.
Sampling field migration (
ft_sdk >= 0.1.18):setTraceSamplingRateuses a floating-point percentage from 0 to 100. The legacysetSamplingRateretains the 0 to 1 unit, so multiply the value by 100 during migration. The new field takes precedence over the old field only when it is explicitly set. Invalid values throw an exception and do not change the previously valid value. On versions earlier than this, continue using the old method. Deprecated does not mean removed: the old method retains its original unit and behavior.
Initial Configuration¶
import { FTSDK, FTTraceConfig, TraceType } from '@guancecloud/ft_sdk/Index';
const traceConfig = new FTTraceConfig()
.setTraceSamplingRate(80)
.setTraceType(TraceType.DDTRACE)
.setEnableAutoTrace(true)
.setEnableLinkRUMData(true);
FTSDK.installTraceConfig(traceConfig);
| Method Name | Type | Required | Description |
|---|---|---|---|
setTraceSamplingRate |
number |
No | Sets the sampling rate. The value range is [0,100], where 0 means no collection and 100 means full collection. The default value is 100. Decimal values are supported. Supported in ft_sdk 0.1.18 and later |
setSamplingRate |
number |
No | Deprecated compatibility API. The value range is [0,1] and the default value is 1. Use setTraceSamplingRate instead |
setTraceType |
TraceType |
No | Distributed tracing type. Defaults to DDTRACE |
setEnableLinkRUMData |
boolean |
No | Whether to associate with RUM data. Defaults to false |
setEnableAutoTrace |
boolean |
No | Whether to enable automatic HTTP Trace. Defaults to false |
setTraceHeaderHandler |
FTTraceHeaderHandler |
No | Sets the global Trace Header generator, which is used to customize request Trace Headers. getTraceContext(request) returns headers, traceId, and spanId in a single call. The SDK uses the same Trace context to inject request Headers and associate the RUM Resource. For an example, see Custom Trace |
Automatic Tracing¶
After setEnableAutoTrace(true) is enabled, the SDK automatically injects Trace Headers into requests that go through an RCP Session or HTTP/Axios and have the SDK interceptor attached.
import { rcp } from '@kit.RemoteCommunicationKit';
import { createFTRCPTrackConfig } from '@guancecloud/ft_sdk/Index';
const session = rcp.createSession(createFTRCPTrackConfig({
baseAddress: 'https://api.example.com'
}));
const request = new rcp.Request('/data', 'GET');
const response = await session.fetch(request);
Manually Obtain Trace Header¶
If your business uses another network framework, you can manually obtain the Trace Header and add it to the request:
Manually obtaining the Trace Header only generates and adds request Headers. To generate and report a RUM Resource, use the NetworkKit/RCP/Axios interceptors provided by the SDK, or call the RUM Resource APIs yourself to perform start/stop/addResource.
import { FTTraceManager } from '@guancecloud/ft_sdk/Index';
import http from '@ohos.net.http';
const url = 'https://api.example.com/data';
const resourceId = 'unique-resource-id';
const traceHeaders: Record<string, string> = FTTraceManager.getInstance().getTraceHeader(resourceId, url);
const httpRequest = http.createHttp();
const response = await httpRequest.request(url, {
method: http.RequestMethod.GET,
header: traceHeaders
});
Customize TraceHeader via NetworkKit/RCP/Axios¶
After FTTraceConfig.setEnableAutoTrace(true) is enabled, the SDK automatically injects Trace Header in the NetworkKit/RCP/Axios interceptor chain. If FTTraceConfig.setTraceHeaderHandler(...) is also configured, the SDK calls the custom Handler when generating the Trace Header, and uses the headers, traceId, and spanId returned by the Handler to complete the Header injection and RUM Resource association for this request.
Using getTraceContext(request) to customize the Trace Header returns the request Header, traceId, and spanId in a single call, ensuring that the request Header and the RUM Resource association use the same set of Trace contexts.
import {
FTSDK,
FTTraceConfig,
FTTraceManager,
FTTraceHeaderHandler,
FTTraceRequestContext,
FTTraceContext,
TraceType
} from '@guancecloud/ft_sdk/Index';
class CustomTraceHeaderHandler implements FTTraceHeaderHandler {
getTraceContext(request: FTTraceRequestContext): FTTraceContext {
const traceContext = FTTraceManager.getInstance().getTraceContextDirect(request.url);
traceContext.traceId = this.getHeaderValue(request.headers, 'x-existing-trace-id');
traceContext.spanId = this.getHeaderValue(request.headers, 'x-existing-span-id');
return traceContext;
}
private getHeaderValue(headers: Record<string, string>, key: string): string {
const directValue = headers[key];
if (directValue !== undefined) {
return directValue;
}
const normalizedKey = key.toLowerCase();
const headerKeys = Object.keys(headers);
for (let i = 0; i < headerKeys.length; i++) {
const headerKey = headerKeys[i];
if (headerKey.toLowerCase() === normalizedKey) {
return headers[headerKey];
}
}
return '';
}
}
import { FTSDK, FTTraceConfig, TraceType } from '@guancecloud/ft_sdk/Index';
const traceConfig = new FTTraceConfig()
.setEnableAutoTrace(true)
.setTraceType(TraceType.TRACEPARENT)
.setTraceHeaderHandler(new CustomTraceHeaderHandler());
FTSDK.installTraceConfig(traceConfig);
Trace Types¶
| Trace Type | Protocol | Main Headers |
|---|---|---|
DDTRACE |
DataDog Trace | x-datadog-trace-id, x-datadog-parent-id, x-datadog-origin, x-datadog-sampling-priority |
ZIPKIN_MULTI_HEADER |
Zipkin Multi-Header | X-B3-TraceId, X-B3-SpanId, X-B3-ParentSpanId, X-B3-Sampled |
ZIPKIN_SINGLE_HEADER |
Zipkin Single-Header | b3 |
JAEGER |
Jaeger | uber-trace-id |
TRACEPARENT |
W3C Trace Context | traceparent, tracestate |
SKYWALKING |
SkyWalking | sw8 |