Skip to content

Commit

Permalink
Added a graph of command time statistics to grafana (#1751)
Browse files Browse the repository at this point in the history
* Added a graph of command time statistics to grafana
  • Loading branch information
Mixficsol committed Jul 18, 2023
1 parent 294e702 commit 299929a
Show file tree
Hide file tree
Showing 6 changed files with 1,063 additions and 715 deletions.
30 changes: 22 additions & 8 deletions src/pika_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ static std::string ConstructPinginPubSubResp(const PikaCmdArgsType& argv) {
return resp.str();
}

static double MethodofCommandStatistics(const uint64_t time_consuming, const uint64_t frequency) {
return (static_cast<double>(time_consuming) / 1000.0) / static_cast<double>(frequency);
}

static double MethodofTotalTimeCalculation(const uint64_t time_consuming) {
return static_cast<double>(time_consuming) / 1000.0;
}

enum AuthResult {
OK,
INVALID_PASSWORD,
Expand Down Expand Up @@ -742,6 +750,8 @@ void InfoCmd::Do(std::shared_ptr<Slot> slot) {
info.append("\r\n");
InfoExecCount(info);
info.append("\r\n");
InfoCommandStats(info);
info.append("\r\n");
InfoCPU(info);
info.append("\r\n");
InfoReplication(info);
Expand Down Expand Up @@ -1254,15 +1264,19 @@ void InfoCmd::InfoCommandStats(std::string& info) {
tmp_stream.precision(2);
tmp_stream.setf(std::ios::fixed);
tmp_stream << "# Commandstats" << "\r\n";
for (auto& iter : *g_pika_server->GetCommandStatMap()) {
if (iter.second.cmd_count != 0) {
tmp_stream << "cmdstat_" << iter.first << ":"
<< "calls=" << iter.second.cmd_count << ",usec="
<< iter.second.cmd_time_consuming
<< ",usec_per_call="
<< static_cast<double>(iter.second.cmd_time_consuming) / static_cast<double>(iter.second.cmd_count)
<< "\r\n";
for (auto iter : *g_pika_server->GetCommandStatMap()) {
if (iter.second.cmd_count != 0) {
tmp_stream << iter.first << ":"
<< "calls=" << iter.second.cmd_count << ", usec="
<< MethodofTotalTimeCalculation(iter.second.cmd_time_consuming)
<< ", usec_per_call=";
if (!iter.second.cmd_time_consuming) {
tmp_stream << 0 << "\r\n";
} else {
tmp_stream << MethodofCommandStatistics(iter.second.cmd_time_consuming, iter.second.cmd_count)
<< "\r\n";
}
}
}
info.append(tmp_stream.str());
}
Expand Down
4 changes: 2 additions & 2 deletions src/pika_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ void PikaServer::Start() {
}
}
CommandStatistics statistics;
auto cmdstat_map = *g_pika_server->GetCommandStatMap();
auto cmdstat_map = g_pika_server->GetCommandStatMap();
for (auto& iter : *g_pika_cmd_table_manager->GetCmdTable()) {
cmdstat_map.emplace(iter.first, statistics);
cmdstat_map->emplace(iter.first, statistics);
}
LOG(INFO) << "Pika Server going to start";

Expand Down
8 changes: 8 additions & 0 deletions tools/pika_exporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ prometheus --config.file=./grafana/prometheus.yml

### Pika Command Execution Time

| Metrics Name | Metric Type | Labels | Metrics Value | Metric Desc |
|-----------------------------------| ----------- |----------------------|-----------------------------------------------|--------------------------------------------------------------------------|
| calls | `Counter` | {addr="", alias=""} | the value of `calls` | Pika Number of times each command is invoked |
| usec | `Counter` | {addr="", alias=""} | the value of `usec` | Total time taken by each Pika command |
| usec_per_call | `Counter` | {addr="", alias=""} | the value of `usec_per_call` | Average time of each Pika command |

### Rocksdb Metrics

| Serial Number | Metric | Meaning |
Expand Down Expand Up @@ -203,3 +209,5 @@ Screenshots:
![Network](./contrib/network.png)

![RocksDB](./contrib/rocksdb.png)

![Commandstats](./contrib/command_time.png)
Binary file added tools/pika_exporter/contrib/command_time.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions tools/pika_exporter/exporter/metrics/commandstats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package metrics

import "regexp"

func init() {
Register(collectCommandstatsMetrics)
}

var collectCommandstatsMetrics = map[string]MetricConfig{
"commandstats_info": {
Parser: &regexParser{
name: "commandstats_info",
reg: regexp.MustCompile(`(?P<cmd>[\w]+)[:\s]*calls=(?P<calls>[\d]+)[,\s]*usec=(?P<usec>[\d+\.\d+]+)[,\s]*usec_per_call=(?P<usec_per_call>[\d+\.\d+]+)`),
Parser: &normalParser{},
},
MetricMeta: MetaDatas{
{
Name: "calls",
Help: "Pika Number of times each command is invoked",
Type: metricTypeGauge,
Labels: []string{LabelNameAddr, LabelNameAlias, "cmd"},
ValueName: "calls",
},
{
Name: "usec",
Help: "Total time taken by each Pika command",
Type: metricTypeGauge,
Labels: []string{LabelNameAddr, LabelNameAlias, "cmd"},
ValueName: "usec",
},
{
Name: "usec_per_call",
Help: "Average time of each Pika command",
Type: metricTypeGauge,
Labels: []string{LabelNameAddr, LabelNameAlias, "cmd"},
ValueName: "usec_per_call",
},
},
},
}
Loading

0 comments on commit 299929a

Please sign in to comment.