1# Development of Distributed Call Chain Tracing 2 3## Introduction 4 5The hiTraceChain module provides APIs to implement call chain tracing throughout a service process. This can help you 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 7hiTraceChain is a lightweight implementation of the cloud-based distributed call chain tracing. It allows applications to trace cross-thread, cross-process, and cross-device service calls. The hiTraceChain module generates a unique **chainId** for a service process and passes it to various information (including application events, system time, and logs) specific to the service process. During debugging and fault locating, you can use the unique **chainId** to quickly correlate various information related to the service process. 8 9## Basic Concepts 10 11- **chainId** 12 13 Distributed call chain tracing ID, which is a part of **HiTraceId** and is used to identify the service process being traced. 14 15## Available APIs 16 17The APIs for distributed call chain tracing are provided by the **hiTraceChain** module. For details, see [API Reference](../reference/apis/js-apis-hitracechain.md). 18 19**APIs for distributed call chain tracing** 20 21| API| Return Value| Description| 22| ------------------------------------------------------------------------------------------------------------------- | -------------- | ------------ | 23| hiTraceChain.begin(name: string, flags: number = HiTraceFlag.DEFAULT) | HiTraceId | Starts call chain tracing.| 24| hiTraceChain.tracepoint(mode: HiTraceCommunicationMode, type: HiTraceTracepointType, id: HiTraceId, msg?: string) | void | Creates a trace point.| 25| hiTraceChain.end(id: HiTraceId) | void | Stops call chain tracing.| 26 27## How to Develop 28 29In this example, distributed call chain tracing begins when the application startup execution page is loaded and stops when the service usage is completed. 30 311. 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: 32 33 ``` 34 import hiTraceChain from '@ohos.hiTraceChain' 35 36 export default { 37 data: { 38 title: "" 39 }, 40 onInit() { 41 this.title = this.$t('strings.world'); 42 43 // 1. Enable distributed call chain tracing. 44 let asyncTraceId = hiTraceChain.begin("business", hiTraceChain.HiTraceFlag.INCLUDE_ASYNC | hiTraceChain.HiTraceFlag.DONOT_CREATE_SPAN); 45 46 // 2. Start the service process. 47 console.log(`business start`); 48 49 // 3. Add a trace point. 50 hiTraceChain.tracepoint(hiTraceChain.HiTraceCommunicationMode.THREAD, hiTraceChain.HiTraceTracepointType.SS, asyncTraceId, "Just an example"); 51 52 // 4. Keep the service process running. 53 console.log(`business running`); 54 55 // 5. End the service process. 56 console.log(`business end`); 57 58 // 6. Stop call chain tracing. 59 hiTraceChain.end(asyncTraceId); 60 } 61 } 62 ``` 63 642. Click the run button on the application page. Then, you'll obtain the log information for service analysis. 65