Skip to content

Commit

Permalink
Add basic Prometheus support and metrics (dragonflydb#67)
Browse files Browse the repository at this point in the history
* Add basic Prometheus support and metrics

* Remove human metrics

* Pull AppendMetric outside

* Fix typo
  • Loading branch information
zacharya19 authored Jun 1, 2022
1 parent 398b2b0 commit 1758503
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/facade/dragonfly_listener.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
45 changes: 41 additions & 4 deletions src/server/server_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,45 @@ error_code ServerFamily::LoadRdb(const std::string& rdb_file) {
return ec;
}

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) {
Expand All @@ -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);

Expand All @@ -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");
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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");
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/server/server_family.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1758503

Please sign in to comment.