Skip to content

Commit

Permalink
Merge a9d46e8 into d5d814d
Browse files Browse the repository at this point in the history
  • Loading branch information
mfine committed Apr 19, 2019
2 parents d5d814d + a9d46e8 commit 57f6036
Show file tree
Hide file tree
Showing 18 changed files with 76 additions and 13 deletions.
1 change: 1 addition & 0 deletions core/include/prometheus/counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Counter {
///
/// Collect is called by the Registry when collecting metrics.
ClientMetric Collect() const;
bool Expired(double) const;

private:
Gauge gauge_{0.0};
Expand Down
2 changes: 2 additions & 0 deletions core/include/prometheus/detail/counter_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ class CounterBuilder {
CounterBuilder& Labels(const std::map<std::string, std::string>& labels);
CounterBuilder& Name(const std::string&);
CounterBuilder& Help(const std::string&);
CounterBuilder& Seconds(double);
Family<Counter>& Register(Registry&);

private:
std::map<std::string, std::string> labels_;
std::string name_;
std::string help_;
double seconds_;
};

} // namespace detail
Expand Down
2 changes: 2 additions & 0 deletions core/include/prometheus/detail/gauge_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ class GaugeBuilder {
GaugeBuilder& Labels(const std::map<std::string, std::string>& labels);
GaugeBuilder& Name(const std::string&);
GaugeBuilder& Help(const std::string&);
GaugeBuilder& Seconds(double);
Family<Gauge>& Register(Registry&);

private:
std::map<std::string, std::string> labels_;
std::string name_;
std::string help_;
double seconds_;
};

} // namespace detail
Expand Down
2 changes: 2 additions & 0 deletions core/include/prometheus/detail/histogram_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ class HistogramBuilder {
HistogramBuilder& Labels(const std::map<std::string, std::string>& labels);
HistogramBuilder& Name(const std::string&);
HistogramBuilder& Help(const std::string&);
HistogramBuilder& Seconds(double);
Family<Histogram>& Register(Registry&);

private:
std::map<std::string, std::string> labels_;
std::string name_;
std::string help_;
double seconds_;
};

} // namespace detail
Expand Down
2 changes: 2 additions & 0 deletions core/include/prometheus/detail/summary_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ class SummaryBuilder {
SummaryBuilder& Labels(const std::map<std::string, std::string>& labels);
SummaryBuilder& Name(const std::string&);
SummaryBuilder& Help(const std::string&);
SummaryBuilder& Seconds(double);
Family<Summary>& Register(Registry&);

private:
std::map<std::string, std::string> labels_;
std::string name_;
std::string help_;
double seconds_;
};

} // namespace detail
Expand Down
13 changes: 9 additions & 4 deletions core/include/prometheus/family.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ class Family : public Collectable {
/// metric. All these labels are propagated to each time series within the
/// metric.
Family(const std::string& name, const std::string& help,
const std::map<std::string, std::string>& constant_labels);
const std::map<std::string, std::string>& constant_labels,
double seconds);

/// \brief Add a new dimensional data.
///
Expand Down Expand Up @@ -130,15 +131,17 @@ class Family : public Collectable {
const std::string name_;
const std::string help_;
const std::map<std::string, std::string> constant_labels_;
double seconds_;
std::mutex mutex_;

ClientMetric CollectMetric(std::size_t hash, T* metric);
};

template <typename T>
Family<T>::Family(const std::string& name, const std::string& help,
const std::map<std::string, std::string>& constant_labels)
: name_(name), help_(help), constant_labels_(constant_labels) {
const std::map<std::string, std::string>& constant_labels,
double seconds)
: name_(name), help_(help), constant_labels_(constant_labels), seconds_(seconds) {
assert(CheckMetricName(name_));
}

Expand Down Expand Up @@ -196,7 +199,9 @@ std::vector<MetricFamily> Family<T>::Collect() {
family.help = help_;
family.type = T::metric_type;
for (const auto& m : metrics_) {
family.metric.push_back(std::move(CollectMetric(m.first, m.second.get())));
if (!m.second.get()->Expired(seconds_)) {
family.metric.push_back(std::move(CollectMetric(m.first, m.second.get())));
}
}
return {family};
}
Expand Down
3 changes: 3 additions & 0 deletions core/include/prometheus/gauge.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <atomic>
#include <ctime>

#include "prometheus/client_metric.h"
#include "prometheus/detail/gauge_builder.h"
Expand Down Expand Up @@ -55,10 +56,12 @@ class Gauge {
///
/// Collect is called by the Registry when collecting metrics.
ClientMetric Collect() const;
bool Expired(double) const;

private:
void Change(double);
std::atomic<double> value_{0.0};
std::atomic<std::time_t> time_{0};
};

/// \brief Return a builder to configure and register a Gauge metric.
Expand Down
2 changes: 2 additions & 0 deletions core/include/prometheus/histogram.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <vector>
#include <ctime>

