-
Notifications
You must be signed in to change notification settings - Fork 562
feat(integrations): Add integration for aiomysql
#4703
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
Open
tonal
wants to merge
7
commits into
getsentry:master
Choose a base branch
from
tonal:patch-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+154
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
878dc5b
integration for aiomysql
tonal 29b4e85
Drop _wrap_cursor_creation in integration aiomysql.py
tonal 28e79b6
Potential KeyError in _wrap_connect
tonal 950032f
Merge branch 'getsentry:master' into patch-2
tonal 421abfa
Merge branch 'master' into patch-2
tonal ab35ef8
Merge branch 'master' into patch-2
antonpirker c09eb44
Merge branch 'master' into patch-2
tonal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Adapted from module sentry_sdk.integrations.asyncpg | ||
""" | ||
from __future__ import annotations | ||
import contextlib | ||
from typing import Any, TypeVar, Callable, Awaitable, Iterator | ||
|
||
import sentry_sdk | ||
from sentry_sdk.consts import OP, SPANDATA | ||
from sentry_sdk.integrations import _check_minimum_version, Integration, DidNotEnable | ||
from sentry_sdk.tracing import Span | ||
from sentry_sdk.tracing_utils import add_query_source, record_sql_queries | ||
from sentry_sdk.utils import ( | ||
ensure_integration_enabled, | ||
parse_version, | ||
capture_internal_exceptions, | ||
) | ||
|
||
try: | ||
import aiomysql # type: ignore[import-not-found] | ||
from aiomysql.connection import Connection, Cursor # type: ignore | ||
except ImportError: | ||
raise DidNotEnable("aiomysql not installed.") | ||
|
||
|
||
class AioMySQLIntegration(Integration): | ||
identifier = "aiomysql" | ||
origin = f"auto.db.{identifier}" | ||
_record_params = False | ||
|
||
def __init__(self, *, record_params: bool = False): | ||
AioMySQLIntegration._record_params = record_params | ||
|
||
@staticmethod | ||
def setup_once() -> None: | ||
aiomysql_version = parse_version(aiomysql.__version__) | ||
_check_minimum_version(AioMySQLIntegration, aiomysql_version) | ||
|
||
aiomysql.Connection.query = _wrap_execute( | ||
aiomysql.Connection.query, | ||
) | ||
|
||
aiomysql.connect = _wrap_connect(aiomysql.connect) | ||
|
||
|
||
T = TypeVar("T") | ||
|
||
|
||
def _wrap_execute(f: Callable[..., Awaitable[T]]) -> Callable[..., Awaitable[T]]: | ||
async def _inner(*args: Any, **kwargs: Any) -> T: | ||
if sentry_sdk.get_client().get_integration(AioMySQLIntegration) is None: | ||
return await f(*args, **kwargs) | ||
|
||
conn = args[0] | ||
query = args[1] # В aiomysql запрос передается первым аргументом | ||
with record_sql_queries( | ||
cursor=None, | ||
query=query, | ||
params_list=None, | ||
paramstyle=None, | ||
executemany=False, | ||
span_origin=AioMySQLIntegration.origin, | ||
) as span: | ||
res = await f(*args, **kwargs) | ||
span.set_data("db.affected_rows", res) | ||
|
||
with capture_internal_exceptions(): | ||
add_query_source(span) | ||
tonal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return res | ||
|
||
return _inner | ||
|
||
|
||
SubCursor = TypeVar("SubCursor", bound=Cursor) | ||
|
||
|
||
@contextlib.contextmanager | ||
def _record( | ||
cursor: SubCursor | None, | ||
query: str, | ||
params_list: tuple[Any, ...] | None, | ||
*, | ||
executemany: bool = False, | ||
) -> Iterator[Span]: | ||
integration = sentry_sdk.get_client().get_integration(AioMySQLIntegration) | ||
if integration is not None and not integration._record_params: | ||
params_list = None | ||
|
||
param_style = "pyformat" if params_list else None | ||
|
||
with record_sql_queries( | ||
cursor=cursor, | ||
query=query, | ||
params_list=params_list, | ||
paramstyle=param_style, | ||
executemany=executemany, | ||
record_cursor_repr=cursor is not None, | ||
span_origin=AioMySQLIntegration.origin, | ||
) as span: | ||
yield span | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
def _wrap_connect(f: Callable[..., T]) -> Callable[..., T]: | ||
def _inner(*args: Any, **kwargs: Any) -> T: | ||
if sentry_sdk.get_client().get_integration(AioMySQLIntegration) is None: | ||
return f(*args, **kwargs) | ||
|
||
host = kwargs.get("host", "localhost") | ||
port = kwargs.get("port") or 3306 | ||
user = kwargs.get("user") | ||
db = kwargs.get("db") | ||
|
||
with sentry_sdk.start_span( | ||
op=OP.DB, | ||
name="connect", | ||
origin=AioMySQLIntegration.origin, | ||
) as span: | ||
span.set_data(SPANDATA.DB_SYSTEM, "mysql") | ||
span.set_data(SPANDATA.SERVER_ADDRESS, host) | ||
span.set_data(SPANDATA.SERVER_PORT, port) | ||
span.set_data(SPANDATA.DB_NAME, db) | ||
span.set_data(SPANDATA.DB_USER, user) | ||
|
||
with capture_internal_exceptions(): | ||
sentry_sdk.add_breadcrumb( | ||
message="connect", category="query", data=span._data | ||
) | ||
res = f(*args, **kwargs) | ||
|
||
return res | ||
|
||
return _inner | ||
|
||
|
||
def _set_db_data(span: Span, conn: Any) -> None: | ||
span.set_data(SPANDATA.DB_SYSTEM, "mysql") | ||
|
||
host = conn.host | ||
if host: | ||
span.set_data(SPANDATA.SERVER_ADDRESS, host) | ||
|
||
port = conn.port | ||
if port: | ||
span.set_data(SPANDATA.SERVER_PORT, port) | ||
|
||
database = conn.db | ||
if database: | ||
span.set_data(SPANDATA.DB_NAME, database) | ||
|
||
user = conn.user | ||
if user: | ||
span.set_data(SPANDATA.DB_USER, user) | ||
tonal marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Shared Class Variable Causes Instance Conflicts
The
_record_params
attribute is set as a class variable in__init__
. This causes allAioMySQLIntegration
instances to share the samerecord_params
value, leading to unexpected behavior if different instances are configured.