Skip to content

Commit

Permalink
Add fetchMetrics API to BaseStatsReporter (#10178)
Browse files Browse the repository at this point in the history
Summary:
Return the aggregated metrics in a serialized string format.

Pull Request resolved: #10178

Reviewed By: amitkdutta

Differential Revision: D58738756

Pulled By: tanjialiang

fbshipit-source-id: b0a9dd6568a83738452b5f64de7c80caf3c93d52
  • Loading branch information
majetideepak authored and facebook-github-bot committed Jun 20, 2024
1 parent a605b90 commit 0fe2e62
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 7 additions & 0 deletions velox/common/base/StatsReporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ class BaseStatsReporter {
virtual void addHistogramMetricValue(folly::StringPiece key, size_t value)
const = 0;

/// Return the aggregated metrics in a serialized string format.
virtual std::string fetchMetrics() = 0;

static bool registered;
};

Expand Down Expand Up @@ -179,6 +182,10 @@ class DummyStatsReporter : public BaseStatsReporter {

void addHistogramMetricValue(folly::StringPiece /* key */, size_t /* value */)
const override {}

std::string fetchMetrics() override {
return "";
}
};

#define DEFINE_METRIC(key, type) \
Expand Down
17 changes: 16 additions & 1 deletion velox/common/base/tests/StatsReporterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace facebook::velox {
class TestReporter : public BaseStatsReporter {
public:
mutable std::mutex m;
mutable std::unordered_map<std::string, size_t> counterMap;
mutable std::map<std::string, size_t> counterMap;
mutable std::unordered_map<std::string, StatType> statTypeMap;
mutable std::unordered_map<std::string, std::vector<int32_t>>
histogramPercentilesMap;
Expand Down Expand Up @@ -106,6 +106,18 @@ class TestReporter : public BaseStatsReporter {
std::lock_guard<std::mutex> l(m);
counterMap[key.str()] = std::max(counterMap[key.str()], value);
}

std::string fetchMetrics() override {
std::stringstream ss;
ss << "[";
auto sep = "";
for (const auto& [key, value] : counterMap) {
ss << sep << key << ":" << value;
sep = ",";
}
ss << "]";
return ss.str();
}
};

class StatsReporterTest : public testing::Test {
Expand Down Expand Up @@ -149,6 +161,9 @@ TEST_F(StatsReporterTest, trivialReporter) {
EXPECT_EQ(2201, reporter_->counterMap["key2"]);
EXPECT_EQ(1101, reporter_->counterMap["key3"]);
EXPECT_EQ(100, reporter_->counterMap["key4"]);

EXPECT_EQ(
"[key1:36,key2:2201,key3:1101,key4:100]", reporter_->fetchMetrics());
};

class PeriodicStatsReporterTest : public StatsReporterTest {};
Expand Down

0 comments on commit 0fe2e62

Please sign in to comment.