Skip to content
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

Add fetchMetrics API to BaseStatsReporter #10178

Closed
Closed
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
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
Loading