-
Notifications
You must be signed in to change notification settings - Fork 46
Add context propagation for postgres #2540
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 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
There are no files selected for viewing
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
74 changes: 74 additions & 0 deletions
74
...bc/src/main/java/com/splunk/opentelemetry/instrumentation/jdbc/PropagatorInitializer.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,74 @@ | ||
| /* | ||
| * Copyright Splunk Inc. | ||
| * | ||
| * 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 | ||
| * | ||
| * http://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 com.splunk.opentelemetry.instrumentation.jdbc; | ||
|
|
||
| import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getResource; | ||
| import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_NAME; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.propagation.TextMapGetter; | ||
| import io.opentelemetry.context.propagation.TextMapPropagator; | ||
| import io.opentelemetry.context.propagation.TextMapSetter; | ||
| import io.opentelemetry.javaagent.extension.AgentListener; | ||
| import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; | ||
| import io.opentelemetry.sdk.resources.Resource; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
|
|
||
| @AutoService(AgentListener.class) | ||
| public class PropagatorInitializer implements AgentListener { | ||
| // propagates service.name attribute | ||
| static TextMapPropagator defaultPropagator = TextMapPropagator.noop(); | ||
| // propagates service.name attribute and traceparent | ||
| static TextMapPropagator traceContextPropagator = W3CTraceContextPropagator.getInstance(); | ||
|
|
||
| @Override | ||
| public void afterAgent(AutoConfiguredOpenTelemetrySdk sdk) { | ||
| Resource resource = getResource(sdk); | ||
| String serviceName = resource.getAttribute(SERVICE_NAME); | ||
| if (!"unknown_service:java".equals(serviceName)) { | ||
| defaultPropagator = new ServiceAttributePropagator(serviceName); | ||
| traceContextPropagator = | ||
| TextMapPropagator.composite(defaultPropagator, traceContextPropagator); | ||
| } | ||
| } | ||
|
|
||
| private static class ServiceAttributePropagator implements TextMapPropagator { | ||
|
laurit marked this conversation as resolved.
Outdated
|
||
| private final String serviceName; | ||
|
|
||
| ServiceAttributePropagator(String serviceName) { | ||
| this.serviceName = serviceName; | ||
| } | ||
|
|
||
| @Override | ||
| public <C> void inject(Context context, C carrier, TextMapSetter<C> setter) { | ||
| setter.set(carrier, SERVICE_NAME.getKey(), serviceName); | ||
| } | ||
|
|
||
| @Override | ||
| public <C> Context extract(Context context, C carrier, TextMapGetter<C> getter) { | ||
| return context; | ||
| } | ||
|
|
||
| @Override | ||
| public Collection<String> fields() { | ||
| return Collections.emptyList(); | ||
| } | ||
| } | ||
| } | ||
45 changes: 45 additions & 0 deletions
45
.../src/main/java/com/splunk/opentelemetry/instrumentation/jdbc/SqlCommenterInitializer.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,45 @@ | ||
| /* | ||
| * Copyright Splunk Inc. | ||
| * | ||
| * 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 | ||
| * | ||
| * http://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 com.splunk.opentelemetry.instrumentation.jdbc; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import io.opentelemetry.instrumentation.api.incubator.semconv.db.internal.SqlCommenterBuilder; | ||
| import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig; | ||
| import io.opentelemetry.javaagent.bootstrap.internal.sqlcommenter.SqlCommenterCustomizer; | ||
|
|
||
| @AutoService(SqlCommenterCustomizer.class) | ||
| public class SqlCommenterInitializer implements SqlCommenterCustomizer { | ||
|
|
||
| @Override | ||
| public void customize(SqlCommenterBuilder sqlCommenterBuilder) { | ||
| sqlCommenterBuilder.setEnabled( | ||
| AgentInstrumentationConfig.get() | ||
| .getBoolean("otel.instrumentation.splunk-jdbc.enabled", false)); | ||
| sqlCommenterBuilder.setPropagator( | ||
| (connection, executed) -> { | ||
| // for postgres we add traceparent to comments, oracle and mssql use other means to | ||
| // propagate context and other databases are currently unsupported | ||
| if (connection.getClass().getName().startsWith("org.postgresql.jdbc")) { | ||
| return PropagatorInitializer.traceContextPropagator; | ||
| } | ||
|
|
||
| // note that besides jdbc this applies to r2dbc and other data access apis that upstream | ||
| // has sqlcommenter support for | ||
| return PropagatorInitializer.defaultPropagator; | ||
| }); | ||
| } | ||
| } |
94 changes: 94 additions & 0 deletions
94
...k/opentelemetry/instrumentation/jdbc/AbstractConnectionUsingDbContextPropagationTest.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,94 @@ | ||
| /* | ||
| * Copyright Splunk Inc. | ||
| * | ||
| * 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 | ||
| * | ||
| * http://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 com.splunk.opentelemetry.instrumentation.jdbc; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import io.opentelemetry.api.trace.Span; | ||
| import io.opentelemetry.api.trace.SpanContext; | ||
| import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.context.propagation.TextMapGetter; | ||
| import io.opentelemetry.javaagent.bootstrap.CallDepth; | ||
| import java.sql.Connection; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
| import java.util.Collections; | ||
|
|
||
| public abstract class AbstractConnectionUsingDbContextPropagationTest | ||
| extends AbstractDbContextPropagationTest { | ||
|
|
||
| @Override | ||
| protected void assertBeforeQuery(Connection connection) throws Exception { | ||
| assertNoContext(connection); | ||
| } | ||
|
|
||
| @Override | ||
| protected void assertAfterQuery(Connection connection, SpanContext parent, SpanContext jdbc) | ||
| throws Exception { | ||
| assertSameSpan(jdbc, getContext(connection)); | ||
| assertNoContext(connection); | ||
| } | ||
|
|
||
| private static void assertSameSpan(SpanContext expected, Context context) { | ||
| SpanContext actual = Span.fromContext(context).getSpanContext(); | ||
| assertThat(expected.getTraceId()).isEqualTo(actual.getTraceId()); | ||
| assertThat(expected.getSpanId()).isEqualTo(actual.getSpanId()); | ||
| } | ||
|
|
||
| private static Context toContext(String traceparent) { | ||
| if (traceparent == null) { | ||
| return Context.root(); | ||
| } | ||
|
|
||
| return W3CTraceContextPropagator.getInstance() | ||
| .extract( | ||
| Context.root(), | ||
| traceparent, | ||
| new TextMapGetter<>() { | ||
| public String get(String carrier, String key) { | ||
| if ("traceparent".equals(key)) { | ||
| return carrier; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Iterable<String> keys(String carrier) { | ||
| return Collections.singleton("traceparent"); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| protected abstract String getTraceparent(Connection connection) throws SQLException; | ||
|
|
||
| private Context getContext(Connection connection) throws SQLException { | ||
| return toContext(getTraceparent(connection)); | ||
| } | ||
|
|
||
| private void assertNoContext(Connection connection) throws SQLException { | ||
| CallDepth callDepthJdbc = CallDepth.forClass(Statement.class); | ||
| // disable jdbc instrumentation, so it wouldn't create a span for the statement execution | ||
| callDepthJdbc.getAndIncrement(); | ||
| try (Statement statement = connection.createStatement()) { | ||
| statement.execute("SELECT 1"); | ||
| assertSameSpan(SpanContext.getInvalid(), getContext(connection)); | ||
| } finally { | ||
| callDepthJdbc.decrementAndGet(); | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.