Tracing improvements (#6353)

* Prevent tracing in worker thread

* Tweak logic

* Further improvements

* Adds invoke command to launch gunicorn server

* Update docstring

* Add explicit check for migrations or data import

* Update tracing.py

Allow tracing in worker thread
This commit is contained in:
Oliver 2024-01-31 10:29:56 +11:00 committed by GitHub
parent 282ecebc39
commit 3bfde82394
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 1 deletions

View File

@ -19,6 +19,25 @@ def isRunningMigrations():
return any((x in sys.argv for x in ['migrate', 'makemigrations', 'showmigrations']))
def isInWorkerThread():
"""Returns True if the current thread is a background worker thread."""
return 'qcluster' in sys.argv
def isInServerThread():
"""Returns True if the current thread is a server thread."""
if isInWorkerThread():
return False
if 'runserver' in sys.argv:
return True
if 'gunicorn' in sys.argv[0]:
return True
return False
def isInMainThread():
"""Django runserver starts two processes, one for the actual dev server and the other to reload the application.
@ -28,7 +47,7 @@ def isInMainThread():
if 'runserver' in sys.argv and '--noreload' not in sys.argv:
return os.environ.get('RUN_MAIN', None) == 'true'
return True
return not isInWorkerThread()
def canAppAccessDatabase(

View File

@ -19,6 +19,7 @@ from opentelemetry.sdk.metrics.export import (
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
import InvenTree.ready
from InvenTree.version import inventreeVersion
# Logger configuration
@ -42,6 +43,9 @@ def setup_tracing(
resources_input: The resources to send with the traces.
console: Whether to output the traces to the console.
"""
if InvenTree.ready.isImportingData() or InvenTree.ready.isRunningMigrations():
return
if resources_input is None:
resources_input = {}
if auth is None:

View File

@ -672,6 +672,20 @@ def wait(c):
return manage(c, 'wait_for_db')
@task(pre=[wait], help={'address': 'Server address:port (default=0.0.0.0:8000)'})
def gunicorn(c, address='0.0.0.0:8000'):
"""Launch a gunicorn webserver.
Note: This server will not auto-reload in response to code changes.
"""
c.run(
'gunicorn -c ./docker/gunicorn.conf.py InvenTree.wsgi -b {address} --chdir ./InvenTree'.format(
address=address
),
pty=True,
)
@task(pre=[wait], help={'address': 'Server address:port (default=127.0.0.1:8000)'})
def server(c, address='127.0.0.1:8000'):
"""Launch a (development) server using Django's in-built webserver.