Skip to content

Commit 6e7c2fd

Browse files
authored
fix get gauge (#114)
1 parent 234bf0b commit 6e7c2fd

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

spectator/registry.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ struct single_table_state {
6969
return get_or_create<typename types::gauge_t>(std::move(id));
7070
}
7171

72+
auto get_gauge_ttl(IdPtr id, unsigned int ttl_seconds) {
73+
return get_or_create<typename types::gauge_t>(std::move(id), ttl_seconds);
74+
}
75+
7276
auto get_max_gauge(IdPtr id) {
7377
return get_or_create<typename types::max_gauge_t>(std::move(id));
7478
}
@@ -168,6 +172,14 @@ class base_registry {
168172
auto GetGauge(absl::string_view name, Tags tags = {}) {
169173
return GetGauge(Id::of(name, std::move(tags)));
170174
}
175+
176+
auto GetGaugeTTL(const IdPtr& id, unsigned int ttl_seconds) {
177+
return state_.get_gauge_ttl(final_id(id), ttl_seconds);
178+
}
179+
180+
auto GetGaugeTTL(absl::string_view name, unsigned int ttl_seconds, Tags tags = {}) {
181+
return GetGaugeTTL(Id::of(name, std::move(tags)), ttl_seconds);
182+
}
171183

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

299+
auto get_gauge_ttl(IdPtr id, unsigned int ttl_seconds) {
300+
return std::make_shared<typename types::gauge_t>(std::move(id), publisher.get(), ttl_seconds);
301+
}
302+
287303
auto get_max_gauge(IdPtr id) {
288304
return std::make_shared<typename types::max_gauge_t>(std::move(id), publisher.get());
289305
}

spectator/stateless_meters.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ class DistributionSummary : public StatelessMeter<Pub> {
137137
template <typename Pub>
138138
class Gauge : public StatelessMeter<Pub> {
139139
public:
140-
Gauge(IdPtr id, Pub* publisher, unsigned int ttl = 0)
140+
Gauge(IdPtr id, Pub* publisher, unsigned int ttl_seconds = 0)
141141
: StatelessMeter<Pub>(std::move(id), publisher) {
142-
if (ttl > 0) {
143-
type_str_ = "g," + std::to_string(ttl);
142+
if (ttl_seconds > 0) {
143+
type_str_ = "g," + std::to_string(ttl_seconds);
144144
}
145145
}
146146
void Set(double value) noexcept { this->send(value); }

spectator/statelessregistry_test.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,14 @@ TEST(StatelessRegistry, Gauge) {
6161
TestStatelessRegistry r;
6262
auto g = r.GetGauge("foo");
6363
auto g2 = r.GetGauge("bar", {{"id", "2"}});
64+
auto g3 = r.GetGaugeTTL("baz", 1);
65+
auto g4 = r.GetGaugeTTL("quux", 2, {{"id", "2"}});
6466
g->Set(100);
6567
g2->Set(101);
66-
std::vector<std::string> expected = {"g:foo:100", "g:bar,id=2:101"};
68+
g3->Set(102);
69+
g4->Set(103);
70+
std::vector<std::string> expected = {"g:foo:100", "g:bar,id=2:101",
71+
"g,1:baz:102", "g,2:quux,id=2:103"};
6772
EXPECT_EQ(r.SentMessages(), expected);
6873
}
6974

0 commit comments

Comments
 (0)