• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Development of Distributed Call Chain Tracing
2
3## When to Use
4
5HiTraceChain is the module that provides APIs to implement call chain tracing throughout a service process. With HiTraceChain, you can quickly obtain the run log for the call chain of a specified service process and locate faults in inter-device, inter-process, or inter-thread communications.
6
7## Available APIs
8
9The APIs for distributed call chain tracing are provided by the **hiTraceChain** module. For details, see [API Reference](../reference/apis/js-apis-hitracechain.md).
10
11**APIs for distributed call chain tracing**
12
13| API| Return Value| Description|
14| ------------------------------------------------------------------------------------------------------------------- | -------------- | ------------ |
15| hiTraceChain.begin(name: string, flags: number = HiTraceFlag.DEFAULT)                                               | HiTraceId      | Starts call chain tracing.|
16| hiTraceChain.tracepoint(mode: HiTraceCommunicationMode, type: HiTraceTracepointType, id: HiTraceId, msg?: string)   | void           | Creates a trace point.|
17| hiTraceChain.end(id: HiTraceId)                                                                                     | void           | Stops call chain tracing.|
18
19## How to Develop
20
21In this example, distributed call chain tracing begins when the application startup execution page is loaded and stops when the service usage is completed.
22
231. Create a JS application project. In the displayed **Project** window, choose **entry** > **src** > **main** > **js** > **default** > **pages** > **index**, and double-click **index.js**. Add the code to implement call chain tracing upon page loading. The sample code is as follows:
24
25   ```
26   import hiTraceChain from '@ohos.hiTraceChain'
27
28   export default {
29       data: {
30           title: ""
31       },
32       onInit() {
33           this.title = this.$t('strings.world');
34
35           // 1. Enable distributed call chain tracing.
36           let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC | hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN);
37
38           // 2. Start the service process.
39           console.log(`business start`);
40
41           // 3. Add a trace point.
42           hiTraceChain.tracepoint(hiTraceChain.HiTraceCommunicationMode.THREAD, hiTraceChain.HiTraceTracepointType.SS, asyncTraceId, "Just an example");
43
44           // 4. Keep the service process running.
45           console.log(`business running`);
46
47           // 5. End the service process.
48           console.log(`business end`);
49
50           // 6. Stop call chain tracing.
51           hiTraceChain.end(asyncTraceId);
52       }
53   }
54   ```
55
562. Click the run button on the application page. Then, you'll obtain the log information for service analysis.
57