-
Notifications
You must be signed in to change notification settings - Fork 652
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
PyMySQL Integration #504
Merged
Merged
PyMySQL Integration #504
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
de2fda9
pymy
lzchen 789eaed
fix lint
lzchen 69af52d
tox
lzchen 7d25e82
dbapi
lzchen 0037579
Fix import
lzchen 71c4754
add path
lzchen 225bb4c
fix name
lzchen 0512fb5
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
lzchen 6e2f49f
address comments
lzchen 4464472
merge
lzchen a7b82c3
functional tests
lzchen 5d16a3a
fix test
lzchen f6acff9
Apply suggestions from code review
mauriciovasquezbernal c072146
Merge branch 'master' into pymy
mauriciovasquezbernal 6e6be34
undo change in python version for lint
mauriciovasquezbernal 0b0132c
ext/pymysql: Take TracerProvider instead of tracer
mauriciovasquezbernal a2bf714
quick fixes for documentation
mauriciovasquezbernal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.. include:: ../../../ext/opentelemetry-ext-pymysql/README.rst | ||
|
||
|
||
Module contents | ||
--------------- | ||
|
||
.. automodule:: opentelemetry.ext.pymysql | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -125,6 +125,11 @@ def get_connection_attributes(self, connection): | |||||
self.name = self.database_component | ||||||
self.database = self.connection_props.get("database", "") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if self.database: | ||||||
# PyMySQL encodes names with utf-8 | ||||||
try: | ||||||
self.database = self.database.decode() | ||||||
lzchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
except AttributeError: | ||||||
pass | ||||||
self.name += "." + self.database | ||||||
user = self.connection_props.get("user") | ||||||
if user is not None: | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Changelog | ||
|
||
## Unreleased |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,33 @@ | ||||||
OpenTelemetry PyMySQL integration | ||||||
=============================== | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
The integration with PyMySQL supports the `PyMySQL`_ library and is specified | ||||||
to ``trace_integration`` using ``'PyMySQL'``. | ||||||
|
||||||
.. _PyMySQL: https://pypi.org/project/PyMySQL/ | ||||||
|
||||||
Usage | ||||||
----- | ||||||
|
||||||
.. code:: python | ||||||
|
||||||
import pymysql | ||||||
from opentelemetry import trace | ||||||
from opentelemetry.ext.pymysql import trace_integration | ||||||
from opentelemetry.sdk.trace import TracerProvider | ||||||
|
||||||
trace.set_tracer_provider(TracerProvider()) | ||||||
tracer = trace.get_tracer(__name__) | ||||||
trace_integration(tracer) | ||||||
cnx = pymysql.connect(database='MySQL_Database') | ||||||
cursor = cnx.cursor() | ||||||
cursor.execute("INSERT INTO test (testField) VALUES (123)" | ||||||
cnx.commit() | ||||||
cursor.close() | ||||||
cnx.close() | ||||||
|
||||||
|
||||||
References | ||||||
---------- | ||||||
|
||||||
* `OpenTelemetry Project <https://opentelemetry.io/>`_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright 2020, OpenTelemetry Authors | ||
mauriciovasquezbernal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# | ||
# 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-ext-pymysql | ||
description = OpenTelemetry PyMySQL integration | ||
long_description = file: README.rst | ||
long_description_content_type = text/x-rst | ||
author = OpenTelemetry Authors | ||
author_email = [email protected] | ||
url = https://github.com/open-telemetry/opentelemetry-python/ext/opentelemetry-ext-pymysql | ||
mauriciovasquezbernal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.4 | ||
Programming Language :: Python :: 3.5 | ||
Programming Language :: Python :: 3.6 | ||
Programming Language :: Python :: 3.7 | ||
mauriciovasquezbernal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[options] | ||
python_requires = >=3.4 | ||
package_dir= | ||
=src | ||
packages=find_namespace: | ||
install_requires = | ||
opentelemetry-api >= 0.6.dev0 | ||
opentelemetry-ext-dbapi >= 0.6.dev0 | ||
mauriciovasquezbernal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PyMySQL ~= 0.9.3 | ||
|
||
[options.packages.find] | ||
where = src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Copyright 2020, OpenTelemetry Authors | ||
mauriciovasquezbernal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# | ||
# 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", "ext", "pymysql", "version.py" | ||
) | ||
PACKAGE_INFO = {} | ||
with open(VERSION_FILENAME) as f: | ||
exec(f.read(), PACKAGE_INFO) | ||
|
||
setuptools.setup(version=PACKAGE_INFO["__version__"]) |
38 changes: 38 additions & 0 deletions
38
ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# 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. | ||
|
||
""" | ||
The opentelemetry-ext-pymysql package allows tracing MySQL queries made by the | ||
PyMySQL library. | ||
""" | ||
|
||
import pymysql | ||
|
||
from opentelemetry.ext.dbapi import trace_integration as db_integration | ||
from opentelemetry.trace import Tracer | ||
|
||
|
||
def trace_integration(tracer: Tracer): | ||
"""Integrate with the PyMySQL library. | ||
https://github.com/PyMySQL/PyMySQL/ | ||
""" | ||
connection_attributes = { | ||
"database": "db", | ||
"port": "port", | ||
"host": "host", | ||
"user": "user", | ||
} | ||
db_integration( | ||
tracer, pymysql, "connect", "mysql", "sql", connection_attributes | ||
) |
15 changes: 15 additions & 0 deletions
15
ext/opentelemetry-ext-pymysql/src/opentelemetry/ext/pymysql/version.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# 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. | ||
|
||
__version__ = "0.6.dev0" |
Empty file.
43 changes: 43 additions & 0 deletions
43
ext/opentelemetry-ext-pymysql/tests/test_pymysql_integration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright 2019, OpenTelemetry Authors | ||
lzchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# | ||
# 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 unittest | ||
from unittest import mock | ||
|
||
import pymysql | ||
|
||
from opentelemetry import trace as trace_api | ||
from opentelemetry.ext.pymysql import trace_integration | ||
|
||
|
||
class TestPyMysqlIntegration(unittest.TestCase): | ||
def test_trace_integration(self): | ||
tracer = trace_api.DefaultTracer() | ||
span = mock.create_autospec(trace_api.Span, spec_set=True) | ||
start_current_span_patcher = mock.patch.object( | ||
mauriciovasquezbernal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tracer, | ||
"start_as_current_span", | ||
autospec=True, | ||
spec_set=True, | ||
return_value=span, | ||
) | ||
start_as_current_span = start_current_span_patcher.start() | ||
lzchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
with mock.patch("pymysql.connect"): | ||
trace_integration(tracer) | ||
cnx = pymysql.connect(database="test") | ||
cursor = cnx.cursor() | ||
query = "SELECT * FROM test" | ||
cursor.execute(query) | ||
self.assertTrue(start_as_current_span.called) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently we're not including that readme in these files anymore.