acelyc111 commented on a change in pull request #5824:
URL: https://github.com/apache/incubator-doris/pull/5824#discussion_r633060653
##########
File path: be/src/util/metrics.cpp
##########
@@ -422,4 +422,62 @@ std::string MetricRegistry::to_core_string() const {
return ss.str();
}
+std::string MetricRegistry::aggregate_metric(const std::string entity_type,
+ const std::string metric_name,
+ const std::string&
aggregate_label) const {
+ rj::Document doc{rj::kArrayType};
+ rj::Document::AllocatorType &allocator = doc.GetAllocator();
+ std::lock_guard<SpinLock> l(_lock);
+ std::map<std::string, int64_t> label_map;
+ MetricEntityType metric_entity_type = MetricEntityType::kTablet;
+ if (entity_type == "tablet") {
+ metric_entity_type = MetricEntityType::kTablet;
+ } else {
+ metric_entity_type = MetricEntityType::kServer;
+ }
+ if (metric_name.empty() || aggregate_label.empty()) {
+ rj::StringBuffer strBuf;
+ rj::Writer<rj::StringBuffer> writer(strBuf);
+ doc.Accept(writer);
+ return strBuf.GetString();
+ }
+ for (const auto &entity : _entities) {
+ if (entity.first->_type != metric_entity_type) {
+ continue;
+ }
+ std::lock_guard<SpinLock> l(entity.first->_lock);
+ entity.first->trigger_hook_unlocked(false);
+ for (const auto &metric : entity.first->_metrics) {
+ if (metric.first->simple_name() != metric_name) {
+ continue;
+ }
+ for (auto& label : entity.first->_labels) {
+ if (label.first.c_str() == aggregate_label) {
+ std::string value = metric.second->to_string();
+ int64_t realValue = atoi(value.c_str());
Review comment:
How about if it's a double type?
##########
File path: be/src/util/metrics.h
##########
@@ -410,6 +410,9 @@ class MetricRegistry {
std::string to_prometheus(bool with_tablet_metrics = false) const;
std::string to_json(bool with_tablet_metrics = false) const;
std::string to_core_string() const;
+ std::string aggregate_metric(const std::string entityType,
+ const std::string metric_name,
Review comment:
```suggestion
std::string aggregate_metric(const std::string& entityType,
const std::string& metric_name,
```
##########
File path: be/src/util/metrics.cpp
##########
@@ -422,4 +422,62 @@ std::string MetricRegistry::to_core_string() const {
return ss.str();
}
+std::string MetricRegistry::aggregate_metric(const std::string entity_type,
+ const std::string metric_name,
+ const std::string&
aggregate_label) const {
+ rj::Document doc{rj::kArrayType};
+ rj::Document::AllocatorType &allocator = doc.GetAllocator();
+ std::lock_guard<SpinLock> l(_lock);
+ std::map<std::string, int64_t> label_map;
+ MetricEntityType metric_entity_type = MetricEntityType::kTablet;
+ if (entity_type == "tablet") {
+ metric_entity_type = MetricEntityType::kTablet;
+ } else {
+ metric_entity_type = MetricEntityType::kServer;
+ }
+ if (metric_name.empty() || aggregate_label.empty()) {
+ rj::StringBuffer strBuf;
+ rj::Writer<rj::StringBuffer> writer(strBuf);
+ doc.Accept(writer);
+ return strBuf.GetString();
+ }
+ for (const auto &entity : _entities) {
+ if (entity.first->_type != metric_entity_type) {
+ continue;
+ }
+ std::lock_guard<SpinLock> l(entity.first->_lock);
+ entity.first->trigger_hook_unlocked(false);
+ for (const auto &metric : entity.first->_metrics) {
+ if (metric.first->simple_name() != metric_name) {
+ continue;
+ }
+ for (auto& label : entity.first->_labels) {
+ if (label.first.c_str() == aggregate_label) {
+ std::string value = metric.second->to_string();
+ int64_t realValue = atoi(value.c_str());
+ std::string label_value = label.second.c_str();
+ auto it = label_map.find(label_value);
+ if (it == label_map.end()) {
+ label_map.insert(std::make_pair(label_value,
realValue));
+ } else {
+ label_map.insert(std::make_pair(label_value,
realValue+it->second));
Review comment:
```suggestion
it->second += realValue;
```
##########
File path: be/src/olap/base_tablet.cpp
##########
@@ -44,7 +44,9 @@ BaseTablet::BaseTablet(TabletMetaSharedPtr tablet_meta,
DataDir* data_dir)
_metric_entity =
DorisMetrics::instance()->metric_registry()->register_entity(
strings::Substitute("Tablet.$0", tablet_id()),
- {{"tablet_id", std::to_string(tablet_id())}},
MetricEntityType::kTablet);
+ {{"tablet_id", std::to_string(tablet_id())},
+ {"partition_id", std::to_string(partition_id())},
+ {"table_id",
std::to_string(table_id())}},MetricEntityType::kTablet);
Review comment:
```suggestion
{{"tablet_id", std::to_string(tablet_id())},
{"partition_id", std::to_string(partition_id())},
{"table_id", std::to_string(table_id())}},
MetricEntityType::kTablet);
```
##########
File path: be/src/http/action/metrics_action.cpp
##########
@@ -36,11 +36,16 @@ namespace doris {
void MetricsAction::handle(HttpRequest* req) {
const std::string& type = req->param("type");
const std::string& with_tablet = req->param("with_tablet");
+ const std::string& aggregate_label = req->param("aggregate_label");
+ const std::string entity_type = req->param("entity_type");
+ const std::string metric_name = req->param("metric_name");
std::string str;
if (type == "core") {
str = _metric_registry->to_core_string();
} else if (type == "json") {
str = _metric_registry->to_json(with_tablet == "true");
+ } else if (type == "aggregate") {
+ str = _metric_registry->aggregate_metric(entity_type, metric_name,
aggregate_label);
Review comment:
The output must be in JSON format?
##########
File path: be/src/http/action/metrics_action.cpp
##########
@@ -36,11 +36,16 @@ namespace doris {
void MetricsAction::handle(HttpRequest* req) {
const std::string& type = req->param("type");
const std::string& with_tablet = req->param("with_tablet");
+ const std::string& aggregate_label = req->param("aggregate_label");
+ const std::string entity_type = req->param("entity_type");
+ const std::string metric_name = req->param("metric_name");
Review comment:
```suggestion
const std::string& entity_type = req->param("entity_type");
const std::string& metric_name = req->param("metric_name");
```
##########
File path: be/src/util/metrics.cpp
##########
@@ -422,4 +422,62 @@ std::string MetricRegistry::to_core_string() const {
return ss.str();
}
+std::string MetricRegistry::aggregate_metric(const std::string entity_type,
+ const std::string metric_name,
+ const std::string&
aggregate_label) const {
+ rj::Document doc{rj::kArrayType};
+ rj::Document::AllocatorType &allocator = doc.GetAllocator();
+ std::lock_guard<SpinLock> l(_lock);
+ std::map<std::string, int64_t> label_map;
+ MetricEntityType metric_entity_type = MetricEntityType::kTablet;
+ if (entity_type == "tablet") {
+ metric_entity_type = MetricEntityType::kTablet;
+ } else {
+ metric_entity_type = MetricEntityType::kServer;
+ }
+ if (metric_name.empty() || aggregate_label.empty()) {
+ rj::StringBuffer strBuf;
+ rj::Writer<rj::StringBuffer> writer(strBuf);
+ doc.Accept(writer);
+ return strBuf.GetString();
+ }
+ for (const auto &entity : _entities) {
+ if (entity.first->_type != metric_entity_type) {
+ continue;
+ }
+ std::lock_guard<SpinLock> l(entity.first->_lock);
+ entity.first->trigger_hook_unlocked(false);
+ for (const auto &metric : entity.first->_metrics) {
+ if (metric.first->simple_name() != metric_name) {
+ continue;
+ }
+ for (auto& label : entity.first->_labels) {
Review comment:
`_labels` is type of unordered_map, use `find` instead of traverse all
items.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]