openharmony 鸿蒙 js-apis-hitracechain

2025-06-12 浏览 (1)

@ohos.hiTraceChain (Distributed Tracing)

The hiTraceChain module implements call chain trace throughout a service process. It provides functions such as starting and stopping call chain trace and configuring trace points.

NOTE

The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import { hiTraceChain } from '@kit.PerformanceAnalysisKit';

HiTraceFlag

Enumerates trace flag types.

System capability: SystemCapability.HiviewDFX.HiTrace

NameValueDescription
DEFAULT0Default flag.
INCLUDE_ASYNC1Asynchronous call flag. By default, only synchronous calls are traced. If this flag is set, both synchronous and asynchronous calls will be traced.
DONOT_CREATE_SPAN1 << 1No span flag. By default, spans are created within a trace of synchronous and asynchronous service calls. If this flag is set, no spans are created.
TP_INFO1 << 2Trace point flag. By default, no trace point is added when trace is enabled. This flag is used for debugging. If this flag is set, trace points will be automatically added on the TX and RX sides of synchronous and asynchronous calls to output trace point and timestamp information. Trace points are classified into four types: CS, SR, SS, and CR. For a synchronous call, the output trace points are CS, SR, SS, and CR; for an asynchronous call, the output trace points are CS, SR, and SS.
NO_BE_INFO1 << 3No begin/end flag. By default, information about the start and end of the trace task is printed. If this flag is set, information about the start and end of the trace task will not be printed.
DISABLE_LOG1 << 4Log association flag. If this flag is set, information about the trace task will not be printed.
FAILURE_TRIGGER1 << 5Failure trigger flag. This flag is reserved for future use.
D2D_TP_INFO1 << 6Device-to-device trace point flag. It is a subset of TP_INFO. If this flag is set, trace points are added only for call chain trace between devices.

HiTraceTracepointType

Enumerates trace point types.

System capability: SystemCapability.HiviewDFX.HiTrace

NameValueDescription
CS0CS trace point.
CR1CR trace point.
SS2SS trace point.
SR3SR trace point.
GENERAL4General trace points except CS, CR, SS, and SR.

HiTraceCommunicationMode

Enumerates communication modes.

System capability: SystemCapability.HiviewDFX.HiTrace

NameValueDescription
DEFAULT0Default communication.
THREAD1Inter-thread communication.
PROCESS2Inter-process communication.
DEVICE3Inter-device communication.

HiTraceId

Defines a HiTraceId object.

System capability: SystemCapability.HiviewDFX.HiTrace

NameTypeMandatoryDescription
chainIdbigintYesCall chain ID.
spanIdnumberNoSpan ID.
parentSpanIdnumberNoParent span ID.
flagsnumberNoTrace flag combination.

hiTraceChain.begin

begin(name: string, flags?: number): HiTraceId

Starts call chain trace. This API returns the result synchronously.

System capability: SystemCapability.HiviewDFX.HiTrace

Parameters

NameTypeMandatoryDescription
namestringYesTraced service name.
flagsnumberNoTrace flag combination. For details, see HiTraceFlag.

Return value

TypeDescription
HiTraceIdHiTraceId instance.

Example

// Start call chain trace. The trace flag is the union of INCLUDE_ASYNC and DONOT_CREATE_SPAN.
let traceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC|hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
// End the call chain trace after the service logic is executed for several times.
hiTraceChain.end(traceId);

hiTraceChain.end

end(id: HiTraceId): void

Stops call chain trace. This API works in synchronous manner.

System capability: SystemCapability.HiviewDFX.HiTrace

Parameters

NameTypeMandatoryDescription
idHiTraceIdYesHiTraceId instance.

Example

// Enable call chain trace. The trace flag is DEFAULT.
let traceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.DEFAULT);
// End the call chain trace after the service logic is executed for several times.
hiTraceChain.end(traceId);

hiTraceChain.getId

getId(): HiTraceId

Obtains the trace ID. This API returns the result synchronously.

System capability: SystemCapability.HiviewDFX.HiTrace

Return value

TypeDescription
HiTraceIdHiTraceId instance.

Example

// Enable call chain trace. The trace flag is DEFAULT.
let traceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.DEFAULT);
// After the service logic is executed for several times, obtain the current trace ID.
let curTraceId = hiTraceChain.getId();
// The call chain IDs in the trace IDs obtained from the same call chain trace must be the same.
if (curTraceId.chainId != traceId.chainId) {
// Processing logic for exceptions.
}
// End the call chain trace after the service logic is executed for several times.
hiTraceChain.end(traceId);

hiTraceChain.setId

setId(id: HiTraceId): void

Sets a trace ID. This API returns the result synchronously.

System capability: SystemCapability.HiviewDFX.HiTrace

Parameters

NameTypeMandatoryDescription
idHiTraceIdYesHiTraceId instance.

Example

// Obtain the trace ID of the current call chain.
let traceId = hiTraceChain.getId();
// Set traceId to the obtained trace ID.
hiTraceChain.setId(traceId);

hiTraceChain.clearId

clearId(): void

Clears the trace ID. This API returns the result synchronously.

System capability: SystemCapability.HiviewDFX.HiTrace

Example

// Before the service starts, try to clear the trace ID.
hiTraceChain.clearId();
// Enable call chain trace. The trace flag is DEFAULT.
let traceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.DEFAULT);
// End the call chain trace after the service logic is executed for several times.
hiTraceChain.end(traceId);

