2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-01 11:10:54 +00:00

Fix for tracing init (#9860)

- Circular include means that settings.DB_ENGINE may not be available
This commit is contained in:
Oliver
2025-06-26 07:41:44 +10:00
committed by GitHub
parent cd223116b5
commit 1a51c4a3dd
2 changed files with 10 additions and 12 deletions

View File

@ -878,7 +878,7 @@ if TRACING_ENABLED: # pragma: no cover
# Run tracing/logging instrumentation # Run tracing/logging instrumentation
setup_tracing(**TRACING_DETAILS) setup_tracing(**TRACING_DETAILS)
setup_instruments() setup_instruments(DB_ENGINE)
else: else:
logger.warning('OpenTelemetry tracing not enabled because endpoint is not set') logger.warning('OpenTelemetry tracing not enabled because endpoint is not set')

View File

@ -4,8 +4,6 @@ import base64
import logging import logging
from typing import Optional from typing import Optional
from django.conf import settings
from opentelemetry import metrics, trace from opentelemetry import metrics, trace
from opentelemetry.instrumentation.django import DjangoInstrumentor from opentelemetry.instrumentation.django import DjangoInstrumentor
from opentelemetry.instrumentation.redis import RedisInstrumentor from opentelemetry.instrumentation.redis import RedisInstrumentor
@ -75,7 +73,7 @@ def setup_tracing(
headers = {k: v for k, v in headers.items() if v is not None} headers = {k: v for k, v in headers.items() if v is not None}
# Initialize the OTLP Resource # Initialize the OTLP Resource
service_name = 'Unkown' service_name = 'Unknown'
if InvenTree.ready.isInServerThread(): if InvenTree.ready.isInServerThread():
service_name = 'BACKEND' service_name = 'BACKEND'
elif InvenTree.ready.isInWorkerThread(): elif InvenTree.ready.isInWorkerThread():
@ -107,7 +105,7 @@ def setup_tracing(
OTLPSpanExporter, OTLPSpanExporter,
) )
# Spans / Tracs # Spans / Traces
span_exporter = OTLPSpanExporter( span_exporter = OTLPSpanExporter(
headers=headers, headers=headers,
endpoint=endpoint if not (is_http and append_http) else f'{endpoint}/v1/traces', endpoint=endpoint if not (is_http and append_http) else f'{endpoint}/v1/traces',
@ -121,7 +119,7 @@ def setup_tracing(
trace_provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter())) trace_provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))
# Metrics # Metrics
metric_perodic_reader = PeriodicExportingMetricReader( metric_periodic_reader = PeriodicExportingMetricReader(
OTLPMetricExporter( OTLPMetricExporter(
headers=headers, headers=headers,
endpoint=endpoint endpoint=endpoint
@ -129,7 +127,7 @@ def setup_tracing(
else f'{endpoint}/v1/metrics', else f'{endpoint}/v1/metrics',
) )
) )
metric_readers = [metric_perodic_reader] metric_readers = [metric_periodic_reader]
# For debugging purposes, export the metrics to the console # For debugging purposes, export the metrics to the console
if console: if console:
@ -158,17 +156,17 @@ def setup_tracing(
TRACE_PROV = trace_provider TRACE_PROV = trace_provider
def setup_instruments(): # pragma: no cover def setup_instruments(db_engine: str): # pragma: no cover
"""Run auto-insturmentation for OpenTelemetry tracing.""" """Run auto-instrumentation for OpenTelemetry tracing."""
DjangoInstrumentor().instrument() DjangoInstrumentor().instrument()
RedisInstrumentor().instrument() RedisInstrumentor().instrument()
RequestsInstrumentor().instrument() RequestsInstrumentor().instrument()
SystemMetricsInstrumentor().instrument() SystemMetricsInstrumentor().instrument()
# DBs # DBs
if settings.DB_ENGINE == 'sqlite': if db_engine == 'sqlite':
SQLite3Instrumentor().instrument() SQLite3Instrumentor().instrument()
elif settings.DB_ENGINE == 'postgresql': elif db_engine == 'postgresql':
try: try:
from opentelemetry.instrumentation.psycopg import PsycopgInstrumentor from opentelemetry.instrumentation.psycopg import PsycopgInstrumentor
@ -177,7 +175,7 @@ def setup_instruments(): # pragma: no cover
) )
except ModuleNotFoundError: except ModuleNotFoundError:
pass pass
elif settings.DB_ENGINE == 'mysql': elif db_engine == 'mysql':
try: try:
from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor