Skip to content

Add db.system constant #2037

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

Merged
merged 3 commits 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
9 changes: 9 additions & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ class INSTRUMENTER:
OTEL = "otel"


# 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
"""


class OP:
DB = "db"
DB_REDIS = "db.redis"
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
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: 3 additions & 1 deletion tests/integrations/redis/test_redis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import mock

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

from fakeredis import FakeStrictRedis
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
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