-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * 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(); | ||
| } | ||
|
|
||
| /** | ||
| * Augments the given SQL query with comment containing tracing context. | ||
| * | ||
| * @param connection connection object, e.g. JDBC connection or R2DBC connection, that is used to | ||
| * execute the query | ||
| * @param sql original query | ||
| * @param executed whether the query is immediately executed after being processed, e.g. {@link | ||
| * java.sql.Statement#execute(String)}, or may be executed later, e.g. {@link | ||
| * java.sql.Connection#prepareStatement(String)} | ||
| * @return modified query | ||
| */ | ||
| public String processQuery(Object connection, String sql, boolean executed) { | ||
| if (!enabled) { | ||
| return sql; | ||
| } | ||
|
|
||
| return SqlCommenterUtil.processQuery( | ||
| sql, propagator.apply(connection, executed), prepend.test(connection)); | ||
| } | ||
|
|
||
| public boolean isEnabled() { | ||
| return enabled; | ||
| } | ||
| } | ||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is the second argument called There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
it sounds like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed |
||
| * (e.g. JDBC {@link Statement#execute(String)}) immediately after processing. 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); | ||
| } | ||
| } | ||
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 it should be an enum since a boolean is so hard to get right
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.
perhaps we should leave it for later when it is clear whether having this information is useful at all
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.
your call - I just find the boolean a bit hard to follow