From 236ae841063c98836383e9e62f9e2ec6f3067f79 Mon Sep 17 00:00:00 2001 From: Yinnan Yao <35447132+yaoyinnan@users.noreply.github.com> Date: Wed, 19 Jul 2023 15:31:35 +0800 Subject: [PATCH] fix: remove instantaneous_metric and collect network metrics every 5 seconds (#1757) Remove the instantaneous_metric thread and use DoTimingTask() to collect network traffic every 5 seconds. Fixes: #1756 Signed-off-by: yaoyinnan --- include/pika_server.h | 2 +- src/pika_instant.cc | 2 +- src/pika_server.cc | 14 ++++---------- tools/pika_exporter/exporter/metrics/stats.go | 8 ++++---- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/include/pika_server.h b/include/pika_server.h index 138df54ec4..3f79d3e3cd 100644 --- a/include/pika_server.h +++ b/include/pika_server.h @@ -508,7 +508,7 @@ class PikaServer : public pstd::noncopyable { void AutoPurge(); void AutoDeleteExpiredDump(); void AutoKeepAliveRSync(); - void AutoInstantaneousMetric(); + void AutoUpdateNetworkMetric(); std::string host_; int port_ = 0; diff --git a/src/pika_instant.cc b/src/pika_instant.cc index 1592cbf9dd..b2e33287fb 100644 --- a/src/pika_instant.cc +++ b/src/pika_instant.cc @@ -17,7 +17,7 @@ double Instant::getInstantaneousMetric(std::string metric) { return sum / STATS_METRIC_SAMPLES; } -/* ======================= Cron: called every 100 ms ======================== */ +/* ======================= Cron: called every 5 s ======================== */ /* Add a sample to the instantaneous metric. This function computes the quotient * of the increment of value and base, which is useful to record operation count diff --git a/src/pika_server.cc b/src/pika_server.cc index 704ec0f215..8e7765747e 100644 --- a/src/pika_server.cc +++ b/src/pika_server.cc @@ -190,14 +190,6 @@ void PikaServer::Start() { } LOG(INFO) << "Pika Server going to start"; - // Auto update instantaneous metric - std::thread instantaneous_metric([&]() { - while (!exit_) { - AutoInstantaneousMetric(); - // wake up every 100 milliseconds - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - } - }); while (!exit_) { DoTimingTask(); @@ -1313,6 +1305,8 @@ void PikaServer::DoTimingTask() { AutoKeepAliveRSync(); // Reset server qps ResetLastSecQuerynum(); + // Auto update network instantaneous metric + AutoUpdateNetworkMetric(); } void PikaServer::AutoCompactRange() { @@ -1493,9 +1487,9 @@ void PikaServer::AutoKeepAliveRSync() { } } -void PikaServer::AutoInstantaneousMetric() { +void PikaServer::AutoUpdateNetworkMetric() { monotime current_time = getMonotonicUs(); - size_t factor = 1000000; // us + size_t factor = 5e6; // us, 5s instant_->trackInstantaneousMetric(STATS_METRIC_NET_INPUT, g_pika_server->NetInputBytes() + g_pika_server->NetReplInputBytes(), current_time, factor); instant_->trackInstantaneousMetric(STATS_METRIC_NET_OUTPUT, g_pika_server->NetOutputBytes() + g_pika_server->NetReplOutputBytes(), diff --git a/tools/pika_exporter/exporter/metrics/stats.go b/tools/pika_exporter/exporter/metrics/stats.go index dd0816fa84..a1fc59633d 100644 --- a/tools/pika_exporter/exporter/metrics/stats.go +++ b/tools/pika_exporter/exporter/metrics/stats.go @@ -81,7 +81,7 @@ var collectStatsMetrics = map[string]MetricConfig{ Parser: &normalParser{}, MetricMeta: &MetaData{ Name: "instantaneous_input_kbps", - Help: "the network's read rate per second in KB/sec", + Help: "the network's read rate per second in KB/sec, calculated as an average of 16 samples collected every 5 seconds.", Type: metricTypeCounter, Labels: []string{LabelNameAddr, LabelNameAlias}, ValueName: "instantaneous_input_kbps", @@ -91,7 +91,7 @@ var collectStatsMetrics = map[string]MetricConfig{ Parser: &normalParser{}, MetricMeta: &MetaData{ Name: "instantaneous_output_kbps", - Help: "the network's write rate per second in KB/sec", + Help: "the network's write rate per second in KB/sec, calculated as an average of 16 samples collected every 5 seconds.", Type: metricTypeCounter, Labels: []string{LabelNameAddr, LabelNameAlias}, ValueName: "instantaneous_output_kbps", @@ -101,7 +101,7 @@ var collectStatsMetrics = map[string]MetricConfig{ Parser: &normalParser{}, MetricMeta: &MetaData{ Name: "instantaneous_input_repl_kbps", - Help: "the network's read rate per second in KB/sec for replication purposes", + Help: "the network's read rate per second in KB/sec for replication purposes, calculated as an average of 16 samples collected every 5 seconds.", Type: metricTypeCounter, Labels: []string{LabelNameAddr, LabelNameAlias}, ValueName: "instantaneous_input_repl_kbps", @@ -111,7 +111,7 @@ var collectStatsMetrics = map[string]MetricConfig{ Parser: &normalParser{}, MetricMeta: &MetaData{ Name: "instantaneous_output_repl_kbps", - Help: "the network's write rate per second in KB/sec for replication purposes", + Help: "the network's write rate per second in KB/sec for replication purposes, calculated as an average of 16 samples collected every 5 seconds.", Type: metricTypeCounter, Labels: []string{LabelNameAddr, LabelNameAlias}, ValueName: "instantaneous_output_repl_kbps",