From c45dec7ad578b7fed317a0a97f2c55efca897931 Mon Sep 17 00:00:00 2001 From: Thiyagu55 Date: Mon, 1 Aug 2022 16:46:17 +0530 Subject: [PATCH 01/18] Added flask support in dbapi --- .../instrumentation/dbapi/__init__.py | 7 ++++++ .../instrumentation/flask/__init__.py | 22 +++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py b/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py index 1afeb5499c..52b35f9459 100644 --- a/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py @@ -52,6 +52,7 @@ ) from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import SpanKind, TracerProvider, get_tracer +from opentelemetry import context as opentelemetry_context _logger = logging.getLogger(__name__) @@ -430,6 +431,12 @@ def traced_execution( ): commenter_data.update(**_get_opentelemetry_values()) + # Add flask related tags + sqlcommenter_flask_values = opentelemetry_context.get_value( + 'SQLCOMMENTER_FLASK_VALUES') if opentelemetry_context.get_value( + 'SQLCOMMENTER_FLASK_VALUES') else {} + commenter_data.update(**sqlcommenter_flask_values) + # Filter down to just the requested attributes. commenter_data = { k: v diff --git a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py index cca8743556..6441c11c06 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py @@ -229,8 +229,7 @@ def _start_response(status, response_headers, *args, **kwargs): def _wrapped_before_request( - request_hook=None, tracer=None, excluded_urls=None -): + request_hook=None, tracer=None, excluded_urls=None, enable_commenter=True, commenter_options=None): def _before_request(): if excluded_urls and excluded_urls.url_disabled(flask.request.url): return @@ -275,6 +274,21 @@ def _before_request(): flask_request_environ[_ENVIRON_SPAN_KEY] = span flask_request_environ[_ENVIRON_TOKEN] = token + if enable_commenter: + current_context = context.get_current() + flask_info = {} + + # https://flask.palletsprojects.com/en/1.1.x/api/#flask.has_request_context + if flask and flask.request: + if commenter_options.get('framework', True): + flask_info['framework'] = 'flask:%s' % flask.__version__ + if commenter_options.get('controller', True) and flask.request.endpoint: + flask_info['controller'] = flask.request.endpoint + if commenter_options.get('route', True) and flask.request.url_rule and flask.request.url_rule.rule: + flask_info['route'] = flask.request.url_rule.rule + sqlcommenter_context = context.set_value("SQLCOMMENTER_FLASK_VALUES", flask_info, current_context) + context.attach(sqlcommenter_context) + return _before_request @@ -379,6 +393,8 @@ def instrument_app( response_hook=None, tracer_provider=None, excluded_urls=None, + enable_commenter=True, + commenter_options=None, ): if not hasattr(app, "_is_instrumented_by_opentelemetry"): app._is_instrumented_by_opentelemetry = False @@ -400,6 +416,8 @@ def instrument_app( request_hook, tracer, excluded_urls=excluded_urls, + enable_commenter=enable_commenter, + commenter_options=commenter_options if commenter_options else {}, ) app._before_request = _before_request app.before_request(_before_request) From 6c543a9e0eec8ef08ae20efdb80deed6ff11a4c7 Mon Sep 17 00:00:00 2001 From: Thiyagu55 Date: Wed, 3 Aug 2022 10:43:34 +0530 Subject: [PATCH 02/18] Added test cases for flask --- .../instrumentation/flask/__init__.py | 56 +++++++++++++++ .../tests/base_test.py | 9 +++ .../tests/test_sqlcommenter.py | 72 +++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 instrumentation/opentelemetry-instrumentation-flask/tests/test_sqlcommenter.py diff --git a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py index 6441c11c06..792a94b9d8 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py @@ -24,6 +24,53 @@ * The ``http.route`` Span attribute is set so that one can see which URL rule matched a request. +SQLCOMMENTER +***************************************** +You can optionally configure Flask instrumentation to enable sqlcommenter which enriches +the query with contextual information. + +Usage +----- + +.. code:: python + + from opentelemetry.instrumentation.flask import FlaskInstrumentor + + FlaskInstrumentor().instrument(enable_commenter=True, commenter_options={}) + + +For example, +:: + + FlaskInstrumentor when used with SQLAlchemyInstrumentor or Psycopg2Instrumentor, invoking cursor.execute("select * from auth_users") + will lead to sql query "select * from auth_users" but when SQLCommenter is enabled + the query will get appended with some configurable tags like "select * from auth_users /*metrics=value*/;" + + Inorder for the commenter to append flask related tags to sql queries, the commenter needs to enabled on + the respective SQLAlchemyInstrumentor or Psycopg2Instrumentor framework too. + +SQLCommenter Configurations +*************************** +We can configure the tags to be appended to the sqlquery log by adding configuration inside commenter_options(default:{}) keyword + +framework = True(Default) or False + +For example, +:: +Enabling this flag will add flask and it's version which is /*flask%%3A2.9.3*/ + +route = True(Default) or False + +For example, +:: +Enabling this flag will add route uri /*route='/home'*/ + +controller = True(Default) or False + +For example, +:: +Enabling this flag will add controller name /*controller='home_view'*/ + Usage ----- @@ -324,6 +371,8 @@ class _InstrumentedFlask(flask.Flask): _tracer_provider = None _request_hook = None _response_hook = None + _enable_commenter = True + _commenter_options = None def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -345,6 +394,8 @@ def __init__(self, *args, **kwargs): _InstrumentedFlask._request_hook, tracer, excluded_urls=_InstrumentedFlask._excluded_urls, + enable_commenter=_InstrumentedFlask._enable_commenter, + commenter_options=_InstrumentedFlask._commenter_options, ) self._before_request = _before_request self.before_request(_before_request) @@ -381,6 +432,11 @@ def _instrument(self, **kwargs): if excluded_urls is None else parse_excluded_urls(excluded_urls) ) + enable_commenter = kwargs.get("enable_commenter", True) + _InstrumentedFlask._enable_commenter = enable_commenter + + commenter_options = kwargs.get("commenter_options", {}) + _InstrumentedFlask._commenter_options = commenter_options flask.Flask = _InstrumentedFlask def _uninstrument(self, **kwargs): diff --git a/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py b/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py index 79b1edf6ab..0904d8fcc0 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py +++ b/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py @@ -15,6 +15,7 @@ import flask from werkzeug.test import Client from werkzeug.wrappers import Response +from opentelemetry import context class InstrumentationTest: @@ -24,6 +25,13 @@ def _hello_endpoint(helloid): raise ValueError(":-(") return "Hello: " + str(helloid) + @staticmethod + def _sqlcommenter_endpoint(): + current_context = context.get_current() + sqlcommenter_flask_values = current_context.get( + 'SQLCOMMENTER_FLASK_VALUES', {}) + return sqlcommenter_flask_values + @staticmethod def _custom_response_headers(): resp = flask.Response("test response") @@ -43,6 +51,7 @@ def excluded2_endpoint(): # pylint: disable=no-member self.app.route("/hello/")(self._hello_endpoint) + self.app.route("/sqlcommenter")(self._sqlcommenter_endpoint) self.app.route("/excluded/")(self._hello_endpoint) self.app.route("/excluded")(excluded_endpoint) self.app.route("/excluded2")(excluded2_endpoint) diff --git a/instrumentation/opentelemetry-instrumentation-flask/tests/test_sqlcommenter.py b/instrumentation/opentelemetry-instrumentation-flask/tests/test_sqlcommenter.py new file mode 100644 index 0000000000..075e0d0d28 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-flask/tests/test_sqlcommenter.py @@ -0,0 +1,72 @@ +# Copyright The 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 flask +from werkzeug.test import Client +from werkzeug.wrappers import Response + +from opentelemetry.instrumentation.flask import FlaskInstrumentor +from opentelemetry.test.wsgitestutil import WsgiTestBase + +# pylint: disable=import-error +from .base_test import InstrumentationTest + + +class TestSQLCommenter(InstrumentationTest, WsgiTestBase): + def setUp(self): + super().setUp() + FlaskInstrumentor().instrument() + self.app = flask.Flask(__name__) + self._common_initialization() + + def tearDown(self): + super().tearDown() + with self.disable_logging(): + FlaskInstrumentor().uninstrument() + + def test_sqlcommenter_enabled_default(self): + + self.app = flask.Flask(__name__) + self.app.route("/sqlcommenter")(self._sqlcommenter_endpoint) + client = Client(self.app, Response) + + resp = client.get("/sqlcommenter") + self.assertEqual(200, resp.status_code) + self.assertRegex(list(resp.response)[0].strip(), + b"{\"controller\":\"_sqlcommenter_endpoint\",\"framework\":\"flask:(.*)\",\"route\":\"\/sqlcommenter\"}") + + def test_sqlcommenter_enabled_with_configurations(self): + FlaskInstrumentor().uninstrument() + FlaskInstrumentor().instrument(enable_commenter=True, commenter_options={"route": False}) + + self.app = flask.Flask(__name__) + self.app.route("/sqlcommenter")(self._sqlcommenter_endpoint) + client = Client(self.app, Response) + + resp = client.get("/sqlcommenter") + self.assertEqual(200, resp.status_code) + self.assertRegex(list(resp.response)[0].strip(), + b"{\"controller\":\"_sqlcommenter_endpoint\",\"framework\":\"flask:(.*)\"}") + + def test_sqlcommenter_disabled(self): + FlaskInstrumentor().uninstrument() + FlaskInstrumentor().instrument(enable_commenter=False) + + self.app = flask.Flask(__name__) + self.app.route("/sqlcommenter")(self._sqlcommenter_endpoint) + client = Client(self.app, Response) + + resp = client.get("/sqlcommenter") + self.assertEqual(200, resp.status_code) + self.assertEqual(list(resp.response)[0].strip(), b"{}") From 21de4913b3e6970aa29c6bcb75eb36cfcdce07a8 Mon Sep 17 00:00:00 2001 From: Thiyagu55 Date: Thu, 4 Aug 2022 12:09:54 +0530 Subject: [PATCH 03/18] Test case for dbapi --- .../tests/test_dbapi_integration.py | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py b/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py index af2cbeea15..ff197aef7b 100644 --- a/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py +++ b/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py @@ -21,7 +21,7 @@ from opentelemetry.sdk import resources from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.test.test_base import TestBase - +from opentelemetry import context class TestDBApiIntegration(TestBase): def setUp(self): @@ -254,6 +254,36 @@ def test_executemany_comment(self): r"Select 1 /\*dbapi_threadsafety=123,driver_paramstyle='test',libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;", ) + def test_executemany_flask_integration_comment(self): + + connect_module = mock.MagicMock() + connect_module.__version__ = mock.MagicMock() + connect_module.__libpq_version__ = 123 + connect_module.apilevel = 123 + connect_module.threadsafety = 123 + connect_module.paramstyle = "test" + + db_integration = dbapi.DatabaseApiIntegration( + "testname", + "testcomponent", + enable_commenter=True, + commenter_options={"db_driver": False, "dbapi_level": False}, + connect_module=connect_module, + ) + current_context = context.get_current() + sqlcommenter_context = context.set_value("SQLCOMMENTER_FLASK_VALUES", {"flask": 1}, current_context) + context.attach(sqlcommenter_context) + + mock_connection = db_integration.wrapped_connection( + mock_connect, {}, {} + ) + cursor = mock_connection.cursor() + cursor.executemany("Select 1;") + self.assertRegex( + cursor.query, + r"Select 1 /\*dbapi_threadsafety=123,driver_paramstyle='test',flask=1,libpq_version=123,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;", + ) + def test_callproc(self): db_integration = dbapi.DatabaseApiIntegration( "testname", "testcomponent" From 108e61855cebd3b55abc71d591abeb93f2c4de66 Mon Sep 17 00:00:00 2001 From: Thiyagu55 Date: Thu, 4 Aug 2022 13:28:39 +0530 Subject: [PATCH 04/18] linting changes --- .../instrumentation/dbapi/__init__.py | 12 +++++-- .../tests/test_dbapi_integration.py | 5 ++- .../instrumentation/flask/__init__.py | 34 ++++++++++++++----- .../tests/base_test.py | 3 +- .../tests/test_sqlcommenter.py | 16 ++++++--- 5 files changed, 51 insertions(+), 19 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py b/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py index 52b35f9459..6c6777b212 100644 --- a/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py @@ -432,9 +432,15 @@ def traced_execution( commenter_data.update(**_get_opentelemetry_values()) # Add flask related tags - sqlcommenter_flask_values = opentelemetry_context.get_value( - 'SQLCOMMENTER_FLASK_VALUES') if opentelemetry_context.get_value( - 'SQLCOMMENTER_FLASK_VALUES') else {} + sqlcommenter_flask_values = ( + opentelemetry_context.get_value( + "SQLCOMMENTER_FLASK_VALUES" + ) + if opentelemetry_context.get_value( + "SQLCOMMENTER_FLASK_VALUES" + ) + else {} + ) commenter_data.update(**sqlcommenter_flask_values) # Filter down to just the requested attributes. diff --git a/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py b/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py index ff197aef7b..96a5b56d74 100644 --- a/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py +++ b/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py @@ -23,6 +23,7 @@ from opentelemetry.test.test_base import TestBase from opentelemetry import context + class TestDBApiIntegration(TestBase): def setUp(self): super().setUp() @@ -271,7 +272,9 @@ def test_executemany_flask_integration_comment(self): connect_module=connect_module, ) current_context = context.get_current() - sqlcommenter_context = context.set_value("SQLCOMMENTER_FLASK_VALUES", {"flask": 1}, current_context) + sqlcommenter_context = context.set_value( + "SQLCOMMENTER_FLASK_VALUES", {"flask": 1}, current_context + ) context.attach(sqlcommenter_context) mock_connection = db_integration.wrapped_connection( diff --git a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py index 792a94b9d8..d0166c2126 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py @@ -276,7 +276,12 @@ def _start_response(status, response_headers, *args, **kwargs): def _wrapped_before_request( - request_hook=None, tracer=None, excluded_urls=None, enable_commenter=True, commenter_options=None): + request_hook=None, + tracer=None, + excluded_urls=None, + enable_commenter=True, + commenter_options=None, +): def _before_request(): if excluded_urls and excluded_urls.url_disabled(flask.request.url): return @@ -327,13 +332,22 @@ def _before_request(): # https://flask.palletsprojects.com/en/1.1.x/api/#flask.has_request_context if flask and flask.request: - if commenter_options.get('framework', True): - flask_info['framework'] = 'flask:%s' % flask.__version__ - if commenter_options.get('controller', True) and flask.request.endpoint: - flask_info['controller'] = flask.request.endpoint - if commenter_options.get('route', True) and flask.request.url_rule and flask.request.url_rule.rule: - flask_info['route'] = flask.request.url_rule.rule - sqlcommenter_context = context.set_value("SQLCOMMENTER_FLASK_VALUES", flask_info, current_context) + if commenter_options.get("framework", True): + flask_info["framework"] = "flask:%s" % flask.__version__ + if ( + commenter_options.get("controller", True) + and flask.request.endpoint + ): + flask_info["controller"] = flask.request.endpoint + if ( + commenter_options.get("route", True) + and flask.request.url_rule + and flask.request.url_rule.rule + ): + flask_info["route"] = flask.request.url_rule.rule + sqlcommenter_context = context.set_value( + "SQLCOMMENTER_FLASK_VALUES", flask_info, current_context + ) context.attach(sqlcommenter_context) return _before_request @@ -473,7 +487,9 @@ def instrument_app( tracer, excluded_urls=excluded_urls, enable_commenter=enable_commenter, - commenter_options=commenter_options if commenter_options else {}, + commenter_options=commenter_options + if commenter_options + else {}, ) app._before_request = _before_request app.before_request(_before_request) diff --git a/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py b/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py index 0904d8fcc0..6056027a6f 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py +++ b/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py @@ -29,7 +29,8 @@ def _hello_endpoint(helloid): def _sqlcommenter_endpoint(): current_context = context.get_current() sqlcommenter_flask_values = current_context.get( - 'SQLCOMMENTER_FLASK_VALUES', {}) + "SQLCOMMENTER_FLASK_VALUES", {} + ) return sqlcommenter_flask_values @staticmethod diff --git a/instrumentation/opentelemetry-instrumentation-flask/tests/test_sqlcommenter.py b/instrumentation/opentelemetry-instrumentation-flask/tests/test_sqlcommenter.py index 075e0d0d28..7249bfffc5 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/tests/test_sqlcommenter.py +++ b/instrumentation/opentelemetry-instrumentation-flask/tests/test_sqlcommenter.py @@ -43,12 +43,16 @@ def test_sqlcommenter_enabled_default(self): resp = client.get("/sqlcommenter") self.assertEqual(200, resp.status_code) - self.assertRegex(list(resp.response)[0].strip(), - b"{\"controller\":\"_sqlcommenter_endpoint\",\"framework\":\"flask:(.*)\",\"route\":\"\/sqlcommenter\"}") + self.assertRegex( + list(resp.response)[0].strip(), + b'{"controller":"_sqlcommenter_endpoint","framework":"flask:(.*)","route":"\/sqlcommenter"}', + ) def test_sqlcommenter_enabled_with_configurations(self): FlaskInstrumentor().uninstrument() - FlaskInstrumentor().instrument(enable_commenter=True, commenter_options={"route": False}) + FlaskInstrumentor().instrument( + enable_commenter=True, commenter_options={"route": False} + ) self.app = flask.Flask(__name__) self.app.route("/sqlcommenter")(self._sqlcommenter_endpoint) @@ -56,8 +60,10 @@ def test_sqlcommenter_enabled_with_configurations(self): resp = client.get("/sqlcommenter") self.assertEqual(200, resp.status_code) - self.assertRegex(list(resp.response)[0].strip(), - b"{\"controller\":\"_sqlcommenter_endpoint\",\"framework\":\"flask:(.*)\"}") + self.assertRegex( + list(resp.response)[0].strip(), + b'{"controller":"_sqlcommenter_endpoint","framework":"flask:(.*)"}', + ) def test_sqlcommenter_disabled(self): FlaskInstrumentor().uninstrument() From 125cba086656f30848f6c268e001ecbe39c443bd Mon Sep 17 00:00:00 2001 From: Thiyagu55 Date: Tue, 9 Aug 2022 12:09:58 +0530 Subject: [PATCH 05/18] Integrating in sqlalchemy --- .../instrumentation/sqlalchemy/engine.py | 13 +++++++++++ .../tests/test_sqlcommenter.py | 23 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py b/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py index 7441c0aa03..6183b40396 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py +++ b/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py @@ -26,6 +26,7 @@ ) from opentelemetry.semconv.trace import NetTransportValues, SpanAttributes from opentelemetry.trace.status import Status, StatusCode +from opentelemetry import context as opentelemetry_context def _normalize_vendor(vendor): @@ -160,6 +161,18 @@ def _before_cur_exec( if self.commenter_options.get(k, True) } + # Add flask related tags + sqlcommenter_flask_values = ( + opentelemetry_context.get_value( + "SQLCOMMENTER_FLASK_VALUES" + ) + if opentelemetry_context.get_value( + "SQLCOMMENTER_FLASK_VALUES" + ) + else {} + ) + commenter_data.update(**sqlcommenter_flask_values) + statement = _add_sql_comment(statement, **commenter_data) context._otel_span = span diff --git a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlcommenter.py b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlcommenter.py index 616388f5e5..832633c267 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlcommenter.py +++ b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlcommenter.py @@ -18,6 +18,7 @@ from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor from opentelemetry.test.test_base import TestBase +from opentelemetry import context class TestSqlalchemyInstrumentationWithSQLCommenter(TestBase): @@ -54,3 +55,25 @@ def test_sqlcommenter_enabled(self): self.caplog.records[-2].getMessage(), r"SELECT 1 /\*db_driver='(.*)',traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;", ) + + def test_sqlcommenter_flask_integration(self): + engine = create_engine("sqlite:///:memory:") + SQLAlchemyInstrumentor().instrument( + engine=engine, + tracer_provider=self.tracer_provider, + enable_commenter=True, + commenter_options={"db_framework": False}, + ) + cnx = engine.connect() + + current_context = context.get_current() + sqlcommenter_context = context.set_value( + "SQLCOMMENTER_FLASK_VALUES", {"flask": 1}, current_context + ) + context.attach(sqlcommenter_context) + + cnx.execute("SELECT 1;").fetchall() + self.assertRegex( + self.caplog.records[-2].getMessage(), + r"SELECT 1 /\*db_driver='(.*)',flask=1,traceparent='\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\d{1,2}'\*/;", + ) From 63ed9d98f3807bdb96ce1cad3e4da554df3443bc Mon Sep 17 00:00:00 2001 From: Thiyagu55 Date: Tue, 9 Aug 2022 12:26:20 +0530 Subject: [PATCH 06/18] Added PR in CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0197944f9..b5666f0bc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#1206](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1206)) - Add psycopg2 native tags to sqlcommenter ([#1203](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1203)) +- Flask sqlalchemy psycopg2 integration + ([#1224](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1224)) ### Added - `opentelemetry-instrumentation-redis` add support to instrument RedisCluster clients From 9e7058d5128b891a27677bad6a39fcf09f06f322 Mon Sep 17 00:00:00 2001 From: Thiyagu55 Date: Tue, 9 Aug 2022 13:03:55 +0530 Subject: [PATCH 07/18] Linting changes --- .../src/opentelemetry/instrumentation/dbapi/__init__.py | 2 +- .../tests/test_dbapi_integration.py | 2 +- .../opentelemetry-instrumentation-flask/tests/base_test.py | 1 + .../src/opentelemetry/instrumentation/sqlalchemy/engine.py | 2 +- .../tests/test_sqlcommenter.py | 2 +- 5 files changed, 5 insertions(+), 4 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py b/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py index 6c6777b212..20b78d61e9 100644 --- a/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py @@ -43,6 +43,7 @@ import wrapt +from opentelemetry import context as opentelemetry_context from opentelemetry import trace as trace_api from opentelemetry.instrumentation.dbapi.version import __version__ from opentelemetry.instrumentation.utils import ( @@ -52,7 +53,6 @@ ) from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import SpanKind, TracerProvider, get_tracer -from opentelemetry import context as opentelemetry_context _logger = logging.getLogger(__name__) diff --git a/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py b/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py index 96a5b56d74..da10ad0354 100644 --- a/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py +++ b/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py @@ -16,12 +16,12 @@ import logging from unittest import mock +from opentelemetry import context from opentelemetry import trace as trace_api from opentelemetry.instrumentation import dbapi from opentelemetry.sdk import resources from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.test.test_base import TestBase -from opentelemetry import context class TestDBApiIntegration(TestBase): diff --git a/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py b/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py index 6056027a6f..5a942960ee 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py +++ b/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py @@ -15,6 +15,7 @@ import flask from werkzeug.test import Client from werkzeug.wrappers import Response + from opentelemetry import context diff --git a/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py b/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py index 6183b40396..f4afca3d92 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py +++ b/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py @@ -15,6 +15,7 @@ from sqlalchemy.event import listen # pylint: disable=no-name-in-module +from opentelemetry import context as opentelemetry_context from opentelemetry import trace from opentelemetry.instrumentation.sqlalchemy.package import ( _instrumenting_module_name, @@ -26,7 +27,6 @@ ) from opentelemetry.semconv.trace import NetTransportValues, SpanAttributes from opentelemetry.trace.status import Status, StatusCode -from opentelemetry import context as opentelemetry_context def _normalize_vendor(vendor): diff --git a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlcommenter.py b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlcommenter.py index 832633c267..6455ff7be0 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlcommenter.py +++ b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlcommenter.py @@ -16,9 +16,9 @@ import pytest from sqlalchemy import create_engine +from opentelemetry import context from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor from opentelemetry.test.test_base import TestBase -from opentelemetry import context class TestSqlalchemyInstrumentationWithSQLCommenter(TestBase): From 18a82f787c18ac142200dd7e77bfa9011b7a4e93 Mon Sep 17 00:00:00 2001 From: Thiyagu55 Date: Tue, 9 Aug 2022 13:53:36 +0530 Subject: [PATCH 08/18] Linting changes --- .../tests/test_sqlcommenter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-flask/tests/test_sqlcommenter.py b/instrumentation/opentelemetry-instrumentation-flask/tests/test_sqlcommenter.py index 7249bfffc5..e7ad5b63a6 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/tests/test_sqlcommenter.py +++ b/instrumentation/opentelemetry-instrumentation-flask/tests/test_sqlcommenter.py @@ -45,7 +45,7 @@ def test_sqlcommenter_enabled_default(self): self.assertEqual(200, resp.status_code) self.assertRegex( list(resp.response)[0].strip(), - b'{"controller":"_sqlcommenter_endpoint","framework":"flask:(.*)","route":"\/sqlcommenter"}', + b'{"controller":"_sqlcommenter_endpoint","framework":"flask:(.*)","route":"/sqlcommenter"}', ) def test_sqlcommenter_enabled_with_configurations(self): From 1c4f841153a5c6653924e50cbeafaa820a4e50aa Mon Sep 17 00:00:00 2001 From: Thiyagu55 Date: Tue, 9 Aug 2022 14:17:47 +0530 Subject: [PATCH 09/18] Linting changes --- .../src/opentelemetry/instrumentation/flask/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py index b44ce4902a..61f07f51c1 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py @@ -356,7 +356,7 @@ def _before_request(): # https://flask.palletsprojects.com/en/1.1.x/api/#flask.has_request_context if flask and flask.request: if commenter_options.get("framework", True): - flask_info["framework"] = "flask:%s" % flask.__version__ + flask_info["framework"] = f"flask:{flask.__version__}" if ( commenter_options.get("controller", True) and flask.request.endpoint From 4ebf055c24bc02c29e1d8feb45914a6843d82876 Mon Sep 17 00:00:00 2001 From: Thiyagu55 Date: Fri, 26 Aug 2022 12:38:17 +0530 Subject: [PATCH 10/18] PR changes --- .../instrumentation/dbapi/__init__.py | 15 +----- .../tests/test_dbapi_integration.py | 2 +- .../middleware/sqlcommenter_middleware.py | 2 +- .../instrumentation/flask/__init__.py | 2 +- .../tests/base_test.py | 2 +- .../instrumentation/psycopg2/package.py | 2 +- .../instrumentation/sqlalchemy/engine.py | 14 +---- .../tests/test_sqlcommenter.py | 2 +- .../instrumentation/sqlcommenter_utils.py | 52 +++++++++++++++++++ .../opentelemetry/instrumentation/utils.py | 36 ------------- .../tests/test_utils.py | 2 +- 11 files changed, 61 insertions(+), 70 deletions(-) create mode 100644 opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py diff --git a/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py b/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py index 20b78d61e9..20f2a059ae 100644 --- a/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py @@ -43,14 +43,13 @@ import wrapt -from opentelemetry import context as opentelemetry_context from opentelemetry import trace as trace_api from opentelemetry.instrumentation.dbapi.version import __version__ from opentelemetry.instrumentation.utils import ( - _add_sql_comment, _get_opentelemetry_values, unwrap, ) +from opentelemetry.instrumentation.sqlcommenter_utils import _add_sql_comment from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import SpanKind, TracerProvider, get_tracer @@ -431,18 +430,6 @@ def traced_execution( ): commenter_data.update(**_get_opentelemetry_values()) - # Add flask related tags - sqlcommenter_flask_values = ( - opentelemetry_context.get_value( - "SQLCOMMENTER_FLASK_VALUES" - ) - if opentelemetry_context.get_value( - "SQLCOMMENTER_FLASK_VALUES" - ) - else {} - ) - commenter_data.update(**sqlcommenter_flask_values) - # Filter down to just the requested attributes. commenter_data = { k: v diff --git a/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py b/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py index da10ad0354..8502bd46ee 100644 --- a/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py +++ b/instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py @@ -273,7 +273,7 @@ def test_executemany_flask_integration_comment(self): ) current_context = context.get_current() sqlcommenter_context = context.set_value( - "SQLCOMMENTER_FLASK_VALUES", {"flask": 1}, current_context + "SQLCOMMENTER_ORM_TAGS_AND_VALUES", {"flask": 1}, current_context ) context.attach(sqlcommenter_context) diff --git a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware/sqlcommenter_middleware.py b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware/sqlcommenter_middleware.py index 89c1b93008..c8c32ab63a 100644 --- a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware/sqlcommenter_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware/sqlcommenter_middleware.py @@ -23,9 +23,9 @@ from django.db.backends.utils import CursorDebugWrapper from opentelemetry.instrumentation.utils import ( - _add_sql_comment, _get_opentelemetry_values, ) +from opentelemetry.instrumentation.sqlcommenter_utils import _add_sql_comment from opentelemetry.trace.propagation.tracecontext import ( TraceContextTextMapPropagator, ) diff --git a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py index 32519a7fed..9880c54b00 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py @@ -369,7 +369,7 @@ def _before_request(): ): flask_info["route"] = flask.request.url_rule.rule sqlcommenter_context = context.set_value( - "SQLCOMMENTER_FLASK_VALUES", flask_info, current_context + "SQLCOMMENTER_ORM_TAGS_AND_VALUES", flask_info, current_context ) context.attach(sqlcommenter_context) diff --git a/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py b/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py index 5a942960ee..d3ad9a282c 100644 --- a/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py +++ b/instrumentation/opentelemetry-instrumentation-flask/tests/base_test.py @@ -30,7 +30,7 @@ def _hello_endpoint(helloid): def _sqlcommenter_endpoint(): current_context = context.get_current() sqlcommenter_flask_values = current_context.get( - "SQLCOMMENTER_FLASK_VALUES", {} + "SQLCOMMENTER_ORM_TAGS_AND_VALUES", {} ) return sqlcommenter_flask_values diff --git a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py index 9757a8df79..c666a588e2 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py @@ -13,4 +13,4 @@ # limitations under the License. -_instruments = ("psycopg2 >= 2.7.3.1",) +_instruments = ("psycopg2-binary",) diff --git a/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py b/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py index f4afca3d92..7fe1790f23 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py +++ b/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py @@ -22,9 +22,9 @@ ) from opentelemetry.instrumentation.sqlalchemy.version import __version__ from opentelemetry.instrumentation.utils import ( - _add_sql_comment, _get_opentelemetry_values, ) +from opentelemetry.instrumentation.sqlcommenter_utils import _add_sql_comment from opentelemetry.semconv.trace import NetTransportValues, SpanAttributes from opentelemetry.trace.status import Status, StatusCode @@ -161,18 +161,6 @@ def _before_cur_exec( if self.commenter_options.get(k, True) } - # Add flask related tags - sqlcommenter_flask_values = ( - opentelemetry_context.get_value( - "SQLCOMMENTER_FLASK_VALUES" - ) - if opentelemetry_context.get_value( - "SQLCOMMENTER_FLASK_VALUES" - ) - else {} - ) - commenter_data.update(**sqlcommenter_flask_values) - statement = _add_sql_comment(statement, **commenter_data) context._otel_span = span diff --git a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlcommenter.py b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlcommenter.py index 6455ff7be0..6cdc5b2f67 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlcommenter.py +++ b/instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlcommenter.py @@ -68,7 +68,7 @@ def test_sqlcommenter_flask_integration(self): current_context = context.get_current() sqlcommenter_context = context.set_value( - "SQLCOMMENTER_FLASK_VALUES", {"flask": 1}, current_context + "SQLCOMMENTER_ORM_TAGS_AND_VALUES", {"flask": 1}, current_context ) context.attach(sqlcommenter_context) diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py new file mode 100644 index 0000000000..2525923844 --- /dev/null +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py @@ -0,0 +1,52 @@ +from opentelemetry.instrumentation.utils import _url_quote +from opentelemetry import context + + +def _add_sql_comment(sql, **meta) -> str: + """ + Appends comments to the sql statement and returns it + """ + meta.update(**_add_framework_tags()) + comment = _generate_sql_comment(**meta) + sql = sql.rstrip() + if sql[-1] == ";": + sql = sql[:-1] + comment + ";" + else: + sql = sql + comment + return sql + + +def _generate_sql_comment(**meta) -> str: + """ + Return a SQL comment with comma delimited key=value pairs created from + **meta kwargs. + """ + key_value_delimiter = "," + + if not meta: # No entries added. + return "" + + # Sort the keywords to ensure that caching works and that testing is + # deterministic. It eases visual inspection as well. + return ( + " /*" + + key_value_delimiter.join( + f"{_url_quote(key)}={_url_quote(value)!r}" + for key, value in sorted(meta.items()) + if value is not None + ) + + "*/" + ) + + +def _add_framework_tags() -> dict: + """ + Returns orm related tags if any set by the context + """ + + sqlcommenter_framework_values = ( + context.get_value("SQLCOMMENTER_ORM_TAGS_AND_VALUES") + if context.get_value("SQLCOMMENTER_ORM_TAGS_AND_VALUES") + else {} + ) + return sqlcommenter_framework_values diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py index 95a943afb3..3022e6ddd0 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py @@ -124,29 +124,6 @@ def _start_internal_or_server_span( return span, token -def _generate_sql_comment(**meta) -> str: - """ - Return a SQL comment with comma delimited key=value pairs created from - **meta kwargs. - """ - key_value_delimiter = "," - - if not meta: # No entries added. - return "" - - # Sort the keywords to ensure that caching works and that testing is - # deterministic. It eases visual inspection as well. - return ( - " /*" - + key_value_delimiter.join( - f"{_url_quote(key)}={_url_quote(value)!r}" - for key, value in sorted(meta.items()) - if value is not None - ) - + "*/" - ) - - def _url_quote(s) -> str: # pylint: disable=invalid-name if not isinstance(s, (str, bytes)): return s @@ -175,16 +152,3 @@ def _python_path_without_directory(python_path, directory, path_separator): "", python_path, ) - - -def _add_sql_comment(sql, **meta) -> str: - """ - Appends comments to the sql statement and returns it - """ - comment = _generate_sql_comment(**meta) - sql = sql.rstrip() - if sql[-1] == ";": - sql = sql[:-1] + comment + ";" - else: - sql = sql + comment - return sql diff --git a/opentelemetry-instrumentation/tests/test_utils.py b/opentelemetry-instrumentation/tests/test_utils.py index 3fd52ea7c6..441d14e7fc 100644 --- a/opentelemetry-instrumentation/tests/test_utils.py +++ b/opentelemetry-instrumentation/tests/test_utils.py @@ -16,10 +16,10 @@ from http import HTTPStatus from opentelemetry.instrumentation.utils import ( - _add_sql_comment, _python_path_without_directory, http_status_to_status_code, ) +from opentelemetry.instrumentation.sqlcommenter_utils import _add_sql_comment from opentelemetry.trace import StatusCode From 7c17444b04a9fd0f05c774f2f76c4386359822f4 Mon Sep 17 00:00:00 2001 From: Thiyagu55 Date: Fri, 26 Aug 2022 12:53:27 +0530 Subject: [PATCH 11/18] Linting changes --- .../src/opentelemetry/instrumentation/dbapi/__init__.py | 2 +- .../django/middleware/sqlcommenter_middleware.py | 4 +--- .../src/opentelemetry/instrumentation/sqlalchemy/engine.py | 5 +---- .../src/opentelemetry/instrumentation/sqlcommenter_utils.py | 2 +- opentelemetry-instrumentation/tests/test_utils.py | 2 +- 5 files changed, 5 insertions(+), 10 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py b/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py index 20f2a059ae..0645d4c5f6 100644 --- a/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py @@ -45,11 +45,11 @@ from opentelemetry import trace as trace_api from opentelemetry.instrumentation.dbapi.version import __version__ +from opentelemetry.instrumentation.sqlcommenter_utils import _add_sql_comment from opentelemetry.instrumentation.utils import ( _get_opentelemetry_values, unwrap, ) -from opentelemetry.instrumentation.sqlcommenter_utils import _add_sql_comment from opentelemetry.semconv.trace import SpanAttributes from opentelemetry.trace import SpanKind, TracerProvider, get_tracer diff --git a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware/sqlcommenter_middleware.py b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware/sqlcommenter_middleware.py index c8c32ab63a..89d8a9b776 100644 --- a/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware/sqlcommenter_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware/sqlcommenter_middleware.py @@ -22,10 +22,8 @@ from django.db import connections from django.db.backends.utils import CursorDebugWrapper -from opentelemetry.instrumentation.utils import ( - _get_opentelemetry_values, -) from opentelemetry.instrumentation.sqlcommenter_utils import _add_sql_comment +from opentelemetry.instrumentation.utils import _get_opentelemetry_values from opentelemetry.trace.propagation.tracecontext import ( TraceContextTextMapPropagator, ) diff --git a/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py b/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py index 7fe1790f23..73941200c2 100644 --- a/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py +++ b/instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py @@ -15,16 +15,13 @@ from sqlalchemy.event import listen # pylint: disable=no-name-in-module -from opentelemetry import context as opentelemetry_context from opentelemetry import trace from opentelemetry.instrumentation.sqlalchemy.package import ( _instrumenting_module_name, ) from opentelemetry.instrumentation.sqlalchemy.version import __version__ -from opentelemetry.instrumentation.utils import ( - _get_opentelemetry_values, -) from opentelemetry.instrumentation.sqlcommenter_utils import _add_sql_comment +from opentelemetry.instrumentation.utils import _get_opentelemetry_values from opentelemetry.semconv.trace import NetTransportValues, SpanAttributes from opentelemetry.trace.status import Status, StatusCode diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py index 2525923844..1a61813925 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py @@ -1,5 +1,5 @@ -from opentelemetry.instrumentation.utils import _url_quote from opentelemetry import context +from opentelemetry.instrumentation.utils import _url_quote def _add_sql_comment(sql, **meta) -> str: diff --git a/opentelemetry-instrumentation/tests/test_utils.py b/opentelemetry-instrumentation/tests/test_utils.py index 441d14e7fc..cf6cfdfd37 100644 --- a/opentelemetry-instrumentation/tests/test_utils.py +++ b/opentelemetry-instrumentation/tests/test_utils.py @@ -15,11 +15,11 @@ import unittest from http import HTTPStatus +from opentelemetry.instrumentation.sqlcommenter_utils import _add_sql_comment from opentelemetry.instrumentation.utils import ( _python_path_without_directory, http_status_to_status_code, ) -from opentelemetry.instrumentation.sqlcommenter_utils import _add_sql_comment from opentelemetry.trace import StatusCode From 867ab5b131ee1531582f1732f411e7936a9d1d97 Mon Sep 17 00:00:00 2001 From: Thiyagu55 Date: Fri, 26 Aug 2022 14:39:51 +0530 Subject: [PATCH 12/18] PR changes --- .../src/opentelemetry/instrumentation/psycopg2/package.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py index c666a588e2..c6ce0058bc 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py @@ -13,4 +13,4 @@ # limitations under the License. -_instruments = ("psycopg2-binary",) +_instruments = ("psycopg2 >= 2.7.3.1",) \ No newline at end of file From 7ceb8c0d19590148229e885bf3830d3346163bf9 Mon Sep 17 00:00:00 2001 From: Thiyagu55 Date: Fri, 26 Aug 2022 14:40:45 +0530 Subject: [PATCH 13/18] PR changes --- .../src/opentelemetry/instrumentation/psycopg2/package.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py index c6ce0058bc..a06ebbee30 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py @@ -12,5 +12,4 @@ # See the License for the specific language governing permissions and # limitations under the License. - -_instruments = ("psycopg2 >= 2.7.3.1",) \ No newline at end of file +_instruments = ("psycopg2 >= 2.7.3.1",) From c78c3c6048be646935ee899321fda0c3a004bc28 Mon Sep 17 00:00:00 2001 From: Thiyagu55 Date: Fri, 26 Aug 2022 14:42:18 +0530 Subject: [PATCH 14/18] PR changes --- .../src/opentelemetry/instrumentation/psycopg2/package.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py index a06ebbee30..c6ce0058bc 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py @@ -12,4 +12,5 @@ # See the License for the specific language governing permissions and # limitations under the License. -_instruments = ("psycopg2 >= 2.7.3.1",) + +_instruments = ("psycopg2 >= 2.7.3.1",) \ No newline at end of file From 2b79664bec05524f78a9005e524598d3ab84fcdc Mon Sep 17 00:00:00 2001 From: Thiyagu55 Date: Fri, 26 Aug 2022 14:42:54 +0530 Subject: [PATCH 15/18] PR changes --- .../src/opentelemetry/instrumentation/psycopg2/package.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py index c6ce0058bc..9757a8df79 100644 --- a/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py +++ b/instrumentation/opentelemetry-instrumentation-psycopg2/src/opentelemetry/instrumentation/psycopg2/package.py @@ -13,4 +13,4 @@ # limitations under the License. -_instruments = ("psycopg2 >= 2.7.3.1",) \ No newline at end of file +_instruments = ("psycopg2 >= 2.7.3.1",) From 8671dd65d280a306742e395b963590ef5d6de53b Mon Sep 17 00:00:00 2001 From: Thiyagu55 Date: Fri, 26 Aug 2022 17:41:11 +0530 Subject: [PATCH 16/18] Added license header to sqlcomenter_utils.py --- .../instrumentation/sqlcommenter_utils.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py index 1a61813925..1eeefbf206 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/sqlcommenter_utils.py @@ -1,3 +1,17 @@ +# Copyright The 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. + from opentelemetry import context from opentelemetry.instrumentation.utils import _url_quote From 85b630241f9c0cf4abf528867a48236a468e99ec Mon Sep 17 00:00:00 2001 From: Thiyagu55 Date: Thu, 1 Sep 2022 15:53:32 +0530 Subject: [PATCH 17/18] PR changes --- CHANGELOG.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0d5ea57b7..51262570ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed + +### Added +- Flask sqlalchemy psycopg2 integration + ([#1224](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1224)) ### Fixed @@ -25,8 +30,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#1206](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1206)) - Add psycopg2 native tags to sqlcommenter ([#1203](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1203)) -- Flask sqlalchemy psycopg2 integration - ([#1224](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1224)) ### Added - `opentelemetry-instrumentation-redis` add support to instrument RedisCluster clients From d90dfac058039afdb6a2b3300fbc34abb3229f5d Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Fri, 9 Sep 2022 01:34:12 +0530 Subject: [PATCH 18/18] Update CHANGELOG.md --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cd1235191..13ebb67d73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] -### Fixed ### Added - Flask sqlalchemy psycopg2 integration