Connection attributes in `redis` database spans (#2398) · gitcommit90/sentry-python@fc638fd · GitHub
Skip to content

Commit fc638fd

Browse files
authored
Connection attributes in redis database spans (getsentry#2398)
This adds db connection parameters like database host, database port, database name, database system ("redis" in this case) to all database spans that are created by our Redis integration. Works for async and sync connections to redis and redis cluster.
1 parent d0b1cf8 commit fc638fd

5 files changed

Lines changed: 241 additions & 90 deletions

File tree

sentry_sdk/integrations/redis/__init__.py

Lines changed: 90 additions & 74 deletions

sentry_sdk/integrations/redis/asyncio.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22

33
from sentry_sdk import Hub
44
from sentry_sdk.consts import OP
5-
from sentry_sdk.utils import capture_internal_exceptions
65
from sentry_sdk.integrations.redis import (
76
RedisIntegration,
87
_get_redis_command_args,
98
_get_span_description,
109
_set_client_data,
10+
_set_db_data,
1111
_set_pipeline_data,
1212
)
13+
from sentry_sdk._types import TYPE_CHECKING
14+
from sentry_sdk.utils import capture_internal_exceptions
1315

14-
15-
from sentry_sdk._types import MYPY
16-
17-
if MYPY:
16+
if TYPE_CHECKING:
1817
from typing import Any
1918

2019

@@ -33,6 +32,7 @@ async def _sentry_execute(self, *args, **kwargs):
3332
op=OP.DB_REDIS, description="redis.pipeline.execute"
3433
) as span:
3534
with capture_internal_exceptions():
35+
_set_db_data(span, self.connection_pool.connection_kwargs)
3636
_set_pipeline_data(
3737
span,
3838
False,
@@ -60,6 +60,7 @@ async def _sentry_execute_command(self, name, *args, **kwargs):
6060
description = _get_span_description(name, *args)
6161

6262
with hub.start_span(op=OP.DB_REDIS, description=description) as span:
63+
_set_db_data(span, self.connection_pool.connection_kwargs)
6364
_set_client_data(span, False, name, *args)
6465

6566
return await old_execute_command(self, name, *args, **kwargs)

tests/integrations/redis/asyncio/test_redis_asyncio.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

33
from sentry_sdk import capture_message, start_transaction
4+
from sentry_sdk.consts import SPANDATA
45
from sentry_sdk.integrations.redis import RedisIntegration
56

67
from fakeredis.aioredis import FakeRedis
@@ -67,7 +68,13 @@ async def test_async_redis_pipeline(
6768
"redis.commands": {
6869
"count": 3,
6970
"first_ten": expected_first_ten,
70-
}
71+
},
72+
SPANDATA.DB_SYSTEM: "redis",
73+
SPANDATA.DB_NAME: "0",
74+
SPANDATA.SERVER_ADDRESS: connection.connection_pool.connection_kwargs.get(
75+
"host"
76+
),
77+
SPANDATA.SERVER_PORT: 6379,
7178
}
7279
assert span["tags"] == {
7380
"redis.transaction": is_transaction,

tests/integrations/redis/test_redis.py

Lines changed: 60 additions & 6 deletions

0 commit comments

Comments
 (0)