js-apis-hitracechain.md 9.9 KB
Newer Older
S
shawn_he 已提交
1 2
# Distributed Call Chain Tracing

3
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br>
S
shawn_he 已提交
4 5 6 7
> 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

8
```js
S
shawn_he 已提交
9 10 11 12 13 14 15 16
import hiTraceChain from '@ohos.hiTraceChain';
```


## HiTraceFlag

Enumerates trace flag types.

S
shawn_he 已提交
17 18
**System capability**: SystemCapability.HiviewDFX.HiTrace

19
| Name | Default Value | Description |
S
shawn_he 已提交
20
| -------- | -------- | -------- |
21 22 23 24 25
| DEFAULT           | 0      | Default flag. |
| INCLUDE_ASYNC     | 1      | Asynchronous call flag. By default, only synchronous calls are traced. If this flag is set, both synchronous and asynchronous calls will be traced. |
| DONOT_CREATE_SPAN | 1 << 1 | No span flag. By default, a span is automatically created during tracing of synchronous and asynchronous calls. If this flag is set, no span will be created. |
| TP_INFO           | 1 << 2 | Trace point flag. By default, no trace point is added when tracing 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](#hitracetracepointtype). 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_INFO        | 1 << 3 | No 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. |
S
shawn_he 已提交
26 27
| DISABLE_LOG       | 1 << 4 | Log association flag. If this flag is set, information about the trace task will not be printed. |
| FAILURE_TRIGGER   | 1 << 5 | Failure trigger flag. This flag is reserved for future use. |
28
| D2D_TP_INFO       | 1 << 6 | Device-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 tracing between devices. |
S
shawn_he 已提交
29 30 31 32 33

## HiTraceTracepointType

Enumerates trace point types.

S
shawn_he 已提交
34 35
**System capability**: SystemCapability.HiviewDFX.HiTrace

36
| Name| Default Value | Description |
S
shawn_he 已提交
37
| -------- | -------- | -------- |
38 39 40 41 42
| CS       | 0 | Client Send (CS) trace point. |
| CR       | 1 | Client Receive (CS) trace point. |
| SS       | 2 | Server Send (SS) trace point. |
| SR       | 3 | Server Receive (SR) trace point. |
| GENERAL  | 4 | General trace points except CS, CR, SS, and SR. |
S
shawn_he 已提交
43 44 45 46 47

## HiTraceCommunicationMode

Enumerates communication modes.

S
shawn_he 已提交
48 49
**System capability**: SystemCapability.HiviewDFX.HiTrace

50
| Name| Default Value | Description |
S
shawn_he 已提交
51
| -------- | -------- | -------- |
52
| DEFAULT  | 0 | Default communication mode. |
S
shawn_he 已提交
53 54 55
| THREAD   | 1 | Inter-thread communication. |
| PROCESS  | 2 | Inter-process communication. |
| DEVICE   | 3 | Inter-device communication. |
S
shawn_he 已提交
56 57 58 59 60

## HiTraceId

Defines a **HiTraceId** object.

S
shawn_he 已提交
61 62
**System capability**: SystemCapability.HiviewDFX.HiTrace

63
| Name | Type | Mandatory | Description |
S
shawn_he 已提交
64
| -------- | -------- | -------- | -------- |
65 66 67 68
| chainId      | bigint | Yes| Call chain ID. |
| spanId      | number | No| Span ID. |
| parentSpanId | number | No| Parent span ID. |
| flags        | number | No| Trace flag combination. |
S
shawn_he 已提交
69 70 71 72 73 74 75

## hiTraceChain.begin

begin(name: string, flags: number = HiTraceFlag.DEFAULT): HiTraceId

Starts call chain tracing. This API works in synchronous manner.

S
shawn_he 已提交
76 77
**System capability**: SystemCapability.HiviewDFX.HiTrace

S
shawn_he 已提交
78 79
**Parameters**

80
| Name | Type | Mandatory | Description |
S
shawn_he 已提交
81
| -------- | -------- | -------- | -------- |
82 83
| name  | string | Yes| Traced service name. |
| flags | number | Yes| Trace flag combination. For details, see [HiTraceFlag](#hitraceflag). |
S
shawn_he 已提交
84

S
shawn_he 已提交
85
**Return Value**
S
shawn_he 已提交
86

87
| Type | Description |
S
shawn_he 已提交
88
| -------- | -------- |
89
| [HiTraceId](#hitraceid) | **HiTraceId** instance. |
S
shawn_he 已提交
90 91 92

**Example**

93
```js
S
shawn_he 已提交
94 95
let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC | hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
```
S
shawn_he 已提交
96 97 98 99 100 101 102

## hiTraceChain.end

end(id: HiTraceId): void

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

S
shawn_he 已提交
103 104
**System capability**: SystemCapability.HiviewDFX.HiTrace

S
shawn_he 已提交
105 106
**Parameters**

107
| Name | Type | Mandatory | Description |
S
shawn_he 已提交
108
| -------- | -------- | -------- | -------- |
109
| id | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance. |
S
shawn_he 已提交
110 111

**Example**
S
shawn_he 已提交
112

113
```js
S
shawn_he 已提交
114 115 116 117
let asyncTraceId = hiTraceChain.begin("business");
// End the call chain tracing after the service logic is executed for several times.
hiTraceChain.end(asyncTraceId);
```
S
shawn_he 已提交
118 119 120 121 122 123 124

## hiTraceChain.getId

getId(): HiTraceId

Obtains the trace ID. This API works in synchronous manner.

S
shawn_he 已提交
125 126
**System capability**: SystemCapability.HiviewDFX.HiTrace

S
shawn_he 已提交
127
**Return Value**
S
shawn_he 已提交
128

129
| Type | Description |
S
shawn_he 已提交
130
| -------- | -------- |
131
| [HiTraceId](#hitraceid) | **HiTraceId** instance. |
S
shawn_he 已提交
132

S
shawn_he 已提交
133 134
**Example**

135
```js
S
shawn_he 已提交
136 137 138 139
let traceId = hiTraceChain.begin("business");
// Obtain the current trace ID after the service logic is executed for several times.
let curTraceId = hiTraceChain.getId();
```
S
shawn_he 已提交
140 141 142 143 144 145 146

## hiTraceChain.setId

setId(id: HiTraceId): void

Sets a trace ID. This API works in synchronous manner.

S
shawn_he 已提交
147 148
**System capability**: SystemCapability.HiviewDFX.HiTrace

S
shawn_he 已提交
149 150
**Parameters**

151
| Name | Type | Mandatory | Description |
S
shawn_he 已提交
152
| -------- | -------- | -------- | -------- |
153
| id | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance. |
S
shawn_he 已提交
154

S
shawn_he 已提交
155 156
**Example**

157
```js
X
xuyong 已提交
158
let asyncTraceId;
S
shawn_he 已提交
159 160 161 162
let traceId = hiTraceChain.begin("business");
// Set the current trace ID after the service logic is executed for several times.
hiTraceChain.setId(asyncTraceId);
```
S
shawn_he 已提交
163 164 165 166 167 168 169

## hiTraceChain.clearId

clearId(): void

Clears the trace ID. This API works in synchronous manner.

S
shawn_he 已提交
170 171
**System capability**: SystemCapability.HiviewDFX.HiTrace

S
shawn_he 已提交
172 173
**Example**

174
```js
S
shawn_he 已提交
175 176 177 178
let traceId = hiTraceChain.begin("business");
// Clear the current trace ID after the service logic is executed for several times.
hiTraceChain.clearId();
```
S
shawn_he 已提交
179 180 181 182 183 184 185

## hiTraceChain.createSpan

createSpan(): HiTraceId

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

S
shawn_he 已提交
186 187
**System capability**: SystemCapability.HiviewDFX.HiTrace

S
shawn_he 已提交
188
**Return Value**
S
shawn_he 已提交
189

190
| Type | Description |
S
shawn_he 已提交
191
| -------- | -------- |
192
| [HiTraceId](#hitraceid) | **HiTraceId** instance. |
S
shawn_he 已提交
193 194 195

**Example**

196
```js
S
shawn_he 已提交
197 198 199 200
let traceId = hiTraceChain.begin("business");
// Create a trace span after the service logic is executed for several times.
let spanTraceId = hiTraceChain.createSpan();
```
S
shawn_he 已提交
201 202 203 204 205 206 207

## hiTraceChain.tracepoint

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

Triggers a trace point. This API works in synchronous manner.

S
shawn_he 已提交
208 209
**System capability**: SystemCapability.HiviewDFX.HiTrace

S
shawn_he 已提交
210 211
**Parameters**

212
| Name | Type | Mandatory | Description |
S
shawn_he 已提交
213
| -------- | -------- | -------- | -------- |
214 215 216 217
| mode | [HiTraceCommunicationMode](#hitracecommunicationmode) | Yes| Communication mode for the trace point. |
| type | [HiTraceTracepointType](#hitracetracepointtype)| Yes| Trace point type. |
| id   | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance for trace point triggering. |
| msg  | string | No| Trace description passed for trace point triggering. |
S
shawn_he 已提交
218 219 220

**Example**

221
```js
S
shawn_he 已提交
222 223 224 225
let asyncTraceId = 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, asyncTraceId, "Just a example");
```
S
shawn_he 已提交
226 227 228 229 230 231 232

## hiTraceChain.isValid

isValid(id: HiTraceId): boolean

Checks whether a **HiTraceId** instance is valid. This API works in synchronous manner.

S
shawn_he 已提交
233 234
**System capability**: SystemCapability.HiviewDFX.HiTrace

S
shawn_he 已提交
235 236
**Parameters**

237
| Name | Type | Mandatory | Description |
S
shawn_he 已提交
238
| -------- | -------- | -------- | -------- |
239
| id  | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance. |
S
shawn_he 已提交
240

S
shawn_he 已提交
241
**Return Value**
S
shawn_he 已提交
242

243
| Type | Description |
S
shawn_he 已提交
244
| -------- | -------- |
245
| boolean | Returns **true** if the **HiTraceId** instance is valid; returns **false** otherwise. |
S
shawn_he 已提交
246 247 248

**Example**

249
```js
S
shawn_he 已提交
250 251 252
let traceId = hiTraceChain.begin("business");
let traceIdIsvalid = hiTraceChain.isValid(traceId);
```
S
shawn_he 已提交
253 254 255 256 257 258 259

## hiTraceChain.isFlagEnabled

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

Checks whether the specified trace flag in the **HiTraceId** instance is enabled. This API works in synchronous manner.

S
shawn_he 已提交
260 261
**System capability**: SystemCapability.HiviewDFX.HiTrace

S
shawn_he 已提交
262 263
**Parameters**

264
| Name | Type | Mandatory | Description |
S
shawn_he 已提交
265
| -------- | -------- | -------- | -------- |
266 267
| id  | [HiTraceId](#hitraceid) | Yes | **HiTraceId** instance. |
| flag | [HiTraceFlag](#hitraceflag) | Yes | Specified trace flag. |
S
shawn_he 已提交
268

S
shawn_he 已提交
269
**Return Value**
S
shawn_he 已提交
270

271
| Type | Description |
S
shawn_he 已提交
272
| -------- | -------- |
273
| boolean | Returns **true** if the specified trace flag in the **HiTraceId** instance is enabled; returns **false** otherwise. |
S
shawn_he 已提交
274 275 276

**Example**

277
```js
S
shawn_he 已提交
278 279 280 281
let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC);
// The value of enabledDoNotCreateSpanFlag is true.
let enabledDoNotCreateSpanFlag = hiTraceChain.isFlagEnabled(asyncTraceId, hiTraceChain.HiTraceFlag.INCLUDE_ASYNC);
```
S
shawn_he 已提交
282 283 284 285 286 287 288

## hiTraceChain.enableFlag

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

Enables the specified trace flag in the **HiTraceId** instance. This API works in synchronous manner.

S
shawn_he 已提交
289 290
**System capability**: SystemCapability.HiviewDFX.HiTrace

S
shawn_he 已提交
291
**Parameters**
292
| Name | Type | Mandatory | Description |
S
shawn_he 已提交
293
| -------- | -------- | -------- | -------- |
294 295
| id | [HiTraceId](#hitraceid) | Yes | **HiTraceId** instance. |
| flag | [HiTraceFlag](#hitraceflag) | Yes | Specified trace flag. |
S
shawn_he 已提交
296 297 298

**Example**

299
```js
S
shawn_he 已提交
300
let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC);
X
xuyong 已提交
301
hiTraceChain.enableFlag(asyncTraceId, hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
S
shawn_he 已提交
302 303
// The value of enabledDoNotCreateSpanFlag is true.
let enabledDoNotCreateSpanFlag = hiTraceChain.isFlagEnabled(asyncTraceId, hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
X
xuyong 已提交
304
```