hiTraceChain.createSpan

createSpan(): HiTraceId

Creates a trace span. This API works in synchronous manner.

System capability: SystemCapability.HiviewDFX.HiTrace

Return value

TypeDescription
HiTraceIdHiTraceId instance.

Example

// Enable call chain trace. The trace flag is DEFAULT.
let traceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.DEFAULT);
// Create a trace span after the service logic is executed for several times.
let spanTraceId = hiTraceChain.createSpan();
// The call chain IDs in the trace IDs obtained from the same call chain trace must be the same.
if (spanTraceId.chainId != traceId.chainId) {
// Processing logic for exceptions.
}
// The service is complete. Disable call chain trace.
hiTraceChain.end(traceId);

hiTraceChain.tracepoint

tracepoint(mode: HiTraceCommunicationMode, type: HiTraceTracepointType, id: HiTraceId, msg?: string): void

Triggers a trace point. This API returns the result synchronously.

System capability: SystemCapability.HiviewDFX.HiTrace

Parameters

NameTypeMandatoryDescription
modeHiTraceCommunicationModeYesCommunication mode for the trace point.
typeHiTraceTracepointTypeYesTrace point type.
idHiTraceIdYesHiTraceId instance for trace point triggering.
msgstringNoTrace description passed for trace point triggering.

Example

// Start call chain trace. The trace flag is the union of INCLUDE_ASYNC and DONOT_CREATE_SPAN.
let traceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC|hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
// Trigger the trace point after the service logic is executed for several times.
hiTraceChain.tracepoint(hiTraceChain.HiTraceCommunicationMode.THREAD, hiTraceChain.HiTraceTracepointType.SS, traceId, "Just a example");
// The service is complete. Disable call chain trace.
hiTraceChain.end(traceId);

hiTraceChain.isValid

isValid(id: HiTraceId): boolean

Checks whether a HiTraceId instance is valid. This API returns the result synchronously.

System capability: SystemCapability.HiviewDFX.HiTrace

Parameters

NameTypeMandatoryDescription
idHiTraceIdYesHiTraceId instance.

Return value

TypeDescription
booleanBoolean value indicating whether the HiTraceId instance is valid. The value true means yes and the value false means no.

Example

// Enable call chain trace. The trace flag is DEFAULT.
let traceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.DEFAULT);
// Set the value of traceIdIsvalid to true.
let traceIdIsvalid = hiTraceChain.isValid(traceId);
if (traceIdIsvalid) {
// Processing logic for the scenario where the validity check on the trace ID is successful.
}
// The service is complete. Disable call chain trace.
hiTraceChain.end(traceId);

hiTraceChain.isFlagEnabled

isFlagEnabled(id: HiTraceId, flag: HiTraceFlag): boolean

Checks whether the specified trace flag in the HiTraceId instance is enabled. This API returns the result synchronously.

System capability: SystemCapability.HiviewDFX.HiTrace

Parameters

NameTypeMandatoryDescription
idHiTraceIdYesHiTraceId instance.
flagHiTraceFlagYesSpecified trace flag.

Return value

TypeDescription
booleanBoolean value indicating whether the specified trace flag in the HiTraceId instance is enabled. The value true means yes and the value false means no.

Example

// Enable call chain trace. The trace flag is INCLUDE_ASYNC.
let traceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC);
// Set the value of enabledIncludeAsyncFlag to true.
let enabledIncludeAsyncFlag = hiTraceChain.isFlagEnabled(traceId, hiTraceChain.HiTraceFlag.INCLUDE_ASYNC);
if (enabledIncludeAsyncFlag) {
// Processing logic for the scenario where the INCLUDE_ASYNC trace flag has been set.
}
// The service is complete. Disable call chain trace.
hiTraceChain.end(traceId);

hiTraceChain.enableFlag

enableFlag(id: HiTraceId, flag: HiTraceFlag): void

Enables the specified trace flag in the HiTraceId instance. This API returns the result synchronously.

System capability: SystemCapability.HiviewDFX.HiTrace

Parameters

NameTypeMandatoryDescription
idHiTraceIdYesHiTraceId instance.
flagHiTraceFlagYesSpecified trace flag.

Example

// Enable call chain trace. The trace flag is INCLUDE_ASYNC.
let traceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC);
// Set the value of enabledDoNotCreateSpanFlag to false.
let enabledDoNotCreateSpanFlag = hiTraceChain.isFlagEnabled(traceId, hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
// Set the DONOT_CREATE_SPAN trace flag.
hiTraceChain.enableFlag(traceId, hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
// Set the value of enabledDoNotCreateSpanFlag to true.
enabledDoNotCreateSpanFlag = hiTraceChain.isFlagEnabled(traceId, hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
if (enabledDoNotCreateSpanFlag) {
// Processing logic for the scenario where the DONOT_CREATE_SPAN trace flag has been set.
}
// The service is complete. Disable call chain trace.
hiTraceChain.end(traceId);

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Performance Analysis Kit

harmony 鸿蒙Performance Analysis Kit

harmony 鸿蒙HiAppEvent

harmony 鸿蒙HiAppEvent_AppEventGroup

harmony 鸿蒙HiAppEvent_AppEventInfo

harmony 鸿蒙HiCollie

harmony 鸿蒙HiCollie_DetectionParam

harmony 鸿蒙HiCollie_SetTimerParam

harmony 鸿蒙HiDebug

harmony 鸿蒙HiDebug_MemoryLimit

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/VXpVIX