-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- psycopg2 - version :1.2.1
- Loading branch information
Showing
12 changed files
with
267 additions
and
14 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,100 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: UTF-8 -*- | ||
|
||
# ------------------------------------------------------------------------------ | ||
# Copyright 2024. NAVER Corp. - | ||
# - | ||
# Licensed under the Apache License, Version 2.0 (the "License"); - | ||
# you may not use this file except in compliance with the License. - | ||
# You may obtain a copy of the License at - | ||
# - | ||
# http://www.apache.org/licenses/LICENSE-2.0 - | ||
# - | ||
# Unless required by applicable law or agreed to in writing, software - | ||
# distributed under the License is distributed on an "AS IS" BASIS, - | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - | ||
# See the License for the specific language governing permissions and - | ||
# limitations under the License. - | ||
# ------------------------------------------------------------------------------ | ||
|
||
from pinpointPy import Common, pinpoint, Defines | ||
from functools import wraps | ||
import psycopg2 | ||
|
||
|
||
class QueryPlugin(Common.PinTrace): | ||
|
||
def __init__(self, name): | ||
super().__init__(name) | ||
|
||
def onBefore(self, parentId, *args, **kwargs): | ||
trace_id, _, _ = super().onBefore(parentId, *args, **kwargs) | ||
############################################################### | ||
pinpoint.add_trace_header( | ||
Defines.PP_INTERCEPTOR_NAME, self.getUniqueName(), trace_id) | ||
pinpoint.add_trace_header( | ||
Defines.PP_SERVER_TYPE, Defines.PP_POSTGRESQL, trace_id) | ||
query = str(args[1]) | ||
pinpoint.add_trace_header(Defines.PP_SQL_FORMAT, query, trace_id) | ||
############################################################### | ||
cursor = args[0] | ||
dst = cursor.connection.get_dsn_parameters()['host'] | ||
pinpoint.add_trace_header( | ||
Defines.PP_DESTINATION, dst, trace_id=trace_id) | ||
return trace_id, args, kwargs | ||
|
||
def onEnd(self, trace_id, ret): | ||
super().onEnd(trace_id, ret) | ||
return ret | ||
|
||
def onException(self, trace_id, e): | ||
pinpoint.add_trace_header(Defines.PP_ADD_EXCEPTION, str(e), trace_id) | ||
|
||
|
||
class FetchPlugin(QueryPlugin): | ||
def onBefore(self, parentId, *args, **kwargs: None): | ||
trace_id, _, _ = super(QueryPlugin, self).onBefore( | ||
parentId, *args, **kwargs) | ||
pinpoint.add_trace_header( | ||
Defines.PP_INTERCEPTOR_NAME, self.getUniqueName(), trace_id) | ||
return trace_id, args, kwargs | ||
|
||
|
||
class LoggingCursor(psycopg2.extensions.cursor): | ||
@QueryPlugin("execute") | ||
def execute(self, sql, args=None): | ||
return super().execute(sql, args) | ||
|
||
@QueryPlugin("executemany") | ||
def executemany(self, query, args=None): | ||
return super().executemany(query, args) | ||
|
||
@QueryPlugin("mogrify") | ||
def mogrify(self, query, args=None): | ||
return super().mogrify(query, args) | ||
|
||
@FetchPlugin("fetchall") | ||
def fetchall(self): | ||
return super().fetchall() | ||
|
||
@FetchPlugin("fetchmany") | ||
def fetchmany(self, size: None): | ||
return super().fetchmany(size) | ||
|
||
@FetchPlugin("fetchone") | ||
def fetchone(self): | ||
return super().fetchone() | ||
|
||
|
||
class ConnectionPlugin: | ||
def __init__(self, name): | ||
pass | ||
|
||
def __call__(self, func): | ||
|
||
@wraps(func) | ||
def pinpointTrace(*args, **kwargs): | ||
kwargs['cursor_factory'] = LoggingCursor | ||
ret = func(*args, **kwargs) | ||
return ret | ||
return pinpointTrace |
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,41 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: UTF-8 -*- | ||
|
||
# ------------------------------------------------------------------------------ | ||
# Copyright 2024. NAVER Corp. - | ||
# - | ||
# Licensed under the Apache License, Version 2.0 (the "License"); - | ||
# you may not use this file except in compliance with the License. - | ||
# You may obtain a copy of the License at - | ||
# - | ||
# http://www.apache.org/licenses/LICENSE-2.0 - | ||
# - | ||
# Unless required by applicable law or agreed to in writing, software - | ||
# distributed under the License is distributed on an "AS IS" BASIS, - | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - | ||
# See the License for the specific language governing permissions and - | ||
# limitations under the License. - | ||
# ------------------------------------------------------------------------------ | ||
from pinpointPy.Interceptor import Interceptor, intercept_once | ||
from pinpointPy import get_logger | ||
from .PsycopgPlugins import ConnectionPlugin | ||
|
||
|
||
@intercept_once | ||
def monkey_patch(): | ||
|
||
try: | ||
import psycopg2 | ||
Interceptors = [ | ||
Interceptor(psycopg2, 'connect', ConnectionPlugin), | ||
] | ||
for interceptor in Interceptors: | ||
interceptor.enable() | ||
except ImportError as e: | ||
get_logger().info(f'exception at {e}') | ||
|
||
|
||
__all__ = ['monkey_patch'] | ||
|
||
__version__ = '0.0.1' | ||
__author__ = 'liu.mingyi@navercorp.com' |
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,66 @@ | ||
import psycopg2.extensions | ||
import logging | ||
from pinpointPy.libs._psycopg2 import monkey_patch | ||
import unittest | ||
from pinpointPy import PinTransaction | ||
from pinpointPy.tests import TestCase, GenTestHeader | ||
|
||
|
||
class Test_Case(TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
monkey_patch() | ||
|
||
@PinTransaction("testcase", GenTestHeader()) | ||
def test_case(self): | ||
# reference from https://www.psycopg.org/docs/usage.html | ||
import psycopg2 | ||
conn = psycopg2.connect( | ||
dbname="test", user="test", password="pinpoint", host="postgres", port=5432) | ||
cur = conn.cursor() | ||
cur.execute( | ||
"CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);") | ||
cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)", | ||
(100, "abc'def")) | ||
cur.execute("SELECT * FROM test;") | ||
resp = cur.fetchone() | ||
conn.commit() | ||
cur.close() | ||
conn.close() | ||
|
||
# import psycopg2 | ||
|
||
# class LoggingCursor(psycopg2.extensions.cursor): | ||
# def execute(self, sql, args=None): | ||
# logger = logging.getLogger('sql_debug') | ||
# logger.info(self.mogrify(sql, args)) | ||
|
||
# try: | ||
# print(f"start sql: {sql} {self.connection}") | ||
# psycopg2.extensions.cursor.execute(self, sql, args) | ||
# print(f"end sql: {sql}") | ||
# except Exception as exc: | ||
# logger.error("%s: %s" % (exc.__class__.__name__, exc)) | ||
# raise | ||
|
||
# conn = psycopg2.connect( | ||
# dbname="test", user="test", password="pinpoint", host="10.34.130.156", port=5432, cursor_factory=LoggingCursor) | ||
# cur = conn.cursor() | ||
# # cur.execute("INSERT INTO mytable VALUES (%s, %s, %s);", | ||
# # (10, 20, 30)) | ||
# cur.execute("DROP TABLE test") | ||
# cur.execute( | ||
# "CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);") | ||
# cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)", | ||
# (100, "abc'def")) | ||
# cur.execute("SELECT * FROM test;") | ||
# resp = cur.fetchone() | ||
# conn.commit() | ||
# cur.close() | ||
# conn.close() | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
Django==4.1.13 | ||
mysqlclient | ||
requests | ||
requests | ||
psycopg2-binary |
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 @@ | ||
CREATE DATABASE test; |