-
Notifications
You must be signed in to change notification settings - Fork 873
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add spring starter r2dbc support (#11221)
Co-authored-by: Jean Bisutti <[email protected]> Co-authored-by: Lauri Tulmin <[email protected]>
- Loading branch information
1 parent
68ce6b5
commit ebc38b4
Showing
18 changed files
with
228 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 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
37 changes: 37 additions & 0 deletions
37
...ry/instrumentation/spring/autoconfigure/instrumentation/r2dbc/R2dbcAutoConfiguration.java
This file contains 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,37 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.r2dbc; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.SdkEnabled; | ||
import io.r2dbc.spi.ConnectionFactory; | ||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Conditional; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@ConditionalOnBean(OpenTelemetry.class) | ||
@ConditionalOnClass(ConnectionFactory.class) | ||
@ConditionalOnProperty(name = "otel.instrumentation.r2dbc.enabled", matchIfMissing = true) | ||
@Conditional(SdkEnabled.class) | ||
@Configuration(proxyBeanMethods = false) | ||
public class R2dbcAutoConfiguration { | ||
|
||
public R2dbcAutoConfiguration() {} | ||
|
||
@Bean | ||
// static to avoid "is not eligible for getting processed by all BeanPostProcessors" warning | ||
static R2dbcInstrumentingPostProcessor r2dbcInstrumentingPostProcessor( | ||
ObjectProvider<OpenTelemetry> openTelemetryProvider, | ||
@Value("${otel.instrumentation.common.db-statement-sanitizer.enabled:true}") | ||
boolean statementSanitizationEnabled) { | ||
return new R2dbcInstrumentingPostProcessor(openTelemetryProvider, statementSanitizationEnabled); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...mentation/spring/autoconfigure/instrumentation/r2dbc/R2dbcInstrumentingPostProcessor.java
This file contains 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,52 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.r2dbc; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.instrumentation.r2dbc.v1_0.R2dbcTelemetry; | ||
import io.r2dbc.spi.ConnectionFactory; | ||
import io.r2dbc.spi.ConnectionFactoryOptions; | ||
import org.springframework.aop.scope.ScopedProxyUtils; | ||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.beans.factory.config.BeanPostProcessor; | ||
import org.springframework.boot.r2dbc.OptionsCapableConnectionFactory; | ||
|
||
class R2dbcInstrumentingPostProcessor implements BeanPostProcessor { | ||
|
||
private final ObjectProvider<OpenTelemetry> openTelemetryProvider; | ||
private final boolean statementSanitizationEnabled; | ||
|
||
R2dbcInstrumentingPostProcessor( | ||
ObjectProvider<OpenTelemetry> openTelemetryProvider, boolean statementSanitizationEnabled) { | ||
this.openTelemetryProvider = openTelemetryProvider; | ||
this.statementSanitizationEnabled = statementSanitizationEnabled; | ||
} | ||
|
||
@Override | ||
public Object postProcessAfterInitialization(Object bean, String beanName) { | ||
if (bean instanceof ConnectionFactory && !ScopedProxyUtils.isScopedTarget(beanName)) { | ||
ConnectionFactory connectionFactory = (ConnectionFactory) bean; | ||
return R2dbcTelemetry.builder(openTelemetryProvider.getObject()) | ||
.setStatementSanitizationEnabled(statementSanitizationEnabled) | ||
.build() | ||
.wrapConnectionFactory(connectionFactory, getConnectionFactoryOptions(connectionFactory)); | ||
} | ||
return bean; | ||
} | ||
|
||
private static ConnectionFactoryOptions getConnectionFactoryOptions( | ||
ConnectionFactory connectionFactory) { | ||
OptionsCapableConnectionFactory optionsCapableConnectionFactory = | ||
OptionsCapableConnectionFactory.unwrapFrom(connectionFactory); | ||
if (optionsCapableConnectionFactory != null) { | ||
return optionsCapableConnectionFactory.getOptions(); | ||
} else { | ||
// in practice should never happen | ||
// fall back to empty options; or reconstruct them from the R2dbcProperties | ||
return ConnectionFactoryOptions.builder().build(); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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 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
34 changes: 34 additions & 0 deletions
34
...r/spring-boot-reactive-common/src/main/java/io/opentelemetry/spring/smoketest/Player.java
This file contains 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,34 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.spring.smoketest; | ||
|
||
import org.springframework.data.annotation.Id; | ||
|
||
public class Player { | ||
@Id Integer id; | ||
String name; | ||
Integer age; | ||
|
||
public Player() {} | ||
|
||
public Player(Integer id, String name, Integer age) { | ||
this.id = id; | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Integer getAge() { | ||
return age; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...oot-reactive-common/src/main/java/io/opentelemetry/spring/smoketest/PlayerRepository.java
This file contains 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,10 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.spring.smoketest; | ||
|
||
import org.springframework.data.repository.reactive.ReactiveCrudRepository; | ||
|
||
public interface PlayerRepository extends ReactiveCrudRepository<Player, Integer> {} |
6 changes: 6 additions & 0 deletions
6
smoke-tests-otel-starter/spring-boot-reactive-common/src/main/resources/application.yaml
This file contains 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,6 @@ | ||
spring: | ||
r2dbc: | ||
url: r2dbc:h2:mem:///testdb | ||
jpa: | ||
hibernate: | ||
ddl-auto: create |
1 change: 1 addition & 0 deletions
1
smoke-tests-otel-starter/spring-boot-reactive-common/src/main/resources/schema.sql
This file contains 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 @@ | ||
CREATE TABLE IF NOT EXISTS player(id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255), age INT, PRIMARY KEY (id)); |
This file contains 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