Skip to content
Merged
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
16 changes: 16 additions & 0 deletions spectator/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ struct single_table_state {
return get_or_create<typename types::gauge_t>(std::move(id));
}

auto get_gauge_ttl(IdPtr id, unsigned int ttl_seconds) {
return get_or_create<typename types::gauge_t>(std::move(id), ttl_seconds);
}

auto get_max_gauge(IdPtr id) {
return get_or_create<typename types::max_gauge_t>(std::move(id));
}
Expand Down Expand Up @@ -168,6 +172,14 @@ class base_registry {
auto GetGauge(absl::string_view name, Tags tags = {}) {
return GetGauge(Id::of(name, std::move(tags)));
}

auto GetGaugeTTL(const IdPtr& id, unsigned int ttl_seconds) {
return state_.get_gauge_ttl(final_id(id), ttl_seconds);
}

auto GetGaugeTTL(absl::string_view name, unsigned int ttl_seconds, Tags tags = {}) {
return GetGaugeTTL(Id::of(name, std::move(tags)), ttl_seconds);
}

auto GetMaxGauge(const IdPtr& id) {
return state_.get_max_gauge(final_id(id));
Expand Down Expand Up @@ -284,6 +296,10 @@ struct stateless {
return std::make_shared<typename types::gauge_t>(std::move(id), publisher.get());
}

auto get_gauge_ttl(IdPtr id, unsigned int ttl_seconds) {
return std::make_shared<typename types::gauge_t>(std::move(id), publisher.get(), ttl_seconds);
}

auto get_max_gauge(IdPtr id) {
return std::make_shared<typename types::max_gauge_t>(std::move(id), publisher.get());
}
Expand Down
6 changes: 3 additions & 3 deletions spectator/stateless_meters.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ class DistributionSummary : public StatelessMeter<Pub> {
template <typename Pub>
class Gauge : public StatelessMeter<Pub> {
public:
Gauge(IdPtr id, Pub* publisher, unsigned int ttl = 0)
Gauge(IdPtr id, Pub* publisher, unsigned int ttl_seconds = 0)
: StatelessMeter<Pub>(std::move(id), publisher) {
if (ttl > 0) {
type_str_ = "g," + std::to_string(ttl);
if (ttl_seconds > 0) {
type_str_ = "g," + std::to_string(ttl_seconds);
}
}
void Set(double value) noexcept { this->send(value); }
Expand Down
7 changes: 6 additions & 1 deletion spectator/statelessregistry_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@ TEST(StatelessRegistry, Gauge) {
TestStatelessRegistry r;
auto g = r.GetGauge("foo");
auto g2 = r.GetGauge("bar", {{"id", "2"}});
auto g3 = r.GetGaugeTTL("baz", 1);
auto g4 = r.GetGaugeTTL("quux", 2, {{"id", "2"}});
g->Set(100);
g2->Set(101);
std::vector<std::string> expected = {"g:foo:100", "g:bar,id=2:101"};
g3->Set(102);
g4->Set(103);
std::vector<std::string> expected = {"g:foo:100", "g:bar,id=2:101",
"g,1:baz:102", "g,2:quux,id=2:103"};
EXPECT_EQ(r.SentMessages(), expected);
}

Expand Down
Loading