Skip to content
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

feat: Add db.system to sqlalchemy spans #2039

Merged
merged 1 commit into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions sentry_sdk/integrations/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re

from sentry_sdk._types import TYPE_CHECKING
from sentry_sdk.consts import SPANDATA
from sentry_sdk.hub import Hub
from sentry_sdk.integrations import Integration, DidNotEnable
from sentry_sdk.tracing_utils import record_sql_queries
Expand Down Expand Up @@ -67,6 +68,9 @@ def _before_cursor_execute(
span = ctx_mgr.__enter__()

if span is not None:
db_system = _get_db_system(conn.engine.name)
if db_system is not None:
span.set_data(SPANDATA.DB_SYSTEM, db_system)
context._sentry_sql_span = span


Expand Down Expand Up @@ -102,3 +106,24 @@ def _handle_error(context, *args):
if ctx_mgr is not None:
execution_context._sentry_sql_span_manager = None
ctx_mgr.__exit__(None, None, None)


# See: https://docs.sqlalchemy.org/en/20/dialects/index.html
def _get_db_system(name):
# type: (str) -> Optional[str]
if "sqlite" in name:
return "sqlite"

if "postgres" in name:
return "postgresql"

if "mariadb" in name:
return "mariadb"

if "mysql" in name:
return "mysql"

if "oracle" in name:
return "oracle"

return None
4 changes: 4 additions & 0 deletions tests/integrations/sqlalchemy/test_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sqlalchemy.orm import relationship, sessionmaker

from sentry_sdk import capture_message, start_transaction, configure_scope
from sentry_sdk.consts import SPANDATA
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
from sentry_sdk.serializer import MAX_EVENT_BYTES
from sentry_sdk.utils import json_dumps, MAX_STRING_LENGTH
Expand Down Expand Up @@ -119,6 +120,9 @@ class Address(Base):

(event,) = events

for span in event["spans"]:
assert span["data"][SPANDATA.DB_SYSTEM] == "sqlite"

assert (
render_span_tree(event)
== """\
Expand Down