-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add context appender to sql based connectors #14500
Conversation
...rino-postgresql/src/test/java/io/trino/plugin/postgresql/TestQueryContextLoggingEnabled.java
Outdated
Show resolved
Hide resolved
...rino-postgresql/src/test/java/io/trino/plugin/postgresql/TestQueryContextLoggingEnabled.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some comments
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/DefaultQueryBuilder.java
Show resolved
Hide resolved
|
||
import io.trino.spi.connector.ConnectorSession; | ||
|
||
public interface QueryExecutionConnectorSessionContextAppender |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Name is confusing. I don't really understand the "connector" part.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's from ConnectorSession
But maybe it would be better to change it to QueryExecutionContextAppender
...rino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/QueryCommentAsContextAppender.java
Outdated
Show resolved
Hide resolved
...rino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/QueryCommentAsContextAppender.java
Outdated
Show resolved
Hide resolved
...rino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/QueryCommentAsContextAppender.java
Outdated
Show resolved
Hide resolved
...rino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/QueryCommentAsContextAppender.java
Outdated
Show resolved
Hide resolved
...rino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/QueryCommentAsContextAppender.java
Outdated
Show resolved
Hide resolved
...rino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/QueryCommentAsContextAppender.java
Outdated
Show resolved
Hide resolved
Awesome! What does this PR aim to solve? |
if you don't feel like filling this in, just remove it. thank you! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just skimming.
not possible to do better review without knowing what is the purpose of the code
private static final String LOG_CANCELLATION_EVENT = "ERROR: canceling statement due to user request"; | ||
|
||
private static final Pattern SQL_QUERY_FIND_PATTERN = Pattern.compile("^(: |/C_\\d: )(.*)"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be worth adorning this with example log line we're parsing
plugin/trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestingPostgreSqlServer.java
Show resolved
Hide resolved
public class TestQueryContextLoggingEnabled | ||
extends AbstractTestQueryFramework | ||
{ | ||
private final TestingPostgreSqlServer postgreSqlServer = closeAfterClass(new TestingPostgreSqlServer()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never allocate resources or do IO in a test static or instance initializer.
- in case of failure, surefire we'll be glad to misguide you on what happened
- you use precious memory which is needed only for the test's duration
Some context would be useful when reviewing this (to understand expected usage). |
c362b5d
to
d5aa7c1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your responses, please take a look again.
|
||
import io.trino.spi.connector.ConnectorSession; | ||
|
||
public interface QueryExecutionConnectorSessionContextAppender |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's from ConnectorSession
But maybe it would be better to change it to QueryExecutionContextAppender
...rino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/QueryCommentAsContextAppender.java
Outdated
Show resolved
Hide resolved
...rino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/QueryCommentAsContextAppender.java
Outdated
Show resolved
Hide resolved
...rino-postgresql/src/test/java/io/trino/plugin/postgresql/TestQueryContextLoggingEnabled.java
Outdated
Show resolved
Hide resolved
plugin/trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestingPostgreSqlServer.java
Show resolved
Hide resolved
I really hope people won't abuse this to implement passing query hints to Oracle. Will take a look at the PR. |
DistributedQueryRunner distributedQueryRunner = createPostgreSqlQueryRunner( | ||
postgreSqlServer, | ||
Map.of(), | ||
Map.of("comment-logging.format", "query executed by $USER"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add all variables here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be hard to use them, as some of them are generated automatically (query_id) and some of them are not provided (as they would have to come from the client itself, like trace_token and source). It's tested extensively elsewhere.
core/trino-main/src/main/java/io/trino/testing/TestingConnectorSession.java
Show resolved
Hide resolved
QueryExecutionContextAppender NO_CONTEXT_APPENDER = (query, session) -> query; | ||
|
||
String appendContext(String query, ConnectorSession session) | ||
throws IllegalArgumentException; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove throws
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? That's how you notify the clients that this method can actually throws something and it's by design. What's more you are showing to the implementors what kind of exceptions can be thrown, without the need of adding additional handling.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IllegalArgumentException
can be expected from any method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok.
What do you mean by "Ok"? I was already preparing popcorn and awaiting epic philosophical battle. Try be more assertive next time, other reviewers demand entertainment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha. Ha. Ha. ;)
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/QueryExecutionContextAppender.java
Outdated
Show resolved
Hide resolved
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/QueryExecutionContextAppender.java
Outdated
Show resolved
Hide resolved
.../trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/QueryCommentContextAppender.java
Outdated
Show resolved
Hide resolved
.../trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/QueryCommentContextAppender.java
Outdated
Show resolved
Hide resolved
.../trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/QueryCommentContextAppender.java
Outdated
Show resolved
Hide resolved
...-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/QueryCommentContextAppenderConfig.java
Outdated
Show resolved
Hide resolved
...rino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/QueryCommentAsContextAppender.java
Outdated
Show resolved
Hide resolved
core/trino-main/src/main/java/io/trino/testing/TestingConnectorSession.java
Show resolved
Hide resolved
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/DefaultQueryBuilder.java
Outdated
Show resolved
Hide resolved
.../trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/QueryCommentContextAppender.java
Outdated
Show resolved
Hide resolved
.../trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/QueryCommentContextAppender.java
Outdated
Show resolved
Hide resolved
.../trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/QueryCommentContextAppender.java
Outdated
Show resolved
Hide resolved
...-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/QueryCommentContextAppenderModule.java
Outdated
Show resolved
Hide resolved
...no-base-jdbc/src/test/java/io/trino/plugin/jdbc/logging/TestQueryCommentContextAppender.java
Outdated
Show resolved
Hide resolved
What if we implement a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please take a look now. I've made a 3 commits but I will squash them into a single one later on.
QueryExecutionContextAppender NO_CONTEXT_APPENDER = (query, session) -> query; | ||
|
||
String appendContext(String query, ConnectorSession session) | ||
throws IllegalArgumentException; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok.
.../trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/QueryCommentContextAppender.java
Outdated
Show resolved
Hide resolved
DistributedQueryRunner distributedQueryRunner = createPostgreSqlQueryRunner( | ||
postgreSqlServer, | ||
Map.of(), | ||
Map.of("comment-logging.format", "query executed by $USER"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be hard to use them, as some of them are generated automatically (query_id) and some of them are not provided (as they would have to come from the client itself, like trace_token and source). It's tested extensively elsewhere.
plugin/trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestingPostgreSqlServer.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good. I reviewed everything at once (not commit by commit).
@@ -658,7 +665,7 @@ public JdbcOutputTableHandle beginInsertTable(ConnectorSession session, JdbcTabl | |||
} | |||
} | |||
|
|||
protected void copyTableSchema(Connection connection, String catalogName, String schemaName, String tableName, String newTableName, List<String> columnNames) | |||
protected void copyTableSchema(Connection connection, String catalogName, String schemaName, String tableName, String newTableName, List<String> columnNames, ConnectorSession session) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: ConnectorSession
is usually passed as first argument. Please apply that to all places.
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/DefaultQueryBuilder.java
Outdated
Show resolved
Hide resolved
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/BaseJdbcClient.java
Outdated
Show resolved
Hide resolved
log.debug("Execute: %s", appendedQuery); | ||
statement.execute(appendedQuery); | ||
} | ||
catch (IllegalArgumentException e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this catch? Is this an user, admin or developer error? The same comment to other places like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's potential SQLInject attack, that's why I want to handle it explicitly with JDBC_NON_TRANSIENT_ERROR
- meaning that this SQL should not be repeated as the error won't go away afer a while and there is no point of repeating the query. You need to change the client setup to make it disappear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then I suggest that the query modifier should throw proper exception directly or return Optional
should be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So Optional.empty()
would mean that you can execute code without context being logged, even though configuration says that it will be logged. That's a security risk as well. I think that we should be fine throwing this exception directly though.
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/QueryExecutionContextAppender.java
Outdated
Show resolved
Hide resolved
plugin/trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestingPostgreSqlServer.java
Show resolved
Hide resolved
plugin/trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestingPostgreSqlServer.java
Show resolved
Hide resolved
...trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestRemoteQueryCommentLogging.java
Show resolved
Hide resolved
...trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestRemoteQueryCommentLogging.java
Show resolved
Hide resolved
...trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestRemoteQueryCommentLogging.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm % kokosing remarks
QueryExecutionContextAppender NO_CONTEXT_APPENDER = (query, session) -> query; | ||
|
||
String appendContext(String query, ConnectorSession session) | ||
throws IllegalArgumentException; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok.
What do you mean by "Ok"? I was already preparing popcorn and awaiting epic philosophical battle. Try be more assertive next time, other reviewers demand entertainment.
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/BaseJdbcClient.java
Outdated
Show resolved
Hide resolved
plugin/trino-phoenix5/src/main/java/io/trino/plugin/phoenix5/PhoenixClient.java
Outdated
Show resolved
Hide resolved
Change the PR description to cover the current scope |
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/DefaultQueryBuilder.java
Outdated
Show resolved
Hide resolved
...in/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/RemoteQueryModifierConfig.java
Outdated
Show resolved
Hide resolved
...in/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/RemoteQueryModifierModule.java
Outdated
Show resolved
Hide resolved
plugin/trino-druid/src/main/java/io/trino/plugin/druid/DruidJdbcClient.java
Outdated
Show resolved
Hide resolved
plugin/trino-druid/src/main/java/io/trino/plugin/druid/DruidJdbcClient.java
Outdated
Show resolved
Hide resolved
plugin/trino-oracle/src/test/java/io/trino/plugin/oracle/TestOracleClient.java
Outdated
Show resolved
Hide resolved
plugin/trino-postgresql/src/main/java/io/trino/plugin/postgresql/PostgreSqlClient.java
Outdated
Show resolved
Hide resolved
...trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestRemoteQueryCommentLogging.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for review, changes are not yet done, but please take a look at the comments.
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/BaseJdbcClient.java
Outdated
Show resolved
Hide resolved
log.debug("Execute: %s", appendedQuery); | ||
statement.execute(appendedQuery); | ||
} | ||
catch (IllegalArgumentException e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's potential SQLInject attack, that's why I want to handle it explicitly with JDBC_NON_TRANSIENT_ERROR
- meaning that this SQL should not be repeated as the error won't go away afer a while and there is no point of repeating the query. You need to change the client setup to make it disappear.
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/DefaultQueryBuilder.java
Outdated
Show resolved
Hide resolved
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/QueryExecutionContextAppender.java
Outdated
Show resolved
Hide resolved
for (PredefinedValue predefinedValue : PredefinedValue.values()) { | ||
message = message.replaceAll(predefinedValue.getMatchCase(), predefinedValue.value(session)); | ||
} | ||
return query + "/*" + message + "*/"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm ok it will look better in logs and I guess humans will be reading it with the query.
...trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestRemoteQueryCommentLogging.java
Outdated
Show resolved
Hide resolved
@@ -88,6 +93,18 @@ private static void execute(String url, Properties properties, String sql) | |||
} | |||
} | |||
|
|||
DatabaseEvents startCollecting() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
plugin/trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestingPostgreSqlServer.java
Show resolved
Hide resolved
plugin/trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestingPostgreSqlServer.java
Show resolved
Hide resolved
plugin/trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestingPostgreSqlServer.java
Outdated
Show resolved
Hide resolved
I am fine with responses, let me know when you need another round of the review. |
09bfc83
to
5db6244
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All comments addressed. Please check again.
log.debug("Execute: %s", appendedQuery); | ||
statement.execute(appendedQuery); | ||
} | ||
catch (IllegalArgumentException e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So Optional.empty()
would mean that you can execute code without context being logged, even though configuration says that it will be logged. That's a security risk as well. I think that we should be fine throwing this exception directly though.
...trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestRemoteQueryCommentLogging.java
Show resolved
Hide resolved
a824a6b
to
8f90f2e
Compare
It still shows some conflict, can you resolve them. |
7fe0d2b
to
f9b73fb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor comments.
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/BaseJdbcClient.java
Outdated
Show resolved
Hide resolved
statement = connection.prepareStatement(insertSql); | ||
} | ||
catch (TrinoException e) { | ||
throw closeAllSuppress(e, connection); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would throw new TrinoException
with e
as cause. So all you need to do is catch (SQLException | TrinoException e)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that it's correct from the exception handling stnadnpoint. We already have the exception and there is no need to wrap it again without adding any meaningful information to the caller. Plus we are not returning JDBC_ERROR but rather NON_TRANSIENT_JDBC_ERROR wich is the whole point of this exception being thrown.
|
||
import static java.util.stream.Collectors.joining; | ||
|
||
public class RemoteQueryModifierConfig |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe inline this class into QueryConfig
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this idea. It has quite a lot of tests and there is no point in adding these tests to default jdbc config tests as they are very specific
private static final Pattern VALIDATION_PATTERN = Pattern.compile("[\\w ,=]|" + String.join("|", PREDEFINED_MATCHES)); | ||
private String format = ""; | ||
|
||
@Config("remote-query.comment.format") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
query.comment-format
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that remote-query is better, as it tells exactly which query we are talking about in this case. comment-format
might be a bit closer to what we do in other places. so remote-query.comment-format
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then I think we need to change query.reuse-connection
. I am fine with this. However then the scope of this PR gets bigger.
Also I know we are all aware of concept of "remote query". I am not sure if it is well known to our users. There is a risk where "remote" part may confuse them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I will follow your lead here.
|
||
@Config("remote-query.comment.format") | ||
@ConfigDescription("Format in which logs about query execution context should be added as comments sent through jdbc driver." + | ||
" It can consist only of letters, digits, spaces, underscores, equality signs and predefined values: $QUERY_ID, $SOURCE, $USER, $TRACE_TOKEN") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is too long. Let's add details to docs and here let's be short.
Message format which will be added as comment to remote query.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok.
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/RemoteQueryModifier.java
Outdated
Show resolved
Hide resolved
|
||
private static RemoteQueryModifier createRemoteQueryModifier(String commentFormat) | ||
{ | ||
return new RemoteQueryModifier(new RemoteQueryModifierConfig().setFormat(commentFormat)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a constructor that takes only format string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could and I could make it package-private to not be used outside of this package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please take a look at my responses. I will append change shortly.
statement = connection.prepareStatement(insertSql); | ||
} | ||
catch (TrinoException e) { | ||
throw closeAllSuppress(e, connection); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that it's correct from the exception handling stnadnpoint. We already have the exception and there is no need to wrap it again without adding any meaningful information to the caller. Plus we are not returning JDBC_ERROR but rather NON_TRANSIENT_JDBC_ERROR wich is the whole point of this exception being thrown.
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/RemoteQueryModifier.java
Outdated
Show resolved
Hide resolved
private static final Pattern VALIDATION_PATTERN = Pattern.compile("[\\w ,=]|" + String.join("|", PREDEFINED_MATCHES)); | ||
private String format = ""; | ||
|
||
@Config("remote-query.comment.format") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that remote-query is better, as it tells exactly which query we are talking about in this case. comment-format
might be a bit closer to what we do in other places. so remote-query.comment-format
?
|
||
@Config("remote-query.comment.format") | ||
@ConfigDescription("Format in which logs about query execution context should be added as comments sent through jdbc driver." + | ||
" It can consist only of letters, digits, spaces, underscores, equality signs and predefined values: $QUERY_ID, $SOURCE, $USER, $TRACE_TOKEN") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok.
|
||
private static RemoteQueryModifier createRemoteQueryModifier(String commentFormat) | ||
{ | ||
return new RemoteQueryModifier(new RemoteQueryModifierConfig().setFormat(commentFormat)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could and I could make it package-private to not be used outside of this package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice if we could have test for default behaviour. Overall design looks good to me
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/RemoteQueryModifier.java
Outdated
Show resolved
Hide resolved
plugin/trino-base-jdbc/src/main/java/io/trino/plugin/jdbc/logging/RemoteQueryModifier.java
Outdated
Show resolved
Hide resolved
return this; | ||
} | ||
|
||
public String getFormat() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have a NotNull
annotation ?
for (PredefinedValue predefinedValue : PredefinedValue.values()) { | ||
message = message.replaceAll(predefinedValue.getMatchCase(), predefinedValue.value(session)); | ||
} | ||
return query + " /*" + message + "*/"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By default - we shouldn’t add any conext to the query
. Won’t this add a /* NOT USED*/
for all queries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it would not in previous implementation and after the change it won't be an issue as well.
} | ||
|
||
@Test | ||
public void testFormatWithEmptyValues() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have an additional test case - for default behaviour (if format is not specified)
.initialize() | ||
.getInstance(RemoteQueryModifier.class); | ||
|
||
assertThat(remoteQueryModifier) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of asserting remoteQueryModifier
- can we assert its behaviour.
...trino-postgresql/src/test/java/io/trino/plugin/postgresql/TestRemoteQueryCommentLogging.java
Show resolved
Hide resolved
return distributedQueryRunner; | ||
} | ||
|
||
@Test(singleThreaded = true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there is any specific reason on why we didn’t annotate the TestClass with this ?
ef49450
to
a86828e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two minor change. LGTM
} | ||
|
||
@Inject | ||
public DefaultQueryBuilder(RemoteQueryModifier contextAppender) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit : Can rename the variable as queryModifier
and in the rnn message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry missed that last time.
return server.getRemoteDatabaseEvents() | ||
.stream() | ||
.skip(startingFromEventsCount) | ||
.limit(lastEventsCount) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be the difference between the starting and lastEventCount ?
BTW Do we need this bottom limit, since the test are single threaded and we already have a starting point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was a request from @kokosing to make it immutable. Thanks to this it is, as every method will always return same events. We are not using this fact at this moment in our tests, but it's still better to have it as such and don't be forced to think about it in the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, should limit be the difference between the startingEvent and lastEvents or should we re-order the streams ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I believe that this is correct, as skip doesn't truncate the stream, it just skips first few and limit limits all values, but to be honest, I would prefer to have a test for that to be 100% sure, and we don't have that here. Due to that, I've decided to rewrite this part quickly, to not confuse anyone and to have immutability down to the values identity.
a86828e
to
52dc6f9
Compare
I've added also a suite of tests checking for correct default behaviour - meaning that nothing should be logged at all when it's disabled. I've noticed that I have only partially tested that in guice setup tests. |
52dc6f9
to
91dc517
Compare
CI is red. Can you please take a look. |
8303ff2
to
9ed7df5
Compare
This change allows for sending additional logs, that are send in comments to external systems (jdbc only atm). It allows to pass additional security and observability information to these systems, that wouldn't be possible to send otherwie. This implementation covers SELECT, DDL and DML queries, although it does not cover any metadata queries - as these are made by the drivers itself and we can't easily intercept these.
9ed7df5
to
e2771a8
Compare
Description
Add context appender to sql based connectors
This is a first iteration for SELECT Queries of a mechanism
that will allow to send to the actual database a bit more
information, that might be available only in Trino. It's for the
purpose of these other systems to be able to capture this info and
log it for its own purposes. For now we are sending it as a comments
added at the end of a query.
Non-technical explanation
This pr adds an ability to add additional context of query execution and send it to external database,
like pgsql, oracle etc.
Release notes
( ) This is not user-visible or docs only and no release notes are required.
( ) Release notes are required, please propose a release note for me.
(x) Release notes are required, with the following suggested text: