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

S
shawn_he 已提交
3
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
S
shawn_he 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
> 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 '@ohos.hiTraceChain';
```

## System Capabilities

SystemCapability.HiviewDFX.HiTrace

## HiTraceFlag

Enumerates trace flag types.

| Name| Default Value| Description|
| -------- | -------- | -------- |
| DEFAULT           | 0      | Default flag|
| INCLUDE_ASYNC     | 1      | Asynchronous call flag|
| DONOT_CREATE_SPAN | 1 << 1 | No span flag|
| TP_INFO           | 1 << 2 | Trace point flag|
| NO_BE_INFO        | 1 << 3 | No begin/end flag|
| DISABLE_LOG       | 1 << 4 | Log association flag|
| FAILURE_TRIGGER   | 1 << 5 | Failure trigger flag|
| D2D_TP_INFO       | 1 << 6 | Device-to-device trace point flag|

## HiTraceTracepointType

Enumerates trace point types.

| Name| Default Value| Description|
| -------- | -------- | -------- |
| CS       | 0 | Client Send|
| CR       | 1 | Client Receive|
| SS       | 2 | Server Send|
| SR       | 3 | Server Receive|
| GENERAL  | 4 | General|

## HiTraceCommunicationMode

Enumerates communication modes.

| Name| Default Value| Description|
| -------- | -------- | -------- |
| DEFAULT  | 0 | Default mode|
| THREAD   | 1 | Inter-thread communication|
| PROCESS  | 2 | Inter-process communication|
| DEVICE   | 3 | Inter-device communication|

## HiTraceId

Defines a **HiTraceId** object.

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

## hiTraceChain.begin

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

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

S
shawn_he 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| name  | string | Yes| Traced service name.|
| flags | number | Yes| Trace flag combination. For details, see [HiTraceFlag](#hitraceflag).|

**Return value**

| Type| Description|
| -------- | -------- |
| [HiTraceId](#hitraceid) | **HiTraceId** instance.|

**Example**

```
let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC | hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
```
S
shawn_he 已提交
89 90 91 92 93 94 95

## hiTraceChain.end

end(id: HiTraceId): void

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

S
shawn_he 已提交
96 97 98 99 100 101 102
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| id | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance.|

**Example**
S
shawn_he 已提交
103

S
shawn_he 已提交
104 105 106 107 108
```
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 已提交
109 110 111 112 113 114 115

## hiTraceChain.getId

getId(): HiTraceId

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

S
shawn_he 已提交
116 117 118 119 120
**Return value**

| Type| Description|
| -------- | -------- |
| [HiTraceId](#hitraceid) | **HiTraceId** instance.|
S
shawn_he 已提交
121

S
shawn_he 已提交
122 123 124 125 126 127 128
**Example**

```
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 已提交
129 130 131 132 133 134 135

## hiTraceChain.setId

setId(id: HiTraceId): void

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

S
shawn_he 已提交
136 137 138 139 140
**Parameters**

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

S
shawn_he 已提交
142 143 144 145 146 147 148
**Example**

```
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 已提交
149 150 151 152 153 154 155

## hiTraceChain.clearId

clearId(): void

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

S
shawn_he 已提交
156 157 158 159 160 161 162
**Example**

```
let traceId = hiTraceChain.begin("business");
// Clear the current trace ID after the service logic is executed for several times.
hiTraceChain.clearId();
```
S
shawn_he 已提交
163 164 165 166 167 168 169

## hiTraceChain.createSpan

createSpan(): HiTraceId

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

S
shawn_he 已提交
170
**Return value**
S
shawn_he 已提交
171

S
shawn_he 已提交
172 173 174 175 176 177 178 179 180 181 182
| Type| Description|
| -------- | -------- |
| [HiTraceId](#hitraceid) | **HiTraceId** instance.|

**Example**

```
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 已提交
183 184 185 186 187 188 189

## 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 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| 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.|

**Example**

```
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 已提交
206 207 208 209 210 211 212

## hiTraceChain.isValid

isValid(id: HiTraceId): boolean

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

S
shawn_he 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| id  | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance.|

**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the **HiTraceId** instance is valid; returns **false** otherwise.|

**Example**

```
let traceId = hiTraceChain.begin("business");
let traceIdIsvalid = hiTraceChain.isValid(traceId);
```
S
shawn_he 已提交
231 232 233 234 235 236 237

## 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 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| id  | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance.|
| flag | [HiTraceFlag](#hitraceflag) | Yes| Specified trace flag.|

**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified trace flag in the **HiTraceId** instance is enabled; returns **false** otherwise.|

**Example**

```
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 已提交
258 259 260 261 262 263 264

## 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 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| id  | [HiTraceId](#hitraceid) | Yes| **HiTraceId** instance.|
| flag | [HiTraceFlag](#hitraceflag) | Yes| Specified trace flag.|

**Example**

```
let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC);
hiTraceChain.enable(asyncTraceId, hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
// The value of enabledDoNotCreateSpanFlag is true.
let enabledDoNotCreateSpanFlag = hiTraceChain.isFlagEnabled(asyncTraceId, hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
```