总体说明
接口简介
Trace模块提供推理服务化性能数据采集(C++)接口,用于Trace数据监测。
Trace接口功能介绍和使用示例请参见msServiceProfiler Trace数据监测。
头文件:${INSTALL_DIR}/include/Tracer.h
库文件:${INSTALL_DIR}/lib64/libms_service_profiler.so
${INSTALL_DIR}请替换为CANN软件安装后文件存储路径。以root安装举例,则安装后文件存储路径为:/usr/local/Ascend/cann。
示例代码
以下是关键步骤的代码示例,请勿直接拷贝编译运行,仅供参考。
// 设置全局资源属性
if (msServiceProfiler::Tracer::IsEnable()) {
msServiceProfiler::TraceContext::addResAttribute("service.name", "my-service");
msServiceProfiler::TraceContext::addResAttribute("service.version", "1.0.0");
}
auto& ctx = msServiceProfiler::TraceContext::GetTraceCtx();
size_t indexHeader = ctx.ExtractAndAttach(traceParentHeader, b3Header);
size_t index = ctx.Attach(TraceId{1, 1}, SpanId{1}, true); // Span 会自动Attach,一般不需要主动调用该函数
// 创建跨度
auto span = msServiceProfiler::Tracer::StartSpanAsActive("MyOperation", "MyModule");
// 设置属性
span.SetAttribute("key", "value")
.SetStatus(true, "Operation completed successfully");
span.End();
ctx.Unattach(index);
ctx.Unattach(indexHeader);
接口列表
具体接口如下:
表 1 Trace API(C++)
| 接口 | 说明 |
|---|---|
| TraceContext类 | Trace上下文管理类,负责管理线程级别的Trace信息。 |
| GetTraceCtx | 获取当前线程的Trace上下文实例。 |
| addResAttribute | 添加资源属性(全局属性)。 |
| ExtractAndAttach | 解析HTTPTrace信息并附加到当前上下文。 |
| Attach | 附加Trace信息到当前上下文。 |
| Unattach | 解除指定索引的Trace上下文。 |
| GetCurrent | 获取当前Trace上下文信息。 |
| Span类 | 跨度类,表示一个具体的操作或请求。 |
| Span | 创建一个跨度。 |
| Activate | 激活跨度并开始计时。 |
| SetAttribute | 设置跨度属性。 |
| SetStatus | 设置跨度状态。 |
| End | 结束跨度。 |
| Tracer类 | 提供创建跨度的接口。 |
| StartSpanAsActive | 创建并激活一个跨度。 |
| IsEnable | 检查Trace功能是否启用。 |