Exemplo n.º 1
0
    def setUp(self):
        # create an engine with the given arguments
        self.engine = self.create_engine(self.ENGINE_ARGS)

        # create the database / entities and prepare a session for the test
        Base.metadata.drop_all(bind=self.engine)
        Base.metadata.create_all(self.engine, checkfirst=False)
        Session = sessionmaker(bind=self.engine)
        self.session = Session()

        # trace the engine
        self.tracer = get_dummy_tracer()
        trace_engine(self.engine, self.tracer)
Exemplo n.º 2
0
    def setUp(self):
        # create an engine with the given arguments
        self.engine = self.create_engine(self.ENGINE_ARGS)

        # create the database / entities and prepare a session for the test
        Base.metadata.drop_all(bind=self.engine)
        Base.metadata.create_all(self.engine, checkfirst=False)
        Session = sessionmaker(bind=self.engine)
        self.session = Session()

        # trace the engine
        self.tracer = get_dummy_tracer()
        trace_engine(self.engine, self.tracer)
Exemplo n.º 3
0
def _test_engine(engine, service, vendor, expected_meta):
    """ a test suite for various sqlalchemy engines. """
    tracer = Tracer()
    tracer.writer = DummyWriter()

    # create an engine and start tracing.
    trace_engine(engine, tracer, service=service)
    start = time.time()

    @contextlib.contextmanager
    def _connect():
        try:
            conn = engine.connect()
            yield conn
        finally:
            conn.close()

    with _connect() as conn:
        try:
            conn.execute("delete from players")
        except Exception:
            pass

    # boilerplate
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)
    session = Session()

    # do an ORM insert
    wayne = Player(id=1, name="wayne")
    session.add(wayne)
    session.commit()

    out = list(session.query(Player).filter_by(name="nothing"))
    eq_(len(out), 0)

    # do a regular old query that works
    with _connect() as conn:
        rows = conn.execute("select * from players").fetchall()
        eq_(len(rows), 1)
        eq_(rows[0]['name'], 'wayne')

    with _connect() as conn:
        try:
            conn.execute("select * from foo_Bah_blah")
        except Exception:
            pass
        else:
            assert 0

    end = time.time()

    spans = tracer.writer.pop()
    for span in spans:
        eq_(span.name, "%s.query" % vendor)
        eq_(span.service, service)
        eq_(span.span_type, "sql")

        for k, v in expected_meta.items():
            eq_(span.meta[k], v)

        # FIXME[matt] could be finer grained but i'm lazy
        assert start < span.start < end
        assert span.duration
        assert span.duration < end - start

    by_rsc = {s.resource: s for s in spans}

    # ensure errors work
    s = by_rsc["select * from foo_Bah_blah"]
    eq_(s.error, 1)
    assert "foo_Bah_blah" in s.get_tag(errorsx.ERROR_MSG)
    assert "foo_Bah_blah" in s.get_tag(errorsx.ERROR_STACK)

    expected = [
        "select * from players",
        "select * from foo_Bah_blah",
    ]

    for i in expected:
        assert i in by_rsc, "%s not in %s" % (i, by_rsc.keys())

    # ensure we have the service types
    services = tracer.writer.pop_services()
    expected = {service: {"app": vendor, "app_type": "db"}}
    eq_(services, expected)