README_zh.md

HiTraceChain ArkTS接口使用样例

介绍

本示例主要展示了使用HiTraceChain提供分布式跟踪接口。

HiTraceChain是基于云计算分布式跟踪调用链思想,在端侧业务流程(涉及跨线程、跨进程、跨设备)中的一种轻量级实现。hiTraceChain在业务控制面流程中,生成和传递唯一跟踪标识,在业务流程中输出的各类信息中(包括应用事件、系统时间、日志等)记录该跟踪标识。在调试、问题定位过程中,开发者可以通过该唯一跟踪标识将本次业务流程端到端的各类信息快速关联起来。

本样例介绍在async/await和promise/then异步任务中使用HiTraceChain。

该工程中的展示的代码详细描述可查如下链接:

效果预览

主页
<img src="./screenshots/Snipaste_2025-10-17_15-32-47.png" alt="z" width = "400">

使用说明

  1. 在DevEco Studio Terminal 中执行如下命令:
$ hitrace -t 10 app | grep onClick
  1. 点击设备上的“clickTime=0”按钮(需在10秒内完成,否则步骤3捕获不到trace数据),触发业务逻辑。 使用关键字“testTag”过滤日志,查看该业务代码打印的hilog日志。
06-04 17:46:45.156   55451-55451   C02D33/com.exa...tion/HiTraceC  com.examp...lication  I     [a92ab116052648e 0 0]HiTraceBegin name:testTag: hiTraceChain begin flags:0x01.
06-04 17:46:45.157   55451-55451   A00000/com.exa...ation/testTag  com.examp...lication  I     [a92ab116052648e 0 0]promise task
06-04 17:46:45.158   55451-55451   A00000/com.exa...ation/testTag  com.examp...lication  I     [a92ab116052648e 0 0]test1_1
06-04 17:46:45.158   55451-55451   A00000/com.exa...ation/testTag  com.examp...lication  I     [a92ab116052648e 0 0]test2
06-04 17:46:45.158   55451-55451   A00000/com.exa...ation/testTag  com.examp...lication  I     hiTraceChain end in main thread
06-04 17:46:45.158   55451-55451   A00000/com.exa...ation/testTag  com.examp...lication  I     [a92ab116052648e 3457eff 0]test1_2
06-04 17:46:45.158   55451-55451   A00000/com.exa...ation/testTag  com.examp...lication  I     [a92ab116052648e 3457eff 0]test3
06-04 17:46:45.158   55451-55451   A00000/com.exa...ation/testTag  com.examp...lication  E     [a92ab116052648e 1bb5a1b 35d9c46]Random number is too small
06-04 17:46:45.158   55451-55451   A00000/com.exa...ation/testTag  com.examp...lication  I     hiTraceChain end in promise/then
06-04 17:46:45.158   55451-55451   A00000/com.exa...ation/testTag  com.examp...lication  I     [a92ab116052648e 2ddfb70 3457eff]test1_3
06-04 17:46:45.158   55451-55451   A00000/com.exa...ation/testTag  com.examp...lication  I     [a92ab116052648e 225a1d9 2ddfb70]then task
06-04 17:46:45.158   55451-55451   A00000/com.exa...ation/testTag  com.examp...lication  I     hiTraceChain end in async/await
06-04 17:46:45.163   55451-55451   A00000/com.exa...ation/testTag  com.examp...lication  I     [a92ab116052648e 3a75cb2 520a92]Succeeded in writing an app event
06-04 17:46:45.163   55451-55451   A00000/com.exa...ation/testTag  com.examp...lication  I     hiTraceChain end in hiAppEvent
  1. 在DevEco Studio Terminal窗口查看trace数据,HiTraceChain跟踪开启期间,HiTraceMeter打点得到的trace日志会自动携带HiTraceId信息。
 e.myapplication-55451   (  55451) [010] .... 27164.174417: tracing_mark_write: B|55451|H:[a92ab116052648e,0,0]#onClick|M62|clickTime=1

工程目录

└── entry
    └── src
        └── main
            └── ets
                ├── entryability
                │   └── EntryAbility.ets
                ├── entrybackupability
                │   └── EntryBackupAbility.ets
                └── pages
                    └── Index.ets     		//主页

具体实现

  1. ArkTS项目可以在"@kit.PerformanceAnalysisKit"中导入对应模块即可在各种场景下调用对应函数,如在index.ets中直接调用,或者在EntryAbility.ets函数中在应用的各个生命周期内添加功能函数以实现应用自动在“启动”或“结束”时进行性能分析。

相关权限

不涉及。

依赖

不涉及。

约束与限制

  1. 本示例仅支持标准系统上运行,支持设备:华为手机;
  2. 本示例已适配API20版本SDK,版本号:6.0.0.47;
  3. 本示例需要使用DevEco Studio(6.0.0.858)及以上版本才可编译运行。

下载

如需单独下载本工程,执行如下命令:

git init
git config core.sparsecheckout true
echo code/DocsSample/PerformanceAnalysisKit/HiTrace/HitraceChain_ArkTS_Sample_A/ > .git/info/sparse-checkout
git remote add origin https://gitcode.com/openharmony/applications_app_samples.git
git pull origin master