FANNG1 commented on code in PR #9010:
URL: https://github.com/apache/gravitino/pull/9010#discussion_r2517343306


##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/metrics/IcebergMetricsManager.java:
##########
@@ -196,11 +206,35 @@ private void logMetrics(String message, MetricsReport 
metricsReport) {
     LOG.info("{} {}.", message, 
icebergMetricsFormatter.toPrintableString(metricsReport));
   }
 
-  private void doRecordMetric(MetricsReport metricsReport) {
+  private void doRecordMetric(String catalog, Namespace namespace, 
MetricsReport metricsReport) {
     try {
-      icebergMetricsStore.recordMetric(metricsReport);
+      icebergMetricsStore.recordMetric(catalog, namespace, metricsReport);
     } catch (Exception e) {
       LOG.warn("Write Iceberg metrics failed.", e);
     }
   }
+
+  private static class MetricsReportWrapper {
+    private final String catalog;
+    private final Namespace namespace;
+    private final MetricsReport metricsReport;
+
+    public MetricsReportWrapper(String catalog, Namespace namespace, 
MetricsReport metricsReport) {
+      this.catalog = catalog;
+      this.namespace = namespace;
+      this.metricsReport = metricsReport;
+    }
+
+    public Namespace getNamespace() {

Review Comment:
   How about removing `get` from the method name?



##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/metrics/JDBCMetricsStore.java:
##########
@@ -0,0 +1,237 @@
+/*
+ * 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.gravitino.iceberg.service.metrics;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import java.io.IOException;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.time.Instant;
+import java.util.Map;
+import java.util.function.Consumer;
+import org.apache.gravitino.catalog.lakehouse.iceberg.IcebergPropertiesUtils;
+import org.apache.gravitino.json.JsonUtils;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.jdbc.JdbcClientPool;
+import org.apache.iceberg.jdbc.UncheckedInterruptedException;
+import org.apache.iceberg.jdbc.UncheckedSQLException;
+import org.apache.iceberg.metrics.CommitReport;
+import org.apache.iceberg.metrics.CounterResult;
+import org.apache.iceberg.metrics.MetricsReport;
+import org.apache.iceberg.metrics.ScanReport;
+import org.apache.iceberg.metrics.TimerResult;
+
+public class JDBCMetricsStore implements IcebergMetricsStore {
+  public static final String ICEBERG_METRICS_STORE_JDBC_NAME = "jdbc";
+  private static final String URI = "uri";
+  private static final String INSERT_COMMIT_REPORT_METRICS_SQL =
+      "INSERT INTO commit_metrics_report ("
+          + "timestamp, namespace, table_name, snapshot_id, sequence_number, 
operation,"
+          + "added_data_files, removed_data_files, total_data_files,"
+          + "added_delete_files, added_equality_delete_files,  
added_positional_delete_files, "
+          + "removed_delete_files, removed_equality_delete_files, 
removed_positional_delete_files, total_delete_files,"
+          + "added_records, removed_records, total_records,"
+          + "added_file_size_bytes, removed_file_size_bytes, 
total_file_size_bytes,"
+          + "added_positional_deletes, removed_positional_deletes, 
total_positional_deletes,"
+          + "added_equality_deletes, removed_equality_deletes, 
total_equality_deletes,"
+          + "manifests_created, manifests_replaced, manifests_kept, 
manifest_entries_processed,"
+          + "added_dvs, removed_dvs,"
+          + "total_duration_ms, attempts, metadata"
+          + ") VALUES "
+          + "(?, ?, ?, ?, ?, ?,"
+          + " ?, ?, ?,"
+          + " ?, ?, ?,"
+          + " ?, ?, ?, ?,"
+          + " ?, ?, ?,"
+          + " ?, ?, ?,"
+          + " ?, ?, ?,"
+          + " ?, ?, ?,"
+          + " ?, ?, ?, ?,"
+          + " ?, ?,"
+          + " ?, ?, ?);";
+
+  private static final String INSERT_SCAN_REPORT_METRICS_SQL =
+      "INSERT INTO scan_metrics_report ("
+          + "timestamp, namespace, table_name, snapshot_id, schema_id, "
+          + "filter, metadata, projected_field_ids, projected_field_names, "
+          + "equality_delete_files, indexed_delete_files, 
positional_delete_files, "
+          + "result_data_files, result_delete_files, "
+          + "scanned_data_manifests, scanned_delete_manifests, "
+          + "skipped_data_files, skipped_data_manifests, "
+          + "skipped_delete_files, skipped_delete_manifests, "
+          + "total_data_manifests, total_delete_file_size_in_bytes, "
+          + "total_delete_manifests, total_file_size_in_bytes,"
+          + "total_planning_duration_ms) VALUES "
+          + "(?, ?, ?, ?, ?,"
+          + "?, ?, ?, ?,"
+          + "?, ? ,?,"
+          + "?, ?,"
+          + "?, ?,"
+          + "?, ?,"
+          + "?, ?,"
+          + "?, ?,"
+          + "?, ?,"
+          + "?);";
+
+  private static final String DELETE_EXPIRED_SCAN_METRICS_SQL =
+      "DELETE FROM scan_metrics_report WHERE timestamp < ?; ";
+
+  private static final String DELETE_EXPIRED_COMMIT_METRICS_SQL =
+      "DELETE FROM commit_metrics_report WHERE timestamp < ?;";
+
+  @VisibleForTesting JdbcClientPool connections;
+
+  @Override
+  public void init(Map<String, String> properties) throws IOException {

Review Comment:
   Could you check the existing of the metrics table? if the table not exists, 
we could tell the user how to create table in the error message



##########
docs/iceberg-rest-service.md:
##########
@@ -425,6 +425,15 @@ Gravitino provides a pluggable metrics store interface to 
store and delete Icebe
 | `gravitino.iceberg-rest.metricsStoreRetainDays` | The days to retain Iceberg 
metrics in store, the value not greater than 0 means retain forever.            
                         | -1            | No       | 0.4.0         |
 | `gravitino.iceberg-rest.metricsQueueCapacity`   | The size of queue to store 
metrics temporally before storing to the persistent storage. Metrics will be 
dropped when queue is full. | 1000          | No       | 0.4.0         |
 
+If you want to use jdbc as metrics store, you can set the 
`gravitino.iceberg-rest.metricsStore` to `jdbc`, and set the following 
configurations to connect to the database. You should initialize the database 
using the sql scripts in the directory `scripts`.

Review Comment:
   Could you add more context about how to use JDBC metrics store, like adding 
jdbc drivers, how to  init metrics table.



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

Reply via email to