Пример #1
0
    def call(self, module, method, wrapped, instance, args, kwargs):
        name = getattr(instance, 'name', None)

        if not name:
            name = '<template string>'
        with trace(name, "template.django"):
            return wrapped(*args, **kwargs)
Пример #2
0
    def call(self, module, method, wrapped, instance, args, kwargs):
        if 'method' in kwargs:
            method = kwargs['method']
        else:
            method = args[0]

        if 'url' in kwargs:
            url = kwargs['url']
        else:
            url = args[1]

        signature = method.upper()

        if url:
            parsed_url = urlparse.urlparse(url)
            host = parsed_url.hostname or " "
            signature += " " + host

            if parsed_url.port and \
               default_ports.get(parsed_url.scheme) != parsed_url.port:
                signature += ":" + str(parsed_url.port)

        with trace(signature, "ext.http.requests",
                   {'url': url}, leaf=True):
            return wrapped(*args, **kwargs)
Пример #3
0
    def test_requests_instrumentation(self, _):
        self.client.begin_transaction("transaction.test")
        with trace("test_pipeline", "test"):
            requests.get('http://example.com')
        self.client.end_transaction("MyView")

        _, traces = self.client.instrumentation_store.get_all()
        self.assertIn('GET example.com', map(lambda x: x['signature'], traces))
Пример #4
0
 def _trace_sql(self, method, sql, params):
     signature = self.extract_signature(sql)
     kind = "db.{0}.sql".format(self.provider_name)
     with trace(signature, kind, {"sql": sql}):
         if params is None:
             return method(sql)
         else:
             return method(sql, params)
Пример #5
0
    def call(self, module, method, wrapped, instance, args, kwargs):
        signature = ".".join([module, method])

        if len(args) == 1:
            signature += " " + str(args[0])

        with trace(signature, "db.sqlite.connect"):
            return SQLiteConnectionProxy(wrapped(*args, **kwargs))
Пример #6
0
    def call(self, module, method, wrapped, instance, args, kwargs):
        if len(args) > 0:
            wrapped_name = str(args[0])
        else:
            wrapped_name = self.get_wrapped_name(wrapped, instance, method)

        with trace(wrapped_name, "cache.redis", leaf=True):
            return wrapped(*args, **kwargs)
    def test_lru_get_frames_cache(self):
        self.requests_store.transaction_start(None, "transaction.test")

        for i in range(10):
            with trace("bleh", "custom"):
                time.sleep(0.01)

        self.assertEqual(self.mock_get_frames.call_count, 1)
Пример #8
0
    def call(self, module, method, wrapped, instance, args, kwargs):
        if 'request' in kwargs:
            request = kwargs['request']
        else:
            request = args[0]

        signature = request.method.upper()
        signature += " " + get_host_from_url(request.url)

        with trace(signature, "ext.http.requests",
                   {'url': request.url}, leaf=True):
            return wrapped(*args, **kwargs)
    def test_leaf_tracing(self):
        self.requests_store.transaction_start(None, "transaction.test")

        with trace("root", "custom"):
            with trace("child1-leaf", "custom", leaf=True):

                # These two traces should not show up
                with trace("ignored-child1", "custom", leaf=True):
                    time.sleep(0.01)

                with trace("ignored-child2", "custom", leaf=False):
                    time.sleep(0.01)

        self.requests_store.transaction_end(None, "transaction")

        transactions, traces = self.requests_store.get_all()

        self.assertEqual(len(traces), 3)

        signatures = ['transaction', 'root', 'child1-leaf']
        self.assertEqual(set([t['signature'] for t in traces]),
                         set(signatures))
Пример #10
0
    def test_requests_instrumentation_via_session(self, mock_send):
        mock_send.return_value = mock.Mock(
            url='http://example.com',
            history=[],
            headers={'location': ''},
        )
        self.client.begin_transaction("transaction.test")
        with trace("test_pipeline", "test"):
            s = requests.Session()
            s.get('http://example.com', allow_redirects=False)
        self.client.end_transaction("MyView")

        _, traces = self.client.instrumentation_store.get_all()
        self.assertIn('GET example.com', map(lambda x: x['signature'], traces))
Пример #11
0
    def test_requests_instrumentation(self, mock_send):
        mock_send.return_value = mock.Mock(
            url='http://example.com',
            history=[],
            headers={'location': ''},
        )
        self.client.begin_transaction("transaction.test")
        with trace("test_pipeline", "test"):
            # NOTE: The `allow_redirects` argument has to be set to `False`,
            # because mocking is done a level deeper, and the mocked response
            # from the `HTTPAdapter` is about to be used to make further
            # requests to resolve redirects, which doesn't make sense for this
            # test case.
            requests.get('http://example.com', allow_redirects=False)
        self.client.end_transaction("MyView")

        _, traces = self.client.instrumentation_store.get_all()
        self.assertIn('GET example.com', map(lambda x: x['signature'], traces))
