Files
spark/src/lib/metrics/metrics.cpp
T

71 lines
1.8 KiB
C++

#include "metrics.h"
#include "lib/utils/ConcurrentAppendCollection.h"
#include <sstream>
using std::shared_ptr;
using std::vector;
namespace bts {
namespace metrics {
Metric::Metric(const std::string &cname, const std::vector<Label> &clabels)
: name(cname), labels(clabels) {}
std::string Metric::str() const {
std::stringstream ss;
print(ss);
return ss.str();
}
std::ostream &operator<<(std::ostream &os, const std::vector<Label> &labels) {
if (!labels.empty()) {
os << "{";
for (auto iter = labels.begin(); iter != labels.end(); ++iter) {
if (iter != labels.begin())
os << ",";
os << iter->name << "=\"" << iter->value << "\"";
}
os << "}";
}
return os;
}
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;
}
//
// Histogram
// -- collection of CounterMetrics?
struct MetricCollection {
ConcurrentAppendCollection<shared_ptr<Metric>> metrics;
};
MetricsContainer::MetricsContainer()
: _me(std::make_unique<MetricCollection>()) {}
MetricsContainer::~MetricsContainer() {}
void MetricsContainer::print(std::ostream &out) const {
for (const auto &m : _me->metrics) {
m->print(out);
}
}
void MetricsContainer::publish(std::shared_ptr<Metric> m) {
_me->metrics.push_back(m);
}
} // namespace metrics
} // namespace bts