Skip to content

Commit

Permalink
save span with minmum latency us
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglimingcn committed May 1, 2024
1 parent be22f1c commit 4e8a5e6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/cn/rpcz.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
| rpcz_database_dir | ./rpc_data/rpcz | For storing requests/contexts collected by rpcz. | src/baidu/rpc/span.cpp |
| rpcz_keep_span_db | false | Don't remove DB of rpcz at program's exit | src/baidu/rpc/span.cpp |
| rpcz_keep_span_seconds (R) | 3600 | Keep spans for at most so many seconds | src/baidu/rpc/span.cpp |
| rpcz_save_span_min_latency_us (R) | 0 (default:0) | The minimum latency microseconds of span saved | src/baidu/rpc/span.cpp |

若启动时未加-enable_rpcz,则可在启动后访问SERVER_URL/rpcz/enable动态开启rpcz,访问SERVER_URL/rpcz/disable则关闭,这两个链接等价于访问SERVER_URL/flags/enable_rpcz?setvalue=true和SERVER_URL/flags/enable_rpcz?setvalue=false。在r31010之后,rpc在html版本中增加了一个按钮可视化地开启和关闭。

Expand Down
13 changes: 11 additions & 2 deletions src/brpc/span.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ BRPC_VALIDATE_GFLAG(rpcz_keep_span_seconds, PositiveInteger);

DEFINE_bool(rpcz_keep_span_db, false, "Don't remove DB of rpcz at program's exit");

DEFINE_int64(rpcz_save_span_min_latency_us, 0, "The minimum latency microseconds of span saved");
BRPC_VALIDATE_GFLAG(rpcz_save_span_min_latency_us, NonNegativeInteger);

struct IdGen {
bool init;
uint16_t seq;
Expand Down Expand Up @@ -508,7 +511,8 @@ inline uint64_t ToLittleEndian(const uint32_t* buf) {
SpanDB* SpanDB::Open() {
// Remove old rpcz directory even if crash occurs.
if (!FLAGS_rpcz_keep_span_db) {
std::string cmd = butil::string_printf("rm -rf %s", FLAGS_rpcz_database_dir.c_str());
std::string cmd = butil::string_printf("rm -rf %s/[0-9]*\.*[0-9]*\.[0-9]*",
FLAGS_rpcz_database_dir.c_str());
butil::ignore_result(system(cmd.c_str()));
}

Expand Down Expand Up @@ -574,6 +578,11 @@ leveldb::Status SpanDB::Index(const Span* span, std::string* value_buf) {
// of time window.

const int64_t start_time = span->GetStartRealTimeUs();
const int64_t latency_us = span->GetEndRealTimeUs() - start_time;
// if latency_us < FLAGS_rpcz_save_span_min_latency_us, don't save this span
if (latency_us < FLAGS_rpcz_save_span_min_latency_us) {
return leveldb::Status::OK();
}
BriefSpan brief;
brief.set_trace_id(span->trace_id());
brief.set_span_id(span->span_id());
Expand All @@ -583,7 +592,7 @@ leveldb::Status SpanDB::Index(const Span* span, std::string* value_buf) {
brief.set_request_size(span->request_size());
brief.set_response_size(span->response_size());
brief.set_start_real_us(start_time);
brief.set_latency_us(span->GetEndRealTimeUs() - start_time);
brief.set_latency_us(latency_us);
brief.set_full_method_name(span->full_method_name());
if (!brief.SerializeToString(value_buf)) {
return leveldb::Status::InvalidArgument(
Expand Down

0 comments on commit 4e8a5e6

Please sign in to comment.