Skip to content

Conversation

@rite7sh
Copy link
Contributor

@rite7sh rite7sh commented Jan 21, 2026

Description

This PR updates the MySQL instrumentation integration tests to provide concrete DBAPI connection attributes (host, port, database) in mocked connection and cursor objects.
Previously, several tests emitted OpenTelemetry warnings due to MagicMock values being used for attributes such as db.name, net.peer.name, and net.peer.port. These warnings were noisy but did not cause test failures.
By setting realistic primitive values in the mocks, the tests now run without attribute type warnings, improving signal-to-noise ratio while keeping behavior unchanged.

Fixes #4114

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?

Ran MySQL instrumentation integration tests locally: tox -e py310-test-instrumentation-mysql-1

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

@rite7sh
Copy link
Contributor Author

rite7sh commented Jan 21, 2026

This PR only updates tests and does not change runtime behavior or public APIs.
I guess no changelog entry is required. Could a maintainer please add the 'no changelog required' label if you agree?
Thank you :)

Comment on lines 31 to 34
cnx.database = "test"
cnx.host = "localhost"
cnx.port = 3306
# Provide set values to avoid Magicbook attribute warnings
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you sure we need these?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@xrmx yes i could think of them to avoid attribute type warnings in the tests. Without setting concrete primitive values, 'MagicMock' objects get propagated into span attributes like db.name, net.peer.name, and net.peer.port, which triggers noisy warnings.
If you had a different approach in mind , I’m happy to make changes.

Copy link
Contributor

Choose a reason for hiding this comment

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

But cnx is not a mock, it's the real thing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But cnx is not a mock, it's the real thing

Oh my bad i did not take it into account, i will update it asap thanks for review :)



def make_mysql_connection_mock():
cnx = mock.MagicMock()
Copy link
Contributor

Choose a reason for hiding this comment

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

You can pass attributes to your mock invocation, e.g. mock.MagicMock(database="test")

cnx.host = "localhost"
cnx.port = 3306

cursor = mock.MagicMock()
Copy link
Contributor

Choose a reason for hiding this comment

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

Since some of this code is open coded in the tests, maybe create an helper and use it there and in the tests that requires it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@xrmx gotcha I’ve updated the helper to pass attributes directly via MagicMock() and refactored the repeated setup into a small reusable helper to avoid duplication across tests.
Let me know if you'd prefer a different structure or naming here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant to call this helper in the tests where you are adding the very same thing but open coded. If you can't do that then try to split this helper in smaller pieces you can reuse in the tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@xrmx Yep, I went with the helper approach you suggested - refactored the repeated MagicMock setup into a shared make_mysql_commenter_mocks helper and updated all SQL commenter tests to use it.
This cleans up a lot of duplication . Let me know if you'd like anything changed or tweaked

@xrmx xrmx moved this to Reviewed PRs that need fixes in @xrmx's Python PR digest Jan 22, 2026
@rite7sh rite7sh requested a review from a team as a code owner January 25, 2026 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Reviewed PRs that need fixes

Development

Successfully merging this pull request may close these issues.

opentelemetry-instrumentation-mysql: warnings on attributes with wrong type in tests

2 participants