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 indicators to redis, sqlalchemy, and django #2035

Closed
wants to merge 4 commits into from
Closed
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
9 changes: 9 additions & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ class OP:
SOCKET_DNS = "socket.dns"


# See: https://develop.sentry.dev/sdk/performance/span-data-conventions/
class SPANDATA:
DB_SYSTEM = "db.system"
"""
An identifier for the database management system (DBMS) product being used.
See: https://github.com/open-telemetry/opentelemetry-python/blob/e00306206ea25cf8549eca289e39e0b6ba2fa560/opentelemetry-semantic-conventions/src/opentelemetry/semconv/trace/__init__.py#L58
"""


# This type exists to trick mypy and PyCharm into thinking `init` and `Client`
# take these arguments (even though they take opaque **kwargs)
class ClientConstructor(object):
Expand Down
20 changes: 16 additions & 4 deletions sentry_sdk/integrations/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import weakref

from sentry_sdk._types import TYPE_CHECKING
from sentry_sdk.consts import OP
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.hub import Hub, _should_send_default_pii
from sentry_sdk.scope import add_global_event_processor
from sentry_sdk.serializer import add_global_repr_processor
Expand Down Expand Up @@ -570,6 +570,8 @@ def install_sql_hook():
# This won't work on Django versions < 1.6
return

from django.db import connection

def execute(self, sql, params=None):
# type: (CursorWrapper, Any, Optional[Any]) -> Any
hub = Hub.current
Expand All @@ -578,7 +580,10 @@ def execute(self, sql, params=None):

with record_sql_queries(
hub, self.cursor, sql, params, paramstyle="format", executemany=False
):
) as span:
vendor = connection.vendor
Copy link
Member Author

Choose a reason for hiding this comment

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

I realized this strategy doesn't work if you have multiple connections open, but not sure how common that is.

Copy link
Member

Choose a reason for hiding this comment

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

In Django it is common to have one connection to a read only replica of the database and one you use to change the data, so probably on bigger installations quite common.

if vendor:
span.set_data(SPANDATA.DB_SYSTEM, vendor)
return real_execute(self, sql, params)

def executemany(self, sql, param_list):
Expand All @@ -589,7 +594,10 @@ def executemany(self, sql, param_list):

with record_sql_queries(
hub, self.cursor, sql, param_list, paramstyle="format", executemany=True
):
) as span:
vendor = connection.vendor
if vendor:
span.set_data(SPANDATA.DB_SYSTEM, vendor)
return real_executemany(self, sql, param_list)

def connect(self):
Expand All @@ -601,7 +609,11 @@ def connect(self):
with capture_internal_exceptions():
hub.add_breadcrumb(message="connect", category="query")

with hub.start_span(op=OP.DB, description="connect"):
with hub.start_span(op=OP.DB, description="connect") as span:
vendor = connection.vendor
if vendor:
span.set_data(SPANDATA.DB_SYSTEM, vendor)

return real_connect(self)

CursorWrapper.execute = execute
Expand Down
3 changes: 2 additions & 1 deletion sentry_sdk/integrations/redis.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import absolute_import

from sentry_sdk import Hub
from sentry_sdk.consts import OP
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.hub import _should_send_default_pii
from sentry_sdk.utils import (
SENSITIVE_DATA_SUBSTITUTE,
Expand Down Expand Up @@ -63,6 +63,7 @@ def sentry_patched_execute(self, *args, **kwargs):
"redis.commands",
{"count": len(self.command_stack), "first_ten": commands},
)
span.set_data(SPANDATA.DB_SYSTEM, "redis")

return old_execute(self, *args, **kwargs)

Expand Down
15 changes: 15 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,14 @@ def _handle_error(context, *args):
if ctx_mgr is not None:
execution_context._sentry_sql_span_manager = None
ctx_mgr.__exit__(None, None, None)


def _get_db_system(name):
# type: (str) -> Optional[str]
if "sqlite" in name:
return "sqlite"

if "postgres" in name:
return "postgresql"

return None
1 change: 1 addition & 0 deletions sentry_sdk/tracing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def record_sql_queries(
with hub.start_span(op=OP.DB, description=query) as span:
for k, v in data.items():
span.set_data(k, v)

yield span


Expand Down
4 changes: 3 additions & 1 deletion tests/integrations/redis/test_redis.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import mock

from sentry_sdk.consts import SPANDATA
from sentry_sdk import capture_message, start_transaction
from sentry_sdk.integrations.redis import RedisIntegration

Expand Down Expand Up @@ -53,7 +54,8 @@ def test_redis_pipeline(sentry_init, capture_events, is_transaction):
"redis.commands": {
"count": 3,
"first_ten": ["GET 'foo'", "SET 'bar' 1", "SET 'baz' 2"],
}
},
SPANDATA.DB_SYSTEM: "redis",
}
assert span["tags"] == {
"redis.transaction": is_transaction,
Expand Down
4 changes: 3 additions & 1 deletion tests/integrations/rediscluster/test_rediscluster.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from sentry_sdk import capture_message
from sentry_sdk.consts import SPANDATA
from sentry_sdk.api import start_transaction
from sentry_sdk.integrations.redis import RedisIntegration

Expand Down Expand Up @@ -71,7 +72,8 @@ def test_rediscluster_pipeline(sentry_init, capture_events):
"redis.commands": {
"count": 3,
"first_ten": ["GET 'foo'", "SET 'bar' 1", "SET 'baz' 2"],
}
},
SPANDATA.DB_SYSTEM: "redis",
}
assert span["tags"] == {
"redis.transaction": False, # For Cluster, this is always False
Expand Down
3 changes: 3 additions & 0 deletions tests/integrations/sqlalchemy/test_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker

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

(event,) = events

assert event["spans"][0]["data"][SPANDATA.DB_SYSTEM] == "sqlite"

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