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:
29
reference/ddtrace/contrib/mongoengine/__init__.py
Normal file
29
reference/ddtrace/contrib/mongoengine/__init__.py
Normal 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']
|
20
reference/ddtrace/contrib/mongoengine/patch.py
Normal file
20
reference/ddtrace/contrib/mongoengine/patch.py
Normal 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
|
32
reference/ddtrace/contrib/mongoengine/trace.py
Normal file
32
reference/ddtrace/contrib/mongoengine/trace.py
Normal 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
|
Reference in New Issue
Block a user