obelix74 commented on code in PR #3385: URL: https://github.com/apache/polaris/pull/3385#discussion_r2833770990
########## persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcMetricsPersistence.java: ########## @@ -0,0 +1,481 @@ +/* + * 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.persistence.relational.jdbc; + +import static org.apache.polaris.persistence.relational.jdbc.QueryGenerator.PreparedQuery; + +import jakarta.annotation.Nonnull; +import jakarta.annotation.Nullable; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import org.apache.polaris.core.persistence.metrics.CommitMetricsRecord; +import org.apache.polaris.core.persistence.metrics.MetricsPersistence; +import org.apache.polaris.core.persistence.metrics.MetricsQueryCriteria; +import org.apache.polaris.core.persistence.metrics.ReportIdToken; +import org.apache.polaris.core.persistence.metrics.ScanMetricsRecord; +import org.apache.polaris.core.persistence.pagination.Page; +import org.apache.polaris.core.persistence.pagination.PageToken; +import org.apache.polaris.core.persistence.pagination.Token; +import org.apache.polaris.persistence.relational.jdbc.models.ModelCommitMetricsReport; +import org.apache.polaris.persistence.relational.jdbc.models.ModelCommitMetricsReportConverter; +import org.apache.polaris.persistence.relational.jdbc.models.ModelScanMetricsReport; +import org.apache.polaris.persistence.relational.jdbc.models.ModelScanMetricsReportConverter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * JDBC implementation of {@link MetricsPersistence}. + * + * <p>This class provides direct JDBC persistence for metrics reports, converting between SPI record + * types ({@link ScanMetricsRecord}, {@link CommitMetricsRecord}) and JDBC model types ({@link + * ModelScanMetricsReport}, {@link ModelCommitMetricsReport}). + * + * <p>This implementation assumes that metrics tables exist. The producer ({@link + * JdbcMetricsPersistenceProducer}) checks for metrics table availability at startup and returns + * {@link MetricsPersistence#NOOP} if tables are not present. + */ +public class JdbcMetricsPersistence implements MetricsPersistence { + + private static final Logger LOGGER = LoggerFactory.getLogger(JdbcMetricsPersistence.class); + + private final DatasourceOperations datasourceOperations; + private final String realmId; + + /** + * Creates a new JdbcMetricsPersistence instance. + * + * @param datasourceOperations the datasource operations for JDBC access + * @param realmId the realm ID for multi-tenancy + */ + public JdbcMetricsPersistence(DatasourceOperations datasourceOperations, String realmId) { + this.datasourceOperations = datasourceOperations; + this.realmId = realmId; + } + + @Override + public void writeScanReport(@Nonnull ScanMetricsRecord record) { + ModelScanMetricsReport model = SpiModelConverter.toModelScanReport(record, realmId); Review Comment: I remember working on this before with Otel CDI beans, shouldn't have squashed my commits. Added first-class fields to MetricsRecordIdentity for all five context fields: - principalName: from SecurityContext.getUserPrincipal() - requestId: from RequestIdFilter.REQUEST_ID_KEY request property - otelTraceId / otelSpanId: from Span.current().getSpanContext() - reportTraceId: from Iceberg report's metadata() map using key report-trace-id (client-provided) The values are captured in PersistingMetricsReporter.reportMetric() and passed through the converter to the JDBC model. -- 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]
