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_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, 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_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()
import os import sys import pyramid.tweens from opbeat.instrumentation import control from pyramid import events from pyramid import httpexceptions from pyramid import settings from opbeat_pyramid import tweens control.instrument() DEFAULT_UNKNOWN_ROUTE_TEXT = 'Unknown Route' NO_DEFAULT_PROVIDED = {} OPBEAT_SETTING_PREFIX = 'opbeat.' TRUTHY_VALUES = {True, 'true', 'yes', 'on'} DEFAULT_UNSAFE_SETTINGS_PHRASES = ( 'token,password,passphrase,' 'secret,private,key' ) logger = logging.getLogger(__name__)