Don't fail if sqlalchemy driver fails to initialize

Given that the driver is initialized during the service
startup (eg. like Keystone, Nova, ...) a osprofiler driver failure has
a huge impact on the service (when the driver fails, the whole service
is not usable).
We want to avoid that and just log error/exceptions but keep the
services running.

Change-Id: I5688f10364884a74b7eb44c0c8bda15730ccd424
Closes-Bug: 1819433
This commit is contained in:
Thomas Bechtold
2019-03-11 11:20:19 +01:00
parent da7a8592ae
commit add4794120

View File

@@ -34,18 +34,16 @@ class SQLAlchemyDriver(base.Driver):
from sqlalchemy import Table, MetaData, Column from sqlalchemy import Table, MetaData, Column
from sqlalchemy import String, JSON, Integer from sqlalchemy import String, JSON, Integer
except ImportError: except ImportError:
raise exc.CommandError( LOG.exception("To use this command, install 'SQLAlchemy'")
"To use this command, you should install 'SQLAlchemy'") else:
self._engine = create_engine(connection_str)
self._conn = self._engine.connect()
self._metadata = MetaData() self._metadata = MetaData()
self._data_table = Table( self._data_table = Table(
"data", self._metadata, "data", self._metadata,
Column("id", Integer, primary_key=True), Column("id", Integer, primary_key=True),
# timestamp - date/time of the trace point # timestamp - date/time of the trace point
Column("timestamp", String(26), index=True), Column("timestamp", String(26), index=True),
# base_id - uuid common for all notifications related to one trace # base_id - uuid common for all notifications related to
# one trace
Column("base_id", String(255), index=True), Column("base_id", String(255), index=True),
# parent_id - uuid of parent element in trace # parent_id - uuid of parent element in trace
Column("parent_id", String(255), index=True), Column("parent_id", String(255), index=True),
@@ -59,9 +57,17 @@ class SQLAlchemyDriver(base.Driver):
Column("data", JSON) Column("data", JSON)
) )
# we don't want to kill any service that does use osprofiler
try:
self._engine = create_engine(connection_str)
self._conn = self._engine.connect()
# FIXME(toabctl): Not the best idea to create the table on every # FIXME(toabctl): Not the best idea to create the table on every
# startup when using the sqlalchemy driver... # startup when using the sqlalchemy driver...
self._metadata.create_all(self._engine, checkfirst=True) self._metadata.create_all(self._engine, checkfirst=True)
except Exception:
LOG.exception("Failed to create engine/connection and setup "
"intial database tables")
@classmethod @classmethod
def get_name(cls): def get_name(cls):
@@ -79,6 +85,7 @@ class SQLAlchemyDriver(base.Driver):
service = data.pop("service", self.service) service = data.pop("service", self.service)
name = data.pop("name", None) name = data.pop("name", None)
try:
ins = self._data_table.insert().values( ins = self._data_table.insert().values(
timestamp=timestamp, timestamp=timestamp,
base_id=base_id, base_id=base_id,
@@ -90,7 +97,6 @@ class SQLAlchemyDriver(base.Driver):
name=name, name=name,
data=jsonutils.dumps(data) data=jsonutils.dumps(data)
) )
try:
self._conn.execute(ins) self._conn.execute(ins)
except Exception: except Exception:
LOG.exception("Can not store osprofiler tracepoint {} " LOG.exception("Can not store osprofiler tracepoint {} "