-
Notifications
You must be signed in to change notification settings - Fork 38
Add pymssql instrumentation
#893
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
pvital
wants to merge
3
commits into
main
Choose a base branch
from
pymssql_instrumentation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,17 @@ | ||
| # (c) Copyright IBM Corp. 2026 | ||
|
|
||
| from instana.log import logger | ||
| from instana.instrumentation.pep0249 import ConnectionFactory | ||
|
|
||
| try: | ||
| import pymssql | ||
|
|
||
| cf = ConnectionFactory(connect_func=pymssql.connect, module_name="mssql") | ||
|
|
||
| setattr(pymssql, "connect", cf) | ||
| if hasattr(pymssql, "Connect"): | ||
| setattr(pymssql, "Connect", cf) | ||
|
|
||
| logger.debug("Instrumenting pymssql") | ||
| except ImportError: | ||
| pass |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -51,6 +51,7 @@ | |
| "httpx", | ||
| "log", | ||
| "memcache", | ||
| "mssql", | ||
| "mongo", | ||
| "mysql", | ||
| "postgres", | ||
|
|
||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -170,6 +170,8 @@ def record_exception( | |
| self.set_attribute("lambda.error", message) | ||
| elif self.name.startswith("kafka"): | ||
| self.set_attribute("kafka.error", message) | ||
| elif self.name == "mssql": | ||
| self.set_attribute("mssql.error", message[1]) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure that it should be set to |
||
| else: | ||
| _attributes = {"message": message} | ||
| if attributes: | ||
|
|
||
This file contains hidden or 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,242 @@ | ||
| # (c) Copyright IBM Corp. 2026 | ||
|
|
||
| from typing import Generator | ||
|
|
||
| import pytest | ||
|
|
||
| from instana.singletons import agent, get_tracer | ||
| from tests.helpers import testenv | ||
|
|
||
|
|
||
| class TestPyMSSQL: | ||
| @pytest.fixture(autouse=True) | ||
| def _resource(self) -> Generator[None, None, None]: | ||
| import pymssql | ||
|
|
||
| try: | ||
| self.db = pymssql.connect( | ||
| server=testenv["mssql_host"], | ||
| port=testenv["mssql_port"], | ||
| user=testenv["mssql_user"], | ||
| password=testenv["mssql_pw"], | ||
| database=testenv["mssql_db"], | ||
| ) | ||
| except Exception: | ||
| pytest.skip("SQL Server not available") | ||
|
|
||
| setup_cursor = self.db.cursor() | ||
| setup_cursor.execute("IF OBJECT_ID('users', 'U') IS NOT NULL DROP TABLE users") | ||
| setup_cursor.execute( | ||
| "CREATE TABLE users (id INT, name NVARCHAR(50), email NVARCHAR(50))" | ||
| ) | ||
| setup_cursor.execute( | ||
| "INSERT INTO users (id, name, email) VALUES (1, 'kermit', 'kermit@muppets.com')" | ||
| ) | ||
| self.db.commit() | ||
|
|
||
| self.cursor = self.db.cursor() | ||
| self.tracer = get_tracer() | ||
| self.recorder = self.tracer.span_processor | ||
| self.recorder.clear_spans() | ||
| self.tracer.cur_ctx = None | ||
| yield | ||
| try: | ||
| cleanup_cursor = self.db.cursor() | ||
| cleanup_cursor.execute( | ||
| "IF OBJECT_ID('users', 'U') IS NOT NULL DROP TABLE users" | ||
| ) | ||
| self.db.commit() | ||
| self.cursor.close() | ||
| self.db.close() | ||
| except Exception: | ||
| pass | ||
| agent.options.allow_exit_as_root = False | ||
|
|
||
| # ------------------------------------------------------------------ US1 -- | ||
|
|
||
| def test_vanilla_query(self) -> None: | ||
| """No tracer context → zero spans emitted.""" | ||
| self.cursor.execute("SELECT * FROM users") | ||
| rows = self.cursor.fetchall() | ||
| assert len(rows) == 1 | ||
|
|
||
| spans = self.recorder.queued_spans() | ||
| assert len(spans) == 0 | ||
|
|
||
| def test_basic_query(self) -> None: | ||
| """SELECT inside tracer context → one mssql child span with all attributes.""" | ||
| with self.tracer.start_as_current_span("test"): | ||
| self.cursor.execute("SELECT * FROM users") | ||
| rows = self.cursor.fetchall() | ||
|
|
||
| assert len(rows) == 1 | ||
|
|
||
| spans = self.recorder.queued_spans() | ||
| assert len(spans) == 2 | ||
|
|
||
| db_span, test_span = spans | ||
|
|
||
| assert test_span.data["sdk"]["name"] == "test" | ||
| assert test_span.t == db_span.t | ||
| assert db_span.p == test_span.s | ||
|
|
||
| assert not db_span.ec | ||
| assert db_span.n == "mssql" | ||
| assert db_span.data["mssql"]["db"] == testenv["mssql_db"] | ||
| assert db_span.data["mssql"]["user"] == testenv["mssql_user"] | ||
| assert db_span.data["mssql"]["stmt"] == "SELECT * FROM users" | ||
| assert db_span.data["mssql"]["host"] == testenv["mssql_host"] | ||
| assert db_span.data["mssql"]["port"] == testenv["mssql_port"] | ||
|
|
||
| def test_basic_query_as_root_exit_span(self) -> None: | ||
| """Root exit span (no parent) is captured when allow_exit_as_root is True.""" | ||
| agent.options.allow_exit_as_root = True | ||
| self.cursor.execute("SELECT * FROM users") | ||
| rows = self.cursor.fetchall() | ||
|
|
||
| assert len(rows) == 1 | ||
|
|
||
| spans = self.recorder.queued_spans() | ||
| assert len(spans) == 1 | ||
|
|
||
| db_span = spans[0] | ||
|
|
||
| assert not db_span.ec | ||
| assert db_span.n == "mssql" | ||
| assert db_span.data["mssql"]["db"] == testenv["mssql_db"] | ||
| assert db_span.data["mssql"]["user"] == testenv["mssql_user"] | ||
| assert db_span.data["mssql"]["stmt"] == "SELECT * FROM users" | ||
| assert db_span.data["mssql"]["host"] == testenv["mssql_host"] | ||
| assert db_span.data["mssql"]["port"] == testenv["mssql_port"] | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "sql,expected_stmt", | ||
| [ | ||
| ( | ||
| "SELECT * FROM users WHERE id = 1", | ||
| "SELECT * FROM users WHERE id = ?", | ||
| ), | ||
| ( | ||
| "INSERT INTO users (id, name, email) VALUES (2, 'beaker', 'beaker@muppets.com')", | ||
| "INSERT INTO users (id, name, email) VALUES (?, ?, ?)", | ||
| ), | ||
| ( | ||
| "UPDATE users SET name = 'gonzo' WHERE id = 1", | ||
| "UPDATE users SET name = ? WHERE id = ?", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_span_attributes(self, sql: str, expected_stmt: str) -> None: | ||
| """All five non-error span attributes are populated for each DML statement.""" | ||
| with self.tracer.start_as_current_span("test"): | ||
| try: | ||
| self.cursor.execute(sql) | ||
| self.db.commit() | ||
| except Exception: | ||
| self.db.rollback() | ||
|
|
||
| spans = self.recorder.queued_spans() | ||
| assert len(spans) == 2 | ||
| db_span = spans[0] | ||
|
|
||
| assert db_span.n == "mssql" | ||
| assert db_span.data["mssql"]["db"] == testenv["mssql_db"] | ||
| assert db_span.data["mssql"]["user"] == testenv["mssql_user"] | ||
| assert db_span.data["mssql"]["stmt"] == expected_stmt | ||
| assert db_span.data["mssql"]["host"] == testenv["mssql_host"] | ||
| assert db_span.data["mssql"]["port"] == testenv["mssql_port"] | ||
| assert not db_span.ec | ||
|
|
||
| def test_connect_cursor_ctx_mgr(self) -> None: | ||
| """Cursor used as a context manager produces the same span output.""" | ||
| with self.tracer.start_as_current_span("test"), self.cursor: | ||
| self.cursor.execute("SELECT * FROM users") | ||
| rows = self.cursor.fetchall() | ||
|
|
||
| assert len(rows) == 1 | ||
|
|
||
| spans = self.recorder.queued_spans() | ||
| assert len(spans) == 2 | ||
| db_span = spans[0] | ||
|
|
||
| assert db_span.n == "mssql" | ||
| assert db_span.data["mssql"]["stmt"] == "SELECT * FROM users" | ||
| assert not db_span.ec | ||
|
|
||
| # ------------------------------------------------------------------ US2 -- | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "bad_sql", | ||
| [ | ||
| "SELECT * FROM nonexistent_table_xyz", | ||
| "THIS IS NOT VALID SQL AT ALL", | ||
| ], | ||
| ) | ||
| def test_error_capture(self, bad_sql: str) -> None: | ||
| """Failed queries record ec=1 and populate the error attribute.""" | ||
| with self.tracer.start_as_current_span("test"), pytest.raises(Exception): | ||
| self.cursor.execute(bad_sql) | ||
|
|
||
| spans = self.recorder.queued_spans() | ||
| assert len(spans) == 2 | ||
| db_span = spans[0] | ||
|
|
||
| assert db_span.n == "mssql" | ||
| assert db_span.ec == 2 | ||
| assert db_span.data["mssql"]["error"] is not None | ||
| assert len(db_span.data["mssql"]["error"]) > 0 | ||
|
|
||
| def test_no_error_on_success(self) -> None: | ||
| """Successful queries leave ec falsy and error attribute as None.""" | ||
| with self.tracer.start_as_current_span("test"): | ||
| self.cursor.execute("SELECT * FROM users") | ||
|
|
||
| spans = self.recorder.queued_spans() | ||
| assert len(spans) == 2 | ||
| db_span = spans[0] | ||
|
|
||
| assert db_span.n == "mssql" | ||
| assert not db_span.ec | ||
| assert db_span.data["mssql"]["error"] is None | ||
|
|
||
| # ------------------------------------------------------------------ US3 -- | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "batch_rows", | ||
| [ | ||
| [(2, "beaker", "beaker@muppets.com"), (3, "fozzie", "fozzie@muppets.com")], | ||
| [ | ||
| (2, "beaker", "b@m.com"), | ||
| (3, "fozzie", "f@m.com"), | ||
| (4, "gonzo", "g@m.com"), | ||
| (5, "piggy", "p@m.com"), | ||
| (6, "animal", "a@m.com"), | ||
| ], | ||
| ], | ||
| ) | ||
| def test_executemany(self, batch_rows: list) -> None: | ||
| """executemany produces exactly one mssql span regardless of batch size.""" | ||
| sql = "INSERT INTO users (id, name, email) VALUES (%d, %s, %s)" | ||
| with self.tracer.start_as_current_span("test"): | ||
| self.cursor.executemany(sql, batch_rows) | ||
| self.db.commit() | ||
|
|
||
| spans = self.recorder.queued_spans() | ||
| assert len(spans) == 2 | ||
|
|
||
| db_span = spans[0] | ||
| assert db_span.n == "mssql" | ||
| assert db_span.data["mssql"]["stmt"] is not None | ||
| assert not db_span.ec | ||
|
|
||
| # --------------------------------------------------------------- Polish -- | ||
|
|
||
| def test_sqlalchemy_bypass(self) -> None: | ||
| """When the active span is 'sqlalchemy', no mssql span is created.""" | ||
| with self.tracer.start_as_current_span("sqlalchemy"): | ||
| self.cursor.execute("SELECT * FROM users") | ||
|
|
||
| spans = self.recorder.queued_spans() | ||
| # Only the sqlalchemy span; no mssql child | ||
| assert len(spans) == 1 | ||
| assert spans[0].n == "sqlalchemy" |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you're changing the file, should we refactor
Dict,List,Tupletodict,list,tuple? We can do this for any file we touch.