hubgeter commented on code in PR #41752:
URL: https://github.com/apache/doris/pull/41752#discussion_r1826987403


##########
be/src/util/jni_metrics.cpp:
##########
@@ -0,0 +1,131 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "jni_metrics.h"
+
+#include <memory>
+#include <utility>
+
+#include "util/jni-util.h"
+#include "util/metrics.h"
+
+namespace doris {
+
+DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(jdbc_scan_connection_percent, 
MetricUnit::PERCENT);
+struct JdbcConnectionMetrics {
+    JdbcConnectionMetrics(std::shared_ptr<MetricEntity> entity) : 
entity(entity) {
+        INT_GAUGE_METRIC_REGISTER(entity, jdbc_scan_connection_percent);
+    }
+    void upate(int value) { jdbc_scan_connection_percent->set_value(value); }
+    std::shared_ptr<MetricEntity> entity;
+    IntGauge* jdbc_scan_connection_percent;
+};
+
+const char* JniMetrics::_s_hook_name = "jni_metrics";
+
+JniMetrics::JniMetrics(MetricRegistry* registry) {
+    DCHECK(registry != nullptr);
+    _registry = registry;
+    _server_entity = _registry->register_entity("server");
+    DCHECK(_server_entity != nullptr);
+    _server_entity->register_hook(_s_hook_name,
+                                  
std::bind(&JniMetrics::update_jdbc_connection_metrics, this));
+    _init();
+}
+
+void JniMetrics::_init() {
+    JNIEnv* env = nullptr;
+    Status st = JniUtil::GetJNIEnv(&env);
+    if (!st.ok()) {
+        LOG(WARNING) << "get jni env failed. " << st.to_string();
+        return;
+    }
+    st = JniUtil::get_jni_scanner_class(env, 
"org/apache/doris/jdbc/JdbcDataSource",
+                                        &_jdbc_data_source_clz);
+    if (!st.ok()) {
+        LOG(WARNING) << "class org/apachhe/doris/jdbc/JdbcDataSource not find 
in jvm."
+                     << st.to_string();
+        return;
+    }
+    _get_connection_percent_id = env->GetStaticMethodID(
+            _jdbc_data_source_clz, "getConnectionPercent", 
"()Ljava/util/Map;");
+    st = JniUtil::GetJniExceptionMsg(env);
+    if (!st.ok()) {
+        LOG(WARNING) << "get getConnectionPercent method id failed. " << 
st.to_string();
+        return;
+    }
+    _is_init = true;
+    LOG(INFO) << "jni metrics inited successfully";
+}
+
+void JniMetrics::update_jdbc_connection_metrics() {
+    if (!_is_init) {
+        return;
+    }
+    JNIEnv* env = nullptr;
+    Status st = JniUtil::GetJNIEnv(&env);
+    if (!st.ok()) {
+        LOG(WARNING) << "get jni env failed. " << st.to_string();
+        return;
+    }
+    jobject metrics =
+            env->CallStaticObjectMethod(_jdbc_data_source_clz, 
_get_connection_percent_id);
+    std::map<std::string, std::string> result = 
JniUtil::convert_to_cpp_map(env, metrics);
+    st = JniUtil::GetJniExceptionMsg(env);
+    if (!st.ok()) {
+        LOG(WARNING) << "invoke getConnectionPercent method failed. " << 
st.to_string();
+        return;
+    }
+    for (auto item : result) {
+        std::string catalog_id = item.first;
+        int percent = std::stoi(item.second);
+        auto iter = _jdbc_connection_metrics.find(catalog_id);
+        if (iter == _jdbc_connection_metrics.end()) {
+            auto entity =
+                    _registry->register_entity_unlocked(catalog_id, 
{{"catalog", catalog_id}});

Review Comment:
   I think it is better to use `catalog name` not `catalog id`. This is just my 
personal feeling.



##########
be/src/util/jni_metrics.cpp:
##########
@@ -0,0 +1,131 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "jni_metrics.h"
+
+#include <memory>
+#include <utility>
+
+#include "util/jni-util.h"
+#include "util/metrics.h"
+
+namespace doris {
+
+DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(jdbc_scan_connection_percent, 
MetricUnit::PERCENT);
+struct JdbcConnectionMetrics {
+    JdbcConnectionMetrics(std::shared_ptr<MetricEntity> entity) : 
entity(entity) {
+        INT_GAUGE_METRIC_REGISTER(entity, jdbc_scan_connection_percent);
+    }
+    void upate(int value) { jdbc_scan_connection_percent->set_value(value); }
+    std::shared_ptr<MetricEntity> entity;
+    IntGauge* jdbc_scan_connection_percent;
+};
+
+const char* JniMetrics::_s_hook_name = "jni_metrics";
+
+JniMetrics::JniMetrics(MetricRegistry* registry) {
+    DCHECK(registry != nullptr);
+    _registry = registry;
+    _server_entity = _registry->register_entity("server");
+    DCHECK(_server_entity != nullptr);
+    _server_entity->register_hook(_s_hook_name,
+                                  
std::bind(&JniMetrics::update_jdbc_connection_metrics, this));
+    _init();
+}
+
+void JniMetrics::_init() {
+    JNIEnv* env = nullptr;
+    Status st = JniUtil::GetJNIEnv(&env);
+    if (!st.ok()) {
+        LOG(WARNING) << "get jni env failed. " << st.to_string();
+        return;
+    }
+    st = JniUtil::get_jni_scanner_class(env, 
"org/apache/doris/jdbc/JdbcDataSource",
+                                        &_jdbc_data_source_clz);
+    if (!st.ok()) {
+        LOG(WARNING) << "class org/apachhe/doris/jdbc/JdbcDataSource not find 
in jvm."
+                     << st.to_string();
+        return;
+    }
+    _get_connection_percent_id = env->GetStaticMethodID(
+            _jdbc_data_source_clz, "getConnectionPercent", 
"()Ljava/util/Map;");
+    st = JniUtil::GetJniExceptionMsg(env);
+    if (!st.ok()) {
+        LOG(WARNING) << "get getConnectionPercent method id failed. " << 
st.to_string();
+        return;
+    }
+    _is_init = true;
+    LOG(INFO) << "jni metrics inited successfully";
+}
+
+void JniMetrics::update_jdbc_connection_metrics() {
+    if (!_is_init) {
+        return;
+    }
+    JNIEnv* env = nullptr;
+    Status st = JniUtil::GetJNIEnv(&env);
+    if (!st.ok()) {
+        LOG(WARNING) << "get jni env failed. " << st.to_string();
+        return;
+    }
+    jobject metrics =
+            env->CallStaticObjectMethod(_jdbc_data_source_clz, 
_get_connection_percent_id);

Review Comment:
   It is recommended to do an exception check after `CallStaticObjectMethod`. 
We recently provided some macros to make it easier to do this: 
`JNI_CALL_METHOD_CHECK_EXCEPTION` and 
`JNI_CALL_METHOD_CHECK_EXCEPTION_DELETE_REF`



##########
be/src/util/jni_metrics.cpp:
##########
@@ -0,0 +1,130 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "jni_metrics.h"
+
+#include <memory>
+#include <utility>
+
+#include "util/jni-util.h"
+#include "util/metrics.h"
+
+namespace doris {
+
+DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(jdbc_scan_connection_percent, 
MetricUnit::PERCENT);
+struct JdbcConnectionMetrics {
+    JdbcConnectionMetrics(std::shared_ptr<MetricEntity> entity) : 
entity(entity) {
+        INT_GAUGE_METRIC_REGISTER(entity, jdbc_scan_connection_percent);
+    }
+    void upate(int value) { jdbc_scan_connection_percent->set_value(value); }
+    std::shared_ptr<MetricEntity> entity;
+    IntGauge* jdbc_scan_connection_percent;
+};
+
+const char* JniMetrics::_s_hook_name = "jni_metrics";
+
+JniMetrics::JniMetrics(MetricRegistry* registry) {
+    DCHECK(registry != nullptr);
+    _registry = registry;
+    _server_entity = _registry->register_entity("server");
+    DCHECK(_server_entity != nullptr);
+    _server_entity->register_hook(_s_hook_name,
+                                  
std::bind(&JniMetrics::update_jdbc_connection_metrics, this));
+    _init();

Review Comment:
   ```cpp
   _init();
   if (_is_init) {
        _server_entity->register_hook(xxx)
   }
   ```
   i think if init fail, don`t  need  update metrics.



##########
be/src/util/jni_metrics.cpp:
##########
@@ -0,0 +1,131 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "jni_metrics.h"
+
+#include <memory>
+#include <utility>
+
+#include "util/jni-util.h"
+#include "util/metrics.h"
+
+namespace doris {
+
+DEFINE_GAUGE_METRIC_PROTOTYPE_2ARG(jdbc_scan_connection_percent, 
MetricUnit::PERCENT);
+struct JdbcConnectionMetrics {
+    JdbcConnectionMetrics(std::shared_ptr<MetricEntity> entity) : 
entity(entity) {
+        INT_GAUGE_METRIC_REGISTER(entity, jdbc_scan_connection_percent);
+    }
+    void upate(int value) { jdbc_scan_connection_percent->set_value(value); }
+    std::shared_ptr<MetricEntity> entity;
+    IntGauge* jdbc_scan_connection_percent;
+};
+
+const char* JniMetrics::_s_hook_name = "jni_metrics";
+
+JniMetrics::JniMetrics(MetricRegistry* registry) {
+    DCHECK(registry != nullptr);
+    _registry = registry;
+    _server_entity = _registry->register_entity("server");
+    DCHECK(_server_entity != nullptr);
+    _server_entity->register_hook(_s_hook_name,
+                                  
std::bind(&JniMetrics::update_jdbc_connection_metrics, this));
+    _init();
+}
+
+void JniMetrics::_init() {
+    JNIEnv* env = nullptr;
+    Status st = JniUtil::GetJNIEnv(&env);
+    if (!st.ok()) {
+        LOG(WARNING) << "get jni env failed. " << st.to_string();
+        return;
+    }
+    st = JniUtil::get_jni_scanner_class(env, 
"org/apache/doris/jdbc/JdbcDataSource",
+                                        &_jdbc_data_source_clz);
+    if (!st.ok()) {
+        LOG(WARNING) << "class org/apachhe/doris/jdbc/JdbcDataSource not find 
in jvm."
+                     << st.to_string();
+        return;
+    }
+    _get_connection_percent_id = env->GetStaticMethodID(
+            _jdbc_data_source_clz, "getConnectionPercent", 
"()Ljava/util/Map;");
+    st = JniUtil::GetJniExceptionMsg(env);
+    if (!st.ok()) {
+        LOG(WARNING) << "get getConnectionPercent method id failed. " << 
st.to_string();
+        return;
+    }
+    _is_init = true;
+    LOG(INFO) << "jni metrics inited successfully";
+}
+
+void JniMetrics::update_jdbc_connection_metrics() {
+    if (!_is_init) {
+        return;
+    }
+    JNIEnv* env = nullptr;
+    Status st = JniUtil::GetJNIEnv(&env);
+    if (!st.ok()) {
+        LOG(WARNING) << "get jni env failed. " << st.to_string();
+        return;
+    }
+    jobject metrics =
+            env->CallStaticObjectMethod(_jdbc_data_source_clz, 
_get_connection_percent_id);

Review Comment:
   If it is convenient, change other places to this form,thanks



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to