Skip to content

Commit 28ff20f

Browse files
authored
Merge branch 'main' into pymemcache_instruments_version_bump
2 parents 17c0d48 + e861b93 commit 28ff20f

File tree

5 files changed

+110
-2
lines changed

5 files changed

+110
-2
lines changed

CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.10.0-0.29b0...HEAD)
99

10+
### Added
11+
12+
- `opentelemetry-instrumentation-psycopg2` extended the sql commenter support of dbapi into psycopg2
13+
([#940](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/940))
1014
- `opentelemetry-instrumentation-flask` Fix non-recording span bug
1115
([#999])(https://github.com/open-telemetry/opentelemetry-python-contrib/pull/999)
1216
- `opentelemetry-instrumentation-tornado` Fix non-recording span bug
@@ -32,7 +36,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3236
([#908](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/908))
3337
- `opentelemetry-instrumentation-requests` make span attribute available to samplers
3438
([#931](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/931))
35-
3639
- `opentelemetry-datadog-exporter` add deprecation note to example.
3740
([#900](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/900))
3841

instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def _instrument(self, **kwargs):
7676
Psycopg: http://initd.org/psycopg/
7777
"""
7878
tracer_provider = kwargs.get("tracer_provider")
79-
79+
enable_sqlcommenter = kwargs.get("enable_commenter", False)
8080
dbapi.wrap_connect(
8181
__name__,
8282
psycopg2,
@@ -86,6 +86,7 @@ def _instrument(self, **kwargs):
8686
version=__version__,
8787
tracer_provider=tracer_provider,
8888
db_api_integration_factory=DatabaseApiIntegration,
89+
enable_commenter=enable_sqlcommenter,
8990
)
9091

9192
def _uninstrument(self, **kwargs):

instrumentation/opentelemetry-instrumentation-psycopg2/tests/test_psycopg2_integration.py

+20
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,23 @@ def test_uninstrument_connection_with_instrument_connection(self):
224224

225225
spans_list = self.memory_exporter.get_finished_spans()
226226
self.assertEqual(len(spans_list), 1)
227+
228+
@mock.patch("opentelemetry.instrumentation.dbapi.wrap_connect")
229+
def test_sqlcommenter_enabled(self, event_mocked):
230+
cnx = psycopg2.connect(database="test")
231+
Psycopg2Instrumentor().instrument(enable_commenter=True)
232+
query = "SELECT * FROM test"
233+
cursor = cnx.cursor()
234+
cursor.execute(query)
235+
kwargs = event_mocked.call_args[1]
236+
self.assertEqual(kwargs["enable_commenter"], True)
237+
238+
@mock.patch("opentelemetry.instrumentation.dbapi.wrap_connect")
239+
def test_sqlcommenter_disabled(self, event_mocked):
240+
cnx = psycopg2.connect(database="test")
241+
Psycopg2Instrumentor().instrument()
242+
query = "SELECT * FROM test"
243+
cursor = cnx.cursor()
244+
cursor.execute(query)
245+
kwargs = event_mocked.call_args[1]
246+
self.assertEqual(kwargs["enable_commenter"], False)

tests/opentelemetry-docker-tests/tests/postgres/test_psycopg_functional.py

+25
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,28 @@ def test_composed_queries(self):
155155
span.attributes[SpanAttributes.DB_STATEMENT],
156156
'SELECT FROM "users" where "name"=\'"abc"\'',
157157
)
158+
159+
def test_commenter_enabled(self):
160+
161+
stmt = "CREATE TABLE IF NOT EXISTS users (id integer, name varchar)"
162+
with self._tracer.start_as_current_span("rootSpan"):
163+
self._cursor.execute(stmt)
164+
self.validate_spans("CREATE")
165+
Psycopg2Instrumentor().uninstrument()
166+
Psycopg2Instrumentor().instrument(enable_commenter=True)
167+
168+
self._cursor.execute(
169+
sql.SQL("SELECT FROM {table} where {field}='{value}'").format(
170+
table=sql.Identifier("users"),
171+
field=sql.Identifier("name"),
172+
value=sql.Identifier("abc"),
173+
)
174+
)
175+
176+
spans = self.memory_exporter.get_finished_spans()
177+
span = spans[2]
178+
self.assertEqual(span.name, "SELECT")
179+
self.assertEqual(
180+
span.attributes[SpanAttributes.DB_STATEMENT],
181+
'SELECT FROM "users" where "name"=\'"abc"\'',
182+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2020, OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import psycopg2
16+
from test_psycopg_functional import (
17+
POSTGRES_DB_NAME,
18+
POSTGRES_HOST,
19+
POSTGRES_PASSWORD,
20+
POSTGRES_PORT,
21+
POSTGRES_USER,
22+
)
23+
24+
from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor
25+
from opentelemetry.test.test_base import TestBase
26+
27+
28+
class TestFunctionalPsycopg(TestBase):
29+
@classmethod
30+
def setUpClass(cls):
31+
super().setUpClass()
32+
cls._connection = None
33+
cls._cursor = None
34+
cls._tracer = cls.tracer_provider.get_tracer(__name__)
35+
Psycopg2Instrumentor().instrument(enable_commenter=True)
36+
cls._connection = psycopg2.connect(
37+
dbname=POSTGRES_DB_NAME,
38+
user=POSTGRES_USER,
39+
password=POSTGRES_PASSWORD,
40+
host=POSTGRES_HOST,
41+
port=POSTGRES_PORT,
42+
)
43+
cls._connection.set_session(autocommit=True)
44+
cls._cursor = cls._connection.cursor()
45+
46+
@classmethod
47+
def tearDownClass(cls):
48+
if cls._cursor:
49+
cls._cursor.close()
50+
if cls._connection:
51+
cls._connection.close()
52+
Psycopg2Instrumentor().uninstrument()
53+
54+
def test_commenter_enabled(self):
55+
self._cursor.execute("SELECT 1;")
56+
self.assertRegex(
57+
self._cursor.query.decode("ascii"),
58+
r"SELECT 1; /\*traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/",
59+
)

0 commit comments

Comments
 (0)