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

Adds DB connection attributes in spans #2274

Merged
merged 27 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
62c713c
Added connection data to sqlalchemy spans.
antonpirker Jul 27, 2023
f8dcc41
Add connection data for mongo db spans
antonpirker Jul 27, 2023
2590506
Add connection data to django db spans.
antonpirker Jul 27, 2023
97ee7e5
Cleanup
antonpirker Jul 27, 2023
62d31c2
Cleanup
antonpirker Jul 27, 2023
ac3e3f5
Added db connection data to redis spans
antonpirker Jul 27, 2023
561c50f
Fixed tests
antonpirker Jul 27, 2023
41182c0
Made redis db name a string
antonpirker Jul 27, 2023
0163e8b
Fixed some tests and some redis tinkering
antonpirker Jul 28, 2023
5fcdee7
Reverted redis changes (will do in a separate pr)
antonpirker Jul 28, 2023
a47b993
Fixing hosts in tests
antonpirker Jul 28, 2023
a064190
Fixing tests
antonpirker Jul 28, 2023
c7aa223
Fixing tests
antonpirker Jul 28, 2023
e6991b9
Updated chalice versions
antonpirker Jul 28, 2023
c0330e4
Updated chalice deps
antonpirker Jul 28, 2023
6da2894
Removed mockupdb from general deps because it is only used in mongodb…
antonpirker Jul 28, 2023
23581da
Do not test newest chalice versions
antonpirker Jul 28, 2023
5e3a07f
Better fetching of db attributes
antonpirker Jul 28, 2023
dfa12a4
fix
antonpirker Jul 28, 2023
0be3b1b
fix tests
antonpirker Jul 28, 2023
8e9c8ba
remove old unused test args
sentrivana Jul 28, 2023
dd83203
Merge branch 'master' into antonpirker/2270-db-connection-attributes-…
sentrivana Jul 28, 2023
f0f9770
ok, maybe they were not unused
sentrivana Jul 28, 2023
2914c0c
add django test
sentrivana Jul 28, 2023
ce0089e
hello, github?
sentrivana Jul 28, 2023
b170287
fix test
sentrivana Jul 28, 2023
50735ce
github having a day again
sentrivana Jul 28, 2023
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
31 changes: 31 additions & 0 deletions sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ class SPANDATA:
See: https://develop.sentry.dev/sdk/performance/span-data-conventions/
"""

DB_NAME = "db.name"
"""
The name of the database being accessed. For commands that switch the database, this should be set to the target database (even if the command fails).
Example: myDatabase
"""

DB_OPERATION = "db.operation"
"""
The name of the operation being executed, e.g. the MongoDB command name such as findAndModify, or the SQL keyword.
Expand Down Expand Up @@ -118,6 +124,31 @@ class SPANDATA:
Example: 418
"""

SERVER_ADDRESS = "server.address"
"""
Name of the database host.
Example: example.com
"""

SERVER_PORT = "server.port"
"""
Logical server port number
Example: 80; 8080; 443
"""

SERVER_SOCKET_ADDRESS = "server.socket.address"
"""
Physical server IP address or Unix socket address.
Example: 10.5.3.2
"""

SERVER_SOCKET_PORT = "server.socket.port"
"""
Physical server port.
Recommended: If different than server.port.
Example: 16456
"""


class OP:
CACHE_GET_ITEM = "cache.get_item"
Expand Down
15 changes: 8 additions & 7 deletions sentry_sdk/integrations/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@
with record_sql_queries(
hub, self.cursor, sql, params, paramstyle="format", executemany=False
) as span:
_set_db_system_on_span(span, self.db.vendor)
_set_db_data(span, self.db.vendor, self.db.get_connection_params())
return real_execute(self, sql, params)

def executemany(self, sql, param_list):
Expand All @@ -624,7 +624,7 @@
with record_sql_queries(
hub, self.cursor, sql, param_list, paramstyle="format", executemany=True
) as span:
_set_db_system_on_span(span, self.db.vendor)
_set_db_data(span, self.db.vendor, self.db.get_connection_params())

Check warning on line 627 in sentry_sdk/integrations/django/__init__.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/django/__init__.py#L627

Added line #L627 was not covered by tests
return real_executemany(self, sql, param_list)

def connect(self):
Expand All @@ -637,7 +637,7 @@
hub.add_breadcrumb(message="connect", category="query")

with hub.start_span(op=OP.DB, description="connect") as span:
_set_db_system_on_span(span, self.vendor)
_set_db_data(span, self.vendor, self.get_connection_params())
return real_connect(self)

CursorWrapper.execute = execute
Expand All @@ -646,8 +646,9 @@
ignore_logger("django.db.backends")


# https://github.com/django/django/blob/6a0dc2176f4ebf907e124d433411e52bba39a28e/django/db/backends/base/base.py#L29
# Avaliable in Django 1.8+
def _set_db_system_on_span(span, vendor):
# type: (Span, str) -> None
def _set_db_data(span, vendor, connection_params):
# type: (Span, str, Dict[str, str]) -> None
span.set_data(SPANDATA.DB_SYSTEM, vendor)
span.set_data(SPANDATA.DB_NAME, connection_params.get("database"))
antonpirker marked this conversation as resolved.
Show resolved Hide resolved
span.set_data(SPANDATA.SERVER_ADDRESS, connection_params.get("host"))
span.set_data(SPANDATA.SERVER_PORT, connection_params.get("port"))
antonpirker marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 4 additions & 1 deletion sentry_sdk/integrations/pymongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,13 @@ def started(self, event):
pass

data = {"operation_ids": {}} # type: Dict[str, Any]

data["operation_ids"]["operation"] = event.operation_id
data["operation_ids"]["request"] = event.request_id

data[SPANDATA.DB_SYSTEM] = "mongodb"
data[SPANDATA.DB_NAME] = event.database_name
data[SPANDATA.SERVER_ADDRESS] = event.connection_id[0]
data[SPANDATA.SERVER_PORT] = event.connection_id[1]

try:
lsid = command.pop("lsid")["id"]
Expand Down
16 changes: 13 additions & 3 deletions sentry_sdk/integrations/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@
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)
_set_db_data(span, conn)

Check warning on line 70 in sentry_sdk/integrations/sqlalchemy.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/sqlalchemy.py#L70

Added line #L70 was not covered by tests
context._sentry_sql_span = span


Expand Down Expand Up @@ -128,3 +126,15 @@
return "oracle"

return None


def _set_db_data(span, conn):

Check warning on line 131 in sentry_sdk/integrations/sqlalchemy.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/sqlalchemy.py#L131

Added line #L131 was not covered by tests
# type: (Span, Any) -> None
db_system = _get_db_system(conn.engine.name)

Check warning on line 133 in sentry_sdk/integrations/sqlalchemy.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/sqlalchemy.py#L133

Added line #L133 was not covered by tests
if db_system is not None:
span.set_data(SPANDATA.DB_SYSTEM, db_system)

Check warning on line 135 in sentry_sdk/integrations/sqlalchemy.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/sqlalchemy.py#L135

Added line #L135 was not covered by tests

span.set_data(SPANDATA.DB_NAME, conn.engine.url.database)

Check warning on line 137 in sentry_sdk/integrations/sqlalchemy.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/sqlalchemy.py#L137

Added line #L137 was not covered by tests

span.set_data(SPANDATA.SERVER_ADDRESS, conn.engine.url.host)
span.set_data(SPANDATA.SERVER_PORT, conn.engine.url.port)

Check warning on line 140 in sentry_sdk/integrations/sqlalchemy.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/integrations/sqlalchemy.py#L139-L140

Added lines #L139 - L140 were not covered by tests
3 changes: 3 additions & 0 deletions tests/integrations/pymongo/test_pymongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def test_transactions(sentry_init, capture_events, mongo_server, with_pii):
}
for span in find, insert_success, insert_fail:
assert span["data"][SPANDATA.DB_SYSTEM] == "mongodb"
assert span["data"][SPANDATA.DB_NAME] == "test_db"
assert span["data"][SPANDATA.SERVER_ADDRESS] == "localhost"
assert span["data"][SPANDATA.SERVER_PORT] == mongo_server.port
for field, value in common_tags.items():
assert span["tags"][field] == value

Expand Down
7 changes: 7 additions & 0 deletions tests/integrations/sqlalchemy/test_sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ class Address(Base):

for span in event["spans"]:
assert span["data"][SPANDATA.DB_SYSTEM] == "sqlite"
assert span["data"][SPANDATA.DB_NAME] == ":memory:"
assert (
span["data"][SPANDATA.SERVER_ADDRESS] is None
) # sqlite does not have a server
assert (
span["data"][SPANDATA.SERVER_PORT] is None
) # sqlite does not have a server

assert (
render_span_tree(event)
Expand Down
Loading