How to use the ddtrace.pin.Pin 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 / test_instance_config.py View on Github external
def test_configuration_copy_upside_down(self):
        # ensure when a Pin is created, it does not copy the given configuration
        # until it's used for at least once
        global_config = {
            'service_name': 'service',
        }
        Pin(service='service', _config=global_config).onto(self.Klass)
        # override the global config: users do that before using the integration
        global_config['service_name'] = 'metrics'
        # use the Pin via `get_from`
        instance = self.Klass()
        cfg = config.get_from(instance)
        # it should have users updated value
        eq_(cfg['service_name'], 'metrics')
github DataDog / dd-trace-py / ddtrace / contrib / redis / patch.py View on Github external
with require_package('redis<3.0.0') as exists:
        if exists:
            _w('redis', 'StrictRedis.execute_command', traced_execute_command)
            _w('redis', 'StrictRedis.pipeline', traced_pipeline)
            _w('redis', 'Redis.pipeline', traced_pipeline)
            _w('redis.client', 'BasePipeline.execute', traced_execute_pipeline)
            _w('redis.client', 'BasePipeline.immediate_execute_command', traced_execute_command)

    with require_package('redis>=3.0.0') as exists:
        if exists:
            _w('redis', 'Redis.execute_command', traced_execute_command)
            _w('redis', 'Redis.pipeline', traced_pipeline)
            _w('redis.client', 'Pipeline.execute', traced_execute_pipeline)
            _w('redis.client', 'Pipeline.immediate_execute_command', traced_execute_command)

    Pin(service=redisx.DEFAULT_SERVICE, app=redisx.APP, app_type=AppTypes.db).onto(redis.StrictRedis)
github DataDog / dd-trace-py / ddtrace / contrib / aiohttp / patch.py View on Github external
def patch():
    """
    Patch aiohttp third party modules:
        * aiohttp_jinja2
    """
    if template_module:
        if getattr(aiohttp_jinja2, '__datadog_patch', False):
            return
        setattr(aiohttp_jinja2, '__datadog_patch', True)

        _w = wrapt.wrap_function_wrapper
        _w('aiohttp_jinja2', 'render_template', _trace_render_template)
        Pin(app='aiohttp', service=None).onto(aiohttp_jinja2)
github DataDog / dd-trace-py / ddtrace / contrib / mako / patch.py View on Github external
def patch():
    if getattr(mako, '__datadog_patch', False):
        # already patched
        return
    setattr(mako, '__datadog_patch', True)

    Pin(service='mako', app='mako').onto(Template)

    _w(mako, 'template.Template.render', _wrap_render)
    _w(mako, 'template.Template.render_unicode', _wrap_render)
    _w(mako, 'template.Template.render_context', _wrap_render)
github DataDog / dd-trace-py / ddtrace / contrib / jinja2 / patch.py View on Github external
def patch():
    if getattr(jinja2, '__datadog_patch', False):
        # already patched
        return
    setattr(jinja2, '__datadog_patch', True)
    Pin(
        service=config.jinja2['service_name'],
        _config=config.jinja2,
    ).onto(jinja2.environment.Environment)
    _w(jinja2, 'environment.Template.render', _wrap_render)
    _w(jinja2, 'environment.Template.generate', _wrap_render)
    _w(jinja2, 'environment.Environment.compile', _wrap_compile)
    _w(jinja2, 'environment.Environment._load_template', _wrap_load_template)
github DataDog / dd-trace-py / ddtrace / contrib / vertica / patch.py View on Github external
def init_wrapper(wrapped, instance, args, kwargs):
        r = wrapped(*args, **kwargs)

        # create and attach a pin with the defaults
        Pin(
            service=config['service_name'],
            app=config['app'],
            tags=config.get('tags', {}),
            tracer=config.get('tracer', ddtrace.tracer),
            _config=config['patch'][patch_item],
        ).onto(instance)
        return r
github DataDog / dd-trace-py / ddtrace / contrib / sqlalchemy / engine.py View on Github external
def __init__(self, tracer, service, engine):
        self.tracer = tracer
        self.engine = engine
        self.vendor = sqlx.normalize_vendor(engine.name)
        self.service = service or self.vendor
        self.name = '%s.query' % self.vendor

        # attach the PIN
        Pin(
            app=self.vendor,
            tracer=tracer,
            service=self.service,
            app_type=sqlx.APP_TYPE,
        ).onto(engine)

        listen(engine, 'before_cursor_execute', self._before_cur_exec)
        listen(engine, 'after_cursor_execute', self._after_cur_exec)
        listen(engine, 'dbapi_error', self._dbapi_error)
github DataDog / dd-trace-py / ddtrace / contrib / jinja2 / patch.py View on Github external
def _wrap_load_template(wrapped, instance, args, kwargs):
    pin = Pin.get_from(instance)
    if not pin or not pin.enabled():
        return wrapped(*args, **kwargs)

    template_name = kwargs.get('name', args[0])
    with pin.tracer.trace('jinja2.load', pin.service, span_type=SpanTypes.TEMPLATE) as span:
        template = None
        try:
            template = wrapped(*args, **kwargs)
            return template
        finally:
            span.resource = template_name
            span.set_tag('jinja2.template_name', template_name)
            if template:
                span.set_tag('jinja2.template_path', template.filename)
github DataDog / dd-trace-py / ddtrace / contrib / dbapi / __init__.py View on Github external
def __init__(self, conn, pin=None, cursor_cls=None):
        # Set default cursor class if one was not provided
        if not cursor_cls:
            # Do not trace `fetch*` methods by default
            cursor_cls = TracedCursor
            if config.dbapi2.trace_fetch_methods:
                cursor_cls = FetchTracedCursor

        super(TracedConnection, self).__init__(conn)
        name = _get_vendor(conn)
        self._self_datadog_name = '{}.connection'.format(name)
        db_pin = pin or Pin(service=name, app=name)
        db_pin.onto(self)
        # wrapt requires prefix of `_self` for attributes that are only in the
        # proxy (since some of our source objects will use `__slots__`)
        self._self_cursor_cls = cursor_cls