Skip to content
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

Extending SQLCommenter support of dbapi into psycopg2 #940

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d6c6a15
Added configuration for sqlcommenter
Thiyagu55 Feb 21, 2022
305d893
Merge branch 'open-telemetry:main' into sqlcommenter-psycopg2-integra…
Thiyagu55 Mar 2, 2022
b902658
Fixed linting errors
Thiyagu55 Mar 2, 2022
cf72cb4
Merge remote-tracking branch 'origin/sqlcommenter-psycopg2-integratio…
Thiyagu55 Mar 2, 2022
e878f0a
Updated CHANGELOG.md
Thiyagu55 Mar 2, 2022
e80e9f9
Merge branch 'main' into sqlcommenter-psycopg2-integration-v2
srikanthccv Mar 4, 2022
bd8f208
Unit test case for sqlcommenter in psycopg2
Thiyagu55 Mar 11, 2022
da3378e
Merge conflict resolved
Thiyagu55 Mar 11, 2022
145e1bf
Merge conflict resolved
Thiyagu55 Mar 11, 2022
3e481d2
Update CHANGELOG.md
Thiyagu55 Mar 11, 2022
b27ec20
psycopg2 sqlcommenter integration test
Thiyagu55 Mar 14, 2022
f0bbeb7
Merge remote-tracking branch 'origin/sqlcommenter-psycopg2-integratio…
Thiyagu55 Mar 14, 2022
47d8180
linting changes
Thiyagu55 Mar 14, 2022
bc6c746
linting changes
Thiyagu55 Mar 14, 2022
e49fcd4
Merge branch 'open-telemetry:main' into sqlcommenter-psycopg2-integra…
Thiyagu55 Mar 14, 2022
7c86ec6
linting changes
Thiyagu55 Mar 14, 2022
db81dce
Merge remote-tracking branch 'origin/sqlcommenter-psycopg2-integratio…
Thiyagu55 Mar 14, 2022
fa62d9d
linting changes
Thiyagu55 Mar 14, 2022
28e332e
linting changes
Thiyagu55 Mar 14, 2022
0a0ea3f
Merge branch 'main' into sqlcommenter-psycopg2-integration-v2
lzchen Mar 14, 2022
36737e4
Resolving merge conflicts
Thiyagu55 Mar 16, 2022
abb1b15
version compatability issue fix
Thiyagu55 Mar 17, 2022
6912bf9
Merge branch 'main' into sqlcommenter-psycopg2-integration-v2
srikanthccv Mar 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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

### Added

- `opentelemetry-instrumentation-psycopg2` extended the sql commenter support of dbapi into psycopg2
([#940](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/940))
- `opentelemetry-instrumentation-flask` Fix non-recording span bug
([#999])(https://github.com/open-telemetry/opentelemetry-python-contrib/pull/999)
- `opentelemetry-instrumentation-tornado` Fix non-recording span bug
Expand All @@ -32,7 +36,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#908](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/908))
- `opentelemetry-instrumentation-requests` make span attribute available to samplers
([#931](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/931))

- `opentelemetry-datadog-exporter` add deprecation note to example.
([#900](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/900))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def _instrument(self, **kwargs):
Psycopg: http://initd.org/psycopg/
"""
tracer_provider = kwargs.get("tracer_provider")

enable_sqlcommenter = kwargs.get("enable_commenter", False)
dbapi.wrap_connect(
__name__,
psycopg2,
Expand All @@ -86,6 +86,7 @@ def _instrument(self, **kwargs):
version=__version__,
tracer_provider=tracer_provider,
db_api_integration_factory=DatabaseApiIntegration,
enable_commenter=enable_sqlcommenter,
)

def _uninstrument(self, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,23 @@ def test_uninstrument_connection_with_instrument_connection(self):

spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)

@mock.patch("opentelemetry.instrumentation.dbapi.wrap_connect")
def test_sqlcommenter_enabled(self, event_mocked):
cnx = psycopg2.connect(database="test")
Psycopg2Instrumentor().instrument(enable_commenter=True)
query = "SELECT * FROM test"
cursor = cnx.cursor()
cursor.execute(query)
kwargs = event_mocked.call_args[1]
self.assertEqual(kwargs["enable_commenter"], True)

@mock.patch("opentelemetry.instrumentation.dbapi.wrap_connect")
def test_sqlcommenter_disabled(self, event_mocked):
cnx = psycopg2.connect(database="test")
Psycopg2Instrumentor().instrument()
query = "SELECT * FROM test"
cursor = cnx.cursor()
cursor.execute(query)
kwargs = event_mocked.call_args[1]
self.assertEqual(kwargs["enable_commenter"], False)
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,28 @@ def test_composed_queries(self):
span.attributes[SpanAttributes.DB_STATEMENT],
'SELECT FROM "users" where "name"=\'"abc"\'',
)

def test_commenter_enabled(self):

stmt = "CREATE TABLE IF NOT EXISTS users (id integer, name varchar)"
with self._tracer.start_as_current_span("rootSpan"):
self._cursor.execute(stmt)
self.validate_spans("CREATE")
Psycopg2Instrumentor().uninstrument()
Psycopg2Instrumentor().instrument(enable_commenter=True)

self._cursor.execute(
sql.SQL("SELECT FROM {table} where {field}='{value}'").format(
table=sql.Identifier("users"),
field=sql.Identifier("name"),
value=sql.Identifier("abc"),
)
)

spans = self.memory_exporter.get_finished_spans()
span = spans[2]
self.assertEqual(span.name, "SELECT")
self.assertEqual(
span.attributes[SpanAttributes.DB_STATEMENT],
'SELECT FROM "users" where "name"=\'"abc"\'',
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright 2020, OpenTelemetry Authors
#
# 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.

import psycopg2
from test_psycopg_functional import (
POSTGRES_DB_NAME,
POSTGRES_HOST,
POSTGRES_PASSWORD,
POSTGRES_PORT,
POSTGRES_USER,
)

from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor
from opentelemetry.test.test_base import TestBase


class TestFunctionalPsycopg(TestBase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls._connection = None
cls._cursor = None
cls._tracer = cls.tracer_provider.get_tracer(__name__)
Psycopg2Instrumentor().instrument(enable_commenter=True)
cls._connection = psycopg2.connect(
dbname=POSTGRES_DB_NAME,
user=POSTGRES_USER,
password=POSTGRES_PASSWORD,
host=POSTGRES_HOST,
port=POSTGRES_PORT,
)
cls._connection.set_session(autocommit=True)
cls._cursor = cls._connection.cursor()

@classmethod
def tearDownClass(cls):
if cls._cursor:
cls._cursor.close()
if cls._connection:
cls._connection.close()
Psycopg2Instrumentor().uninstrument()

def test_commenter_enabled(self):
self._cursor.execute("SELECT 1;")
self.assertRegex(
self._cursor.query.decode("ascii"),
r"SELECT 1; /\*traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/",
)