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 = self.client.instrumentation_store.get_all() traces = transactions[0]['traces'] expected_signatures = ['transaction', 'test_redis_client', 'RPUSH', 'EXPIRE'] self.assertEqual(set([t['name'] for t in traces]), set(expected_signatures)) self.assertEqual(traces[0]['name'], 'RPUSH') self.assertEqual(traces[0]['type'], 'cache.redis') self.assertEqual(traces[1]['name'], 'EXPIRE') self.assertEqual(traces[1]['type'], 'cache.redis') self.assertEqual(traces[2]['name'], 'test_redis_client') self.assertEqual(traces[2]['type'], 'test') self.assertEqual(traces[3]['name'], 'transaction') self.assertEqual(traces[3]['type'], 'transaction') self.assertEqual(len(traces), 4)
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)
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 = self.client.instrumentation_store.get_all() traces = transactions[0]['traces'] expected_signatures = ['transaction', 'test_pipeline', expected_sig] self.assertEqual(set([t['name'] for t in traces]), set(expected_signatures)) self.assertEqual(len(traces), 3) self.assertEqual(traces[0]['name'], expected_sig) self.assertEqual(traces[0]['type'], 'ext.http.urllib3') self.assertEqual(traces[0]['context']['url'], url) self.assertEqual(traces[0]['parent'], 1) self.assertEqual(traces[1]['name'], 'test_pipeline') self.assertEqual(traces[1]['type'], 'test') self.assertEqual(traces[2]['name'], 'transaction') self.assertEqual(traces[2]['type'], 'transaction')
def test_pipeline(self, should_collect): should_collect.return_value = False 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 = self.client.instrumentation_store.get_all() traces = transactions[0]['traces'] expected_signatures = ['transaction', 'test_pipeline', 'StrictPipeline.execute'] self.assertEqual(set([t['name'] for t in traces]), set(expected_signatures)) self.assertEqual(traces[0]['name'], 'StrictPipeline.execute') self.assertEqual(traces[0]['type'], 'cache.redis') self.assertEqual(traces[1]['name'], 'test_pipeline') self.assertEqual(traces[1]['type'], 'test') self.assertEqual(traces[2]['name'], 'transaction') self.assertEqual(traces[2]['type'], 'transaction') self.assertEqual(len(traces), 3)
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 = self.client.instrumentation_store.get_all() traces = transactions[0]['traces'] expected_signatures = ['transaction', 'test_pipeline', 'StrictPipeline.execute'] self.assertEqual(set([t['name'] for t in traces]), set(expected_signatures)) self.assertEqual(traces[0]['name'], 'StrictPipeline.execute') self.assertEqual(traces[0]['type'], 'cache.redis') self.assertEqual(traces[1]['name'], 'test_pipeline') self.assertEqual(traces[1]['type'], 'test') self.assertEqual(traces[2]['name'], 'transaction') self.assertEqual(traces[2]['type'], 'transaction') self.assertEqual(len(traces), 3)
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 _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)
def test_lru_get_frames_cache(self): self.requests_store.begin_transaction("transaction.test") for i in range(10): with trace("bleh", "custom"): time.sleep(0.01) self.assertEqual(self.mock_get_frames.call_count, 10)
def test_leaf_tracing(self): self.requests_store.begin_transaction("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.end_transaction(None, "transaction") transactions = self.requests_store.get_all() traces = transactions[0]['traces'] self.assertEqual(len(traces), 3) signatures = ['transaction', 'root', 'child1-leaf'] self.assertEqual(set([t['name'] for t in traces]), set(signatures))
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_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") transactions = self.client.instrumentation_store.get_all() traces = transactions[0]['traces'] self.assertEqual('GET example.com', traces[0]['name']) self.assertEqual('http://example.com/', traces[0]['context']['url'])
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))
def test_botocore_instrumentation(self, mock_make_request): mock_response = mock.Mock() mock_response.status_code = 200 mock_make_request.return_value = (mock_response, {}) self.client.begin_transaction("transaction.test") with trace("test_pipeline", "test"): session = boto3.Session(aws_access_key_id='foo', aws_secret_access_key='bar', region_name='us-west-2') ec2 = session.client('ec2') ec2.describe_instances() self.client.end_transaction("MyView") transactions = self.client.instrumentation_store.get_all() traces = transactions[0]['traces'] self.assertIn('ec2:DescribeInstances', map(lambda x: x['name'], traces))
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)
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") transactions = self.client.instrumentation_store.get_all() traces = transactions[0]['traces'] self.assertEqual('GET example.com', traces[0]['name']) self.assertEqual('http://example.com/', traces[0]['context']['url'])
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 = self.client.instrumentation_store.get_all() traces = transactions[0]['traces'] expected_signatures = ['transaction', 'test_memcached', 'Client.set', 'Client.get', 'Client.get_multi'] self.assertEqual(set([t['name'] for t in traces]), set(expected_signatures)) self.assertEqual(traces[0]['name'], 'Client.set') self.assertEqual(traces[0]['type'], 'cache.memcached') self.assertEqual(traces[0]['parent'], 1) self.assertEqual(traces[1]['name'], 'Client.get') self.assertEqual(traces[1]['type'], 'cache.memcached') self.assertEqual(traces[1]['parent'], 1) self.assertEqual(traces[2]['name'], 'Client.get_multi') self.assertEqual(traces[2]['type'], 'cache.memcached') self.assertEqual(traces[2]['parent'], 1) self.assertEqual(traces[3]['name'], 'test_memcached') self.assertEqual(traces[3]['type'], 'test') self.assertEqual(traces[4]['name'], 'transaction') self.assertEqual(traces[4]['type'], 'transaction') self.assertEqual(len(traces), 5)
def call(self, module, method, wrapped, instance, args, kwargs): collection = instance.collection signature = '.'.join([collection.full_name, 'cursor.refresh']) with trace(signature, "db.mongodb.query"): return wrapped(*args, **kwargs)
def call(self, module, method, wrapped, instance, args, kwargs): collection = instance._BulkOperationBuilder__bulk.collection signature = '.'.join([collection.full_name, 'bulk.execute']) with trace(signature, "db.mongodb.query"): return wrapped(*args, **kwargs)
def call(self, module, method, wrapped, instance, args, kwargs): cls_name, method_name = method.split('.', 1) signature = '.'.join([instance.full_name, method_name]) with trace(signature, "db.mongodb.query", leaf=True): return wrapped(*args, **kwargs)
def test_requests_instrumentation_malformed_schema(self): self.client.begin_transaction("transaction.test") with trace("test_pipeline", "test"): self.assertRaises(MissingSchema, requests.get, '')
def test_requests_instrumentation_malformed_path(self): self.client.begin_transaction("transaction.test") with trace("test_pipeline", "test"): self.assertRaises(InvalidURL, requests.get, 'http://')
def call(self, module, method, wrapped, instance, args, kwargs): name = self.get_wrapped_name(wrapped, instance, method) with trace(name, "cache.memcached"): return wrapped(*args, **kwargs)
def something_expensive(): with trace("something_expensive", "code"): return [User(username='******'), User(username='******')]
def something_expensive(): with trace("something_expensive", "code"): for i in range(100): users = list(User.objects.all()) return users
def call(self, module, method, wrapped, instance, args, kwargs): wrapped_name = module + "." + method with trace(wrapped_name, "compression.zlib"): return wrapped(*args, **kwargs)