Commit 6c79ae0d authored by Luciano Orsini's avatar Luciano Orsini
Browse files

references #110: refactorized for portability

parent d910b3dc
......@@ -14,11 +14,8 @@
#define _prometheus_Example_h_
#include "xdaq/Application.h"
#include "xgi/framework/UIManager.h"
#include "toolbox/ActionListener.h"
#include "xdata/ActionListener.h"
#include "toolbox/task/TimerListener.h"
#include "xgi/framework/UIManager.h"
#include "xdata/Counter.h"
......@@ -27,26 +24,24 @@ namespace prometheus
{
// Example of a prometheus counter in XDAQ
//
class Example : public xdaq::Application, public xgi::framework::UIManager, public toolbox::ActionListener, public xdata::ActionListener, public toolbox::task::TimerListener
class Example : public xdaq::Application, public xgi::framework::UIManager, public toolbox::task::TimerListener
{
public:
static const std::string MY_COUNTER;
XDAQ_INSTANTIATOR();
Example(xdaq::ApplicationStub* s);
~Example();
void actionPerformed ( xdata::Event& e );
void actionPerformed(toolbox::Event& e);
void timeExpired(toolbox::task::TimerEvent& e);
void Default(xgi::Input * in, xgi::Output * out );
protected:
xdata::Counter * counter_;
xdata::Counter * counter2_;
xdata::Counter counter_;
};
}
......
......@@ -32,6 +32,28 @@ namespace xdata
{
public:
class Metric
{
public:
Metric(prometheus::Counter & c);
/// \brief Increment the counter by 1.
void Increment();
/// \brief Increment the counter by a given amount.
///
/// The counter will not change if the given amount is negative.
void Increment(double);
/// \brief Get the current value of the counter.
double Value() const;
protected:
prometheus::Counter & metric_;
};
Counter(const std::string & name, const std::string & help, const std::map<std::string, std::string>& labels);
std::string type() const;
......@@ -44,24 +66,13 @@ namespace xdata
void fromString(const std::string& value);
prometheus::Counter & add (const std::string & name, const std::map<std::string, std::string>& labels);
prometheus::Counter & operator[](const std::string & name);
Metric operator[](const std::map<std::string, std::string>& labels);
prometheus::Family<prometheus::Counter> & family_;
std::map<std::string, prometheus::Counter *> counters_;
std::map<size_t, prometheus::Counter *> labels_;
};
class CounterElement
{
public:
CounterElement(prometheus::Counter & c): reference_(c) {}
prometheus::Counter & reference_;
};
}
#endif
......@@ -109,8 +109,12 @@ static std::size_t WriteResponse(xgi::Input * in, xgi::Output * out, const std::
// //mg_write(conn, body.data(), body.size());
auto bcl = toolbox::toString("%lu",static_cast<unsigned long>(body.size()));
out->getHTTPResponseHeader().addHeader("Content-Length", bcl.c_str());
out->write(body.data(), body.size());
//DEBUG
std::cout << body.data() << std::endl;
return body.size();
//end write response
}
......@@ -179,30 +183,6 @@ prometheus::Application::Application (xdaq::ApplicationStub* s)
collectables_.push_back(exposer_registry_);
collectables_.push_back(user_exposer_registry_);
// alternative API for XDAQ
//auto tmp = new xdata::Counter("another_time_running_seconds_total", "Another How many seconds is this server running?", {{"label", "value"}});
//auto * element = new prometheus::serializable::CounterElement(tmp->family_.Add({{"my_another_label", "value"}, {"my_yet_another_label", "value"}}));
//element = new prometheus::serializable::CounterElement(tmp->family_.Add({{"your_another_label", "value"}, {"your_yet_another_label", "value"}}));
/*auto * aptr = &(tmp->family_.Add({{"my_another_label", "value"}, {"my_yet_another_label", "value"}}));
aptr->Increment();
tmp->family_.Add({{"your_another_label", "value"}, {"your_yet_another_label", "value"}});
tmp->add("my_xdata", {{"xdata_label", "value"}, {"yet_another_xdata_label", "value"}});
tmp->add("my_xdata2", {});
(*tmp)["my_xdata"].Increment();
(*tmp)["my_xdata"].Increment();
(*tmp)["my_xdata"].Increment();
(*tmp)["my_xdata2"].Increment();
(*tmp)["my_xdata2"].Increment(39);
//element->reference_.Increment();
*/
//collectables_.push_back(tmp->registry_);
s->getDescriptor()->setAttribute("icon", "/prometheus/images/prometheus_64x64.png");
s->getDescriptor()->setAttribute("icon16x16", "/prometheus/images/prometheus_16x16.ico");
......@@ -297,16 +277,6 @@ void prometheus::Application::actionPerformed( xdata::Event& event)
{
// activate a timer for user example
toolbox::task::Timer * timer = 0;
timer = toolbox::task::getTimerFactory()->createTimer("urn:prometheus:timer");
toolbox::TimeInterval interval;
toolbox::TimeVal start;
start = toolbox::TimeVal::gettimeofday();
interval.fromString("PT3S");
timer->scheduleAtFixedRate( start, this,interval , 0, "urn:prometheus:check" );
}
......@@ -322,6 +292,17 @@ void prometheus::Application::actionPerformed(toolbox::Event& e)
LOG4CPLUS_DEBUG(this->getApplicationLogger(), "Received event " << e.type());
if ( e.type() == "urn:xdaq-event:profile-loaded")
{
// activate a timer for user example
toolbox::task::Timer * timer = 0;
timer = toolbox::task::getTimerFactory()->createTimer("urn:prometheus:timer");
toolbox::TimeInterval interval;
toolbox::TimeVal start;
start = toolbox::TimeVal::gettimeofday();
interval.fromString("PT5S");
timer->scheduleAtFixedRate( start, this,interval , 0, "urn:prometheus:check" );
}
}
......@@ -342,7 +323,7 @@ void prometheus::Application::metrics(xgi::Input * in, xgi::Output * out )
}
auto&& metrics = collectable->Collect();
std::cout << "collectable insert" << std::endl;
//std::cout << "collectable insert" << std::endl;
collected_metrics.insert(collected_metrics.end(), std::make_move_iterator(metrics.begin()), std::make_move_iterator(metrics.end()));
}
// end collect metrics
......
......@@ -21,28 +21,32 @@
XDAQ_INSTANTIATOR_IMPL(prometheus::Example);
const std::string prometheus::Example::MY_COUNTER = "my_counter_total";
prometheus::Example::Example (xdaq::ApplicationStub* s)
: xdaq::Application(s), xgi::framework::UIManager(this)
: xdaq::Application(s), xgi::framework::UIManager(this),
counter_(prometheus::Example::MY_COUNTER, "How many times it ticks?", {{"my_counter_total_label", "value"}})
{
// create a qualified infospace to contain prometheus objects
toolbox::net::URN urn = this->createQualifiedInfoSpace("prometheus");
xdata::InfoSpace * is = xdata::getInfoSpaceFactory()->get(urn.toString());
// publish counter
is->lock();
is->fireItemAvailable(prometheus::Example::MY_COUNTER, &counter_);
is->unlock();
// create a prometheus object ( e.g. xdata::Counter )
counter_ = new xdata::Counter("my_counter_total", "How many times it ticks?", {{"my_counter_total_label", "value"}});
counter_->add("my_counter", {{"my_example_label", "value"}});
// create a prometheus object ( e.g. xdata::Counter )
counter2_ = new xdata::Counter("my_counter_total2", "How many times it ticks2?", {{"my_counter_total_label2", "value"}});
counter2_->add("my_counter2", {{"my_example_label2", "value"}});
// activate a timer for user example
toolbox::task::Timer * timer = 0;
timer = toolbox::task::getTimerFactory()->createTimer("urn:prometheus:application");
toolbox::TimeInterval interval(3.0);
toolbox::TimeVal start = toolbox::TimeVal::gettimeofday();
xgi::framework::deferredbind(this, this, &prometheus::Example::Default, "Default");
timer->scheduleAtFixedRate( start, this,interval , 0, "urn:prometheus:watchdog" );
//
// Bind setting of default parameters
getApplicationInfoSpace()->addListener(this, "urn:xdaq-event:setDefaultValues");
getApplicationContext()->addActionListener(this);
xgi::framework::deferredbind(this, this, &prometheus::Example::Default, "Default");
}
......@@ -58,54 +62,14 @@ void prometheus::Example::timeExpired(toolbox::task::TimerEvent& e)
if (e.getTimerTask()->name == "urn:prometheus:watchdog" )
{
// user example update
(*counter_)["my_counter"].Increment();
(*counter2_)["my_counter2"].Increment(2);
counter_[{{"my_example_label", "value"}}].Increment();
counter_[{{"my_example_label2", "value1"}}].Increment(2);
counter_[{{"my_example_label2", "value2"}}].Increment(4);
counter_[{{"my_example_label4", "value3"}}].Increment(8);
counter_[{{"my_example_label5", "value4"}}].Increment(16);
return;
}
}
void prometheus::Example::actionPerformed(toolbox::Event& e)
{
LOG4CPLUS_DEBUG(this->getApplicationLogger(), "Received event " << e.type());
if ( e.type() == "urn:xdaq-event:profile-loaded")
{
std::cout << "got event " << e.type() << std::endl;
}
}
void prometheus::Example::actionPerformed( xdata::Event& event)
{
if (event.type() == "urn:xdaq-event:setDefaultValues")
{
// create a qualified infospace to contain prometheus objects
toolbox::net::URN urn = this->createQualifiedInfoSpace("prometheus");
xdata::InfoSpace * is = xdata::getInfoSpaceFactory()->get(urn.toString());
// publish counter to prometheus
is->lock();
is->fireItemAvailable("another_time_running_seconds_total", counter_);
is->fireItemAvailable("another_time_running_seconds_total2", counter2_);
is->unlock();
// activate a timer for user example
toolbox::task::Timer * timer = 0;
timer = toolbox::task::getTimerFactory()->createTimer("urn:prometheus:application");
toolbox::TimeInterval interval;
toolbox::TimeVal start;
start = toolbox::TimeVal::gettimeofday();
interval.fromString("PT3S");
timer->scheduleAtFixedRate( start, this,interval , 0, "urn:prometheus:watchdog" );
}
else
{
LOG4CPLUS_ERROR (this->getApplicationLogger(), "Received unsupported event type '" << event.type() << "'");
}
}
void prometheus::Example::Default(xgi::Input * in, xgi::Output * out )
......
......@@ -12,6 +12,28 @@
#include "xdata/Counter.h"
#include "prometheus/utils.h"
xdata::Counter::Metric::Metric( prometheus::Counter & c): metric_(c)
{
}
void xdata::Counter::Metric::Increment()
{
metric_.Increment();
}
void xdata::Counter::Metric::Increment(double v)
{
metric_.Increment(v);
}
double xdata::Counter::Metric::Value() const
{
return metric_.Value();
}
xdata::Counter::Counter(const std::string & name, const std::string & help, const std::map<std::string, std::string>& labels):
xdata::Collectable(std::make_shared<prometheus::Registry>()),family_(prometheus::BuildCounter().Name(name).Help(help).Labels(labels).Register(*xdata::Collectable::registry_))
{
......@@ -30,7 +52,7 @@ void xdata::Counter::setValue (const xdata::Serializable& s)
int xdata::Counter::equals(const xdata::Serializable & s) const
{
return 1;
return 0;
}
std::string xdata::Counter::toString() const
......@@ -43,28 +65,18 @@ void xdata::Counter::fromString(const std::string& value)
}
prometheus::Counter & xdata::Counter::add (const std::string & name, const std::map<std::string, std::string>& labels)
xdata::Counter::Metric xdata::Counter::operator[] ( const std::map<std::string, std::string>& labels)
{
if ( counters_.find(name) != counters_.end() )
auto lhash = prometheus::detail::hash_labels(labels);
std::cout << "get hash for labels: " << lhash << std::endl;
if ( labels_.find(lhash) != labels_.end() )
{
return *(counters_[name]);
return xdata::Counter::Metric(*(labels_[lhash]));
}
else
{
counters_[name] = &(family_.Add(labels));
return *(counters_[name]);
labels_[lhash] = &(family_.Add(labels));
return xdata::Counter::Metric(*(labels_[lhash]));
}
}
prometheus::Counter& xdata::Counter::operator[](const std::string & name)
{
if ( counters_.find(name) != counters_.end() )
{
return *(counters_[name]);
}
else
{
counters_[name] = &(family_.Add({}));
return *(counters_[name]);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment