Skip to content

Commit

Permalink
feat: start returning metric types in Prometheus response (#48)
Browse files Browse the repository at this point in the history
Co-authored-by: michal-drobniak <[email protected]>
  • Loading branch information
mdrobny and michal-drobniak authored Jul 19, 2024
1 parent 001b572 commit 0fcdaaf
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/metrics/PrometheusRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,35 @@ const std::string PrometheusRenderer::RenderMetrics(Server* server) {
auto metrics = server->getAggregatedMetrics();
auto& config = server->config();

std::vector<std::pair<std::string, long long>> metricList = {
{"worker_count", metrics.worker_count},
{"publish_count", metrics.publish_count},
{"redis_connection_fail_count", metrics.redis_connection_fail_count},
{"redis_publish_delay_ms", metrics.redis_publish_delay_ms},
std::vector<std::tuple<std::string, std::string, long long>> metricList = {
{"worker_count", "gauge", metrics.worker_count},
{"publish_count", "counter", metrics.publish_count},
{"redis_connection_fail_count", "counter", metrics.redis_connection_fail_count},
{"redis_publish_delay_ms", "histogram", metrics.redis_publish_delay_ms},

{"current_connections_count", metrics.current_connections_count},
{"total_connect_count", metrics.total_connect_count},
{"total_disconnect_count", metrics.total_disconnect_count},
{"eventloop_delay_ms", metrics.eventloop_delay_ms}};
{"current_connections_count", "gauge", metrics.current_connections_count},
{"total_connect_count", "counter", metrics.total_connect_count},
{"total_disconnect_count", "counter", metrics.total_disconnect_count},
{"eventloop_delay_ms", "histogram", metrics.eventloop_delay_ms}};

char h_buf[128] = {0};
std::stringstream ss;

gethostname(h_buf, sizeof(h_buf));

for (auto& m : metricList) {
for (const auto& metric : metricList) {
const std::string& metricName = std::get<0>(metric);
const std::string& metricType = std::get<1>(metric);
const long long& metricValue = std::get<2>(metric);

// Output the type of each metric
ss << "# TYPE " << metricName << " " << metricType << "\n";

// Add prefix to metric key if set in configuration.
const std::string metricKey = !config.get<std::string>("prometheus_metric_prefix").empty() ? (config.get<std::string>("prometheus_metric_prefix") + "_" + m.first) : m.first;
const std::string metricKey = !config.get<std::string>("prometheus_metric_prefix").empty() ? (config.get<std::string>("prometheus_metric_prefix") + "_" + metricName) : metricName;

ss << metricKey << "{instance=\"" << h_buf << ":" << config.get<int>("listen_port") << "\""
<< "} " << m.second << "\n";
<< "} " << metricValue << "\n";
}

return ss.str();
Expand Down

0 comments on commit 0fcdaaf

Please sign in to comment.