mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-08-02 02:52:18 +08:00
Move DD code into its own directory (#6)
This commit is contained in:
38
reference/ddtrace/contrib/mysqldb/__init__.py
Normal file
38
reference/ddtrace/contrib/mysqldb/__init__.py
Normal file
@ -0,0 +1,38 @@
|
||||
"""Instrument mysqlclient / MySQL-python to report MySQL queries.
|
||||
|
||||
``patch_all`` will automatically patch your mysql connection to make it work.
|
||||
|
||||
::
|
||||
|
||||
# Make sure to import MySQLdb and not the 'connect' function,
|
||||
# otherwise you won't have access to the patched version
|
||||
from ddtrace import Pin, patch
|
||||
import MySQLdb
|
||||
|
||||
# If not patched yet, you can patch mysqldb specifically
|
||||
patch(mysqldb=True)
|
||||
|
||||
# This will report a span with the default settings
|
||||
conn = MySQLdb.connect(user="alice", passwd="b0b", host="localhost", port=3306, db="test")
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT 6*7 AS the_answer;")
|
||||
|
||||
# Use a pin to specify metadata related to this connection
|
||||
Pin.override(conn, service='mysql-users')
|
||||
|
||||
This package works for mysqlclient or MySQL-python. Only the default
|
||||
full-Python integration works. The binary C connector provided by
|
||||
_mysql is not yet supported.
|
||||
|
||||
Help on mysqlclient can be found on:
|
||||
https://mysqlclient.readthedocs.io/
|
||||
"""
|
||||
from ...utils.importlib import require_modules
|
||||
|
||||
required_modules = ['MySQLdb']
|
||||
|
||||
with require_modules(required_modules) as missing_modules:
|
||||
if not missing_modules:
|
||||
from .patch import patch
|
||||
|
||||
__all__ = ['patch']
|
63
reference/ddtrace/contrib/mysqldb/patch.py
Normal file
63
reference/ddtrace/contrib/mysqldb/patch.py
Normal file
@ -0,0 +1,63 @@
|
||||
# 3p
|
||||
import MySQLdb
|
||||
|
||||
from ddtrace.vendor.wrapt import wrap_function_wrapper as _w
|
||||
|
||||
# project
|
||||
from ddtrace import Pin
|
||||
from ddtrace.contrib.dbapi import TracedConnection
|
||||
|
||||
from ...ext import net, db
|
||||
from ...utils.wrappers import unwrap as _u
|
||||
|
||||
KWPOS_BY_TAG = {
|
||||
net.TARGET_HOST: ('host', 0),
|
||||
db.USER: ('user', 1),
|
||||
db.NAME: ('db', 3),
|
||||
}
|
||||
|
||||
|
||||
def patch():
|
||||
# patch only once
|
||||
if getattr(MySQLdb, '__datadog_patch', False):
|
||||
return
|
||||
setattr(MySQLdb, '__datadog_patch', True)
|
||||
|
||||
# `Connection` and `connect` are aliases for
|
||||
# `Connect`; patch them too
|
||||
_w('MySQLdb', 'Connect', _connect)
|
||||
if hasattr(MySQLdb, 'Connection'):
|
||||
_w('MySQLdb', 'Connection', _connect)
|
||||
if hasattr(MySQLdb, 'connect'):
|
||||
_w('MySQLdb', 'connect', _connect)
|
||||
|
||||
|
||||
def unpatch():
|
||||
if not getattr(MySQLdb, '__datadog_patch', False):
|
||||
return
|
||||
setattr(MySQLdb, '__datadog_patch', False)
|
||||
|
||||
# unpatch MySQLdb
|
||||
_u(MySQLdb, 'Connect')
|
||||
if hasattr(MySQLdb, 'Connection'):
|
||||
_u(MySQLdb, 'Connection')
|
||||
if hasattr(MySQLdb, 'connect'):
|
||||
_u(MySQLdb, 'connect')
|
||||
|
||||
|
||||
def _connect(func, instance, args, kwargs):
|
||||
conn = func(*args, **kwargs)
|
||||
return patch_conn(conn, *args, **kwargs)
|
||||
|
||||
|
||||
def patch_conn(conn, *args, **kwargs):
|
||||
tags = {t: kwargs[k] if k in kwargs else args[p]
|
||||
for t, (k, p) in KWPOS_BY_TAG.items()
|
||||
if k in kwargs or len(args) > p}
|
||||
tags[net.TARGET_PORT] = conn.port
|
||||
pin = Pin(service='mysql', app='mysql', tags=tags)
|
||||
|
||||
# grab the metadata from the conn
|
||||
wrapped = TracedConnection(conn, pin=pin)
|
||||
pin.onto(wrapped)
|
||||
return wrapped
|
Reference in New Issue
Block a user