Skip to content

Commit

Permalink
Add pymssql instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumep committed Apr 1, 2021
1 parent a946d5c commit 0860e38
Show file tree
Hide file tree
Showing 11 changed files with 371 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#387](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/387))

### Added
- `opentelemetry-instrumentation-pymssql` Add pymssql instrumentation
- `opentelemetry-instrumentation-urllib3` Add urllib3 instrumentation
([#299](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/299))

Expand Down
7 changes: 7 additions & 0 deletions docs/instrumentation/pymssql/pymssql.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
OpenTelemetry pymssql Instrumentation
=====================================

.. automodule:: opentelemetry.instrumentation.pymssql
:members:
:undoc-members:
:show-inheritance:
20 changes: 20 additions & 0 deletions instrumentation/opentelemetry-instrumentation-pymssql/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
OpenTelemetry pymssql Instrumentation
=====================================

|pypi|

.. |pypi| image:: https://badge.fury.io/py/opentelemetry-instrumentation-pymssql.svg
:target: https://pypi.org/project/opentelemetry-instrumentation-pymssql/

Installation
------------

::

pip install opentelemetry-instrumentation-pymssql


References
----------
* `OpenTelemetry pymssql Instrumentation <https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/pymssql/pymssql.html>`_
* `OpenTelemetry Project <https://opentelemetry.io/>`_
55 changes: 55 additions & 0 deletions instrumentation/opentelemetry-instrumentation-pymssql/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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.
#
[metadata]
name = opentelemetry-instrumentation-pymssql
description = OpenTelemetry pymssql instrumentation
long_description = file: README.rst
long_description_content_type = text/x-rst
author = OpenTelemetry Authors
author_email = cncf-opentelemetry-contributors@lists.cncf.io
url = https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-pymssql
platforms = any
license = Apache-2.0
classifiers =
Development Status :: 4 - Beta
Intended Audience :: Developers
License :: OSI Approved :: Apache Software License
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8

[options]
python_requires = >=3.6
package_dir=
=src
packages=find_namespace:
install_requires =
opentelemetry-api == 1.0.0
opentelemetry-instrumentation-dbapi == 0.19b0
opentelemetry-instrumentation == 0.19b0
pymssql ~= 2.1.5

[options.extras_require]
test =
opentelemetry-test == 0.19b0

[options.packages.find]
where = src

[options.entry_points]
opentelemetry_instrumentor =
pymssql = opentelemetry.instrumentation.pymssql:PyMSSQLInstrumentor
31 changes: 31 additions & 0 deletions instrumentation/opentelemetry-instrumentation-pymssql/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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 os

import setuptools

BASE_DIR = os.path.dirname(__file__)
VERSION_FILENAME = os.path.join(
BASE_DIR,
"src",
"opentelemetry",
"instrumentation",
"pymssql",
"version.py",
)
PACKAGE_INFO = {}
with open(VERSION_FILENAME) as f:
exec(f.read(), PACKAGE_INFO)

setuptools.setup(version=PACKAGE_INFO["__version__"])
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# 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.

"""
The integration with pymssql supports the `pymssql`_ library and can be enabled
by using ``PyMSSQLInstrumentor``.
.. _pymssql: https://pypi.org/project/pymssql/
Usage
-----
.. code:: python
import pymssql
from opentelemetry.instrumentation.pymssql import PyMSSQLInstrumentor
PyMSSQLInstrumentor().instrument()
cnx = pymssql.connect(database="MySQL_Database")
cursor = cnx.cursor()
cursor.execute("INSERT INTO test (testField) VALUES (123)"
cnx.commit()
cursor.close()
cnx.close()
API
---
"""

import pymssql

from opentelemetry.instrumentation import dbapi
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.pymssql.version import __version__


class PyMSSQLInstrumentor(BaseInstrumentor):
_CONNECTION_ATTRIBUTES = {
"database": "database",
"port": "port",
"host": "host",
"user": "user",
}

_DATABASE_SYSTEM = "mssql"

def _instrument(self, **kwargs):
"""Integrate with the pymssql library.
https://github.com/pymssql/pymssql/
"""
tracer_provider = kwargs.get("tracer_provider")

dbapi.wrap_connect(
__name__,
pymssql,
"connect",
self._DATABASE_SYSTEM,
self._CONNECTION_ATTRIBUTES,
version=__version__,
tracer_provider=tracer_provider,
)

def _uninstrument(self, **kwargs):
""""Disable pymssql instrumentation"""
dbapi.unwrap_connect(pymssql, "connect")

# pylint:disable=no-self-use
def instrument_connection(self, connection):
"""Enable instrumentation in a pymssql connection.
Args:
connection: The connection to instrument.
Returns:
An instrumented connection.
"""

return dbapi.instrument_connection(
__name__,
connection,
self._DATABASE_SYSTEM,
self._CONNECTION_ATTRIBUTES,
version=__version__,
)

def uninstrument_connection(self, connection):
"""Disable instrumentation in a pymssql connection.
Args:
connection: The connection to uninstrument.
Returns:
An uninstrumented connection.
"""
return dbapi.uninstrument_connection(connection)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 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.

__version__ = "0.19b0"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# 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 unittest import mock

import pymssql

import opentelemetry.instrumentation.pymssql
from opentelemetry.instrumentation.pymssql import PyMSSQLInstrumentor
from opentelemetry.sdk import resources
from opentelemetry.test.test_base import TestBase


class TestPyMSSQLIntegration(TestBase):
def tearDown(self):
super().tearDown()
with self.disable_logging():
PyMSSQLInstrumentor().uninstrument()

@mock.patch("pymssql.connect")
# pylint: disable=unused-argument
def test_instrumentor(self, mock_connect):
PyMSSQLInstrumentor().instrument()

cnx = pymssql.connect(database="test")
cursor = cnx.cursor()
query = "SELECT * FROM test"
cursor.execute(query)

spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
span = spans_list[0]

# Check version and name in span's instrumentation info
self.check_span_instrumentation_info(
span, opentelemetry.instrumentation.pymssql
)

# check that no spans are generated after uninstrument
PyMSSQLInstrumentor().uninstrument()

cnx = pymssql.connect(database="test")
cursor = cnx.cursor()
query = "SELECT * FROM test"
cursor.execute(query)

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

@mock.patch("pymssql.connect")
# pylint: disable=unused-argument
def test_custom_tracer_provider(self, mock_connect):
resource = resources.Resource.create({})
result = self.create_tracer_provider(resource=resource)
tracer_provider, exporter = result

PyMSSQLInstrumentor().instrument(tracer_provider=tracer_provider)

cnx = pymssql.connect(database="test")
cursor = cnx.cursor()
query = "SELECT * FROM test"
cursor.execute(query)

spans_list = exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
span = spans_list[0]

self.assertIs(span.resource, resource)

@mock.patch("pymssql.connect")
# pylint: disable=unused-argument
def test_instrument_connection(self, mock_connect):
cnx = pymssql.connect(database="test")
query = "SELECT * FROM test"
cursor = cnx.cursor()
cursor.execute(query)

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

cnx = PyMSSQLInstrumentor().instrument_connection(cnx)
cursor = cnx.cursor()
cursor.execute(query)

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

@mock.patch("pymssql.connect")
# pylint: disable=unused-argument
def test_uninstrument_connection(self, mock_connect):
PyMSSQLInstrumentor().instrument()
cnx = pymssql.connect(database="test")
query = "SELECT * FROM test"
cursor = cnx.cursor()
cursor.execute(query)

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

cnx = PyMSSQLInstrumentor().uninstrument_connection(cnx)
cursor = cnx.cursor()
cursor.execute(query)

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

0 comments on commit 0860e38

Please sign in to comment.