Changed code to support older Python versions
This commit is contained in:
parent
eb92d2d36f
commit
582458cdd0
5027 changed files with 794942 additions and 4 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1,118 @@
|
|||
"""
|
||||
Code used for the Caches module in Sentry
|
||||
"""
|
||||
|
||||
from sentry_sdk.consts import OP, SPANDATA
|
||||
from sentry_sdk.integrations.redis.utils import _get_safe_key, _key_as_string
|
||||
from sentry_sdk.utils import capture_internal_exceptions
|
||||
|
||||
GET_COMMANDS = ("get", "mget")
|
||||
SET_COMMANDS = ("set", "setex")
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from sentry_sdk.integrations.redis import RedisIntegration
|
||||
from sentry_sdk.tracing import Span
|
||||
from typing import Any, Optional
|
||||
|
||||
|
||||
def _get_op(name):
|
||||
# type: (str) -> Optional[str]
|
||||
op = None
|
||||
if name.lower() in GET_COMMANDS:
|
||||
op = OP.CACHE_GET
|
||||
elif name.lower() in SET_COMMANDS:
|
||||
op = OP.CACHE_PUT
|
||||
|
||||
return op
|
||||
|
||||
|
||||
def _compile_cache_span_properties(redis_command, args, kwargs, integration):
|
||||
# type: (str, tuple[Any, ...], dict[str, Any], RedisIntegration) -> dict[str, Any]
|
||||
key = _get_safe_key(redis_command, args, kwargs)
|
||||
key_as_string = _key_as_string(key)
|
||||
keys_as_string = key_as_string.split(", ")
|
||||
|
||||
is_cache_key = False
|
||||
for prefix in integration.cache_prefixes:
|
||||
for kee in keys_as_string:
|
||||
if kee.startswith(prefix):
|
||||
is_cache_key = True
|
||||
break
|
||||
if is_cache_key:
|
||||
break
|
||||
|
||||
value = None
|
||||
if redis_command.lower() in SET_COMMANDS:
|
||||
value = args[-1]
|
||||
|
||||
properties = {
|
||||
"op": _get_op(redis_command),
|
||||
"description": _get_cache_span_description(
|
||||
redis_command, args, kwargs, integration
|
||||
),
|
||||
"key": key,
|
||||
"key_as_string": key_as_string,
|
||||
"redis_command": redis_command.lower(),
|
||||
"is_cache_key": is_cache_key,
|
||||
"value": value,
|
||||
}
|
||||
|
||||
return properties
|
||||
|
||||
|
||||
def _get_cache_span_description(redis_command, args, kwargs, integration):
|
||||
# type: (str, tuple[Any, ...], dict[str, Any], RedisIntegration) -> str
|
||||
description = _key_as_string(_get_safe_key(redis_command, args, kwargs))
|
||||
|
||||
if integration.max_data_size and len(description) > integration.max_data_size:
|
||||
description = description[: integration.max_data_size - len("...")] + "..."
|
||||
|
||||
return description
|
||||
|
||||
|
||||
def _set_cache_data(span, redis_client, properties, return_value):
|
||||
# type: (Span, Any, dict[str, Any], Optional[Any]) -> None
|
||||
with capture_internal_exceptions():
|
||||
span.set_data(SPANDATA.CACHE_KEY, properties["key"])
|
||||
|
||||
if properties["redis_command"] in GET_COMMANDS:
|
||||
if return_value is not None:
|
||||
span.set_data(SPANDATA.CACHE_HIT, True)
|
||||
size = (
|
||||
len(str(return_value).encode("utf-8"))
|
||||
if not isinstance(return_value, bytes)
|
||||
else len(return_value)
|
||||
)
|
||||
span.set_data(SPANDATA.CACHE_ITEM_SIZE, size)
|
||||
else:
|
||||
span.set_data(SPANDATA.CACHE_HIT, False)
|
||||
|
||||
elif properties["redis_command"] in SET_COMMANDS:
|
||||
if properties["value"] is not None:
|
||||
size = (
|
||||
len(properties["value"].encode("utf-8"))
|
||||
if not isinstance(properties["value"], bytes)
|
||||
else len(properties["value"])
|
||||
)
|
||||
span.set_data(SPANDATA.CACHE_ITEM_SIZE, size)
|
||||
|
||||
try:
|
||||
connection_params = redis_client.connection_pool.connection_kwargs
|
||||
except AttributeError:
|
||||
# If it is a cluster, there is no connection_pool attribute so we
|
||||
# need to get the default node from the cluster instance
|
||||
default_node = redis_client.get_default_node()
|
||||
connection_params = {
|
||||
"host": default_node.host,
|
||||
"port": default_node.port,
|
||||
}
|
||||
|
||||
host = connection_params.get("host")
|
||||
if host is not None:
|
||||
span.set_data(SPANDATA.NETWORK_PEER_ADDRESS, host)
|
||||
|
||||
port = connection_params.get("port")
|
||||
if port is not None:
|
||||
span.set_data(SPANDATA.NETWORK_PEER_PORT, port)
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
"""
|
||||
Code used for the Queries module in Sentry
|
||||
"""
|
||||
|
||||
from sentry_sdk.consts import OP, SPANDATA
|
||||
from sentry_sdk.integrations.redis.utils import _get_safe_command
|
||||
from sentry_sdk.utils import capture_internal_exceptions
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from redis import Redis
|
||||
from sentry_sdk.integrations.redis import RedisIntegration
|
||||
from sentry_sdk.tracing import Span
|
||||
from typing import Any
|
||||
|
||||
|
||||
def _compile_db_span_properties(integration, redis_command, args):
|
||||
# type: (RedisIntegration, str, tuple[Any, ...]) -> dict[str, Any]
|
||||
description = _get_db_span_description(integration, redis_command, args)
|
||||
|
||||
properties = {
|
||||
"op": OP.DB_REDIS,
|
||||
"description": description,
|
||||
}
|
||||
|
||||
return properties
|
||||
|
||||
|
||||
def _get_db_span_description(integration, command_name, args):
|
||||
# type: (RedisIntegration, str, tuple[Any, ...]) -> str
|
||||
description = command_name
|
||||
|
||||
with capture_internal_exceptions():
|
||||
description = _get_safe_command(command_name, args)
|
||||
|
||||
if integration.max_data_size and len(description) > integration.max_data_size:
|
||||
description = description[: integration.max_data_size - len("...")] + "..."
|
||||
|
||||
return description
|
||||
|
||||
|
||||
def _set_db_data_on_span(span, connection_params):
|
||||
# type: (Span, dict[str, Any]) -> None
|
||||
span.set_data(SPANDATA.DB_SYSTEM, "redis")
|
||||
|
||||
db = connection_params.get("db")
|
||||
if db is not None:
|
||||
span.set_data(SPANDATA.DB_NAME, str(db))
|
||||
|
||||
host = connection_params.get("host")
|
||||
if host is not None:
|
||||
span.set_data(SPANDATA.SERVER_ADDRESS, host)
|
||||
|
||||
port = connection_params.get("port")
|
||||
if port is not None:
|
||||
span.set_data(SPANDATA.SERVER_PORT, port)
|
||||
|
||||
|
||||
def _set_db_data(span, redis_instance):
|
||||
# type: (Span, Redis[Any]) -> None
|
||||
try:
|
||||
_set_db_data_on_span(span, redis_instance.connection_pool.connection_kwargs)
|
||||
except AttributeError:
|
||||
pass # connections_kwargs may be missing in some cases
|
||||
Loading…
Add table
Add a link
Reference in a new issue