Skip to content

DataSourcePostProcessor is incorrectly wrapping AbstractRoutingDataSource #13051

@rghugikar

Description

@rghugikar

Describe the bug

We have a multi-tenant use-case where multiple data-sources need to be configured and the correct one needs to be selected at runtime based on the tenant-id in Context. We are extending the spring provided AbstractRoutingDataSource for this purpose. However the AbstractRoutingDataSource in-turn implements javax.sql.DataSource interface so our Routing class gets incorrectly wrapped up by the OpenTelemetryDataSource implementation. Below piece of code should probably exclude the AbstractRoutingDataSource from wrapping. since its purpose is different from just being a DataSource even though it implements the same java.sql.DataSource interface.

package io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.jdbc;
....

final class DataSourcePostProcessor implements BeanPostProcessor, Ordered {

......

  @CanIgnoreReturnValue
  @Override
  public Object postProcessAfterInitialization(Object bean, String beanName) {
    // Exclude scoped proxy beans to avoid double wrapping
   // This probably needs the fix? <-----
    if (bean instanceof DataSource && !ScopedProxyUtils.isScopedTarget(beanName)) {
      DataSource dataSource = (DataSource) bean;
      return JdbcTelemetry.builder(openTelemetryProvider.getObject())
          .setStatementSanitizationEnabled(
              InstrumentationConfigUtil.isStatementSanitizationEnabled(
                  configPropertiesProvider.getObject(),
                  "otel.instrumentation.jdbc.statement-sanitizer.enabled"))
          .build()
          .wrap(dataSource);
    }
    return bean;
  }

If there are any workarounds available then please share the same.
Thanks!

Steps to reproduce

Implement AbstractRoutingDataSource and enable opentelemetry springboot starter dependency for metrics.

Expected behavior

The AbstractRoutingDataSource implementations should not be replaced/wrapped by the OpenTelemetry JDBC wrapper classes. Only the actual JDBC data-sources should be wrapped. The AbstractRoutingDataSource is meant to select one of many actual data-sources from the list. So we are wrapping the list of all datasources within AbstractRoutingDataSource along with wrapping up the AbstractRoutingDataSource itself.

Actual behavior

AbstractRoutingDataSource is wrpped up incorrectly by DataSourcePostProcessor resulting in type exceptions in code.

Javaagent or library instrumentation version

opentelemetry-instrumentation-bom: 2.10.0 and opentelemetry-bom: 1.44.1

Environment

JDK: 17
OS: Ubuntu/Windows

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions