Skip to content

Database API instrumentation does not respect suppress_instrumentation functionality #3459

@nao23

Description

@nao23

Describe your environment

OS: macOS
Python version: 3.12
opentelemetry-api: 1.32.1
opentelemetry-sdk: 1.32.1
opentelemetry-instrumentation-dbapi: 0.53b.1

What happened?

Database API instrumentation does not respect opentelemetry.instrumentations.util.supress_instrumentation functionality.
Span is always produced even if we use supress_instrumentation.

Steps to Reproduce

You can reproduce by run a below script by uv run reproduce.py

# /// script
# requires-python = ">=3.12"
# dependencies = [
#     "opentelemetry-api",
#     "opentelemetry-sdk",
#     "opentelemetry-instrumentation-dbapi",
#     "mysql-connector-python",
#     "testcontainers[mysql]"
# ]
# ///

import mysql.connector
from opentelemetry import trace
from opentelemetry.instrumentation.dbapi import instrument_connection
from opentelemetry.instrumentation.utils import suppress_instrumentation
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
    ConsoleSpanExporter,
    SimpleSpanProcessor,
)
from testcontainers.mysql import MySqlContainer


tracer_provider = TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(tracer_provider)

tracer = trace.get_tracer(__name__)


with MySqlContainer("mysql:8.0") as mysql_container:
    conn_attributes = {
        "host": mysql_container.get_container_host_ip(),
        "port": mysql_container.get_exposed_port(3306),
        "user": mysql_container.username,
        "password": mysql_container.password,
        "database": mysql_container.dbname
    }
    conn = instrument_connection(
        "reproduce.py",
        mysql.connector.connect(**conn_attributes),
        "mysql",
        conn_attributes,
        connect_module=mysql.connector
    )

    with suppress_instrumentation():
        cursor = conn.cursor()
        cursor.execute("SELECT 1;")
        cursor.fetchone()
        cursor.close()
        conn.close()

Expected Result

No span is produced.

Actual Result

Span is produced.

Additional context

No response

Would you like to implement a fix?

Yes

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