def test_psycopg2_composable_query_works(postgres_connection): """ Check that we pass queries with %-notation but without parameters properly to the dbapi backend """ client = get_client() control.instrument() cursor = postgres_connection.cursor() query = sql_compose.SQL( "SELECT * FROM {table} WHERE {row} LIKE 't%' ORDER BY {row} DESC" ).format( table=sql_compose.Identifier('test'), row=sql_compose.Identifier('name'), ) baked_query = query.as_string(cursor.__wrapped__) result = None try: client.begin_transaction("web.django") cursor.execute(query) result = cursor.fetchall() client.end_transaction(None, "test-transaction") finally: # make sure we've cleared out the spans for the other tests. assert [(2, 'two'), (3, 'three')] == result transactions, traces = client.instrumentation_store.get_all() traces = [t for t in traces if t['parents']] assert traces[0]['signature'] == 'SELECT FROM test' assert traces[0]['extra']['sql'] == baked_query
def test_psycopg2_composable_query_works(postgres_connection): """ Check that we pass queries with %-notation but without parameters properly to the dbapi backend """ client = get_client() control.instrument() cursor = postgres_connection.cursor() query = sql_compose.SQL("SELECT * FROM {table} WHERE {row} LIKE 't%' ORDER BY {row} DESC").format( table=sql_compose.Identifier('test'), row=sql_compose.Identifier('name'), ) baked_query = query.as_string(cursor.__wrapped__) result = None try: client.begin_transaction("web.django") cursor.execute(query) result = cursor.fetchall() client.end_transaction(None, "test-transaction") finally: # make sure we've cleared out the spans for the other tests. assert [(2, 'two'), (3, 'three')] == result transactions, traces = client.instrumentation_store.get_all() traces = [t for t in traces if t['parents']] assert traces[0]['signature'] == 'SELECT FROM test' assert traces[0]['extra']['sql'] == baked_query
def test_psycopg2_tracing_outside_of_elasticapm_transaction(postgres_connection): client = get_client() control.instrument() cursor = postgres_connection.cursor() # check that the cursor is a proxy, even though we're not in an elasticapm # transaction assert isinstance(cursor, PGCursorProxy) cursor.execute('SELECT 1') transactions = client.instrumentation_store.get_all() assert transactions == []
def test_psycopg2_tracing_outside_of_opbeat_transaction(postgres_connection): client = get_client() control.instrument() cursor = postgres_connection.cursor() # check that the cursor is a proxy, even though we're not in an opbeat # transaction assert isinstance(cursor, PGCursorProxy) cursor.execute('SELECT 1') transactions = client.instrumentation_store.get_all() assert transactions == ([], [])
def test_psycopg2_register_type(postgres_connection): import psycopg2.extras client = get_client() control.instrument() try: client.begin_transaction("web.django") new_type = psycopg2.extras.register_uuid(None, postgres_connection) client.end_transaction(None, "test-transaction") finally: # make sure we've cleared out the traces for the other tests. client.instrumentation_store.get_all() assert new_type is not None
def test_psycopg2_register_type(): import psycopg2.extras client = get_client() control.instrument(client) try: client.begin_transaction() conn = psycopg2.connect(database="opbeat_test", user="******") new_type = psycopg2.extras.register_uuid(None, conn) client.end_transaction(None, "test-transaction") finally: # make sure we've cleared out the traces for the other tests. client.instrumentation_store.get_all() assert new_type is not None
def test_psycopg2_select_LIKE(postgres_connection): """ Check that we pass queries with %-notation but without parameters properly to the dbapi backend """ client = get_client() control.instrument() cursor = postgres_connection.cursor() try: client.begin_transaction("web.django") cursor.execute("SELECT * FROM test WHERE name LIKE 't%'") cursor.fetchall() client.end_transaction(None, "test-transaction") finally: # make sure we've cleared out the traces for the other tests. transactions = client.instrumentation_store.get_all() traces = transactions[0]['traces'] assert traces[0]['name'] == 'SELECT FROM test'
def test_psycopg2_select_LIKE(postgres_connection): """ Check that we pass queries with %-notation but without parameters properly to the dbapi backend """ client = get_client() control.instrument() cursor = postgres_connection.cursor() try: client.begin_transaction("web.django") cursor.execute("SELECT * FROM test WHERE name LIKE 't%'") cursor.fetchall() client.end_transaction(None, "test-transaction") finally: # make sure we've cleared out the traces for the other tests. transactions, traces = client.instrumentation_store.get_all() traces = [t for t in traces if t['parents']] assert traces[0]['signature'] == 'SELECT FROM test'
def test_psycopg2_register_json(postgres_connection): # register_json bypasses register_type, so we have to test unwrapping # separately import psycopg2.extras client = get_client() control.instrument() try: client.begin_transaction("web.django") # as arg new_type = psycopg2.extras.register_json(postgres_connection, loads=lambda x: x) assert new_type is not None # as kwarg new_type = psycopg2.extras.register_json(conn_or_curs=postgres_connection, loads=lambda x: x) assert new_type is not None client.end_transaction(None, "test-transaction") finally: # make sure we've cleared out the traces for the other tests. client.instrumentation_store.get_all()
def test_instrument_nonexisting_method_on_module(): TestInstrumentNonExistingFunctionOnModule(get_client()).instrument()
def setUp(self): self.client = get_client() opbeat.instrumentation.control.instrument(self.client)
def setUp(self): self.client = get_client() opbeat.instrumentation.control.instrument()
def test_instrument_nonexisting_method(): TestInstrumentNonExistingMethod(get_client()).instrument()