Skip to content

Support to hide metrics which hasn't update for a long time #520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_library(core
src/detail/time_window_quantiles.cc
src/detail/utils.cc
src/family.cc
src/metric.cc
src/gauge.cc
src/histogram.cc
src/registry.cc
Expand Down
3 changes: 2 additions & 1 deletion core/include/prometheus/counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "prometheus/detail/builder.h" // IWYU pragma: export
#include "prometheus/detail/core_export.h"
#include "prometheus/gauge.h"
#include "prometheus/metric.h"
#include "prometheus/metric_type.h"

namespace prometheus {
Expand All @@ -23,7 +24,7 @@ namespace prometheus {
///
/// The class is thread-safe. No concurrent call to any API of this type causes
/// a data race.
class PROMETHEUS_CPP_CORE_EXPORT Counter {
class PROMETHEUS_CPP_CORE_EXPORT Counter : public Metric {
public:
static const MetricType metric_type{MetricType::Counter};

Expand Down
3 changes: 2 additions & 1 deletion core/include/prometheus/gauge.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "prometheus/client_metric.h"
#include "prometheus/detail/builder.h" // IWYU pragma: export
#include "prometheus/detail/core_export.h"
#include "prometheus/metric.h"
#include "prometheus/metric_type.h"

namespace prometheus {
Expand All @@ -21,7 +22,7 @@ namespace prometheus {
///
/// The class is thread-safe. No concurrent call to any API of this type causes
/// a data race.
class PROMETHEUS_CPP_CORE_EXPORT Gauge {
class PROMETHEUS_CPP_CORE_EXPORT Gauge : public Metric {
public:
static const MetricType metric_type{MetricType::Gauge};

Expand Down
3 changes: 2 additions & 1 deletion core/include/prometheus/histogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "prometheus/detail/builder.h" // IWYU pragma: export
#include "prometheus/detail/core_export.h"
#include "prometheus/gauge.h"
#include "prometheus/metric.h"
#include "prometheus/metric_type.h"

namespace prometheus {
Expand All @@ -27,7 +28,7 @@ namespace prometheus {
///
/// The class is thread-safe. No concurrent call to any API of this type causes
/// a data race.
class PROMETHEUS_CPP_CORE_EXPORT Histogram {
class PROMETHEUS_CPP_CORE_EXPORT Histogram : public Metric {
public:
using BucketBoundaries = std::vector<double>;

Expand Down
28 changes: 28 additions & 0 deletions core/include/prometheus/metric.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <ctime>

#include "prometheus/detail/core_export.h"
#include "prometheus/metric_type.h"

namespace prometheus {

/// \brief The base metric.
///
class PROMETHEUS_CPP_CORE_EXPORT Metric {
public:
static const MetricType metric_type{MetricType::Untyped};

Metric();
virtual ~Metric() = default;

bool Expired(time_t current_ts) const;

protected:
void UpdateTS();

private:
time_t last_update_ts_;
};

} // namespace prometheus
3 changes: 2 additions & 1 deletion core/include/prometheus/summary.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "prometheus/detail/ckms_quantiles.h"
#include "prometheus/detail/core_export.h"
#include "prometheus/detail/time_window_quantiles.h"
#include "prometheus/metric.h"
#include "prometheus/metric_type.h"

namespace prometheus {
Expand Down Expand Up @@ -38,7 +39,7 @@ namespace prometheus {
///
/// The class is thread-safe. No concurrent call to any API of this type causes
/// a data race.
class PROMETHEUS_CPP_CORE_EXPORT Summary {
class PROMETHEUS_CPP_CORE_EXPORT Summary : public Metric {
public:
using Quantiles = std::vector<detail::CKMSQuantiles::Quantile>;

Expand Down
6 changes: 5 additions & 1 deletion core/src/counter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace prometheus {

void Counter::Increment() { gauge_.Increment(); }
void Counter::Increment() {
gauge_.Increment();
UpdateTS();
}

void Counter::Increment(const double val) {
if (val < 0.0) {
return;
}
gauge_.Increment(val);
UpdateTS();
}

double Counter::Value() const { return gauge_.Value(); }
Expand Down
6 changes: 5 additions & 1 deletion core/src/family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const std::map<std::string, std::string> Family<T>::GetConstantLabels() const {

template <typename T>
std::vector<MetricFamily> Family<T>::Collect() const {
auto current_ts = std::time(nullptr);
std::lock_guard<std::mutex> lock{mutex_};

if (metrics_.empty()) {
Expand All @@ -108,7 +109,10 @@ std::vector<MetricFamily> Family<T>::Collect() const {
family.type = T::metric_type;
family.metric.reserve(metrics_.size());
for (const auto& m : metrics_) {
family.metric.push_back(std::move(CollectMetric(m.first, m.second.get())));
if (!m.second->Expired(current_ts)) {
family.metric.push_back(
std::move(CollectMetric(m.first, m.second.get())));
Copy link
Contributor

@sjanel sjanel Nov 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This std::move is useless here. CollectMetric returns a prvalue.

}
}
return {family};
}
Expand Down
6 changes: 5 additions & 1 deletion core/src/gauge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ void Gauge::Decrement() { Decrement(1.0); }

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);
UpdateTS();
}

void Gauge::Change(const double value) {
auto current = value_.load();
while (!value_.compare_exchange_weak(current, current + value))
;
UpdateTS();
}

void Gauge::SetToCurrentTime() {
Expand Down
2 changes: 2 additions & 0 deletions core/src/histogram.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void Histogram::Observe(const double value) {
[value](const double boundary) { return boundary >= value; })));
sum_.Increment(value);
bucket_counts_[bucket_index].Increment();
UpdateTS();
}

void Histogram::ObserveMultiple(const std::vector<double>& bucket_increments,
Expand All @@ -41,6 +42,7 @@ void Histogram::ObserveMultiple(const std::vector<double>& bucket_increments,
for (std::size_t i{0}; i < bucket_counts_.size(); ++i) {
bucket_counts_[i].Increment(bucket_increments[i]);
}
UpdateTS();
}

ClientMetric Histogram::Collect() const {
Expand Down
19 changes: 19 additions & 0 deletions core/src/metric.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "prometheus/metric.h"

#include <ctime>

namespace prometheus {

Metric::Metric() : last_update_ts_(std::time(nullptr)) {
}

bool Metric::Expired(time_t current_ts) const {
static const double retire_time = 600;
return difftime(current_ts, last_update_ts_) > retire_time;
}

void Metric::UpdateTS() {
last_update_ts_ = std::time(nullptr);
}

} // namespace prometheus
1 change: 1 addition & 0 deletions core/src/summary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ void Summary::Observe(const double value) {
count_ += 1;
sum_ += value;
quantile_values_.insert(value);
UpdateTS();
}

ClientMetric Summary::Collect() const {
Expand Down