Add basic Prometheus support and metrics (#67)
* Add basic Prometheus support and metrics * Remove human metrics * Pull AppendMetric outside * Fix typo
This commit is contained in:
parent
398b2b00bf
commit
1758503e97
|
@ -127,7 +127,6 @@ Listener::Listener(Protocol protocol, ServiceInterface* si) : service_(si), prot
|
|||
}
|
||||
http_base_.reset(new HttpListener<>);
|
||||
http_base_->set_resource_prefix("https://romange.s3.eu-west-1.amazonaws.com/static");
|
||||
http_base_->enable_metrics();
|
||||
si->ConfigureHttpHandlers(http_base_.get());
|
||||
}
|
||||
|
||||
|
|
|
@ -266,8 +266,45 @@ error_code ServerFamily::LoadRdb(const std::string& rdb_file) {
|
|||
return ec;
|
||||
}
|
||||
|
||||
void ServerFamily::ConfigureMetrics(util::HttpListenerBase* http_base) {
|
||||
void AppendMetric(http::StringResponse* resp, absl::AlphaNum name, absl::AlphaNum val) {
|
||||
/**
|
||||
* Gets a metric name and a value and write it using Prometheus format.
|
||||
*
|
||||
* TODO:
|
||||
* 1. add metrics descriptions.
|
||||
* 2. support other types of metrics, not just gauge :)
|
||||
*/
|
||||
|
||||
const auto full_name = StrCat("dragonfly_", name);
|
||||
absl::StrAppend(&resp->body(), "# HELP ", full_name, " ", name, "\n");
|
||||
absl::StrAppend(&resp->body(), "# TYPE ", full_name, " gauge\n");
|
||||
absl::StrAppend(&resp->body(), full_name, " ", val, "\n");
|
||||
}
|
||||
|
||||
void ServerFamily::ConfigureMetrics(util::HttpListenerBase* http_base) {
|
||||
// The naming of the metrics should be compatible with redis_exporter, see https://github.com/oliver006/redis_exporter/blob/master/exporter/exporter.go#L111
|
||||
|
||||
auto cb = [this](const http::QueryArgs& args, HttpContext* send) {
|
||||
http::StringResponse resp = http::MakeStringResponse(boost::beast::http::status::ok);
|
||||
Metrics m = this->GetMetrics();
|
||||
|
||||
// Server metrics
|
||||
AppendMetric(&resp, "uptime_in_seconds", m.uptime);
|
||||
|
||||
// Clients metrics
|
||||
AppendMetric(&resp, "connected_clients", m.conn_stats.num_conns);
|
||||
AppendMetric(&resp, "client_read_buf_capacity", m.conn_stats.read_buf_capacity);
|
||||
AppendMetric(&resp, "blocked_clients", m.conn_stats.num_blocked_clients);
|
||||
|
||||
// Memory metrics
|
||||
AppendMetric(&resp, "used_memory", m.heap_used_bytes);
|
||||
AppendMetric(&resp, "used_memory_peak", used_mem_peak.load(memory_order_relaxed));
|
||||
AppendMetric(&resp, "comitted_memory", _mi_stats_main.committed.current);
|
||||
|
||||
return send->Invoke(std::move(resp));
|
||||
};
|
||||
|
||||
http_base->RegisterCb("/metrics", cb);
|
||||
}
|
||||
|
||||
void ServerFamily::StatsMC(std::string_view section, facade::ConnectionContext* cntx) {
|
||||
|
@ -279,7 +316,6 @@ void ServerFamily::StatsMC(std::string_view section, facade::ConnectionContext*
|
|||
#define ADD_LINE(name, val) absl::StrAppend(&info, "STAT " #name " ", val, "\r\n")
|
||||
|
||||
time_t now = time(NULL);
|
||||
size_t uptime = now - start_time_;
|
||||
struct rusage ru;
|
||||
getrusage(RUSAGE_SELF, &ru);
|
||||
|
||||
|
@ -293,7 +329,7 @@ void ServerFamily::StatsMC(std::string_view section, facade::ConnectionContext*
|
|||
Metrics m = GetMetrics();
|
||||
|
||||
ADD_LINE(pid, getpid());
|
||||
ADD_LINE(uptime, uptime);
|
||||
ADD_LINE(uptime, m.uptime);
|
||||
ADD_LINE(time, now);
|
||||
ADD_LINE(version, kGitTag);
|
||||
ADD_LINE(libevent, "iouring");
|
||||
|
@ -597,6 +633,7 @@ Metrics ServerFamily::GetMetrics() const {
|
|||
|
||||
lock_guard<fibers::mutex> lk(mu);
|
||||
|
||||
result.uptime = time(NULL) - this->start_time_;
|
||||
result.conn_stats += ss->connection_stats;
|
||||
result.qps += uint64_t(ss->MovingSum6());
|
||||
|
||||
|
@ -648,6 +685,7 @@ void ServerFamily::Info(CmdArgList args, ConnectionContext* cntx) {
|
|||
};
|
||||
|
||||
#define ADD_HEADER(x) absl::StrAppend(&info, x "\r\n")
|
||||
Metrics m = GetMetrics();
|
||||
|
||||
if (should_enter("SERVER")) {
|
||||
ADD_HEADER("# Server");
|
||||
|
@ -658,12 +696,11 @@ void ServerFamily::Info(CmdArgList args, ConnectionContext* cntx) {
|
|||
append("multiplexing_api", "iouring");
|
||||
append("tcp_port", GetFlag(FLAGS_port));
|
||||
|
||||
size_t uptime = time(NULL) - start_time_;
|
||||
size_t uptime = m.uptime;
|
||||
append("uptime_in_seconds", uptime);
|
||||
append("uptime_in_days", uptime / (3600 * 24));
|
||||
}
|
||||
|
||||
Metrics m = GetMetrics();
|
||||
auto sdata_res = io::ReadStatusInfo();
|
||||
|
||||
DbStats total;
|
||||
|
|
|
@ -29,6 +29,7 @@ struct Metrics {
|
|||
TieredStats tiered_stats;
|
||||
EngineShard::Stats shard_stats;
|
||||
|
||||
size_t uptime = 0;
|
||||
size_t qps = 0;
|
||||
size_t heap_used_bytes = 0;
|
||||
size_t heap_comitted_bytes = 0;
|
||||
|
|
Loading…
Reference in New Issue