mirror of
https://github.com/inventree/InvenTree.git
synced 2026-06-06 08:54:24 +00:00
fix (backend): only instrument tracing once (#12050)
* ensure tracing is only set up once * check if instrument was already called
This commit is contained in:
@@ -50,12 +50,17 @@ def setup_tracing(
|
|||||||
"""
|
"""
|
||||||
if InvenTree.ready.isImportingData() or InvenTree.ready.isRunningMigrations():
|
if InvenTree.ready.isImportingData() or InvenTree.ready.isRunningMigrations():
|
||||||
return
|
return
|
||||||
|
|
||||||
if endpoint is None or headers is None:
|
if endpoint is None or headers is None:
|
||||||
print(
|
print(
|
||||||
'Tracing endpoint or headers not specified - skipping tracing setup'
|
'Tracing endpoint or headers not specified - skipping tracing setup'
|
||||||
) # pragma: no cover
|
) # pragma: no cover
|
||||||
return # pragma: no cover
|
return # pragma: no cover
|
||||||
|
|
||||||
|
# check if trace is already set up - if so, skip
|
||||||
|
if trace.get_tracer_provider() is not None:
|
||||||
|
return
|
||||||
|
|
||||||
# Logger configuration
|
# Logger configuration
|
||||||
logger = logging.getLogger('inventree')
|
logger = logging.getLogger('inventree')
|
||||||
|
|
||||||
@@ -163,6 +168,7 @@ def setup_tracing(
|
|||||||
|
|
||||||
def setup_instruments(db_engine: str): # pragma: no cover
|
def setup_instruments(db_engine: str): # pragma: no cover
|
||||||
"""Run auto-instrumentation for OpenTelemetry tracing."""
|
"""Run auto-instrumentation for OpenTelemetry tracing."""
|
||||||
|
if not DjangoInstrumentor()._is_instrumented_by_opentelemetry:
|
||||||
DjangoInstrumentor().instrument()
|
DjangoInstrumentor().instrument()
|
||||||
RedisInstrumentor().instrument()
|
RedisInstrumentor().instrument()
|
||||||
RequestsInstrumentor().instrument()
|
RequestsInstrumentor().instrument()
|
||||||
@@ -172,20 +178,24 @@ def setup_instruments(db_engine: str): # pragma: no cover
|
|||||||
|
|
||||||
# DBs
|
# DBs
|
||||||
if 'sqlite' in db_engine:
|
if 'sqlite' in db_engine:
|
||||||
SQLite3Instrumentor().instrument()
|
inst = SQLite3Instrumentor()
|
||||||
|
if not inst._is_instrumented_by_opentelemetry:
|
||||||
|
inst.instrument()
|
||||||
elif 'postgresql' in db_engine:
|
elif 'postgresql' in db_engine:
|
||||||
try:
|
try:
|
||||||
from opentelemetry.instrumentation.psycopg import PsycopgInstrumentor
|
from opentelemetry.instrumentation.psycopg import PsycopgInstrumentor
|
||||||
|
|
||||||
PsycopgInstrumentor().instrument(
|
inst = PsycopgInstrumentor()
|
||||||
enable_commenter=False, commenter_options={}
|
if not inst._is_instrumented_by_opentelemetry:
|
||||||
)
|
inst.instrument(enable_commenter=False, commenter_options={})
|
||||||
except ModuleNotFoundError:
|
except ModuleNotFoundError:
|
||||||
pass
|
pass
|
||||||
elif 'mysql' in db_engine:
|
elif 'mysql' in db_engine:
|
||||||
try:
|
try:
|
||||||
from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor
|
from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor
|
||||||
|
|
||||||
PyMySQLInstrumentor().instrument()
|
inst = PyMySQLInstrumentor()
|
||||||
|
if not inst._is_instrumented_by_opentelemetry:
|
||||||
|
inst.instrument()
|
||||||
except ModuleNotFoundError:
|
except ModuleNotFoundError:
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user