-
Notifications
You must be signed in to change notification settings - Fork 1k
Make sqlcommenter more configurable #15169
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
Open
laurit
wants to merge
3
commits into
open-telemetry:main
Choose a base branch
from
laurit:sqlcommenter2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
...java/io/opentelemetry/instrumentation/api/incubator/semconv/db/internal/SqlCommenter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.api.incubator.semconv.db.internal; | ||
|
|
||
| import io.opentelemetry.context.propagation.TextMapPropagator; | ||
| import java.util.function.BiFunction; | ||
| import java.util.function.Predicate; | ||
|
|
||
| /** | ||
| * This class is internal and experimental. Its APIs are unstable and can change at any time. Its | ||
| * APIs (or a version of them) may be promoted to the public stable API in the future, but no | ||
| * guarantees are made. | ||
| */ | ||
| public final class SqlCommenter { | ||
| private final boolean enabled; | ||
| private final BiFunction<Object, Boolean, TextMapPropagator> propagator; | ||
| private final Predicate<Object> prepend; | ||
|
|
||
| SqlCommenter( | ||
| boolean enabled, | ||
| BiFunction<Object, Boolean, TextMapPropagator> propagator, | ||
| Predicate<Object> prepend) { | ||
| this.enabled = enabled; | ||
| this.propagator = propagator; | ||
| this.prepend = prepend; | ||
| } | ||
|
|
||
| public static SqlCommenterBuilder builder() { | ||
| return new SqlCommenterBuilder(); | ||
| } | ||
|
|
||
| public static SqlCommenter noop() { | ||
| return builder().build(); | ||
| } | ||
|
|
||
| public String processQuery(Object connection, String sql, boolean safe) { | ||
| if (!enabled) { | ||
| return sql; | ||
| } | ||
|
|
||
| return SqlCommenterUtil.processQuery( | ||
| sql, propagator.apply(connection, safe), prepend.test(connection)); | ||
| } | ||
|
|
||
| public boolean isEnabled() { | ||
| return enabled; | ||
| } | ||
| } |
89 changes: 89 additions & 0 deletions
89
.../opentelemetry/instrumentation/api/incubator/semconv/db/internal/SqlCommenterBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.api.incubator.semconv.db.internal; | ||
|
|
||
| import com.google.errorprone.annotations.CanIgnoreReturnValue; | ||
| import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; | ||
| import io.opentelemetry.context.propagation.TextMapPropagator; | ||
| import java.sql.Connection; | ||
| import java.sql.Statement; | ||
| import java.util.function.BiFunction; | ||
| import java.util.function.Predicate; | ||
|
|
||
| /** | ||
| * This class is internal and experimental. Its APIs are unstable and can change at any time. Its | ||
| * APIs (or a version of them) may be promoted to the public stable API in the future, but no | ||
| * guarantees are made. | ||
| */ | ||
| public final class SqlCommenterBuilder { | ||
| private boolean enabled; | ||
| private BiFunction<Object, Boolean, TextMapPropagator> propagator = | ||
| (unused1, unused2) -> W3CTraceContextPropagator.getInstance(); | ||
| private Predicate<Object> prepend = unused -> false; | ||
|
|
||
| SqlCommenterBuilder() {} | ||
|
|
||
| /** Enable adding sqlcommenter comments to sql queries. Default is disabled. */ | ||
| @CanIgnoreReturnValue | ||
| public SqlCommenterBuilder setEnabled(boolean enabled) { | ||
| this.enabled = enabled; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Prepend the sqlcommenter comment to the query instead of appending it. Default is to append. | ||
| */ | ||
| @CanIgnoreReturnValue | ||
| public SqlCommenterBuilder setPrepend(boolean prepend) { | ||
| this.prepend = unused -> prepend; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Prepend the sqlcommenter comment to the query instead of appending it. Default is to append. | ||
| * | ||
| * @param prepend a predicate that receives the database connection. Connection may be a jdbc | ||
| * Connection, R2DBC Connection, or any other connection type used by the data access | ||
| * framework performing the operation. | ||
| */ | ||
| @CanIgnoreReturnValue | ||
| public SqlCommenterBuilder setPrepend(Predicate<Object> prepend) { | ||
| this.prepend = prepend; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the propagator used to inject tracing context into sql comments. Default is W3C Trace | ||
| * Context propagator. | ||
| */ | ||
| @CanIgnoreReturnValue | ||
| public SqlCommenterBuilder setPropagator(TextMapPropagator propagator) { | ||
| this.propagator = (unused1, unused2) -> propagator; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the propagator used to inject tracing context into sql comments. Default is W3C Trace | ||
| * Context propagator. | ||
| * | ||
| * @param propagator a function that receives the database connection and whether the query is | ||
| * executed only once or could be reused. Connection may be a jdbc Connection, R2DBC | ||
| * Connection, or any other connection type used by the data access framework performing the | ||
| * operation. If the second argument to the function is true, the query is executed only once | ||
| * (e.g. JDBC {@link Statement#execute(String)}). If false, the query could be reused (e.g. | ||
| * JDBC {@link Connection#prepareStatement(String)}). | ||
| */ | ||
| @CanIgnoreReturnValue | ||
| public SqlCommenterBuilder setPropagator( | ||
| BiFunction<Object, Boolean, TextMapPropagator> propagator) { | ||
| this.propagator = propagator; | ||
| return this; | ||
| } | ||
|
|
||
| public SqlCommenter build() { | ||
| return new SqlCommenter(enabled, propagator, prepend); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 the second argument called
safeabove? If so, it sounds like it should be calledonceto match this descriptionThere 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.
yes it is. Open to suggestions for a better name. What I wanted to express is that when you use
Connection.prepareStatementthen adding dynamic components to the query text isn't always safe as it wouldn't work correctly when the prepared statement is reused (tracing context points to where the query is prepared not where it is executed). In contrast when you useStatement.executethen you won't have that issue. Based on that boolean instrumentations could choose to use different strategies.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.
safeis a good name - but I wasn't sure if it aligns with the javadocit sounds like
safewould allow you to do things more than onceThere 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.
Renamed
safetoexecuted. I had the value flipped for most of the callers, should be corrected now.