15 #ifndef IMPALA_UTIL_METRICS_H
16 #define IMPALA_UTIL_METRICS_H
22 #include <boost/foreach.hpp>
23 #include <boost/function.hpp>
24 #include <boost/scoped_ptr.hpp>
25 #include <boost/thread/locks.hpp>
26 #include <boost/thread/mutex.hpp>
59 virtual void ToJson(rapidjson::Document* document, rapidjson::Value* val) = 0;
65 virtual void ToLegacyJson(rapidjson::Document* document) = 0;
71 const std::string&
key()
const {
return key_; }
103 template<
typename T, TMetricKind::type metric_kind=TMetricKind::GAUGE>
107 const T& initial_value,
const std::string&
description =
"")
119 boost::lock_guard<boost::mutex> l(
lock_);
126 boost::lock_guard<boost::mutex> l(
lock_);
132 DCHECK(
kind() != TMetricKind::PROPERTY)
133 <<
"Can't change value of PROPERTY metric: " <<
key();
134 DCHECK(
kind() != TMetricKind::COUNTER || delta >= 0)
135 <<
"Can't decrement value of COUNTER metric: " <<
key();
136 boost::lock_guard<boost::mutex> l(
lock_);
140 virtual void ToJson(rapidjson::Document* document, rapidjson::Value* val) {
141 rapidjson::Value container(rapidjson::kObjectType);
144 rapidjson::Value metric_value;
146 container.AddMember(
"value", metric_value, document->GetAllocator());
149 document->GetAllocator());
150 container.AddMember(
"kind", type_value, document->GetAllocator());
151 rapidjson::Value units(
PrintTUnit(
unit()).c_str(), document->GetAllocator());
152 container.AddMember(
"units", units, document->GetAllocator());
161 rapidjson::Value val;
163 document->AddMember(
key_.c_str(), val, document->GetAllocator());
167 const TMetricKind::type
kind()
const {
return metric_kind; }
210 template <
typename M>
212 boost::lock_guard<boost::mutex> l(
lock_);
213 DCHECK(!metric->key_.empty());
224 const T& value,
const TUnit::type unit = TUnit::NONE,
225 const std::string& description =
"") {
227 (key, unit, value, description));
232 const std::string& key,
const T& value,
233 const std::string& description =
"") {
235 TUnit::NONE, value, description));
240 const T& value,
const TUnit::type unit = TUnit::UNIT,
241 const std::string& description =
"") {
252 template <
typename M>
254 boost::lock_guard<boost::mutex> l(
lock_);
255 std::stack<MetricGroup*> groups;
257 while (!groups.empty()) {
260 MetricMap::const_iterator it = group->
metric_map_.find(key);
261 if (it != group->
metric_map_.end())
return reinterpret_cast<M*>(it->second);
262 BOOST_FOREACH(
const ChildGroupMap::value_type& child, group->
children_) {
263 groups.push(child.second);
274 void ToJson(
bool include_children, rapidjson::Document* document,
275 rapidjson::Value* out_val);
307 rapidjson::Document* document);
313 rapidjson::Document* document);
327 #endif // IMPALA_UTIL_METRICS_H
SimpleMetric< T, TMetricKind::COUNTER > * AddCounter(const std::string &key, const T &value, const TUnit::type unit=TUnit::UNIT, const std::string &description="")
class SimpleMetric< std::string, TMetricKind::PROPERTY > StringProperty
const std::string key_
Unique key identifying this metric.
boost::scoped_ptr< ObjectPool > obj_pool_
Pool containing all metric objects.
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.
virtual void ToJson(rapidjson::Document *document, rapidjson::Value *val)
name, value, human_readable, description
SimpleMetric(const std::string &key, const TUnit::type unit, const std::string &description="")
std::map< std::string, MetricGroup * > ChildGroupMap
All child metric groups.
MetricGroup(const std::string &name)
class SimpleMetric< int64_t, TMetricKind::COUNTER > IntCounter
M * RegisterMetric(M *metric)
MetricGroup * GetChildGroup(const std::string &name)
Creates or returns an already existing child metric group.
MetricGroups may be organised hierarchically as a tree.
std::map< std::string, Metric * > MetricMap
Contains all Metric objects, indexed by key.
virtual void ToJson(rapidjson::Document *document, rapidjson::Value *val)=0
name, value, human_readable, description
const TUnit::type unit_
Units of this metric.
static std::string Print(bool value, TUnit::type ignored, bool verbose=false)
SimpleMetric(const std::string &key, const TUnit::type unit, const T &initial_value, const std::string &description="")
Status Init(Webserver *webserver)
std::map< std::string, std::string > ArgumentMap
std::string PrintTMetricKind(const TMetricKind::type &type)
M * FindMetricForTesting(const std::string &key)
Used for testing only.
const std::string description_
virtual void ToLegacyJson(rapidjson::Document *document)=0
This method is kept for backwards-compatibility with CM5.0.
const std::string & name() const
std::string DebugString()
Useful for debuggers, returns the output of CMCompatibleCallback().
void TemplateCallback(const Webserver::ArgumentMap &args, rapidjson::Document *document)
SimpleMetric< T, TMetricKind::PROPERTY > * AddProperty(const std::string &key, const T &value, const std::string &description="")
const std::string & key() const
T value()
Returns the current value, updating it if necessary. Thread-safe.
virtual void CalculateValue()
std::string name_
Name of this metric group.
void set_value(const T &value)
Sets the current value. Thread-safe.
const std::string & description() const
class SimpleMetric< bool, TMetricKind::PROPERTY > BooleanProperty
class SimpleMetric< double, TMetricKind::GAUGE > DoubleGauge
void ToJson(bool include_children, rapidjson::Document *document, rapidjson::Value *out_val)
Converts this metric group (and optionally all of its children recursively) to JSON.
SimpleMetric< T > * AddGauge(const std::string &key, const T &value, const TUnit::type unit=TUnit::NONE, const std::string &description="")
Create a gauge metric object with given key and initial value (owned by this object) ...
void CMCompatibleCallback(const Webserver::ArgumentMap &args, rapidjson::Document *document)
boost::mutex lock_
Guards access to value_.
const TUnit::type unit() const
void Increment(const T &delta)
Adds 'delta' to the current value atomically.
virtual std::string ToHumanReadable()=0
Metric(const std::string &key, const std::string &description)
TODO: Add ToThrift() for conversion to an RPC-friendly format.
boost::mutex lock_
Guards metric_map_ and children_.
T value_
The current value of the metric.
virtual ~Metric()
Empty virtual destructor.
const TMetricKind::type kind() const
virtual std::string ToHumanReadable()
ToJsonValue(const T &value, const TUnit::type unit, rapidjson::Document *document, rapidjson::Value *out_val)