Skip to content

Commit c44c1b4

Browse files
ZStriker19brettlangdonKyle-VerhoogYun-Kimmergify[bot]
authored
Add mariadb (#2570)
* adding comment to clarify that log line occurs when DATADOG_PRIORITY_SAMPLING=false is set, not just when an older tracer version is being used * Update writer.py * got mariadb integration working on sample app * testing for mariadb integration setup * fixed riot.py file so mariadb tests run * beginning conversion of mysql tests to mariadb tests with new formats * committing to merge in master updates * made all mariadb tests pass * added first snapshot test * removed print statement and added more snapshot tests for mariadb integration * updated docs to include mariadb integration and made documentation in __init__.py file for mariadb correct * added mariadb to circleci config.yaml * added mariadb build steps to dev image * added malformed query snapshot test * Fix grammar in mariadb docs Co-authored-by: Brett Langdon <[email protected]> * added mariadb test snapshots, reformatted tests, added guards for patching and unpatching, used black formatter * added release notes * add link to library this instruments Co-authored-by: Kyle Verhoog <[email protected]> * remove comment Co-authored-by: Kyle Verhoog <[email protected]> * Update ddtrace/contrib/mariadb/__init__.py Co-authored-by: Kyle Verhoog <[email protected]> * Update ddtrace/contrib/mariadb/__init__.py Co-authored-by: Kyle Verhoog <[email protected]> * Update ddtrace/contrib/mariadb/patch.py Co-authored-by: Kyle Verhoog <[email protected]> * Update docs/integrations.rst Co-authored-by: Kyle Verhoog <[email protected]> * Update releasenotes/notes/mariadb-8e7bc94a0d1b8f02.yaml Co-authored-by: Kyle Verhoog <[email protected]> * edits to comments * Update tests/contrib/mariadb/test_mariadb.py Co-authored-by: Kyle Verhoog <[email protected]> * Update tests/contrib/mariadb/test_mariadb.py Co-authored-by: Kyle Verhoog <[email protected]> * Update tests/contrib/mariadb/test_mariadb.py Co-authored-by: Kyle Verhoog <[email protected]> * switched tests to snapshots and other small changes * getting rid of .DS_Stores * getting rid of DS_Store * Update ddtrace/contrib/mariadb/__init__.py Co-authored-by: Kyle Verhoog <[email protected]> * Update tests/contrib/mariadb/test_mariadb.py Co-authored-by: Kyle Verhoog <[email protected]> * Update tests/contrib/mariadb/test_mariadb.py Co-authored-by: Kyle Verhoog <[email protected]> * fix merge conflict * remove benchmark changes * Update tests/utils.py Co-authored-by: Kyle Verhoog <[email protected]> * Update ddtrace/contrib/mariadb/__init__.py Co-authored-by: Kyle Verhoog <[email protected]> * Update .gitignore Co-authored-by: Kyle Verhoog <[email protected]> * Update ddtrace/contrib/mariadb/patch.py Co-authored-by: Yun Kim <[email protected]> * Update .gitignore Co-authored-by: Kyle Verhoog <[email protected]> * Update .gitignore Co-authored-by: Kyle Verhoog <[email protected]> * fix spelling in circleci * fixing formatting * Remove changes to Dockerfile.buster * flake8 formatting * recreated snapshots and added mariadb to spelling_wordlist * changing test case so tracer flushes * changing test case to pass in CI * formatting * Update ddtrace/contrib/mariadb/__init__.py Co-authored-by: Kyle Verhoog <[email protected]> * Update ddtrace/contrib/mariadb/__init__.py Co-authored-by: Kyle Verhoog <[email protected]> * snapshots * updating tests so connection closes on each * Extend environment instead of replacing it * fix formatting and refactor a bit in tests * Update ddtrace/contrib/mariadb/__init__.py Co-authored-by: Kyle Verhoog <[email protected]> * formatting Co-authored-by: Brett Langdon <[email protected]> Co-authored-by: Kyle Verhoog <[email protected]> Co-authored-by: Yun Kim <[email protected]> Co-authored-by: Kyle Verhoog <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent ffa034f commit c44c1b4

25 files changed

+810
-0
lines changed

.circleci/config.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,14 @@ jobs:
625625
pattern: "grpc"
626626
snapshot: true
627627

628+
mariadb:
629+
<<: *machine_executor
630+
steps:
631+
- run_test:
632+
pattern: 'mariadb$'
633+
snapshot: true
634+
docker_services: "mariadb"
635+
628636
molten:
629637
<<: *contrib_job
630638
steps:
@@ -946,6 +954,7 @@ requires_tests: &requires_tests
946954
- jinja2
947955
- kombu
948956
- mako
957+
- mariadb
949958
- molten
950959
- mongoengine
951960
- mysqlconnector
@@ -1027,6 +1036,7 @@ workflows:
10271036
- jinja2: *requires_base_venvs
10281037
- kombu: *requires_base_venvs
10291038
- mako: *requires_base_venvs
1039+
- mariadb: *requires_base_venvs
10301040
- molten: *requires_base_venvs
10311041
- mongoengine: *requires_base_venvs
10321042
- mysqlconnector: *requires_base_venvs
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
The MariaDB integration instruments the
3+
`MariaDB library <https://mariadb-corporation.github.io/mariadb-connector-python/usage.html>`_ to trace queries.
4+
5+
6+
Enabling
7+
~~~~~~~~
8+
9+
The MariaDB integration is enabled automatically when using
10+
:ref:`ddtrace-run<ddtracerun>` or :ref:`patch_all()<patch_all>`.
11+
12+
Or use :ref:`patch()<patch>` to manually enable the integration::
13+
14+
from ddtrace import patch
15+
patch(mariadb=True)
16+
17+
18+
Global Configuration
19+
~~~~~~~~~~~~~~~~~~~~
20+
21+
.. py:data:: ddtrace.config.mariadb["service"]
22+
23+
The service name reported by default for MariaDB spans.
24+
25+
This option can also be set with the ``DD_MARIADB_SERVICE`` environment
26+
variable.
27+
28+
Default: ``"mariadb"``
29+
30+
31+
Instance Configuration
32+
~~~~~~~~~~~~~~~~~~~~~~
33+
34+
To configure the mariadb integration on an per-connection basis use the
35+
``Pin`` API::
36+
37+
from ddtrace import Pin
38+
from ddtrace import patch
39+
40+
# Make sure to patch before importing mariadb
41+
patch(mariadb=True)
42+
43+
import mariadb.connector
44+
45+
# This will report a span with the default settings
46+
conn = mariadb.connector.connect(user="alice", password="b0b", host="localhost", port=3306, database="test")
47+
48+
# Use a pin to override the service name for this connection.
49+
Pin.override(conn, service="mariadb-users")
50+
51+
cursor = conn.cursor()
52+
cursor.execute("SELECT 6*7 AS the_answer;")
53+
54+
"""
55+
from ...utils.importlib import require_modules
56+
57+
58+
required_modules = ["mariadb"]
59+
60+
with require_modules(required_modules) as missing_modules:
61+
if not missing_modules:
62+
from .patch import patch
63+
from .patch import unpatch
64+
65+
__all__ = ["patch", "unpatch"]

ddtrace/contrib/mariadb/patch.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import mariadb
2+
3+
from ddtrace import Pin
4+
from ddtrace import config
5+
from ddtrace.contrib.dbapi import TracedConnection
6+
from ddtrace.ext import db
7+
from ddtrace.ext import net
8+
from ddtrace.utils.formats import asbool
9+
from ddtrace.utils.formats import get_env
10+
from ddtrace.utils.wrappers import unwrap
11+
from ddtrace.vendor import wrapt
12+
13+
14+
config._add(
15+
"mariadb",
16+
dict(
17+
trace_fetch_methods=asbool(get_env("mariadb", "trace_fetch_methods", default=False)),
18+
_default_service="mariadb",
19+
),
20+
)
21+
22+
23+
def patch():
24+
if getattr(mariadb, "_datadog_patch", False):
25+
return
26+
setattr(mariadb, "_datadog_patch", True)
27+
wrapt.wrap_function_wrapper("mariadb", "connect", _connect)
28+
29+
30+
def unpatch():
31+
if getattr(mariadb, "_datadog_patch", False):
32+
setattr(mariadb, "_datadog_patch", False)
33+
unwrap(mariadb, "connect")
34+
35+
36+
def _connect(func, instance, args, kwargs):
37+
conn = func(*args, **kwargs)
38+
tags = {
39+
net.TARGET_HOST: kwargs["host"],
40+
net.TARGET_PORT: kwargs["port"],
41+
db.USER: kwargs["user"],
42+
db.NAME: kwargs["database"],
43+
}
44+
45+
pin = Pin(app="mariadb", tags=tags)
46+
47+
wrapped = TracedConnection(conn, pin=pin, cfg=config.mariadb)
48+
pin.onto(wrapped)
49+
return wrapped

ddtrace/monkey.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"mysql": True,
4444
"mysqldb": True,
4545
"pymysql": True,
46+
"mariadb": True,
4647
"psycopg": True,
4748
"pylibmc": True,
4849
"pymemcache": True,

docker-compose.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ services:
2525
- POSTGRES_DB=postgres
2626
ports:
2727
- "127.0.0.1:5432:5432"
28+
mariadb:
29+
image: mariadb
30+
environment:
31+
- MYSQL_ROOT_PASSWORD=example
32+
- MYSQL_DATABASE=test
33+
- MYSQL_USER=test
34+
- MYSQL_PASSWORD=test
35+
ports:
36+
- "127.0.0.1:3306:3306"
2837
mysql:
2938
image: mysql:5.7
3039
environment:

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ contacting support.
9090
+--------------------------------------------------+---------------+----------------+
9191
| :ref:`mako` | >= 0.1.0 | Yes |
9292
+--------------------------------------------------+---------------+----------------+
93+
| :ref:`mariadb` | >= 1.0.0 | Yes |
94+
+--------------------------------------------------+---------------+----------------+
9395
| :ref:`kombu` | >= 4.0 | No |
9496
+--------------------------------------------------+---------------+----------------+
9597
| :ref:`molten` | >= 0.7.0 | Yes |

docs/integrations.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ Mako
193193
.. automodule:: ddtrace.contrib.mako
194194

195195

196+
.. _mariadb:
197+
198+
MariaDB
199+
^^^^^^^
200+
.. automodule:: ddtrace.contrib.mariadb
201+
202+
196203
.. _molten:
197204

198205
Molten

docs/spelling_wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ kwarg
7272
lifecycle
7373
lookups
7474
mako
75+
mariadb
7576
memcached
7677
metadata
7778
microservices
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
features:
3+
- |
4+
Add MariaDB integration.

riotfile.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,22 @@ def select_pys(min_version=MIN_PYTHON_VERSION, max_version=MAX_PYTHON_VERSION):
862862
pys=select_pys(min_version="3.6"),
863863
command="pytest {cmdargs} tests/contrib/asgi",
864864
),
865+
Venv(
866+
name="mariadb",
867+
command="pytest {cmdargs} tests/contrib/mariadb",
868+
venvs=[
869+
Venv(
870+
pys=select_pys(min_version="3.6"),
871+
pkgs={
872+
"mariadb": [
873+
"~=1.0.0",
874+
"~=1.0",
875+
latest,
876+
],
877+
},
878+
),
879+
],
880+
),
865881
Venv(
866882
name="fastapi",
867883
command="pytest {cmdargs} tests/contrib/fastapi",

0 commit comments

Comments
 (0)