#include "prometheus/client_metric.h"
#include "prometheus/counter.h"
Expand Down Expand Up @@ -55,6 +56,7 @@ class Histogram {
///
/// Collect is called by the Registry when collecting metrics.
ClientMetric Collect() const;
bool Expired(double) const;

private:
const BucketBoundaries bucket_boundaries_;
Expand Down
8 changes: 5 additions & 3 deletions core/include/prometheus/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,19 @@ class Registry : public Collectable {

template <typename T>
Family<T>& Add(const std::string& name, const std::string& help,
const std::map<std::string, std::string>& labels);
const std::map<std::string, std::string>& labels,
double seconds);

std::vector<std::unique_ptr<Collectable>> collectables_;
std::mutex mutex_;
};

template <typename T>
Family<T>& Registry::Add(const std::string& name, const std::string& help,
const std::map<std::string, std::string>& labels) {
const std::map<std::string, std::string>& labels,
double seconds) {
std::lock_guard<std::mutex> lock{mutex_};
auto family = detail::make_unique<Family<T>>(name, help, labels);
auto family = detail::make_unique<Family<T>>(name, help, labels, seconds);
auto& ref = *family;
collectables_.push_back(std::move(family));
return ref;
Expand Down
1 change: 1 addition & 0 deletions core/include/prometheus/summary.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class Summary {
///
/// Collect is called by the Registry when collecting metrics.
ClientMetric Collect();
bool Expired(double) const;

private:
const Quantiles quantiles_;
Expand Down
4 changes: 4 additions & 0 deletions core/src/counter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ ClientMetric Counter::Collect() const {
return metric;
}

bool Counter::Expired(double seconds) const {
return false;
}

detail::CounterBuilder BuildCounter() { return {}; }

} // namespace prometheus
7 changes: 6 additions & 1 deletion core/src/detail/counter_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ CounterBuilder& CounterBuilder::Help(const std::string& help) {
return *this;
}

CounterBuilder& CounterBuilder::Seconds(double seconds) {
seconds_ = seconds;
return *this;
}

Family<Counter>& CounterBuilder::Register(Registry& registry) {
return registry.Add<Counter>(name_, help_, labels_);
return registry.Add<Counter>(name_, help_, labels_, seconds_);
}

} // namespace detail
Expand Down
7 changes: 6 additions & 1 deletion core/src/detail/gauge_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ GaugeBuilder& GaugeBuilder::Help(const std::string& help) {
return *this;
}

GaugeBuilder& GaugeBuilder::Seconds(double seconds) {
seconds_ = seconds;
return *this;
}

Family<Gauge>& GaugeBuilder::Register(Registry& registry) {
return registry.Add<Gauge>(name_, help_, labels_);
return registry.Add<Gauge>(name_, help_, labels_, seconds_);
}

} // namespace detail
Expand Down
7 changes: 6 additions & 1 deletion core/src/detail/histogram_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ HistogramBuilder& HistogramBuilder::Help(const std::string& help) {
return *this;
}

HistogramBuilder& HistogramBuilder::Seconds(double seconds) {
seconds_ = seconds;
return *this;
}

Family<Histogram>& HistogramBuilder::Register(Registry& registry) {
return registry.Add<Histogram>(name_, help_, labels_);
return registry.Add<Histogram>(name_, help_, labels_, seconds_);
}

} // namespace detail
Expand Down
7 changes: 6 additions & 1 deletion core/src/detail/summary_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ SummaryBuilder& SummaryBuilder::Help(const std::string& help) {
return *this;
}

SummaryBuilder& SummaryBuilder::Seconds(double seconds) {
seconds_ = seconds;
return *this;
}

Family<Summary>& SummaryBuilder::Register(Registry& registry) {
return registry.Add<Summary>(name_, help_, labels_);
return registry.Add<Summary>(name_, help_, labels_, seconds_);
}

} // namespace detail
Expand Down
13 changes: 11 additions & 2 deletions core/src/gauge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace prometheus {

Gauge::Gauge(const double value) : value_{value} {}
Gauge::Gauge(const double value) : value_{value}, time_{std::time(nullptr)} {}

void Gauge::Increment() { Increment(1.0); }

Expand All @@ -24,12 +24,16 @@ void Gauge::Decrement(const double value) {
Change(-1.0 * value);
}

void Gauge::Set(const double value) { value_.store(value); }
void Gauge::Set(const double value) {
value_.store(value);
time_.store(std::time(nullptr));
}

void Gauge::Change(const double value) {
auto current = value_.load();
while (!value_.compare_exchange_weak(current, current + value))
;
time_.store(std::time(nullptr));
}

void Gauge::SetToCurrentTime() {
Expand All @@ -45,6 +49,11 @@ ClientMetric Gauge::Collect() const {
return metric;
}

bool Gauge::Expired(double seconds) const {
const auto time = std::time(nullptr);
return std::difftime(time, time_) > seconds;
}

detail::GaugeBuilder BuildGauge() { return {}; }

} // namespace prometheus
4 changes: 4 additions & 0 deletions core/src/histogram.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ ClientMetric Histogram::Collect() const {
return metric;
}

bool Histogram::Expired(double seconds) const {
return false;
}

detail::HistogramBuilder BuildHistogram() { return {}; }

} // namespace prometheus
4 changes: 4 additions & 0 deletions core/src/summary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ ClientMetric Summary::Collect() {
return metric;
}

bool Summary::Expired(double seconds) const {
return false;
}

detail::SummaryBuilder BuildSummary() { return {}; }

} // namespace prometheus

0 comments on commit 57f6036

Please sign in to comment.