Commit 9ac4ed9b authored by Luciano Orsini's avatar Luciano Orsini
Browse files

references #110: added gauge and summary

parent 6c79ae0d
......@@ -48,6 +48,8 @@ Sources=\
summary.cc \
time_window_quantiles.cc \
xdata/Counter.cc \
xdata/Gauge.cc \
xdata/Summary.cc \
Example.cc
......
......@@ -75,7 +75,9 @@ namespace prometheus
std::string uri_;
std::vector<std::weak_ptr<Collectable>> collectables_;
std::shared_ptr<Registry> exposer_registry_;
// resident monitorables
std::shared_ptr<Registry> exposer_registry_;
Family<Counter>& bytes_transferred_family_;
Counter & bytes_transferred_;
......@@ -86,7 +88,7 @@ namespace prometheus
Family<Summary>& request_latencies_family_;
Summary& request_latencies_;
// user example
// resident user example
std::shared_ptr<Registry> user_exposer_registry_;
Family<Counter> & counter_family_;
Counter & second_counter_;
......
......@@ -19,6 +19,9 @@
#include "xdata/Counter.h"
#include "xdata/Gauge.h"
#include "xdata/Summary.h"
namespace prometheus
{
......@@ -29,6 +32,10 @@ namespace prometheus
public:
static const std::string MY_COUNTER;
static const std::string MY_GAUGE;
static const std::string MY_SUMMARY;
XDAQ_INSTANTIATOR();
......@@ -42,6 +49,9 @@ namespace prometheus
protected:
xdata::Counter counter_;
xdata::Gauge gauge_;
xdata::Summary summary_;
};
}
......
// $Id$
/*************************************************************************
* XDAQ Components for Distributed Data Acquisition *
* Copyright (C) 2000-2019, CERN. *
* All rights reserved. *
* Authors: L. Orsini, D. Simelevicius *
* *
* For the licensing terms see LICENSE. *
* For the list of contributors see CREDITS. *
*************************************************************************/
#ifndef _xdata_Gauge_h_
#define _xdata_Gauge_h_
#include <string>
#include <map>
#include "prometheus/gauge.h"
#include "prometheus/registry.h"
#include "xdata/Collectable.h"
#include "xdata/Serializable.h"
namespace xdata
{
// e.g. Gauge("exposer_transferred_bytes_total","Transferred bytes to metrics services", {{"label", "value"}})
class Gauge: public Collectable
{
public:
class Metric
{
public:
Metric(prometheus::Gauge & c);
/// \brief Increment the gauge by 1.
void Increment();
/// \brief Increment the gauge by the given amount.
void Increment(double);
/// \brief Decrement the gauge by 1.
void Decrement();
/// \brief Decrement the gauge by the given amount.
void Decrement(double);
/// \brief Set the gauge to the given value.
void Set(double);
/// \brief Set the gauge to the current unixtime in seconds.
void SetToCurrentTime();
/// \brief Get the current value of the gauge.
double Value() const;
protected:
prometheus::Gauge & metric_;
};
Gauge(const std::string & name, const std::string & help, const std::map<std::string, std::string>& labels);
std::string type() const;
void setValue (const xdata::Serializable& s);
int equals(const xdata::Serializable & s) const;
std::string toString() const;
void fromString(const std::string& value);
Metric operator[](const std::map<std::string, std::string>& labels);
prometheus::Family<prometheus::Gauge> & family_;
std::map<size_t, prometheus::Gauge *> labels_;
};
}
#endif
// $Id$
/*************************************************************************
* XDAQ Components for Distributed Data Acquisition *
* Copyright (C) 2000-2019, CERN. *
* All rights reserved. *
* Authors: L. Orsini, D. Simelevicius *
* *
* For the licensing terms see LICENSE. *
* For the list of contributors see CREDITS. *
*************************************************************************/
#ifndef _xdata_Summary_h_
#define _xdata_Summary_h_
#include <string>
#include <map>
#include "prometheus/summary.h"
#include "prometheus/registry.h"
#include "xdata/Collectable.h"
#include "xdata/Serializable.h"
namespace xdata
{
// e.g. Summary("exposer_transferred_bytes_total","Transferred bytes to metrics services", {{"label", "value"}})
class Summary: public Collectable
{
public:
using Quantiles = std::vector<prometheus::detail::CKMSQuantiles::Quantile>;
class Metric
{
public:
Metric(prometheus::Summary & c);
/// \brief Observe the given amount.
void Observe(double value);
protected:
prometheus::Summary & metric_;
};
Summary(const std::string & name, const std::string & help, const std::map<std::string, std::string>& labels);
std::string type() const;
void setValue (const xdata::Serializable& s);
int equals(const xdata::Serializable & s) const;
std::string toString() const;
void fromString(const std::string& value);
template <typename... Args>
Metric operator()(const std::map<std::string, std::string>& labels, Args&&... args);
prometheus::Family<prometheus::Summary> & family_;
std::map<size_t, prometheus::Summary *> labels_;
};
}
template <typename... Args>
xdata::Summary::Metric xdata::Summary::operator() ( const std::map<std::string, std::string>& labels, Args&&... args)
{
auto lhash = prometheus::detail::hash_labels(labels);
//std::cout << "get hash for labels: " << lhash << std::endl;
if ( labels_.find(lhash) != labels_.end() )
{
return xdata::Summary::Metric(*(labels_[lhash]));
}
else
{
labels_[lhash] = &(family_.Add(labels, args...));
return xdata::Summary::Metric(*(labels_[lhash]));
}
}
#endif
......@@ -244,7 +244,7 @@ void prometheus::Application::timeExpired(toolbox::task::TimerEvent& e)
if ( collectable == registry ) // not sure it compares the actual fired item
{
std::cout << "---found already registered collectable: " << j->first << " of type: " << j->second->type() << std::endl;
//std::cout << "---found already registered collectable: " << j->first << " of type: " << j->second->type() << std::endl;
return true;
}
......@@ -254,7 +254,7 @@ void prometheus::Application::timeExpired(toolbox::task::TimerEvent& e)
if (! exists )
{
std::cout << "---register item: " << j->first << " of type: " << j->second->type() << std::endl;
//std::cout << "---register item: " << j->first << " of type: " << j->second->type() << std::endl;
collectables_.push_back(registry);
}
......
......@@ -22,10 +22,16 @@
XDAQ_INSTANTIATOR_IMPL(prometheus::Example);
const std::string prometheus::Example::MY_COUNTER = "my_counter_total";
const std::string prometheus::Example::MY_GAUGE = "my_gauge_total";
const std::string prometheus::Example::MY_SUMMARY = "my_latency_";
prometheus::Example::Example (xdaq::ApplicationStub* s)
: xdaq::Application(s), xgi::framework::UIManager(this),
counter_(prometheus::Example::MY_COUNTER, "How many times it ticks?", {{"my_counter_total_label", "value"}})
counter_(prometheus::Example::MY_COUNTER, "How many times it ticks?", {{"my_counter_total_label", "value"}}),
gauge_(prometheus::Example::MY_GAUGE, "Temperature?", {{"my_temperature_total_label", "value"}}),
summary_(prometheus::Example::MY_SUMMARY, "Latency?", {{"my_latency_label", "value"}})
{
// create a qualified infospace to contain prometheus objects
......@@ -35,13 +41,15 @@ prometheus::Example::Example (xdaq::ApplicationStub* s)
// publish counter
is->lock();
is->fireItemAvailable(prometheus::Example::MY_COUNTER, &counter_);
is->fireItemAvailable(prometheus::Example::MY_GAUGE, &gauge_);
is->fireItemAvailable(prometheus::Example::MY_SUMMARY, &summary_);
is->unlock();
// 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::TimeInterval interval(1.0);
toolbox::TimeVal start = toolbox::TimeVal::gettimeofday();
timer->scheduleAtFixedRate( start, this,interval , 0, "urn:prometheus:watchdog" );
......@@ -62,11 +70,15 @@ void prometheus::Example::timeExpired(toolbox::task::TimerEvent& e)
if (e.getTimerTask()->name == "urn:prometheus:watchdog" )
{
// user example update
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);
auto c = counter_[{{"my_example_counter_label", "value"}}];
c.Increment(1);
auto g = gauge_[{{"my_example_temperature_label", "value"}}];
g.Set(sin(c.Value()));
auto s = summary_({},xdata::Summary::Quantiles{{0.5, 0.05}, {0.9, 0.01}, {0.99, 0.001}});
s.Observe(g.Value());
return;
}
......
......@@ -68,7 +68,7 @@ void xdata::Counter::fromString(const std::string& value)
xdata::Counter::Metric xdata::Counter::operator[] ( const std::map<std::string, std::string>& labels)
{
auto lhash = prometheus::detail::hash_labels(labels);
std::cout << "get hash for labels: " << lhash << std::endl;
//std::cout << "get hash for labels: " << lhash << std::endl;
if ( labels_.find(lhash) != labels_.end() )
{
return xdata::Counter::Metric(*(labels_[lhash]));
......
// $Id$
/*************************************************************************
* XDAQ Components for Distributed Data Acquisition *
* Copyright (C) 2000-2019, CERN. *
* All rights reserved. *
* Authors: L. Orsini, D. Simelevicius *
* *
* For the licensing terms see LICENSE. *
* For the list of contributors see CREDITS. *
*************************************************************************/
#include "xdata/Gauge.h"
#include "prometheus/utils.h"
xdata::Gauge::Metric::Metric( prometheus::Gauge & c): metric_(c)
{
}
void xdata::Gauge::Metric::Increment()
{
metric_.Increment();
}
void xdata::Gauge::Metric::Increment(double v)
{
metric_.Increment(v);
}
void xdata::Gauge::Metric::Decrement()
{
metric_.Decrement();
}
void xdata::Gauge::Metric::Decrement(double v)
{
metric_.Decrement(v);
}
void xdata::Gauge::Metric::Set(double v)
{
metric_.Set(v);
}
void xdata::Gauge::Metric::SetToCurrentTime()
{
metric_.SetToCurrentTime();
}
double xdata::Gauge::Metric::Value() const
{
return metric_.Value();
}
xdata::Gauge::Gauge(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::BuildGauge().Name(name).Help(help).Labels(labels).Register(*xdata::Collectable::registry_))
{
}
std::string xdata::Gauge::type() const
{
return "prometheus::gauge";
}
void xdata::Gauge::setValue (const xdata::Serializable& s)
{
}
int xdata::Gauge::equals(const xdata::Serializable & s) const
{
return 0;
}
std::string xdata::Gauge::toString() const
{
return "complex value";
}
void xdata::Gauge::fromString(const std::string& value)
{
}
xdata::Gauge::Metric xdata::Gauge::operator[] ( const std::map<std::string, std::string>& labels)
{
auto lhash = prometheus::detail::hash_labels(labels);
//std::cout << "get hash for labels: " << lhash << std::endl;
if ( labels_.find(lhash) != labels_.end() )
{
return xdata::Gauge::Metric(*(labels_[lhash]));
}
else
{
labels_[lhash] = &(family_.Add(labels));
return xdata::Gauge::Metric(*(labels_[lhash]));
}
}
// $Id$
/*************************************************************************
* XDAQ Components for Distributed Data Acquisition *
* Copyright (C) 2000-2019, CERN. *
* All rights reserved. *
* Authors: L. Orsini, D. Simelevicius *
* *
* For the licensing terms see LICENSE. *
* For the list of contributors see CREDITS. *
*************************************************************************/
#include "xdata/Summary.h"
#include "prometheus/utils.h"
xdata::Summary::Metric::Metric( prometheus::Summary & c): metric_(c)
{
}
void xdata::Summary::Metric::Observe(double value)
{
metric_.Observe(value);
}
xdata::Summary::Summary(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::BuildSummary().Name(name).Help(help).Labels(labels).Register(*xdata::Collectable::registry_))
{
}
std::string xdata::Summary::type() const
{
return "prometheus::summary";
}
void xdata::Summary::setValue (const xdata::Serializable& s)
{
}
int xdata::Summary::equals(const xdata::Serializable & s) const
{
return 0;
}
std::string xdata::Summary::toString() const
{
return "complex value";
}
void xdata::Summary::fromString(const std::string& value)
{
}
/*
template <typename... Args>
xdata::Summary::Metric xdata::Summary::operator() ( const std::map<std::string, std::string>& labels, Args&&... args)
{
auto lhash = prometheus::detail::hash_labels(labels);
//std::cout << "get hash for labels: " << lhash << std::endl;
if ( labels_.find(lhash) != labels_.end() )
{
return xdata::Summary::Metric(*(labels_[lhash]));
}
else
{
labels_[lhash] = &(family_.Add(labels, args));
return xdata::Summary::Metric(*(labels_[lhash]));
}
}
*/
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