Skip to content

Commit

Permalink
Merge "rometheus API with grafana uses labels" from Amnon
Browse files Browse the repository at this point in the history
"
This series address issues from the grafana over prometheus integration.

The grafana dashboard uses labels for displaying hostname, and sum metrics from different shards.

After this series, the grafana dashboard (when removing the collectd_ prefix from the metrics definitions) works as expected."

* 'amnon/grafana_metrics_v5' of github.com:cloudius-systems/seastar-dev:
  prometheus: Use labels for metrics
  collectd: expose the hostname
  • Loading branch information
avikivity committed Oct 10, 2016
2 parents 165982b + f441097 commit 04dc20c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
38 changes: 31 additions & 7 deletions core/prometheus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "proto/metrics2.pb.h"

#include "scollectd_api.hh"
#include "scollectd-impl.hh"
#include "http/function_handlers.hh"
#include <boost/algorithm/string/replace.hpp>
#include <boost/range/algorithm_ext/erase.hpp>
Expand Down Expand Up @@ -64,27 +65,50 @@ static std::string safe_name(const sstring& name) {
}

static sstring collectd_name(const scollectd::type_instance_id & id, uint32_t cpu) {
return safe_name(id.plugin()) + ":" + safe_name(id.type()) + ":" + safe_name(id.type_instance()) + ":" + std::to_string(cpu);
return safe_name(id.plugin());
}

static pm::Metric* add_label(pm::Metric* mt, const scollectd::type_instance_id & id, uint32_t cpu) {
auto label = mt->add_label();
label->set_name("shard");
label->set_value(std::to_string(cpu));
label = mt->add_label();
label->set_name("type");
label->set_value(id.type());

static void fill_metric(pm::MetricFamily& mf, const std::vector<scollectd::collectd_value>& vals) {
if (id.type_instance() != "") {
label = mt->add_label();
label->set_name("metric");
label->set_value(id.type_instance());
}
const sstring& host = scollectd::get_impl().host();
if (host != "") {
label = mt->add_label();
label->set_name("instance");
label->set_value(host);
}
return mt;
}

static void fill_metric(pm::MetricFamily& mf, const std::vector<scollectd::collectd_value>& vals,
const scollectd::type_instance_id & id, uint32_t cpu) {
for (const scollectd::collectd_value& c : vals) {
switch (c.type()) {
case scollectd::data_type::DERIVE:
mf.add_metric()->mutable_counter()->set_value(c.i());
add_label(mf.add_metric(), id, cpu)->mutable_counter()->set_value(c.i());
mf.set_type(pm::MetricType::COUNTER);
break;
case scollectd::data_type::GAUGE:
mf.add_metric()->mutable_gauge()->set_value(c.d());
add_label(mf.add_metric(), id, cpu)->mutable_gauge()->set_value(c.d());
mf.set_type(pm::MetricType::GAUGE);
break;
default:
mf.add_metric()->mutable_counter()->set_value(c.ui());
add_label(mf.add_metric(), id, cpu)->mutable_counter()->set_value(c.ui());
mf.set_type(pm::MetricType::COUNTER);
break;
}
}

}

future<> start(httpd::http_server_control& http_server, const config& ctx) {
Expand All @@ -105,9 +129,9 @@ future<> start(httpd::http_server_control& http_server, const config& ctx) {
pm::MetricFamily mtf;
std::string s;
google::protobuf::io::StringOutputStream os(&s);
mtf.set_name(collectd_name(i.first, cpu));
mtf.set_name(ctx.prefix + "_" + collectd_name(i.first, cpu));
mtf.set_help(ctx.metric_help);
fill_metric(mtf, i.second);
fill_metric(mtf, i.second, i.first, cpu);
if (mtf.metric_size() > 0) {
std::stringstream ss;
if (!write_delimited_to(mtf, &os)) {
Expand Down
1 change: 1 addition & 0 deletions core/prometheus.hh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace prometheus {
*/
struct config {
sstring metric_help; //!< Default help message for the returned metrics
sstring prefix = "seastar"; //!< a prefix that will be added to metric names
};

future<> start(httpd::http_server_control& http_server, const config& ctx);
Expand Down
3 changes: 3 additions & 0 deletions core/scollectd-impl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public:
void stop();

value_list_map& get_value_list_map();
const sstring& host() const {
return _host;
}

private:
void arm();
Expand Down

0 comments on commit 04dc20c

Please sign in to comment.