From 71cd2973ee809e5b1e60e67c339b8ad7b02a5bec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Serralheiro?= Date: Mon, 13 May 2024 14:31:36 +0100 Subject: [PATCH] Externalizing the QUERY_LOG so the same mechanism can be used for adding the same information to Spans (tracing) --- .../sql/AbstractSqlRepositoryOperations.java | 17 ++++---- .../sql/LoggingSqlExecutionObserver.java | 41 +++++++++++++++++++ .../internal/sql/SqlExecutionObserver.java | 25 +++++++++++ 3 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 data-runtime/src/main/java/io/micronaut/data/runtime/operations/internal/sql/LoggingSqlExecutionObserver.java create mode 100644 data-runtime/src/main/java/io/micronaut/data/runtime/operations/internal/sql/SqlExecutionObserver.java diff --git a/data-runtime/src/main/java/io/micronaut/data/runtime/operations/internal/sql/AbstractSqlRepositoryOperations.java b/data-runtime/src/main/java/io/micronaut/data/runtime/operations/internal/sql/AbstractSqlRepositoryOperations.java index 12dfa7f0291..3754f1949b9 100644 --- a/data-runtime/src/main/java/io/micronaut/data/runtime/operations/internal/sql/AbstractSqlRepositoryOperations.java +++ b/data-runtime/src/main/java/io/micronaut/data/runtime/operations/internal/sql/AbstractSqlRepositoryOperations.java @@ -52,7 +52,6 @@ import io.micronaut.data.model.runtime.RuntimePersistentProperty; import io.micronaut.data.model.runtime.StoredQuery; import io.micronaut.data.operations.HintsCapableRepository; -import io.micronaut.data.runtime.config.DataSettings; import io.micronaut.data.runtime.convert.DataConversionService; import io.micronaut.data.runtime.date.DateTimeProvider; import io.micronaut.data.runtime.mapper.QueryStatement; @@ -70,7 +69,6 @@ import io.micronaut.inject.annotation.AnnotationMetadataHierarchy; import io.micronaut.inject.qualifiers.Qualifiers; import io.micronaut.json.JsonMapper; -import org.slf4j.Logger; import java.io.IOException; import java.util.AbstractMap; @@ -104,8 +102,6 @@ public abstract class AbstractSqlRepositoryOperations columnNameResultSetReader; @@ -120,6 +116,7 @@ public abstract class AbstractSqlRepositoryOperations entityInserts = new ConcurrentHashMap<>(10); private final Map entityUpdates = new ConcurrentHashMap<>(10); private final Map associationInserts = new ConcurrentHashMap<>(10); + private final List listeners; /** * Default constructor. @@ -147,7 +144,8 @@ protected AbstractSqlRepositoryOperations( DataConversionService conversionService, AttributeConverterRegistry attributeConverterRegistry, JsonMapper jsonMapper, - SqlJsonColumnMapperProvider sqlJsonColumnMapperProvider) { + SqlJsonColumnMapperProvider sqlJsonColumnMapperProvider, + List listeners) { super(dateTimeProvider, runtimeEntityRegistry, conversionService, attributeConverterRegistry); this.dataSourceName = dataSourceName; this.columnNameResultSetReader = columnNameResultSetReader; @@ -155,6 +153,7 @@ protected AbstractSqlRepositoryOperations( this.preparedStatementWriter = preparedStatementWriter; this.jsonMapper = jsonMapper; this.sqlJsonColumnMapperProvider = sqlJsonColumnMapperProvider; + this.listeners = listeners; Collection> beanDefinitions = beanContext .getBeanDefinitions(Object.class, Qualifiers.byStereotype(Repository.class)); for (BeanDefinition beanDefinition : beanDefinitions) { @@ -204,9 +203,7 @@ protected PS prepareStatement(StatementSupplier statementFunction, } String query = sqlPreparedQuery.getQuery(); - if (QUERY_LOG.isDebugEnabled()) { - QUERY_LOG.debug("Executing Query: {}", query); - } + listeners.forEach(listener -> listener.query(query)); final PS ps; try { ps = statementFunction.create(query); @@ -254,8 +251,8 @@ protected void setStatementParameter(PS preparedStatement, int index, DataType d dataType = dialect.getDataType(dataType); - if (QUERY_LOG.isTraceEnabled()) { - QUERY_LOG.trace("Binding parameter at position {} to value {} with data type: {}", index, value, dataType); + for (SqlExecutionObserver listener : listeners) { + listener.parameter(index, value, dataType); } // We want to avoid potential conversion for JSON because mapper already returned value ready to be set as statement parameter diff --git a/data-runtime/src/main/java/io/micronaut/data/runtime/operations/internal/sql/LoggingSqlExecutionObserver.java b/data-runtime/src/main/java/io/micronaut/data/runtime/operations/internal/sql/LoggingSqlExecutionObserver.java new file mode 100644 index 00000000000..ce32c37658b --- /dev/null +++ b/data-runtime/src/main/java/io/micronaut/data/runtime/operations/internal/sql/LoggingSqlExecutionObserver.java @@ -0,0 +1,41 @@ +/* + * Copyright 2017-2020 original authors + * + * Licensed 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 + * + * https://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 io.micronaut.data.runtime.operations.internal.sql; + +import io.micronaut.data.model.DataType; +import io.micronaut.data.runtime.config.DataSettings; +import jakarta.inject.Singleton; +import org.slf4j.Logger; + +@Singleton +public class LoggingSqlExecutionObserver implements SqlExecutionObserver { + + protected static final Logger QUERY_LOG = DataSettings.QUERY_LOG; + + @Override + public void query(String query) { + if (QUERY_LOG.isDebugEnabled()) { + QUERY_LOG.debug("Executing Query: {}", query); + } + } + + @Override + public void parameter(int index, Object value, DataType dataType) { + if (QUERY_LOG.isTraceEnabled()) { + QUERY_LOG.trace("Binding parameter at position {} to value {} with data type: {}", index, value, dataType); + } + } +} diff --git a/data-runtime/src/main/java/io/micronaut/data/runtime/operations/internal/sql/SqlExecutionObserver.java b/data-runtime/src/main/java/io/micronaut/data/runtime/operations/internal/sql/SqlExecutionObserver.java new file mode 100644 index 00000000000..e28ef34314a --- /dev/null +++ b/data-runtime/src/main/java/io/micronaut/data/runtime/operations/internal/sql/SqlExecutionObserver.java @@ -0,0 +1,25 @@ +/* + * Copyright 2017-2020 original authors + * + * Licensed 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 + * + * https://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 io.micronaut.data.runtime.operations.internal.sql; + +import io.micronaut.data.model.DataType; + +public interface SqlExecutionObserver { + + void query(String query); + + void parameter(int index, Object value, DataType datatype); +}