Amway: trace lib
- Dominant language
- C++
- Stars
- 1.9k
- Forks
- 432
- PR merge metrics
- No merged PRs in 30d
Description
通用trace lib:可用于tera模块导出流程调用关链
场景描述:
```
用户发起tera随机读操作,需要跟踪这个随机查询请求的发起,sdk端调度,ts端执行的整个流程,并对可能的长延迟操作记录日志;
```
设计目标:
```
1、通过线程局部变量,进行线程内追踪;
2、提供关联接口,进行线程池调度追踪;
3、利用protobuf的反射机制,关联rpc接口,进行rpc调用追查;
4、所用的trace数据导出到本地端口;外部启动日志agent从端口接收数据并存储;
```
接口:
```
// 创建trace的类
class TraceGuard {
public:
// trace用法1:trace单线程;需要指定日志存储的数据库和表格名字
explicit TraceGuard(const std::string& db_name, const std::string& table_name);
// trace用法2:trace线程池调度;用于trace被线程池调度后,重新关联之前的trace
explicit TraceGuard(uint64_t attach_id);
// trace用法3:trace rpc调用;用于rpc操作后,重新关联之前的trace
enum RpcEventTyep {
CS = 1,// 客户端发送rpc事件
SR = 2,// 服务端接收rpc事件
SS = 3,// 服务端应答rpc事件
CR = 4,// 客户端接收rpc事件
};
explicit TraceGuard(int level, int event, ::google::protobuf::Message* req, ::google::protobuf::Message* resp);
~TraceGuard();
private:
bool need_release;
TraceGuard(const TraceGuard&);
void operator=(const TraceGuard&);
};
//线程调度前关联trace操作;与trace用法2配合
void AttachTrace(uint64_t attach_id);
// trace导出日志接口,异步
void Log(int level, const char* fmt, ...);
```
使用实例:
```
1、teracli的随机读流程:
1)开始前,创建trace,
::mdt::TraceGuard trace_guard("tera_trace", "trace");
2、sdk端:
1)执行打log,
::mdt::Log(30, "%s, trace get", __func__);
2)需要进行读batch调度前,关联trace,
::mdt::AttachTrace((uint64_t)(reader_buffer->row_id_list));
3)读batch被调度,重新关联之前的trace,
::mdt::TraceGuard trace_guard((uint64_t)(row_id_list));
4)发送读rpc前关联trace的发送事件:
::mdt::TraceGuard trace_guard(30, ::mdt::TraceGuard::CS, request, response);
3、ts端:
1)接收请求后,记录server的接收事件,并关联上游的trace
::mdt::TraceGuard trace_guard(30, ::mdt::TraceGuard::SR, const_cast(request), response);
2)执行打log,
::mdt::Log(30, "%s, begin queue into taskpool", __func__);
3)将读请求放入线程池调度前,关联trace
::mdt::AttachTrace(uint64_t(rpc));
4)实际执行读前,重新关联trace
::mdt::TraceGuard trace_guard((uint64_t)rpc);
5)应答rpc前,记录应答事件,
::mdt::TraceGuard trace_guard(30, ::mdt::TraceGuard::SS, const_cast(request), response);
4、sdk端:
1)收到应答,记录server的接收事件,并关联之前trace
::mdt::TraceGuard trace_guard(30, ::mdt::TraceGuard::CR, request, response);
```
Contributor guide
No contributing guide indexed for this repository
Research direction
The issue defines a proposed TraceGuard, AttachTrace, and Log API, with examples spanning teracli, the SDK, the TS server, thread-pool scheduling, and protobuf RPCs. Start by locating those entry points and the existing logging, RPC, and thread-local context code; the issue does not name files or tests. Done would require an agreed implementation covering the four trace events, context propagation, and local-port export, plus validation tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- backend-api-design, databases, distributed-systems
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100