Skip to content
Draft
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
30 changes: 16 additions & 14 deletions sentry_sdk/integrations/aiomysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from typing import Any, Awaitable, Callable, TypeVar

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
Expand Down Expand Up @@ -181,8 +183,8 @@ async def _inner(self: "Connection") -> T:
return await f(self)

span_attributes: dict[str, Any] = {
"sentry.op": OP.DB,
"sentry.origin": AioMySQLIntegration.origin,
ATTRIBUTE_NAMES.SENTRY_OP: OP.DB,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: AioMySQLIntegration.origin,
} | breadcrumb_data

with sentry_sdk.start_span(name="connect", attributes=span_attributes):
Expand All @@ -193,53 +195,53 @@ async def _inner(self: "Connection") -> T:

def _get_connect_data(conn: Any, *, use_streaming_keys: bool = False) -> dict[str, Any]:
if use_streaming_keys:
db_system = SPANDATA.DB_SYSTEM_NAME
db_name = SPANDATA.DB_NAMESPACE
db_system = ATTRIBUTE_NAMES.DB_SYSTEM_NAME
db_name = ATTRIBUTE_NAMES.DB_NAMESPACE
else:
db_system = SPANDATA.DB_SYSTEM
db_name = SPANDATA.DB_NAME

data: dict[str, Any] = {
db_system: "mysql",
SPANDATA.DB_DRIVER_NAME: "aiomysql",
ATTRIBUTE_NAMES.DB_DRIVER_NAME: "aiomysql",
}

host = getattr(conn, "host", None)
if host is not None:
data[SPANDATA.SERVER_ADDRESS] = host
data[ATTRIBUTE_NAMES.SERVER_ADDRESS] = host

port = getattr(conn, "port", None)
if port is not None:
data[SPANDATA.SERVER_PORT] = port
data[ATTRIBUTE_NAMES.SERVER_PORT] = port

database = getattr(conn, "db", None)
if database is not None:
data[db_name] = database

user = getattr(conn, "user", None)
if user is not None:
data[SPANDATA.DB_USER] = user
data[ATTRIBUTE_NAMES.DB_USER] = user

return data


def _set_db_data(span: Any, conn: Any) -> None:
"""Set database-related span data from connection object."""
span.set_attribute(SPANDATA.DB_SYSTEM_NAME, "mysql")
span.set_attribute(SPANDATA.DB_DRIVER_NAME, "aiomysql")
span.set_attribute(ATTRIBUTE_NAMES.DB_SYSTEM_NAME, "mysql")
span.set_attribute(ATTRIBUTE_NAMES.DB_DRIVER_NAME, "aiomysql")

host = getattr(conn, "host", None)
if host is not None:
span.set_attribute(SPANDATA.SERVER_ADDRESS, host)
span.set_attribute(ATTRIBUTE_NAMES.SERVER_ADDRESS, host)

port = getattr(conn, "port", None)
if port is not None:
span.set_attribute(SPANDATA.SERVER_PORT, port)
span.set_attribute(ATTRIBUTE_NAMES.SERVER_PORT, port)

database = getattr(conn, "db", None)
if database is not None:
span.set_attribute(SPANDATA.DB_NAMESPACE, database)
span.set_attribute(ATTRIBUTE_NAMES.DB_NAMESPACE, database)

