Skip to content

Commit

Permalink
Externalizing the QUERY_LOG so the same mechanism can be used for add…
Browse files Browse the repository at this point in the history
…ing the same information to Spans (tracing)
  • Loading branch information
lcavadas committed May 13, 2024
1 parent 46701b0 commit 71cd297
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -104,8 +102,6 @@ public abstract class AbstractSqlRepositoryOperations<RS, PS, Exc extends Except
MethodContextAwareStoredQueryDecorator,
HintsCapableRepository {

protected static final Logger QUERY_LOG = DataSettings.QUERY_LOG;

protected final String dataSourceName;
@SuppressWarnings("WeakerAccess")
protected final ResultReader<RS, String> columnNameResultSetReader;
Expand All @@ -120,6 +116,7 @@ public abstract class AbstractSqlRepositoryOperations<RS, PS, Exc extends Except
private final Map<QueryKey, SqlStoredQuery> entityInserts = new ConcurrentHashMap<>(10);
private final Map<QueryKey, SqlStoredQuery> entityUpdates = new ConcurrentHashMap<>(10);
private final Map<Association, String> associationInserts = new ConcurrentHashMap<>(10);
private final List<SqlExecutionObserver> listeners;

/**
* Default constructor.
Expand Down Expand Up @@ -147,14 +144,16 @@ protected AbstractSqlRepositoryOperations(
DataConversionService conversionService,
AttributeConverterRegistry attributeConverterRegistry,
JsonMapper jsonMapper,
SqlJsonColumnMapperProvider<RS> sqlJsonColumnMapperProvider) {
SqlJsonColumnMapperProvider<RS> sqlJsonColumnMapperProvider,
List<SqlExecutionObserver> listeners) {
super(dateTimeProvider, runtimeEntityRegistry, conversionService, attributeConverterRegistry);
this.dataSourceName = dataSourceName;
this.columnNameResultSetReader = columnNameResultSetReader;
this.columnIndexResultSetReader = columnIndexResultSetReader;
this.preparedStatementWriter = preparedStatementWriter;
this.jsonMapper = jsonMapper;
this.sqlJsonColumnMapperProvider = sqlJsonColumnMapperProvider;
this.listeners = listeners;
Collection<BeanDefinition<Object>> beanDefinitions = beanContext
.getBeanDefinitions(Object.class, Qualifiers.byStereotype(Repository.class));
for (BeanDefinition<Object> beanDefinition : beanDefinitions) {
Expand Down Expand Up @@ -204,9 +203,7 @@ protected <T, R> PS prepareStatement(StatementSupplier<PS> 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);
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 71cd297

Please sign in to comment.