Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_unpatch_patch(self):
""" Tests unpatch-patch cycle """
unpatch()
self.assertIsNone(Pin.get_from(molten))
molten_client()
spans = self.tracer.writer.pop()
self.assertEqual(len(spans), 0)
patch()
# Need to override Pin here as we do in setUp
Pin.override(molten, tracer=self.tracer)
self.assertTrue(Pin.get_from(molten) is not None)
molten_client()
spans = self.tracer.writer.pop()
self.assertTrue(len(spans) > 0)
def test_pin_does_not_override_global(self):
# ensure that when a `Pin` is created from a class, the specific
# instance doesn't override the global one
class A(object):
pass
Pin.override(A, service='metrics')
global_pin = Pin.get_from(A)
global_pin._config['distributed_tracing'] = True
a = A()
pin = Pin.get_from(a)
assert pin is not None
assert pin._config['distributed_tracing'] is True
pin._config['distributed_tracing'] = False
assert global_pin._config['distributed_tracing'] is True
assert pin._config['distributed_tracing'] is False
def test_pin_does_not_override_global_with_new_instance(self):
# ensure that when a `Pin` is created from a class, the specific
# instance doesn't override the global one, even if only the
# `onto()` API has been used
class A(object):
pass
pin = Pin(service='metrics')
pin.onto(A)
global_pin = Pin.get_from(A)
global_pin._config['distributed_tracing'] = True
a = A()
pin = Pin.get_from(a)
assert pin is not None
assert pin._config['distributed_tracing'] is True
pin._config['distributed_tracing'] = False
assert global_pin._config['distributed_tracing'] is True
assert pin._config['distributed_tracing'] is False
def test_patch_app(self):
# When celery.App is patched it must include a `Pin` instance
app = celery.Celery()
assert Pin.get_from(app) is not None
def _get_conn_and_tracer(self):
conn = self._conn = yield from aiopg.connect(**POSTGRES_CONFIG)
Pin.get_from(conn).clone(tracer=self.tracer).onto(conn)
return conn, self.tracer
def patch_app_init(wrapped, instance, args, kwargs):
"""Patch app initialization of middleware, components and renderers
"""
# allow instance to be initialized before wrapping them
wrapped(*args, **kwargs)
# add Pin to instance
pin = Pin.get_from(molten)
if not pin or not pin.enabled():
return
# Wrappers here allow us to trace objects without altering class or instance
# attributes, which presents a problem when classes in molten use
# ``__slots__``
instance.router = WrapperRouter(instance.router)
# wrap middleware functions/callables
instance.middleware = [
WrapperMiddleware(mw)
for mw in instance.middleware
]
def match(self, *args, **kwargs):
# catch matched route and wrap tracer around its handler and set root span resource
func = self.__wrapped__.match
route_and_params = func(*args, **kwargs)
pin = Pin.get_from(molten)
if not pin or not pin.enabled():
return route_and_params
if route_and_params is not None:
route, params = route_and_params
route.handler = trace_func(func_name(route.handler))(route.handler)
# update root span resource while we know the matched route
resource = '{} {}'.format(
route.method,
route.template,
)
root_span = pin.tracer.current_root_span()
root_span.resource = resource
def _trace_func(wrapped, instance, args, kwargs):
pin = Pin.get_from(molten)
if not pin or not pin.enabled():
return wrapped(*args, **kwargs)
with pin.tracer.trace(func_name(wrapped), service=pin.service, resource=resource):
return wrapped(*args, **kwargs)
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
def unpatch():
"""Remove instrumentation
"""
if getattr(molten, '_datadog_patch', False):
setattr(molten, '_datadog_patch', False)
# remove pin
pin = Pin.get_from(molten)
if pin:
pin.remove_from(molten)
_u(molten.BaseApp, '__init__')
_u(molten.App, '__call__')
_u(molten.Router, 'add_route')