How to use the ddtrace.Pin.get_from function in ddtrace

To help you get started, we’ve selected a few ddtrace examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DataDog / dd-trace-py / tests / contrib / molten / test_molten.py View on Github external
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)
github DataDog / dd-trace-py / tests / test_pin.py View on Github external
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
github DataDog / dd-trace-py / tests / test_pin.py View on Github external
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
github DataDog / dd-trace-py / tests / contrib / celery / test_app.py View on Github external
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
github DataDog / dd-trace-py / tests / contrib / aiopg / test_aiopg.py View on Github external
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
github DataDog / dd-trace-py / ddtrace / contrib / molten / patch.py View on Github external
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
    ]
github DataDog / dd-trace-py / ddtrace / contrib / molten / wrappers.py View on Github external
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
github DataDog / dd-trace-py / ddtrace / contrib / molten / wrappers.py View on Github external
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)
github DataDog / dd-trace-py / ddtrace / contrib / mongoengine / trace.py View on Github external
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
github DataDog / dd-trace-py / ddtrace / contrib / molten / patch.py View on Github external
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')