-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add psycopg2 wrapper Closes #344 * Add psycopg2 metric collection * Adding tests * Use psycopg2-binary * Add pragma for overridden methods * Update tests to assert trace values
- Loading branch information
Showing
6 changed files
with
263 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,65 @@ | ||
import wrapt | ||
|
||
COMMAND_KEYWORDS = { | ||
"create": "table", | ||
"delete": "from", | ||
"insert": "into", | ||
"select": "from", | ||
"update": "update", | ||
} | ||
|
||
|
||
def table_name(query, command): | ||
if command in COMMAND_KEYWORDS: | ||
keyword = COMMAND_KEYWORDS[command] | ||
parts = query.lower().split() | ||
|
||
if keyword in parts: | ||
return parts[parts.index(keyword) + 1] | ||
|
||
|
||
class CursorProxy(wrapt.ObjectProxy): | ||
def __init__(self, cursor, connection_proxy): | ||
super(CursorProxy, self).__init__(cursor) | ||
|
||
self._self_connection = connection_proxy | ||
|
||
@property | ||
def connection_proxy(self): | ||
return self._self_connection | ||
|
||
def execute(self, *args, **kwargs): | ||
self.__wrapped__.execute(*args, **kwargs) | ||
|
||
|
||
class ConnectionProxy(wrapt.ObjectProxy): | ||
def __init__(self, connection, args, kwargs): | ||
super(ConnectionProxy, self).__init__(connection) | ||
|
||
self._self_args = args | ||
self._self_kwargs = kwargs | ||
|
||
def cursor(self, *args, **kwargs): # pragma: no cover | ||
cursor = self.__wrapped__.cursor(*args, **kwargs) | ||
return CursorProxy(cursor, self) | ||
|
||
@property | ||
def extract_hostname(self): # pragma: no cover | ||
return self._self_kwargs.get("host", "localhost") | ||
|
||
@property | ||
def extract_dbname(self): # pragma: no cover | ||
return self._self_kwargs.get("db", self._self_kwargs.get("database", "")) | ||
|
||
|
||
class AdapterProxy(wrapt.ObjectProxy): | ||
def prepare(self, *args, **kwargs): # pragma: no cover | ||
if not args: | ||
return self.__wrapped__.prepare(*args, **kwargs) | ||
|
||
connection = args[0] | ||
|
||
if isinstance(connection, wrapt.ObjectProxy): | ||
connection = connection.__wrapped__ | ||
|
||
return self.__wrapped__.prepare(connection, *args[1:], **kwargs) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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