Move DD code into its own directory (#6)

This commit is contained in:
Diego Hurtado
2020-04-08 11:39:44 -06:00
committed by GitHub
parent 72b40ba5f9
commit 5aee3ce32e
611 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,29 @@
"""Instrument mongoengine to report MongoDB queries.
``patch_all`` will automatically patch your mongoengine connect method to make it work.
::
from ddtrace import Pin, patch
import mongoengine
# If not patched yet, you can patch mongoengine specifically
patch(mongoengine=True)
# At that point, mongoengine is instrumented with the default settings
mongoengine.connect('db', alias='default')
# Use a pin to specify metadata related to this client
client = mongoengine.connect('db', alias='master')
Pin.override(client, service="mongo-master")
"""
from ...utils.importlib import require_modules
required_modules = ['mongoengine']
with require_modules(required_modules) as missing_modules:
if not missing_modules:
from .patch import patch, trace_mongoengine
__all__ = ['patch', 'trace_mongoengine']

View File

@ -0,0 +1,20 @@
import mongoengine
from .trace import WrappedConnect
from ...utils.deprecation import deprecated
# Original connect function
_connect = mongoengine.connect
def patch():
setattr(mongoengine, 'connect', WrappedConnect(_connect))
def unpatch():
setattr(mongoengine, 'connect', _connect)
@deprecated(message='Use patching instead (see the docs).', version='1.0.0')
def trace_mongoengine(*args, **kwargs):
return _connect

View File

@ -0,0 +1,32 @@
# 3p
from ddtrace.vendor import wrapt
# project
import ddtrace
from ddtrace.ext import mongo as mongox
from ddtrace.contrib.pymongo.client import TracedMongoClient
# TODO(Benjamin): we should instrument register_connection instead, because more generic
# We should also extract the "alias" attribute and set it as a meta
class WrappedConnect(wrapt.ObjectProxy):
""" WrappedConnect wraps mongoengines 'connect' function to ensure
that all returned connections are wrapped for tracing.
"""
def __init__(self, connect):
super(WrappedConnect, self).__init__(connect)
ddtrace.Pin(service=mongox.SERVICE, tracer=ddtrace.tracer).onto(self)
def __call__(self, *args, **kwargs):
client = self.__wrapped__(*args, **kwargs)
pin = ddtrace.Pin.get_from(self)
if pin:
# mongoengine uses pymongo internally, so we can just piggyback on the
# existing pymongo integration and make sure that the connections it
# uses internally are traced.
client = TracedMongoClient(client)
ddtrace.Pin(service=pin.service, tracer=pin.tracer).onto(client)
return client