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 2 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 metrics in a serialized string format.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/// Return the aggregated metrics in a serialized string format.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if this API should be const. Depending on implementations, clean-like operations can happen inside per each fetch interval.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to remove the const. I address the comments. Thanks!

virtual std::string fetchMetrics() const = 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() const 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() const 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