obelix74 commented on code in PR #3385:
URL: https://github.com/apache/polaris/pull/3385#discussion_r2713741797


##########
runtime/service/src/main/java/org/apache/polaris/service/reporting/PersistingMetricsReporter.java:
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.polaris.service.reporting;
+
+import io.opentelemetry.api.trace.Span;
+import io.opentelemetry.api.trace.SpanContext;
+import io.quarkus.security.identity.SecurityIdentity;
+import io.smallrye.common.annotation.Identifier;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.enterprise.inject.Instance;
+import jakarta.inject.Inject;
+import java.security.Principal;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.metrics.CommitReport;
+import org.apache.iceberg.metrics.MetricsReport;
+import org.apache.iceberg.metrics.ScanReport;
+import org.apache.polaris.core.context.RealmContext;
+import org.apache.polaris.core.persistence.BasePersistence;
+import org.apache.polaris.core.persistence.MetaStoreManagerFactory;
+import org.apache.polaris.persistence.relational.jdbc.JdbcBasePersistenceImpl;
+import 
org.apache.polaris.persistence.relational.jdbc.models.MetricsReportConverter;
+import 
org.apache.polaris.persistence.relational.jdbc.models.ModelCommitMetricsReport;
+import 
org.apache.polaris.persistence.relational.jdbc.models.ModelScanMetricsReport;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A metrics reporter that persists scan and commit reports as first-class 
entities in the database.
+ * This provides better queryability and analytics capabilities compared to 
storing metrics as
+ * generic events.
+ *
+ * <p>To enable this reporter, set the configuration:
+ *
+ * <pre>
+ * polaris:
+ *   iceberg-metrics:
+ *     reporting:
+ *       type: persistence
+ * </pre>
+ *
+ * <p>Note: This reporter requires the relational-jdbc persistence backend. If 
a different
+ * persistence backend is configured, metrics will be logged but not persisted.
+ */
+@ApplicationScoped
+@Identifier("persistence")
+public class PersistingMetricsReporter implements PolarisMetricsReporter {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(PersistingMetricsReporter.class);
+
+  private final MetaStoreManagerFactory metaStoreManagerFactory;
+  private final RealmContext realmContext;
+  private final Instance<SecurityIdentity> securityIdentityInstance;
+
+  @Inject
+  public PersistingMetricsReporter(
+      MetaStoreManagerFactory metaStoreManagerFactory,
+      RealmContext realmContext,
+      Instance<SecurityIdentity> securityIdentityInstance) {
+    this.metaStoreManagerFactory = metaStoreManagerFactory;
+    this.realmContext = realmContext;
+    this.securityIdentityInstance = securityIdentityInstance;
+  }
+
+  @Override
+  public void reportMetric(String catalogName, TableIdentifier table, 
MetricsReport metricsReport) {
+    try {
+      String realmId = realmContext.getRealmIdentifier();
+      String catalogId = catalogName; // Using catalog name as ID for now
+      String namespace = table.namespace().toString();
+
+      // Extract principal name from security context
+      String principalName = extractPrincipalName();
+      String requestId = null;
+
+      // Extract OpenTelemetry trace context from the current span
+      String otelTraceId = null;
+      String otelSpanId = null;
+      Span currentSpan = Span.current();
+      if (currentSpan != null) {
+        SpanContext spanContext = currentSpan.getSpanContext();
+        if (spanContext != null && spanContext.isValid()) {
+          otelTraceId = spanContext.getTraceId();
+          otelSpanId = spanContext.getSpanId();
+          LOGGER.trace(
+              "Captured OpenTelemetry context: traceId={}, spanId={}", 
otelTraceId, otelSpanId);
+        }
+      }
+
+      // Get the persistence session for the current realm
+      BasePersistence session = 
metaStoreManagerFactory.getOrCreateSession(realmContext);
+
+      // Check if the session is a JdbcBasePersistenceImpl (supports metrics 
persistence)
+      if (!(session instanceof JdbcBasePersistenceImpl jdbcPersistence)) {

Review Comment:
   Thank you for the feedback, @dimas-b
   
   Note on PR Split: This PR has been split into two parts for better 
reviewability:
      - PR1 (#3385): Database schema and core persistence layer only
      - PR2 (upcoming): Metrics processing framework including 
PersistenceMetricsProcessor
   
   The PersistenceMetricsProcessor class is no longer part of this PR - it has 
been moved to PR2 (feat-3337-rest-catalog-metrics-reports branch).
   
   Regarding your suggestion: I agree that instanceof checks can become a 
maintenance burden, especially with NoSQL persistence (#3396) coming. Your 
suggestion to introduce a dedicated MetricsPersistence interface with CDI beans 
is a cleaner approach.
   
   For PR2, I'll consider:
   
   1. Creating a MetricsPersistence interface with the metrics write/query 
methods
   2. Having JDBC persistence implement it directly
   3. Providing no-op implementations for backends that don't support metrics 
persistence
   4. Using CDI injection to select the appropriate implementation
   
   This way, PersistenceMetricsProcessor can simply inject MetricsPersistence 
and call methods without worrying about backend compatibility.
   
   I'll address this in PR2. Thank you for the architectural guidance!



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