Пример #12
0
    def call(self, module, method, wrapped, instance, args, kwargs):
        signature = "psycopg2.connect"

        host = kwargs.get('host')
        if host:
            signature += " " + str(host)

            port = kwargs.get('port')
            if port:
                port = str(port)
                if int(port) != default_ports.get("postgresql"):
                    signature += ":" + port
        else:
            # Parse connection string and extract host/port
            pass

        with trace(signature, "db.postgreql.connect"):
            return PGConnectionProxy(wrapped(*args, **kwargs))
Пример #13
0
    def call(self, module, method, wrapped, instance, args, kwargs):
        if 'operation_name' in kwargs:
            operation_name = kwargs['operation_name']
        else:
            operation_name = args[0]

        target_endpoint = instance._endpoint.host
        parsed_url = urlparse.urlparse(target_endpoint)
        service, region, _ = parsed_url.hostname.split('.', 2)

        signature = '{}:{}'.format(service, operation_name)
        extra_data = {
            'service': service,
            'region': region,
            'operation': operation_name,
        }

        with trace(signature, 'ext.http.aws', extra_data, leaf=True):
            return wrapped(*args, **kwargs)
Пример #14
0
    def test_rq_patches_redis(self, should_collect):
        should_collect.return_value = False

        # Let's go ahead and change how something important works
        conn = redis.StrictRedis()
        conn._pipeline = partial(StrictRedis.pipeline, conn)

        self.client.begin_transaction("transaction.test")
        with trace("test_pipeline", "test"):
            # conn = redis.StrictRedis()
            pipeline = conn._pipeline()
            pipeline.rpush("mykey", "a", "b")
            pipeline.expire("mykey", 1000)
            pipeline.execute()
        self.client.end_transaction("MyView")

        transactions, traces = self.client.instrumentation_store.get_all()

        expected_signatures = ['transaction', 'test_pipeline',
                               'StrictPipeline.execute']

        self.assertEqual(set([t['signature'] for t in traces]),
                         set(expected_signatures))

        # Reorder according to the kinds list so we can just test them
        sig_dict = dict([(t['signature'], t) for t in traces])
        traces = [sig_dict[k] for k in expected_signatures]

        self.assertEqual(traces[0]['signature'], 'transaction')
        self.assertEqual(traces[0]['kind'], 'transaction')
        self.assertEqual(traces[0]['transaction'], 'MyView')

        self.assertEqual(traces[1]['signature'], 'test_pipeline')
        self.assertEqual(traces[1]['kind'], 'test')
        self.assertEqual(traces[1]['transaction'], 'MyView')

        self.assertEqual(traces[2]['signature'], 'StrictPipeline.execute')
        self.assertEqual(traces[2]['kind'], 'cache.redis')
        self.assertEqual(traces[2]['transaction'], 'MyView')

        self.assertEqual(len(traces), 3)
Пример #15
0
    def test_redis_client(self, should_collect):
        should_collect.return_value = False
        self.client.begin_transaction("transaction.test")
        with trace("test_redis_client", "test"):
            conn = redis.StrictRedis()
            conn.rpush("mykey", "a", "b")
            conn.expire("mykey", 1000)
        self.client.end_transaction("MyView")

        transactions, traces = self.client.instrumentation_store.get_all()

        expected_signatures = ['transaction', 'test_redis_client',
                               'RPUSH', 'EXPIRE']

        self.assertEqual(set([t['signature'] for t in traces]),
                         set(expected_signatures))

        # Reorder according to the kinds list so we can just test them
        sig_dict = dict([(t['signature'], t) for t in traces])
        traces = [sig_dict[k] for k in expected_signatures]

        self.assertEqual(traces[0]['signature'], 'transaction')
        self.assertEqual(traces[0]['kind'], 'transaction')
        self.assertEqual(traces[0]['transaction'], 'MyView')

        self.assertEqual(traces[1]['signature'], 'test_redis_client')
        self.assertEqual(traces[1]['kind'], 'test')
        self.assertEqual(traces[1]['transaction'], 'MyView')

        self.assertEqual(traces[2]['signature'], 'RPUSH')
        self.assertEqual(traces[2]['kind'], 'cache.redis')
        self.assertEqual(traces[2]['transaction'], 'MyView')

        self.assertEqual(traces[3]['signature'], 'EXPIRE')
        self.assertEqual(traces[3]['kind'], 'cache.redis')
        self.assertEqual(traces[3]['transaction'], 'MyView')

        self.assertEqual(len(traces), 4)
