Commit b0fdaf4f authored by zhujiashun's avatar zhujiashun

refine code

parent c8f2d324
...@@ -52,22 +52,22 @@ public: ...@@ -52,22 +52,22 @@ public:
private: private:
DISALLOW_COPY_AND_ASSIGN(PrometheusMetricsDumper); DISALLOW_COPY_AND_ASSIGN(PrometheusMetricsDumper);
bool ProcessLatencyRecorderSuffix(const butil::StringPiece& name, bool DumpLatencyRecorderSuffix(const butil::StringPiece& name,
const butil::StringPiece& desc); const butil::StringPiece& desc);
bool ProcessPercentileSuffix(const butil::StringPiece& name,
const butil::StringPiece& desc,
butil::StringPiece* metric_name_out);
// 6 is the number of bvars in LatencyRecorder that indicating percentiles // 6 is the number of bvars in LatencyRecorder that indicating percentiles
static const int NPERCENTILES = 6; static const int NPERCENTILES = 6;
private:
struct SummaryItems { struct SummaryItems {
std::string latency_percentiles[NPERCENTILES]; std::string latency_percentiles[NPERCENTILES];
std::string latency_avg; std::string latency_avg;
std::string count; std::string count;
std::string metric_name;
}; };
SummaryItems* ProcessLatencyRecorderSuffix(const butil::StringPiece& name,
const butil::StringPiece& desc);
private:
butil::IOBufBuilder* _os; butil::IOBufBuilder* _os;
const std::string _server_prefix; const std::string _server_prefix;
std::map<std::string, SummaryItems> _m; std::map<std::string, SummaryItems> _m;
...@@ -79,9 +79,9 @@ bool PrometheusMetricsDumper::dump(const std::string& name, ...@@ -79,9 +79,9 @@ bool PrometheusMetricsDumper::dump(const std::string& name,
// there is no necessary to monitor string in prometheus // there is no necessary to monitor string in prometheus
return true; return true;
} }
if (ProcessLatencyRecorderSuffix(name, desc)) { if (DumpLatencyRecorderSuffix(name, desc)) {
// Has encountered name with suffix exposed by LatencyRecorder, // Has encountered name with suffix exposed by LatencyRecorder,
// Leave it to ProcessLatencyRecorderSuffix to output Summary. // Leave it to DumpLatencyRecorderSuffix to output Summary.
return true; return true;
} }
*_os << "# HELP " << name << '\n' *_os << "# HELP " << name << '\n'
...@@ -90,72 +90,72 @@ bool PrometheusMetricsDumper::dump(const std::string& name, ...@@ -90,72 +90,72 @@ bool PrometheusMetricsDumper::dump(const std::string& name,
return true; return true;
} }
bool PrometheusMetricsDumper::ProcessPercentileSuffix(const butil::StringPiece& name, PrometheusMetricsDumper::SummaryItems*
const butil::StringPiece& desc, PrometheusMetricsDumper::ProcessLatencyRecorderSuffix(const butil::StringPiece& name,
butil::StringPiece* metric_name_out) { const butil::StringPiece& desc) {
static std::string latency_names[] = { static std::string latency_names[] = {
butil::string_printf("_latency_%d", (int)bvar::FLAGS_bvar_latency_p1), butil::string_printf("_latency_%d", (int)bvar::FLAGS_bvar_latency_p1),
butil::string_printf("_latency_%d", (int)bvar::FLAGS_bvar_latency_p2), butil::string_printf("_latency_%d", (int)bvar::FLAGS_bvar_latency_p2),
butil::string_printf("_latency_%d", (int)bvar::FLAGS_bvar_latency_p3), butil::string_printf("_latency_%d", (int)bvar::FLAGS_bvar_latency_p3),
"_latency_999", "_latency_9999", "_max_latency" "_latency_999", "_latency_9999", "_max_latency"
}; };
CHECK(NPERCENTILES == sizeof(latency_names) / sizeof(std::string)); CHECK(NPERCENTILES == arraysize(latency_names));
butil::StringPiece metric_name(name); butil::StringPiece metric_name(name);
for (int i = 0; i < NPERCENTILES; ++i) { for (int i = 0; i < NPERCENTILES; ++i) {
if (!metric_name.ends_with(latency_names[i])) { if (!metric_name.ends_with(latency_names[i])) {
continue; continue;
} }
metric_name.remove_suffix(latency_names[i].size()); metric_name.remove_suffix(latency_names[i].size());
_m[metric_name.as_string()].latency_percentiles[i] = desc.as_string(); SummaryItems* si = &_m[metric_name.as_string()];
si->latency_percentiles[i] = desc.as_string();
if (i == NPERCENTILES - 1) { if (i == NPERCENTILES - 1) {
// 'max_latency' is the last suffix name that appear in the sorted bvar // 'max_latency' is the last suffix name that appear in the sorted bvar
// list, which means all related percentiles have been gathered and we are // list, which means all related percentiles have been gathered and we are
// ready to output a Summary. // ready to output a Summary.
*metric_name_out = metric_name; si->metric_name = metric_name.as_string();
return true; return si;
} }
break; return NULL;
} }
return false; // Get the average of latency in recent window size
if (metric_name.ends_with("_latency")) {
metric_name.remove_suffix(8);
_m[metric_name.as_string()].latency_avg = desc.as_string();
return NULL;
}
if (metric_name.ends_with("_count")) {
metric_name.remove_suffix(6);
_m[metric_name.as_string()].count = desc.as_string();
return NULL;
}
return NULL;
} }
bool PrometheusMetricsDumper::ProcessLatencyRecorderSuffix(const butil::StringPiece& name, bool PrometheusMetricsDumper::DumpLatencyRecorderSuffix(const butil::StringPiece& name,
const butil::StringPiece& desc) { const butil::StringPiece& desc) {
if (!name.starts_with(_server_prefix)) { if (!name.starts_with(_server_prefix)) {
return false; return false;
} }
butil::StringPiece metric_name(name); SummaryItems* si = NULL;
if (ProcessPercentileSuffix(name, desc, &metric_name)) { if ((si = ProcessLatencyRecorderSuffix(name, desc))) {
const SummaryItems& items = _m[metric_name.as_string()]; *_os << "# HELP " << si->metric_name << '\n'
*_os << "# HELP " << metric_name << '\n' << "# TYPE " << si->metric_name << " summary\n"
<< "# TYPE " << metric_name << " summary\n" << si->metric_name << "{quantile=\"" << std::setprecision(2)
<< metric_name << "{quantile=\"" << std::setprecision(2)
<< (double)(bvar::FLAGS_bvar_latency_p1) / 100 << "\"} " << (double)(bvar::FLAGS_bvar_latency_p1) / 100 << "\"} "
<< items.latency_percentiles[0] << '\n' << si->latency_percentiles[0] << '\n'
<< metric_name << "{quantile=\"" << std::setprecision(2) << si->metric_name << "{quantile=\"" << std::setprecision(2)
<< (double)(bvar::FLAGS_bvar_latency_p2) / 100 << "\"} " << (double)(bvar::FLAGS_bvar_latency_p2) / 100 << "\"} "
<< items.latency_percentiles[1] << '\n' << si->latency_percentiles[1] << '\n'
<< metric_name << "{quantile=\"" << std::setprecision(2) << si->metric_name << "{quantile=\"" << std::setprecision(2)
<< (double)(bvar::FLAGS_bvar_latency_p3) / 100 << "\"} " << (double)(bvar::FLAGS_bvar_latency_p3) / 100 << "\"} "
<< items.latency_percentiles[2] << '\n' << si->latency_percentiles[2] << '\n'
<< metric_name << "{quantile=\"0.999\"} " << items.latency_percentiles[3] << '\n' << si->metric_name << "{quantile=\"0.999\"} " << si->latency_percentiles[3] << '\n'
<< metric_name << "{quantile=\"0.9999\"} " << items.latency_percentiles[4] << '\n' << si->metric_name << "{quantile=\"0.9999\"} " << si->latency_percentiles[4] << '\n'
<< metric_name << "{quantile=\"1\"} " << items.latency_percentiles[5] << '\n' << si->metric_name << "{quantile=\"1\"} " << si->latency_percentiles[5] << '\n'
<< metric_name << "_sum " << si->metric_name << "_sum "
// There is no sum of latency in bvar output, just use average * count as approximation // There is no sum of latency in bvar output, just use average * count as approximation
<< strtoll(items.latency_avg.data(), NULL, 10) * strtoll(items.count.data(), NULL, 10) << '\n' << strtoll(si->latency_avg.data(), NULL, 10) * strtoll(si->count.data(), NULL, 10) << '\n'
<< metric_name << "_count " << items.count << '\n'; << si->metric_name << "_count " << si->count << '\n';
return true;
}
// Get the average of latency in recent window size
if (metric_name.ends_with("_latency")) {
metric_name.remove_suffix(8);
_m[metric_name.as_string()].latency_avg = desc.as_string();
return true;
}
if (metric_name.ends_with("_count")) {
metric_name.remove_suffix(6);
_m[metric_name.as_string()].count = desc.as_string();
return true; return true;
} }
return false; return false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment