Add HistogramMetric type
This commit is contained in:
1 parent
1558b79c16
commit
03864caa09
4 files changed
+115
-14
No files matched your search
@@ -5,6 +5,7 @@
|
||||
#include "lib/api/apigateway.pb.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/os/os.h"
|
||||
#include "lib/utils/TimeStamp.h"
|
||||
|
||||
#include <websocketpp/config/asio_no_tls.hpp>
|
||||
#include <websocketpp/server.hpp>
|
||||
@@ -49,9 +50,10 @@ namespace http {
|
||||
std::string _www;
|
||||
std::string _conf;
|
||||
const api::ServiceContext& _ctx;
|
||||
const metrics::MetricsContainer& _metrics;
|
||||
metrics::MetricsContainer& _metrics;
|
||||
std::shared_ptr<metrics::HistogramMetric<4>> _mtimer;
|
||||
|
||||
ServerState(const api::ServiceContext& ctx, const metrics::MetricsContainer& metrics, int port) :
|
||||
ServerState(const api::ServiceContext& ctx, metrics::MetricsContainer& metrics, int port) :
|
||||
_m(),
|
||||
_dispatcher(utils::Dispatcher::get_named("Webserver")),
|
||||
_session(),
|
||||
@@ -59,11 +61,15 @@ namespace http {
|
||||
_port(port),
|
||||
_www(),
|
||||
_ctx(ctx),
|
||||
_metrics(metrics)
|
||||
_metrics(metrics),
|
||||
_mtimer(std::make_shared<metrics::HistogramMetric<4>>("spark_metrics_query_duration_us",
|
||||
std::array<double,4>({ 10.0, 20.0, 40.0, 80.0 })))
|
||||
{
|
||||
_www = os::sparkdata_path("www");
|
||||
_conf = os::sparkdata_path("conf");
|
||||
|
||||
_metrics.publish(_mtimer);
|
||||
|
||||
_server.init_asio(_dispatcher.io_service());
|
||||
_server.set_open_handler(bind(&ServerState::on_open, this, ::_1));
|
||||
_server.set_close_handler(bind(&ServerState::on_close, this, ::_1));
|
||||
@@ -142,10 +148,12 @@ namespace http {
|
||||
}
|
||||
|
||||
void return_metrics(connection_ptr& con) {
|
||||
utils::TimeStamp start = utils::TimeStamp::now();
|
||||
std::stringstream ss;
|
||||
_metrics.print(ss);
|
||||
con->set_body(ss.str());
|
||||
con->set_status(websocketpp::http::status_code::ok);
|
||||
_mtimer->observe((utils::TimeStamp::now() - start).microseconds());
|
||||
}
|
||||
|
||||
void directory_index(connection_ptr& con, const std::string& resource, const boost::filesystem::path& path) {
|
||||
|
||||
@@ -31,8 +31,24 @@ std::ostream& operator<<(std::ostream& os, const std::vector<Label>& labels) {
|
||||
return os;
|
||||
}
|
||||
|
||||
void LazyCounterMetric::print(std::ostream& out) const {
|
||||
out << name
|
||||
const std::string double_to_string(double d) {
|
||||
std::stringstream ss;
|
||||
ss << d;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
const std::vector<Label> vector_concat(const std::vector<Label>& orig, const std::vector<Label>& extra) {
|
||||
if (orig.empty()) return extra;
|
||||
std::vector<Label> result;
|
||||
result.reserve(orig.size() + extra.size());
|
||||
result.insert(result.end(), orig.begin(), orig.end());
|
||||
result.insert(result.end(), extra.begin(), extra.end());
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void LazyCounterMetric::print(std::ostream& os) const {
|
||||
os << name
|
||||
<< labels
|
||||
<< " "
|
||||
<< _get()
|
||||
|
||||
@@ -6,14 +6,15 @@
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <atomic>
|
||||
#include <sstream>
|
||||
|
||||
namespace bts {
|
||||
namespace metrics {
|
||||
class MetricsContainer;
|
||||
|
||||
struct Label {
|
||||
const std::string name;
|
||||
const std::string value;
|
||||
std::string name;
|
||||
std::string value;
|
||||
};
|
||||
std::ostream& operator<<(std::ostream& os, const std::vector<Label>& l);
|
||||
|
||||
@@ -26,21 +27,38 @@ struct Metric {
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class CounterMetric : public Metric {
|
||||
std::atomic<T> _val;
|
||||
public:
|
||||
struct CounterMetric : public Metric {
|
||||
T _val;
|
||||
CounterMetric(const std::string& name) : Metric(name, {}), _val() {}
|
||||
CounterMetric(const std::string& name, const std::vector<Label>& labels) : Metric(name, labels), _val() {}
|
||||
CounterMetric(const CounterMetric&) = delete;
|
||||
~CounterMetric() {};
|
||||
void print(std::ostream& out) const override {
|
||||
out << name << labels << " " << _val << std::endl;
|
||||
}
|
||||
void increment() { _val++; }
|
||||
void increment(T val) { _val += val; }
|
||||
void decrement() { _val--; }
|
||||
void decrement(T val) { _val -= val; }
|
||||
void set(T val) { _val = val; }
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class AtomicCounterMetric : public Metric {
|
||||
std::atomic<T> _val;
|
||||
public:
|
||||
AtomicCounterMetric(const std::string& name) : Metric(name, {}), _val() {}
|
||||
AtomicCounterMetric(const std::string& name, const std::vector<Label>& labels) : Metric(name, labels), _val() {}
|
||||
AtomicCounterMetric(const AtomicCounterMetric& o) : Metric(o.name, o.labels), _val(o._val.load()) { }
|
||||
~AtomicCounterMetric() {};
|
||||
void print(std::ostream& out) const override {
|
||||
out << name << labels << " " << _val << std::endl;
|
||||
}
|
||||
void increment() { _val++; }
|
||||
void increment(T val) { _val += val; }
|
||||
void decrement() { _val--; }
|
||||
void decrement(T val) { _val -= val; }
|
||||
void set(T val) { _val = val; }
|
||||
};
|
||||
|
||||
class LazyCounterMetric : public Metric {
|
||||
const std::function<long()> _get;
|
||||
@@ -55,7 +73,66 @@ class LazyCounterMetric : public Metric {
|
||||
void print(std::ostream&) const override;
|
||||
};
|
||||
|
||||
// Histogram
|
||||
const std::string double_to_string(double d);
|
||||
const std::vector<Label> vector_concat(const std::vector<Label>& orig, const std::vector<Label>& extra);
|
||||
|
||||
template <size_t N, size_t... Is>
|
||||
std::array<CounterMetric<long>,N> mk_counters(const std::string& name,
|
||||
const std::vector<Label>& labels,
|
||||
const std::array<double,N>& in,
|
||||
std::index_sequence<Is...>) {
|
||||
return {{ CounterMetric<long>(name+"_bucket", vector_concat(labels, {{ "le", double_to_string(in[Is]) }}))... }};
|
||||
}
|
||||
template <size_t N, size_t... Is>
|
||||
std::array<CounterMetric<long>,N> mk_counters(const std::string& name,
|
||||
const std::vector<Label>& labels,
|
||||
const std::array<double,N>& in) {
|
||||
return mk_counters(name, labels, in, std::make_index_sequence<N>());
|
||||
}
|
||||
|
||||
|
||||
// Calculate the 90th percentile of response times
|
||||
// histogram_quantile(0.9, sum(rate(spark_metrics_query_duration_us_bucket[5m])) by (le))
|
||||
template <size_t N>
|
||||
class HistogramMetric : public Metric {
|
||||
std::array<CounterMetric<long>,N> _buckets;
|
||||
std::array<double,N> _bucket_values;
|
||||
CounterMetric<double> _sum;
|
||||
CounterMetric<long> _count;
|
||||
public:
|
||||
HistogramMetric(
|
||||
const std::string& name,
|
||||
const std::array<double,N>& buckets)
|
||||
: Metric(name,{}), _buckets(mk_counters(name, {}, buckets)), _bucket_values(buckets), _sum(name+"_sum"), _count(name+"_count") {}
|
||||
HistogramMetric(
|
||||
const std::string& name,
|
||||
const std::array<double,N>& buckets,
|
||||
const std::vector<Label>& labels)
|
||||
: Metric(name,labels), _buckets(mk_counters(name, labels, buckets)), _bucket_values(buckets), _sum(name+"_sum"), _count(name+"_count") {}
|
||||
void print(std::ostream& os) const override {
|
||||
for (size_t x = 0; x < N; ++x) {
|
||||
_buckets[x].print(os);
|
||||
}
|
||||
os << name << "_bucket{le=\"+Inf\"";
|
||||
for (auto iter = labels.begin(); iter != labels.end(); ++iter) {
|
||||
os << "," << iter->name << "=\"" << iter->value << "\"";
|
||||
}
|
||||
os << "} " << _count._val << std::endl;
|
||||
_sum.print(os);
|
||||
_count.print(os);
|
||||
}
|
||||
void observe(double v) {
|
||||
for (size_t x = 0; x < N; ++x) {
|
||||
// >= makes more sense to me but this is what the promethius docs say
|
||||
//
|
||||
if (v <= _bucket_values[x]) {
|
||||
_buckets[x].increment();
|
||||
}
|
||||
}
|
||||
_sum.increment(v);
|
||||
_count.increment();
|
||||
}
|
||||
};
|
||||
|
||||
struct MetricCollection;
|
||||
class MetricsContainer {
|
||||
|
||||
@@ -12,8 +12,8 @@ namespace metrics {
|
||||
|
||||
|
||||
void publish_system_metrics(MetricsContainer& stats) {
|
||||
// auto cm = std::make_shared<CounterMetric>("spark_test_counter");
|
||||
// stats.publish(cm);
|
||||
auto cm = std::make_shared<CounterMetric<long>>("spark_test_counter", std::vector<Label> {{"test", "ok"}});
|
||||
stats.publish(cm);
|
||||
// auto lcm = std::make_shared<LazyCounterMetric>("lazy_metric",[](){ return 42; });
|
||||
// stats.publish(lcm);
|
||||
|
||||
|
||||
Reference in new issue
Block a user