Пример #16
0
    def test_urllib3(self, should_collect):
        should_collect.return_value = False
        self.client.begin_transaction("transaction")
        expected_sig = 'GET localhost:{0}'.format(self.port)
        with trace("test_pipeline", "test"):
            pool = urllib3.PoolManager(timeout=0.1)

            url = 'http://localhost:{0}/hello_world'.format(self.port)
            r = pool.request('GET', url)

        self.client.end_transaction("MyView")

        transactions, traces = self.client.instrumentation_store.get_all()

        expected_signatures = ['transaction', 'test_pipeline', expected_sig]

        self.assertEqual(set([t['signature'] for t in traces]),
                         set(expected_signatures))

        # Reorder according to the kinds list so we can just test them
        sig_dict = dict([(t['signature'], t) for t in traces])
        traces = [sig_dict[k] for k in expected_signatures]

        self.assertEqual(len(traces), 3)

        self.assertEqual(traces[0]['signature'], 'transaction')
        self.assertEqual(traces[0]['kind'], 'transaction')
        self.assertEqual(traces[0]['transaction'], 'MyView')

        self.assertEqual(traces[1]['signature'], 'test_pipeline')
        self.assertEqual(traces[1]['kind'], 'test')
        self.assertEqual(traces[1]['transaction'], 'MyView')

        self.assertEqual(traces[2]['signature'], expected_sig)
        self.assertEqual(traces[2]['kind'], 'ext.http.urllib3')
        self.assertEqual(traces[2]['transaction'], 'MyView')

        self.assertEqual(traces[2]['extra']['url'], url)
Пример #17
0
    def call(self, module, method, wrapped, instance, args, kwargs):
        if 'method' in kwargs:
            method = kwargs['method']
        else:
            method = args[0]

        host = instance.host

        if instance.port != default_ports.get(instance.scheme):
            host += ":" + str(instance.port)

        if 'url' in kwargs:
            url = kwargs['url']
        else:
            url = args[1]

        signature = method.upper() + " " + host

        url = instance.scheme + "://" + host + url

        with trace(signature, "ext.http.urllib3",
                                       {'url': url}, leaf=True):
            return wrapped(*args, **kwargs)
    def test_memcached(self, should_collect):
        should_collect.return_value = False
        self.client.begin_transaction("transaction.test")
        with trace("test_memcached", "test"):
            conn = memcache.Client(['127.0.0.1:11211'], debug=0)
            conn.set("mykey", "a")
            assert "a" == conn.get("mykey")
            assert {"mykey": "a"} == conn.get_multi(["mykey", "myotherkey"])
        self.client.end_transaction("BillingView")

        transactions, traces = self.client.instrumentation_store.get_all()

        expected_signatures = ['transaction', 'test_memcached',
                               'Client.set', 'Client.get',
                               'Client.get_multi']

        self.assertEqual(set([t['signature'] for t in traces]),
                         set(expected_signatures))

        # Reorder according to the kinds list so we can just test them
        sig_dict = dict([(t['signature'], t) for t in traces])
        traces = [sig_dict[k] for k in expected_signatures]

        self.assertEqual(traces[0]['signature'], 'transaction')
        self.assertEqual(traces[0]['kind'], 'transaction')
        self.assertEqual(traces[0]['transaction'], 'BillingView')

        self.assertEqual(traces[1]['signature'], 'test_memcached')
        self.assertEqual(traces[1]['kind'], 'test')
        self.assertEqual(traces[1]['transaction'], 'BillingView')

        self.assertEqual(traces[2]['signature'], 'Client.set')
        self.assertEqual(traces[2]['kind'], 'cache.memcached')
        self.assertEqual(traces[2]['transaction'], 'BillingView')

        self.assertEqual(len(traces), 5)
Пример #19
0
 def something_expensive():
     with trace("something_expensive", "code"):
         for i in range(100):
             users = list(User.objects.all())
     return users
Пример #20
0
 def something_expensive():
     with trace("something_expensive", "code"):
         return [User(username='Ron'), User(username='Beni')]
Пример #21
0
 def call(self, module, method, wrapped, instance, args, kwargs):
     signature = instance.name or instance.filename
     with trace(signature, "template.jinja2"):
         return wrapped(*args, **kwargs)
Пример #22
0
 def test_requests_instrumentation_malformed_schema(self):
     self.client.begin_transaction("transaction.test")
     with trace("test_pipeline", "test"):
         self.assertRaises(MissingSchema, requests.get, '')
Пример #23
0
 def test_requests_instrumentation_malformed_path(self):
     self.client.begin_transaction("transaction.test")
     with trace("test_pipeline", "test"):
         self.assertRaises(InvalidURL, requests.get, 'http://')
Пример #24
0
 def something_expensive():
     with trace("something_expensive", "code"):
         return [User(username="Ron"), User(username="Beni")]
Пример #25
0
 def call(self, module, method, wrapped, instance, args, kwargs):
     wrapped_name = self.get_wrapped_name(wrapped, instance, method)
     with trace(wrapped_name, "cache.memcached"):
         return wrapped(*args, **kwargs)
Пример #26
0
 def call(self, module, method, wrapped, instance, args, kwargs):
     wrapped_name = module + "." + method
     with trace(wrapped_name, "compression.zlib"):
         return wrapped(*args, **kwargs)