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/gauge_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ TEST(Gauge, Set) {
EXPECT_EQ(publisher.SentMessages(), expected);
}

TEST(Gauge, SetWithTTL) {
TestPublisher publisher;
auto id = std::make_shared<Id>("gauge", Tags{});
auto id2 = std::make_shared<Id>("gauge2", Tags{{"key", "val"}});
Gauge g{id, &publisher, 1};
Gauge g2{id2, &publisher, 2};

g.Set(42);
g2.Set(2);
g.Set(1);
std::vector<std::string> expected = {"g,1:gauge:42", "g,2:gauge2,key=val:2",
"g,1:gauge:1"};
EXPECT_EQ(publisher.SentMessages(), expected);
}


TEST(Gauge, InvalidTags) {
TestPublisher publisher;
// test with a single tag, because tags order is not guaranteed in a flat_hash_map
Expand Down
13 changes: 10 additions & 3 deletions spectator/stateless_meters.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,19 @@ class DistributionSummary : public StatelessMeter<Pub> {
template <typename Pub>
class Gauge : public StatelessMeter<Pub> {
public:
Gauge(IdPtr id, Pub* publisher)
: StatelessMeter<Pub>(std::move(id), publisher) {}
Gauge(IdPtr id, Pub* publisher, unsigned int ttl = 0)
: StatelessMeter<Pub>(std::move(id), publisher) {
if (ttl > 0) {
type_str_ = "g," + std::to_string(ttl);
}
}
void Set(double value) noexcept { this->send(value); }

protected:
std::string_view Type() override { return "g"; }
std::string_view Type() override { return type_str_; }

private:
std::string type_str_{"g"};
};

template <typename Pub>
Expand Down
Loading