morningman commented on a change in pull request #3635:
URL: https://github.com/apache/incubator-doris/pull/3635#discussion_r427293584



##########
File path: be/src/http/action/metrics_action.cpp
##########
@@ -143,15 +143,74 @@ void SimpleCoreMetricsVisitor::visit(const std::string& 
prefix,
     }
 }
 
+class JsonMetricsVisitor : public MetricsVisitor {
+public:
+    JsonMetricsVisitor() {
+        _ss << "[\n";
+    }
+    virtual ~JsonMetricsVisitor() {}
+    void visit(const std::string& prefix, const std::string& name,
+               MetricCollector* collector) override;
+    std::string to_string() { 
+        std::string json = _ss.str();
+        json = json.substr(0, json.length() - 2);
+        json += "\n]\n";
+        return json;
+    }
+
+private:
+    std::stringstream _ss;
+};
+
+void JsonMetricsVisitor::visit(const std::string& prefix,
+                               const std::string& name,
+                               MetricCollector* collector) {
+    if (collector->empty() || name.empty()) {
+        return;
+    }
+    switch (collector->type()) {
+    case MetricType::COUNTER:
+    case MetricType::GAUGE:
+        for (auto& it : collector->metrics()) {
+            const MetricLabels& labels = it.first;
+            SimpleMetric* metric = reinterpret_cast<SimpleMetric*>(it.second);
+            _ss << "{\n\t\"tags\":\n\t{\n";
+            _ss << "\t\t\"metric\":\"" << name << "\"";
+            // labels
+            if (!labels.empty()) {
+                _ss << ",\n";
+                int i = 0;
+                for (auto& label : labels.labels) {
+                    if (i++ > 0) {
+                        _ss << ",\n";
+                    }
+                    _ss << "\t\t\"" << label.name << "\":\"" << label.value << 
"\"";
+                }
+            }
+            _ss << "\n\t},\n";
+            _ss << "\t\"unit\":\"" << metric->unit() << "\",\n";
+            _ss << "\t\"value\":" << metric->to_string() << "\n";
+            _ss << "},\n";
+        }
+        break;
+    default:
+        break;
+    }
+}
+
 void MetricsAction::handle(HttpRequest* req) {
     const std::string& type = req->param("type");
     std::string str;
-    if (type != "core") {
-        PrometheusMetricsVisitor visitor;
+    if (type == "core") {
+        SimpleCoreMetricsVisitor visitor;
+        _metrics->collect(&visitor);
+        str.assign(visitor.to_string());
+    } else if (type == "agent") {

Review comment:
       what is `agent` mean?

##########
File path: fe/src/main/java/org/apache/doris/metric/JsonMetricVisitor.java
##########
@@ -0,0 +1,104 @@
+// 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.
+
+package org.apache.doris.metric;
+
+import org.apache.doris.monitor.jvm.JvmStats;
+import com.codahale.metrics.Histogram;
+import java.util.List;
+
+/*
+ * Like this:
+ * # HELP doris_fe_job_load_broker_cost_ms doris_fe_job_load_broker_cost_ms 
+ * # TYPE doris_fe_job_load_broker_cost_ms gauge 
+ * doris_fe_job{job="load", type="mini", state="pending"} 0
+ */
+public class JsonMetricVisitor extends MetricVisitor {
+    // jvm
+    private static final String JVM_HEAP_SIZE_BYTES = "jvm_heap_size_bytes";
+    private static final String JVM_NON_HEAP_SIZE_BYTES = 
"jvm_non_heap_size_bytes";
+    private static final String JVM_YOUNG_SIZE_BYTES = "jvm_young_size_bytes";
+    private static final String JVM_OLD_SIZE_BYTES = "jvm_old_size_bytes";
+    private static final String JVM_DIRECT_BUFFER_POOL_SIZE_BYTES = 
"jvm_direct_buffer_pool_size_bytes";
+    private static final String JVM_YOUNG_GC = "jvm_young_gc";
+    private static final String JVM_OLD_GC = "jvm_old_gc";
+    private static final String JVM_THREAD = "jvm_thread";
+
+    private static final String HELP = "# HELP ";
+    private static final String TYPE = "# TYPE ";
+    private int ordinal = 0;
+    private int metric_number = 0;
+
+    public JsonMetricVisitor(String prefix) {
+        super(prefix);
+    }
+
+    @Override
+    public void setMetricNumber(int metric_number) {
+        this.metric_number = metric_number;
+    }
+
+    @Override
+    public void visitJvm(StringBuilder sb, JvmStats jvmStats) {
+        return;
+    }
+
+    @Override
+    public void visit(StringBuilder sb, @SuppressWarnings("rawtypes") Metric 
metric) {
+        if (ordinal++ == 0) {
+            sb.append("[\n");
+        }
+        sb.append("{\n\t\"tags\":\n\t{\n");
+        sb.append("\t\t\"metric\":\"").append(metric.getName()).append("\"");
+
+        // name
+        @SuppressWarnings("unchecked")
+        List<MetricLabel> labels = metric.getLabels();
+        if (!labels.isEmpty()) {
+            sb.append(",\n");
+            int i = 0;
+            for (MetricLabel label : labels) {
+                if (i++ > 0) {
+                    sb.append(",\n");
+                }
+                
sb.append("\t\t\"").append(label.getKey()).append("\":\"").append(label.getValue()).append("\"");
+            }
+        }
+        sb.append("\n\t},\n");
+        
sb.append("\t\"unit\":\"").append(metric.getUnit().name().toLowerCase()).append(
 "\",\n");
+
+        // value
+        
sb.append("\t\"value\":").append(metric.getValue().toString()).append("\n}");

Review comment:
       You can use Gson lib to generate json object

##########
File path: be/src/http/action/metrics_action.cpp
##########
@@ -143,15 +143,74 @@ void SimpleCoreMetricsVisitor::visit(const std::string& 
prefix,
     }
 }
 
+class JsonMetricsVisitor : public MetricsVisitor {
+public:
+    JsonMetricsVisitor() {
+        _ss << "[\n";
+    }
+    virtual ~JsonMetricsVisitor() {}
+    void visit(const std::string& prefix, const std::string& name,
+               MetricCollector* collector) override;
+    std::string to_string() { 
+        std::string json = _ss.str();
+        json = json.substr(0, json.length() - 2);
+        json += "\n]\n";
+        return json;
+    }
+
+private:
+    std::stringstream _ss;
+};
+
+void JsonMetricsVisitor::visit(const std::string& prefix,
+                               const std::string& name,
+                               MetricCollector* collector) {
+    if (collector->empty() || name.empty()) {
+        return;
+    }
+    switch (collector->type()) {
+    case MetricType::COUNTER:
+    case MetricType::GAUGE:
+        for (auto& it : collector->metrics()) {
+            const MetricLabels& labels = it.first;
+            SimpleMetric* metric = reinterpret_cast<SimpleMetric*>(it.second);
+            _ss << "{\n\t\"tags\":\n\t{\n";

Review comment:
       Why not using repidjson lib to generate the json object?

##########
File path: fe/src/main/java/org/apache/doris/metric/JsonMetricVisitor.java
##########
@@ -0,0 +1,104 @@
+// 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.
+
+package org.apache.doris.metric;
+
+import org.apache.doris.monitor.jvm.JvmStats;
+import com.codahale.metrics.Histogram;
+import java.util.List;
+
+/*
+ * Like this:
+ * # HELP doris_fe_job_load_broker_cost_ms doris_fe_job_load_broker_cost_ms 
+ * # TYPE doris_fe_job_load_broker_cost_ms gauge 
+ * doris_fe_job{job="load", type="mini", state="pending"} 0
+ */

Review comment:
       Modify the comment

##########
File path: fe/src/main/java/org/apache/doris/metric/JsonMetricVisitor.java
##########
@@ -0,0 +1,104 @@
+// 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.
+
+package org.apache.doris.metric;
+
+import org.apache.doris.monitor.jvm.JvmStats;
+import com.codahale.metrics.Histogram;
+import java.util.List;
+
+/*
+ * Like this:
+ * # HELP doris_fe_job_load_broker_cost_ms doris_fe_job_load_broker_cost_ms 
+ * # TYPE doris_fe_job_load_broker_cost_ms gauge 
+ * doris_fe_job{job="load", type="mini", state="pending"} 0
+ */
+public class JsonMetricVisitor extends MetricVisitor {
+    // jvm
+    private static final String JVM_HEAP_SIZE_BYTES = "jvm_heap_size_bytes";
+    private static final String JVM_NON_HEAP_SIZE_BYTES = 
"jvm_non_heap_size_bytes";
+    private static final String JVM_YOUNG_SIZE_BYTES = "jvm_young_size_bytes";
+    private static final String JVM_OLD_SIZE_BYTES = "jvm_old_size_bytes";
+    private static final String JVM_DIRECT_BUFFER_POOL_SIZE_BYTES = 
"jvm_direct_buffer_pool_size_bytes";
+    private static final String JVM_YOUNG_GC = "jvm_young_gc";
+    private static final String JVM_OLD_GC = "jvm_old_gc";
+    private static final String JVM_THREAD = "jvm_thread";
+
+    private static final String HELP = "# HELP ";

Review comment:
       Useless

##########
File path: fe/src/main/java/org/apache/doris/metric/SimpleCoreMetricVisitor.java
##########
@@ -56,6 +56,9 @@
 
     public static final String MAX_TABLET_COMPACTION_SCORE = 
"max_tablet_compaction_score";
 
+    private int ordinal = 0;
+    private int metric_number = 0;

Review comment:
       ```suggestion
       private int metricNumber = 0;
   ```

##########
File path: fe/src/main/java/org/apache/doris/metric/SimpleCoreMetricVisitor.java
##########
@@ -71,6 +74,11 @@ public SimpleCoreMetricVisitor(String prefix) {
         super(prefix);
     }
 
+    @Override
+    public void setMetricNumber(int metric_number) {

Review comment:
       ```suggestion
       public void setMetricNumber(int metricNumber) {
   ```

##########
File path: fe/src/main/java/org/apache/doris/metric/PrometheusMetricVisitor.java
##########
@@ -52,10 +52,18 @@
     private static final String HELP = "# HELP ";
     private static final String TYPE = "# TYPE ";
 
+    private int ordinal = 0;
+    private int metric_number = 0;
+
     public PrometheusMetricVisitor(String prefix) {
         super(prefix);
     }
 
+    @Override
+    public void setMetricNumber(int metric_number) {

Review comment:
       ```suggestion
       public void setMetricNumber(int metricNumber) {
   ```

##########
File path: fe/src/main/java/org/apache/doris/metric/PrometheusMetricVisitor.java
##########
@@ -52,10 +52,18 @@
     private static final String HELP = "# HELP ";
     private static final String TYPE = "# TYPE ";
 
+    private int ordinal = 0;
+    private int metric_number = 0;

Review comment:
       ```suggestion
       private int metricNumber = 0;
   ```

##########
File path: fe/src/main/java/org/apache/doris/service/FrontendServiceImpl.java
##########
@@ -757,6 +759,7 @@ private boolean loadTxnCommitImpl(TLoadTxnCommitRequest 
request) throws UserExce
             throw new UserException("unknown database, database=" + dbName);
         }
 
+        MetricRepo.COUNTER_LOAD_FINISHED.increase(1L);

Review comment:
       after `commitAndPublishTransaction()`?

##########
File path: fe/src/main/java/org/apache/doris/qe/StmtExecutor.java
##########
@@ -683,6 +684,7 @@ private void handleInsertStmt() throws Exception {
                 return;
             }
 
+            MetricRepo.COUNTER_LOAD_FINISHED.increase(1L);

Review comment:
       What is the `COUNTER_LOAD_FINISHED` means? If it means that load is done 
successfully, 
   this counter should be added after `commitAndPublishTransaction()`




----------------------------------------------------------------
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:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to