def test_traced_transaction_nested(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, False) sqlalchemy_opentracing.register_engine(self.engine) creat = CreateTable(self.users_table) ins = self.users_table.insert().values(name='John Doe') sel = select([self.users_table]) parent_span = DummySpan('parent span') conn = self.engine.connect() with conn.begin() as trans: sqlalchemy_opentracing.set_parent_span(conn, parent_span) conn.execute(creat) with conn.begin() as trans2: conn.execute(ins) conn.execute(sel) self.assertEqual(3, len(tracer.spans)) self.assertEqual(True, all(map(lambda x: x.is_finished, tracer.spans))) self.assertEqual( True, all(map(lambda x: x.child_of == parent_span, tracer.spans))) self.assertEqual(['create_table', 'insert', 'select'], map(lambda x: x.operation_name, tracer.spans))
def test_traced_error(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, False) sqlalchemy_opentracing.register_engine(self.engine) # Don't trace this one. session = self.session session.add(User(name='John Doe', id=1)) session.commit() # Trace this one. sqlalchemy_opentracing.set_traced(session) session.add(User(name='John Doe', id=1)) try: session.commit() except IntegrityError: pass self.assertEqual(1, len(tracer.spans)) self.assertEqual('insert', tracer.spans[0].operation_name) self.assertEqual(True, tracer.spans[0].is_finished) self.assertEqual(tracer.spans[0].tags, { 'component': 'sqlalchemy', 'db.statement': 'INSERT INTO users (id, name) VALUES (?, ?)', 'db.type': 'sql', 'sqlalchemy.dialect': 'sqlite', 'sqlalchemy.exception': 'UNIQUE constraint failed: users.id', 'error': 'true', })
def test_traced_after_rollback(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, False) sqlalchemy_opentracing.register_engine(self.engine) creat = CreateTable(self.users_table) # Create a table, but don't trace it conn = self.engine.connect() with conn.begin() as tx: conn.execute(creat) try: with conn.begin() as tx: sqlalchemy_opentracing.set_traced(conn) conn.execute(creat) except OperationalError: pass self.assertEqual(1, len(tracer.spans)) # Do something right after with this connection, # no tracing should happen. tracer.clear() ins = self.users_table.insert().values(name='John Doe') with conn.begin() as tx: conn.execute(ins) self.assertEqual(0, len(tracer.spans))
def test_traced_rollback(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, False) sqlalchemy_opentracing.register_engine(self.engine) creat = CreateTable(self.users_table) ins = self.users_table.insert().values(name='John Doe') # Don't trace this. self.engine.execute(creat) parent_span = DummySpan('parent span') conn = self.engine.connect() try: with conn.begin() as tx: sqlalchemy_opentracing.set_parent_span(conn, parent_span) conn.execute(ins) conn.execute(creat) except OperationalError: pass self.assertEqual(2, len(tracer.spans)) self.assertEqual(True, all(map(lambda x: x.is_finished, tracer.spans))) self.assertEqual( True, all(map(lambda x: x.child_of == parent_span, tracer.spans))) self.assertEqual(['insert', 'create_table'], map(lambda x: x.operation_name, tracer.spans)) self.assertEqual(['false', 'true'], map(lambda x: x.tags.get('error', 'false'), tracer.spans))
def test_traced_error(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, False) sqlalchemy_opentracing.register_engine(self.engine) creat = CreateTable(self.users_table) self.engine.execute(creat) self.assertEqual(0, len(tracer.spans)) sqlalchemy_opentracing.set_traced(creat) try: self.engine.execute(creat) except OperationalError: pass # Do nothing - it's responsibility of OT to finish tracing it. self.assertEqual(1, len(tracer.spans)) self.assertEqual(tracer.spans[0].is_finished, True) self.assertEqual( tracer.spans[0].tags, { 'component': 'sqlalchemy', 'db.statement': 'CREATE TABLE users (id INTEGER NOT NULL, name VARCHAR, PRIMARY KEY (id))', 'db.type': 'sql', 'sqlalchemy.dialect': 'sqlite', 'sqlalchemy.exception': 'table users already exists', 'error': 'true', }) self.assertEqual(False, sqlalchemy_opentracing.get_traced(creat))
def test_traced_text_error(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, False) sqlalchemy_opentracing.register_engine(self.engine) session = self.session span = DummySpan('parent span') sqlalchemy_opentracing.set_parent_span(session, span) try: session.execute('SELECT zipcode FROM addresses') except OperationalError: pass self.assertEqual(1, len(tracer.spans)) self.assertEqual(tracer.spans[0].operation_name, 'textclause') self.assertEqual(tracer.spans[0].is_finished, True) self.assertEqual(tracer.spans[0].child_of, span) self.assertEqual(tracer.spans[0].tags, { 'component': 'sqlalchemy', 'db.statement': 'SELECT zipcode FROM addresses', 'db.type': 'sql', 'sqlalchemy.dialect': 'sqlite', 'sqlalchemy.exception': 'no such table: addresses', 'error': 'true', })
def test_register_dup(self): engine = create_engine('sqlite:///:memory:') sqlalchemy_opentracing.init_tracing(DummyTracer(), trace_all_engines=True) with self.assertRaises(RuntimeError): sqlalchemy_opentracing.register_engine(engine) # Manually clear the Engine from listening events. sqlalchemy_opentracing.unregister_engine(Engine)
def test_traced_none(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, False) sqlalchemy_opentracing.register_engine(self.engine) creat = CreateTable(self.users_table) self.engine.execute(creat) self.assertEqual(0, len(tracer.spans))
def test_traced_none(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, False) sqlalchemy_opentracing.register_engine(self.engine) session = self.session session.add(User(name='John Doe')) session.commit() self.assertEqual(0, len(tracer.spans))
def test_traced_all(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, trace_all_queries=True) sqlalchemy_opentracing.register_engine(self.engine) session = self.session session.add(User(name='John Doe')) session.add(User(name='Jason Bourne')) session.add(User(name='Foo Bar')) session.commit() self.assertEqual(3, len(tracer.spans)) self.assertEqual(True, all(map(lambda x: x.operation_name == 'insert', tracer.spans))) self.assertEqual(True, all(map(lambda x: x.is_finished, tracer.spans)))
def test_traced_clear_session(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, False) sqlalchemy_opentracing.register_engine(self.engine) session = self.session sqlalchemy_opentracing.set_traced(session) session.add(User(name='John Doe')) session.add(User(name='Jason Bourne')) # Clear the tracing info right before committing. sqlalchemy_opentracing.clear_traced(session) session.commit() self.assertEqual(0, len(tracer.spans))
def test_traced_parent(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, False) sqlalchemy_opentracing.register_engine(self.engine) session = self.session parent_span = DummySpan('parent') sqlalchemy_opentracing.set_parent_span(session, parent_span) session.query(User).all() session.query(User).all() session.commit() self.assertEqual(2, len(tracer.spans)) self.assertEqual(True, all(map(lambda x: x.operation_name == 'select', tracer.spans))) self.assertEqual(True, all(map(lambda x: x.is_finished, tracer.spans))) self.assertEqual(True, all(map(lambda x: x.child_of == parent_span, tracer.spans)))
def test_traced_savepoint(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, False) sqlalchemy_opentracing.register_engine(self.engine) session = self.session sqlalchemy_opentracing.set_traced(session) session.add(User(name='John Doe')) session.begin_nested() session.add(User(name='Jason Bourne')) session.commit() session.add(User(name='Paris Texas')) session.commit() self.assertEqual(3, len(tracer.spans)) self.assertEqual(True, all(map(lambda x: x.is_finished, tracer.spans)))
def test_unregister_engine(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, trace_all_queries=True) sqlalchemy_opentracing.register_engine(self.engine) creat = CreateTable(self.users_table) self.engine.execute(creat) self.assertEqual(1, len(tracer.spans)) tracer.clear() sqlalchemy_opentracing.unregister_engine(self.engine) # Further events should cause no spans at all. sel = select([self.users_table]) sqlalchemy_opentracing.set_traced(sel) self.engine.execute(sel) self.assertEqual(0, len(tracer.spans))
def test_traced_simple(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, False) sqlalchemy_opentracing.register_engine(self.engine) session = self.session sqlalchemy_opentracing.set_traced(session) session.add(User(name='John Doe')) session.commit() self.assertEqual(1, len(tracer.spans)) self.assertEqual('insert', tracer.spans[0].operation_name) self.assertEqual(True, tracer.spans[0].is_finished) self.assertEqual(tracer.spans[0].tags, { 'component': 'sqlalchemy', 'db.statement': 'INSERT INTO users (name) VALUES (?)', 'db.type': 'sql', 'sqlalchemy.dialect': 'sqlite', })
def test_traced_after_commit(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, False) sqlalchemy_opentracing.register_engine(self.engine) session = self.session sqlalchemy_opentracing.set_traced(session) session.add(User(name='John Doe')) session.commit() self.assertEqual(1, len(tracer.spans)) tracer.clear() # Issue a pair of statements, # making sure we are not tracing # the session's transaction anymore. session.add(User(name='Jason Bourne')) session.query(User).all() session.commit() self.assertEqual(0, len(tracer.spans))
def test_traced_clear_connection(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, False) sqlalchemy_opentracing.register_engine(self.engine) creat = CreateTable(self.users_table) ins = self.users_table.insert().values(name='John Doe') conn = self.engine.connect() with conn.begin() as tx: sqlalchemy_opentracing.set_traced(conn) conn.execute(creat) # Stop tracing from this point. sqlalchemy_opentracing.clear_traced(conn) conn.execute(ins) self.assertEqual(1, len(tracer.spans)) self.assertEqual('create_table', tracer.spans[0].operation_name)
def test_traced_bulk_insert(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, False) sqlalchemy_opentracing.register_engine(self.engine) parent_span = DummySpan('parent') session = self.session sqlalchemy_opentracing.set_parent_span(session, parent_span) users = [User(name = 'User-%s' % i) for i in range(10)] session.bulk_save_objects(users) self.assertEqual(1, len(tracer.spans)) self.assertEqual(True, tracer.spans[0].is_finished) self.assertEqual(parent_span, tracer.spans[0].child_of) self.assertEqual(tracer.spans[0].tags, { 'component': 'sqlalchemy', 'db.statement': 'INSERT INTO users (name) VALUES (?)', 'db.type': 'sql', 'sqlalchemy.dialect': 'sqlite', })
def test_traced_text(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, False) sqlalchemy_opentracing.register_engine(self.engine) session = self.session span = DummySpan('parent span') sqlalchemy_opentracing.set_parent_span(session, span) session.execute('SELECT name FROM users') self.assertEqual(1, len(tracer.spans)) self.assertEqual(tracer.spans[0].operation_name, 'textclause') self.assertEqual(tracer.spans[0].is_finished, True) self.assertEqual(tracer.spans[0].child_of, span) self.assertEqual(tracer.spans[0].tags, { 'component': 'sqlalchemy', 'db.statement': 'SELECT name FROM users', 'db.type': 'sql', 'sqlalchemy.dialect': 'sqlite', })
def test_trace_text(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, trace_all_queries=True) sqlalchemy_opentracing.register_engine(self.engine) self.engine.execute( 'CREATE TABLE users (id INTEGER NOT NULL, name VARCHAR, PRIMARY KEY (id))' ) self.assertEqual(1, len(tracer.spans)) self.assertEqual(tracer.spans[0].operation_name, 'textclause') self.assertEqual(tracer.spans[0].is_finished, True) self.assertEqual( tracer.spans[0].tags, { 'component': 'sqlalchemy', 'db.statement': 'CREATE TABLE users (id INTEGER NOT NULL, name VARCHAR, PRIMARY KEY (id))', 'db.type': 'sql', 'sqlalchemy.dialect': 'sqlite', })
def test_traced(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, False) sqlalchemy_opentracing.register_engine(self.engine) creat = CreateTable(self.users_table) sqlalchemy_opentracing.set_traced(creat) self.engine.execute(creat) self.assertEqual(1, len(tracer.spans)) self.assertEqual(tracer.spans[0].operation_name, 'create_table') self.assertEqual(tracer.spans[0].is_finished, True) self.assertEqual( tracer.spans[0].tags, { 'component': 'sqlalchemy', 'db.statement': 'CREATE TABLE users (id INTEGER NOT NULL, name VARCHAR, PRIMARY KEY (id))', 'db.type': 'sql', 'sqlalchemy.dialect': 'sqlite', }) self.assertEqual(False, sqlalchemy_opentracing.get_traced(creat))
def test_traced_after_rollback(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, False) sqlalchemy_opentracing.register_engine(self.engine) session = self.session sqlalchemy_opentracing.set_traced(session) session.query(User).all() # will be evaluated RIGHT AWAY session.add(User(name='John Doe')) # delayed (not committed) session.rollback() self.assertEqual(1, len(tracer.spans)) self.assertEqual(True, tracer.spans[0].is_finished) self.assertEqual('select', tracer.spans[0].operation_name) tracer.clear() # Rollback should have stopped # the tracing for this session session.query(User).all() session.add(User(name='Jason Bourne')) session.commit() self.assertEqual(0, len(tracer.spans))
def test_traced_commit_repeat(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, False) sqlalchemy_opentracing.register_engine(self.engine) parent_span1 = DummySpan('parent1') session = self.session sqlalchemy_opentracing.set_parent_span(session, parent_span1) session.add(User(name='John Doe')) session.commit() self.assertEqual(1, len(tracer.spans)) self.assertEqual(True, tracer.spans[0].is_finished) self.assertEqual(parent_span1, tracer.spans[0].child_of) # Register the session again for tracing, # now with a different parent span parent_span2 = DummySpan('parent2') sqlalchemy_opentracing.set_parent_span(session, parent_span2) session.add(User(name='Jason Bourne')) session.commit() self.assertEqual(2, len(tracer.spans)) self.assertEqual(True, tracer.spans[1].is_finished) self.assertEqual(parent_span2, tracer.spans[1].child_of)
def test_trace_text_error(self): tracer = DummyTracer() sqlalchemy_opentracing.init_tracing(tracer, False, trace_all_queries=True) sqlalchemy_opentracing.register_engine(self.engine) try: self.engine.execute('SELECT name FROM users') except OperationalError: pass self.assertEqual(1, len(tracer.spans)) self.assertEqual(tracer.spans[0].operation_name, 'textclause') self.assertEqual(tracer.spans[0].is_finished, True) self.assertEqual( tracer.spans[0].tags, { 'component': 'sqlalchemy', 'db.statement': 'SELECT name FROM users', 'db.type': 'sql', 'sqlalchemy.dialect': 'sqlite', 'sqlalchemy.exception': 'no such table: users', 'error': 'true', })
def test_register_no_tracer(self): engine = create_engine('sqlite:///:memory:') with self.assertRaises(RuntimeError): sqlalchemy_opentracing.register_engine(engine)
class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) name = Column(String) if __name__ == '__main__': if os.path.exists(DB_LOCATION): os.remove(DB_LOCATION) # cleanup engine = create_engine('sqlite:///%s' % DB_LOCATION) session = sessionmaker(bind=engine)() sqlalchemy_opentracing.init_tracing(tracer) sqlalchemy_opentracing.register_engine(engine) User.metadata.create_all(engine) # Register the session for the current transaction. sqlalchemy_opentracing.set_traced(session) # Insert a set of rows. users = [User(name='User-%s' % i) for i in xrange(100)] session.bulk_save_objects(users) # Bulk saves objects without intermmediate steps, # so explicitly stop tracing the session. sqlalchemy_opentracing.clear_traced(session)