Skip to content

feat: respect supress_instrumentation functionality in dbapi instrumentation #3460

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

nao23
Copy link

@nao23 nao23 commented May 3, 2025

Description

Fixes #3459

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

I tested by unit test and manual test.

  • Unit Test
  • Manual Test

Manual test is done by run a below script by uv run reproduce.py

# /// script
# requires-python = ">=3.12"
# dependencies = [
#     "opentelemetry-instrumentation-dbapi @ git+https://[email protected]/nao23/opentelemetry-python-contrib.git@feature/dbapi-instrument-respect-suppress-instrumentation#subdirectory=instrumentation/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()

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

Copy link

linux-foundation-easycla bot commented May 3, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

Copy link
Member

@emdneto emdneto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a changelog entry.

@nao23 nao23 requested a review from a team as a code owner May 5, 2025 16:29
@nao23
Copy link
Author

nao23 commented May 5, 2025

@emdneto Thank you for review! I updated a changelog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Database API instrumentation does not respect suppress_instrumentation functionality
2 participants