user = getattr(conn, "user", None)
if user is not None:
span.set_attribute(SPANDATA.DB_USER, user)
span.set_attribute(ATTRIBUTE_NAMES.DB_USER, user)
32 changes: 17 additions & 15 deletions sentry_sdk/integrations/asyncpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import re
from typing import Any, Awaitable, Callable, Iterator, TypeVar

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.consts import OP
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
from sentry_sdk.traces import Span
from sentry_sdk.tracing_utils import (
Expand Down Expand Up @@ -202,17 +204,17 @@ async def _inner(*args: "Any", **kwargs: "Any") -> "T":
addr = kwargs.get("addr")

span_attributes = {
"sentry.op": OP.DB,
"sentry.origin": AsyncPGIntegration.origin,
SPANDATA.DB_SYSTEM_NAME: "postgresql",
SPANDATA.DB_USER: user,
SPANDATA.DB_NAMESPACE: database,
SPANDATA.DB_DRIVER_NAME: "asyncpg",
ATTRIBUTE_NAMES.SENTRY_OP: OP.DB,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: AsyncPGIntegration.origin,
ATTRIBUTE_NAMES.DB_SYSTEM_NAME: "postgresql",
ATTRIBUTE_NAMES.DB_USER: user,
ATTRIBUTE_NAMES.DB_NAMESPACE: database,
ATTRIBUTE_NAMES.DB_DRIVER_NAME: "asyncpg",
}
if addr:
try:
span_attributes[SPANDATA.SERVER_ADDRESS] = addr[0]
span_attributes[SPANDATA.SERVER_PORT] = addr[1]
span_attributes[ATTRIBUTE_NAMES.SERVER_ADDRESS] = addr[0]
span_attributes[ATTRIBUTE_NAMES.SERVER_PORT] = addr[1]
except IndexError:
pass

Expand All @@ -235,17 +237,17 @@ def _set_db_data(span: "Span", conn: "Any") -> None:
database = conn._params.database
user = conn._params.user

span.set_attribute(SPANDATA.DB_SYSTEM_NAME, "postgresql")
span.set_attribute(SPANDATA.DB_DRIVER_NAME, "asyncpg")
span.set_attribute(ATTRIBUTE_NAMES.DB_SYSTEM_NAME, "postgresql")
span.set_attribute(ATTRIBUTE_NAMES.DB_DRIVER_NAME, "asyncpg")
if addr:
try:
span.set_attribute(SPANDATA.SERVER_ADDRESS, addr[0])
span.set_attribute(SPANDATA.SERVER_PORT, addr[1])
span.set_attribute(ATTRIBUTE_NAMES.SERVER_ADDRESS, addr[0])
span.set_attribute(ATTRIBUTE_NAMES.SERVER_PORT, addr[1])
except IndexError:
pass

if database:
span.set_attribute(SPANDATA.DB_NAMESPACE, database)
span.set_attribute(ATTRIBUTE_NAMES.DB_NAMESPACE, database)

if user:
span.set_attribute(SPANDATA.DB_USER, user)
span.set_attribute(ATTRIBUTE_NAMES.DB_USER, user)
20 changes: 11 additions & 9 deletions sentry_sdk/integrations/clickhouse_driver.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import functools
from typing import TYPE_CHECKING, TypeVar

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
Expand Down Expand Up @@ -82,9 +84,9 @@ def _inner(*args: "P.args", **kwargs: "P.kwargs") -> "T":
span = sentry_sdk.start_span(
name=query,
attributes={
"sentry.op": OP.DB,
"sentry.origin": ClickhouseDriverIntegration.origin,
SPANDATA.DB_QUERY_TEXT: str(query),
ATTRIBUTE_NAMES.SENTRY_OP: OP.DB,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: ClickhouseDriverIntegration.origin,
ATTRIBUTE_NAMES.DB_QUERY_TEXT: str(query),
},
)

Expand Down Expand Up @@ -164,9 +166,9 @@ def _set_db_data(span: "Optional[Span]", connection: "Connection") -> None:
if span is None:
return

span.set_attribute(SPANDATA.DB_SYSTEM_NAME, "clickhouse")
span.set_attribute(SPANDATA.DB_NAMESPACE, connection.database)
span.set_attribute(SPANDATA.DB_DRIVER_NAME, "clickhouse-driver")
span.set_attribute(SPANDATA.SERVER_ADDRESS, connection.host)
span.set_attribute(SPANDATA.SERVER_PORT, connection.port)
span.set_attribute(SPANDATA.DB_USER, connection.user)
span.set_attribute(ATTRIBUTE_NAMES.DB_SYSTEM_NAME, "clickhouse")
span.set_attribute(ATTRIBUTE_NAMES.DB_NAMESPACE, connection.database)
span.set_attribute(ATTRIBUTE_NAMES.DB_DRIVER_NAME, "clickhouse-driver")
span.set_attribute(ATTRIBUTE_NAMES.SERVER_ADDRESS, connection.host)
span.set_attribute(ATTRIBUTE_NAMES.SERVER_PORT, connection.port)
span.set_attribute(ATTRIBUTE_NAMES.DB_USER, connection.user)
24 changes: 13 additions & 11 deletions sentry_sdk/integrations/pymongo.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import copy
import json

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.consts import OP
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
from sentry_sdk.traces import SpanStatus
from sentry_sdk.utils import (
Expand Down Expand Up @@ -92,21 +94,21 @@ def _strip_pii(command: "Dict[str, Any]") -> "Dict[str, Any]":
def _get_db_data(event: "Any") -> "Dict[str, Any]":
data = {}

data[SPANDATA.DB_DRIVER_NAME] = "pymongo"
data[ATTRIBUTE_NAMES.DB_DRIVER_NAME] = "pymongo"
db_name = event.database_name

server_address = event.connection_id[0]
if server_address is not None:
data[SPANDATA.SERVER_ADDRESS] = server_address
data[ATTRIBUTE_NAMES.SERVER_ADDRESS] = server_address

server_port = event.connection_id[1]
if server_port is not None:
data[SPANDATA.SERVER_PORT] = server_port
data[ATTRIBUTE_NAMES.SERVER_PORT] = server_port

data["db.system.name"] = "mongodb"
data[ATTRIBUTE_NAMES.DB_SYSTEM_NAME] = "mongodb"

if db_name is not None:
data["db.namespace"] = db_name
data[ATTRIBUTE_NAMES.DB_NAMESPACE] = db_name

return data

Expand Down Expand Up @@ -145,11 +147,11 @@ def started(self, event: "CommandStartedEvent") -> None:
query = json.dumps(command, default=str)

data = {
"db.operation.name": operation_name,
"db.collection.name": collection_name,
SPANDATA.DB_QUERY_TEXT: query,
"sentry.op": OP.DB,
"sentry.origin": PyMongoIntegration.origin,
ATTRIBUTE_NAMES.DB_OPERATION_NAME: operation_name,
ATTRIBUTE_NAMES.DB_COLLECTION_NAME: collection_name,
ATTRIBUTE_NAMES.DB_QUERY_TEXT: query,
ATTRIBUTE_NAMES.SENTRY_OP: OP.DB,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: PyMongoIntegration.origin,
**db_data,
}

Expand Down
22 changes: 12 additions & 10 deletions sentry_sdk/integrations/redis/_async_common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import TYPE_CHECKING

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.consts import OP
from sentry_sdk.integrations.redis.consts import (
SPAN_ORIGIN,
)
Expand Down Expand Up @@ -58,8 +60,8 @@ async def _sentry_execute(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":
span = sentry_sdk.start_span(
name="redis.pipeline.execute",
attributes={
"sentry.origin": SPAN_ORIGIN,
"sentry.op": OP.DB_QUERY,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN,
ATTRIBUTE_NAMES.SENTRY_OP: OP.DB_QUERY,
},
)

Expand Down Expand Up @@ -119,7 +121,7 @@ async def _sentry_execute_command(

additional_cache_span_attributes = {}
with capture_internal_exceptions():
additional_cache_span_attributes[SPANDATA.DB_QUERY_TEXT] = (
additional_cache_span_attributes[ATTRIBUTE_NAMES.DB_QUERY_TEXT] = (
_get_safe_command(name, args)
)

Expand All @@ -128,23 +130,23 @@ async def _sentry_execute_command(
cache_span = sentry_sdk.start_span(
name=cache_properties["description"],
attributes={
"sentry.op": cache_properties["op"],
"sentry.origin": SPAN_ORIGIN,
ATTRIBUTE_NAMES.SENTRY_OP: cache_properties["op"],
ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN,
**additional_cache_span_attributes,
},
)

additional_db_span_attributes = {}
with capture_internal_exceptions():
additional_db_span_attributes[SPANDATA.DB_QUERY_TEXT] = _get_safe_command(
name, args
additional_db_span_attributes[ATTRIBUTE_NAMES.DB_QUERY_TEXT] = (
_get_safe_command(name, args)
)

db_span = sentry_sdk.start_span(
name=db_properties["description"],
attributes={
"sentry.op": db_properties["op"],
"sentry.origin": SPAN_ORIGIN,
ATTRIBUTE_NAMES.SENTRY_OP: db_properties["op"],
ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN,
**additional_db_span_attributes,
},
)
Expand Down
22 changes: 12 additions & 10 deletions sentry_sdk/integrations/redis/_sync_common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import TYPE_CHECKING

from sentry_conventions.attributes import ATTRIBUTE_NAMES

import sentry_sdk
from sentry_sdk.consts import OP, SPANDATA
from sentry_sdk.consts import OP
from sentry_sdk.integrations.redis.consts import (
SPAN_ORIGIN,
)
Expand Down Expand Up @@ -55,8 +57,8 @@ def sentry_patched_execute(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":
span = sentry_sdk.start_span(
name="redis.pipeline.execute",
attributes={
"sentry.origin": SPAN_ORIGIN,
"sentry.op": OP.DB_QUERY,
ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN,
ATTRIBUTE_NAMES.SENTRY_OP: OP.DB_QUERY,
},
)

Expand Down Expand Up @@ -120,7 +122,7 @@ def sentry_patched_execute_command(

additional_cache_span_attributes = {}
with capture_internal_exceptions():
additional_cache_span_attributes[SPANDATA.DB_QUERY_TEXT] = (
additional_cache_span_attributes[ATTRIBUTE_NAMES.DB_QUERY_TEXT] = (
_get_safe_command(name, args)
)

Expand All @@ -129,23 +131,23 @@ def sentry_patched_execute_command(
cache_span = sentry_sdk.start_span(
name=cache_properties["description"],
attributes={
"sentry.op": cache_properties["op"],
"sentry.origin": SPAN_ORIGIN,
ATTRIBUTE_NAMES.SENTRY_OP: cache_properties["op"],
ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN,
**additional_cache_span_attributes,
},
)

additional_db_span_attributes = {}
with capture_internal_exceptions():
additional_db_span_attributes[SPANDATA.DB_QUERY_TEXT] = _get_safe_command(
name, args
additional_db_span_attributes[ATTRIBUTE_NAMES.DB_QUERY_TEXT] = (
_get_safe_command(name, args)
)

db_span = sentry_sdk.start_span(
name=db_properties["description"],
attributes={
"sentry.op": db_properties["op"],
"sentry.origin": SPAN_ORIGIN,
ATTRIBUTE_NAMES.SENTRY_OP: db_properties["op"],
ATTRIBUTE_NAMES.SENTRY_ORIGIN: SPAN_ORIGIN,
**additional_db_span_attributes,
},
)
Expand Down
Loading
Loading