16 #ifndef IMPALA_UTIL_COLLECTION_METRICS_H
17 #define IMPALA_UTIL_COLLECTION_METRICS_H
24 #include <boost/algorithm/string/join.hpp>
25 #include <boost/foreach.hpp>
26 #include <boost/accumulators/accumulators.hpp>
27 #include <boost/accumulators/statistics/count.hpp>
28 #include <boost/accumulators/statistics/mean.hpp>
29 #include <boost/accumulators/statistics/min.hpp>
30 #include <boost/accumulators/statistics/max.hpp>
31 #include <boost/accumulators/statistics/variance.hpp>
49 void Add(
const T& item) {
50 boost::lock_guard<boost::mutex> l(
lock_);
56 boost::lock_guard<boost::mutex> l(
lock_);
62 virtual void ToJson(rapidjson::Document* document, rapidjson::Value* value) {
63 rapidjson::Value container(rapidjson::kObjectType);
65 rapidjson::Value metric_list(rapidjson::kArrayType);
66 BOOST_FOREACH(
const T& s,
value_) {
67 rapidjson::Value entry_value;
68 ToJsonValue(s, TUnit::NONE, document, &entry_value);
69 metric_list.PushBack(entry_value, document->GetAllocator());
71 container.AddMember(
"items", metric_list, document->GetAllocator());
76 rapidjson::Value metric_list(rapidjson::kArrayType);
77 BOOST_FOREACH(
const T& s,
value_) {
78 rapidjson::Value entry_value;
79 ToJsonValue(s, TUnit::NONE, document, &entry_value);
80 metric_list.PushBack(entry_value, document->GetAllocator());
82 document->AddMember(
key_.c_str(), metric_list, document->GetAllocator());
86 std::stringstream out;
87 PrettyPrinter::PrintStringList<std::set<T> >(
88 value_, TUnit::NONE, &out);
106 template <
typename T>
113 boost::lock_guard<boost::mutex> l(
lock_);
119 boost::lock_guard<boost::mutex> l(
lock_);
123 virtual void ToJson(rapidjson::Document* document, rapidjson::Value* val) {
124 boost::lock_guard<boost::mutex> l(
lock_);
125 rapidjson::Value container(rapidjson::kObjectType);
127 rapidjson::Value units(
PrintTUnit(
unit_).c_str(), document->GetAllocator());
128 container.AddMember(
"units", units, document->GetAllocator());
131 document->GetAllocator());
134 container.AddMember(
"last",
value_, document->GetAllocator());
135 container.AddMember(
"min", boost::accumulators::min(
acc_),
136 document->GetAllocator());
137 container.AddMember(
"max", boost::accumulators::max(
acc_),
138 document->GetAllocator());
139 container.AddMember(
"mean", boost::accumulators::mean(
acc_),
140 document->GetAllocator());
141 container.AddMember(
"stddev", sqrt(boost::accumulators::variance(
acc_)),
142 document->GetAllocator());
148 std::stringstream ss;
149 boost::lock_guard<boost::mutex> l(
lock_);
150 rapidjson::Value container(rapidjson::kObjectType);
152 document->GetAllocator());
155 container.AddMember(
"last",
value_, document->GetAllocator());
156 container.AddMember(
"min", boost::accumulators::min(
acc_),
157 document->GetAllocator());
158 container.AddMember(
"max", boost::accumulators::max(
acc_),
159 document->GetAllocator());
160 container.AddMember(
"mean", boost::accumulators::mean(
acc_),
161 document->GetAllocator());
162 container.AddMember(
"stddev", sqrt(boost::accumulators::variance(
acc_)),
163 document->GetAllocator());
165 document->AddMember(
key_.c_str(), container, document->GetAllocator());
169 std::stringstream out;
171 if (boost::accumulators::count(
acc_) > 0) {
177 sqrt(boost::accumulators::variance(
acc_)),
unit_);
193 typedef boost::accumulators::accumulator_set<T,
194 boost::accumulators::features<boost::accumulators::tag::mean,
196 boost::accumulators::tag::min,
197 boost::accumulators::tag::max,
std::set< T > value_
The set of items.
SetMetric(const std::string &key, const std::set< T > &value, const std::string &description="")
TUnit::type unit_
The units of the values captured in this metric, used when pretty-printing.
const std::string key_
Unique key identifying this metric.
void Remove(const T &item)
Remove an item from this set by value.
void AddStandardFields(rapidjson::Document *document, rapidjson::Value *val)
std::string PrintTUnit(const TUnit::type &type)
virtual void ToLegacyJson(rapidjson::Document *document)
This method is kept for backwards-compatibility with CM5.0.
boost::mutex lock_
Lock protecting the set.
void Add(const T &item)
Put an item in this set.
boost::accumulators::accumulator_set< T, boost::accumulators::features< boost::accumulators::tag::mean, boost::accumulators::tag::count, boost::accumulators::tag::min, boost::accumulators::tag::max, boost::accumulators::tag::variance > > Accumulator
The set of accumulators that update the statistics on each Update()
virtual void ToJson(rapidjson::Document *document, rapidjson::Value *val)
name, value, human_readable, description
virtual void ToLegacyJson(rapidjson::Document *document)
This method is kept for backwards-compatibility with CM5.0.
static std::string Print(bool value, TUnit::type ignored, bool verbose=false)
virtual std::string ToHumanReadable()
virtual std::string ToHumanReadable()
const std::string & key() const
void Update(const T &value)
Metric whose value is a set of items.
StatsMetric(const std::string &key, const TUnit::type unit, const std::string &description="")
const std::string & description() const
boost::mutex lock_
Lock protecting the value and the accumulator_set.
TODO: Add ToThrift() for conversion to an RPC-friendly format.
ToJsonValue(const T &value, const TUnit::type unit, rapidjson::Document *document, rapidjson::Value *out_val)
virtual void ToJson(rapidjson::Document *document, rapidjson::Value *value)
name, value, human_readable, description