1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import psycopg
1516import psycopg2
1617from test_psycopg_functional import (
1718 POSTGRES_DB_NAME ,
2122 POSTGRES_USER ,
2223)
2324
25+ from opentelemetry .instrumentation .psycopg import PsycopgInstrumentor
2426from opentelemetry .instrumentation .psycopg2 import Psycopg2Instrumentor
2527from opentelemetry .test .test_base import TestBase
2628
2729
28- class TestFunctionalPsycopg (TestBase ):
30+ class TestFunctionalPsycopg2 (TestBase ):
2931 def setUp (self ):
3032 super ().setUp ()
3133 self ._tracer = self .tracer_provider .get_tracer (__name__ )
@@ -52,3 +54,52 @@ def test_commenter_enabled(self):
5254 self ._cursor .query .decode ("ascii" ),
5355 r"SELECT 1 /\*db_driver='psycopg2(.*)',dbapi_level='\d.\d',dbapi_threadsafety=\d,driver_paramstyle=(.*),libpq_version=\d*,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;" ,
5456 )
57+
58+
59+ class TestFunctionalPsycopg (TestBase ):
60+ def setUp (self ):
61+ super ().setUp ()
62+ self ._tracer = self .tracer_provider .get_tracer (__name__ )
63+ PsycopgInstrumentor ().instrument (enable_commenter = True )
64+
65+ def tearDown (self ):
66+ PsycopgInstrumentor ().uninstrument ()
67+ super ().tearDown ()
68+
69+ def test_commenter_enabled_root_module (self ):
70+ connection = psycopg .connect (
71+ dbname = POSTGRES_DB_NAME ,
72+ user = POSTGRES_USER ,
73+ password = POSTGRES_PASSWORD ,
74+ host = POSTGRES_HOST ,
75+ port = POSTGRES_PORT ,
76+ )
77+ connection .set_session (autocommit = True )
78+ cursor = connection .cursor ()
79+ cursor .execute ("SELECT 1;" )
80+ self .assertRegex (
81+ cursor .query .decode ("ascii" ),
82+ r"SELECT 1 /\*db_driver='psycopg(.*)',dbapi_level='\d.\d',dbapi_threadsafety=\d,driver_paramstyle=(.*),libpq_version=\d*,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;" ,
83+ )
84+
85+ cursor .close ()
86+ connection .close ()
87+
88+ def test_commenter_enabled_not_root_module (self ):
89+ connection = psycopg .Connection .connect (
90+ dbname = POSTGRES_DB_NAME ,
91+ user = POSTGRES_USER ,
92+ password = POSTGRES_PASSWORD ,
93+ host = POSTGRES_HOST ,
94+ port = POSTGRES_PORT ,
95+ )
96+ connection .set_session (autocommit = True )
97+ cursor = connection .cursor ()
98+ cursor .execute ("SELECT 1;" )
99+ self .assertRegex (
100+ cursor .query .decode ("ascii" ),
101+ r"SELECT 1 /\*db_driver='psycopg(.*)',dbapi_level='\d.\d',dbapi_threadsafety=\d,driver_paramstyle=(.*),traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;" ,
102+ )
103+
104+ cursor .close ()
105+ connection .close ()
0 commit comments