You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using Django 4.2 and instrumented the Django and Psycopg2 Instrumentor in the Gunicorn config file, while I can get everything else, Django ORM spans are not present in traces.
"""The OpenTelemetry Python SDK uses the Global Interpreter Lock (GIL), whichcan cause performance issues with Gunicorn spawn multiple processesto serve requests in parallel.To address this, register a post fork hook that runs after each workerprocess is forked. Gunicorn post_fork hook initializes OTel inside eachworker which avoids lock inheritance/deadlocks and the perf hit.Read more - https://grafana.com/docs/opentelemetry/instrument/python/?pg=blog&plcmt=body-txt#global-interpreter-lock"""importosimportmultiprocessingfromuuidimportuuid4fromopentelemetryimportmetrics, tracefromopentelemetry.exporter.otlp.proto.grpc.metric_exporterimport (
OTLPMetricExporter,
)
fromopentelemetry.exporter.otlp.proto.grpc.trace_exporterimport (
OTLPSpanExporter,
)
# support for logs is currently experimentalfromopentelemetry.sdk.metricsimportMeterProviderfromopentelemetry.sdk.metrics.exportimportPeriodicExportingMetricReaderfromopentelemetry.sdk.resourcesimportResourcefromopentelemetry.sdk.resourcesimportSERVICE_INSTANCE_IDfromopentelemetry.sdk.traceimportTracerProviderfromopentelemetry.sdk.trace.exportimportBatchSpanProcessorfromopentelemetry.instrumentation.djangoimportDjangoInstrumentorfromopentelemetry.instrumentation.psycopg2importPsycopg2Instrumentor# your gunicorn config herewsgi_app="spectra.wsgi:application"bind="0.0.0.0:8000"name="spectra-backend"# Multi-process concurrency (bypasses the GIL)workers=multiprocessing.cpu_count() # adjust via WEB_CONCURRENCY if neededthreads=2worker_class="gthread"# Performance & stabilitytimeout=300keepalive=5max_requests=1000max_requests_jitter=100worker_tmp_dir="/dev/shm"# Loggingloglevel="debug"accesslog="-"errorlog="-"capture_output=Falsedefpost_fork(server, worker):
server.log.info("Worker spawned (pid: %s)", worker.pid)
collector_endpoint=os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT")
resource=Resource.create(
attributes={
# each worker needs a unique service.instance.id to distinguish the created metricsSERVICE_INSTANCE_ID: str(uuid4()),
"worker": worker.pid,
}
)
tracer_provider=TracerProvider(resource=resource)
tracer_provider.add_span_processor(
BatchSpanProcessor(OTLPSpanExporter(endpoint=collector_endpoint))
)
trace.set_tracer_provider(tracer_provider)
meter_provider=MeterProvider(
resource=resource,
metric_readers=[
PeriodicExportingMetricReader(
OTLPMetricExporter(endpoint=collector_endpoint)
)
],
)
metrics.set_meter_provider(meter_provider)
# Check lib.logs.OLTPLoggingHandler handler for logging instrumentation# Instruments database calls made through psycopg2Psycopg2Instrumentor().instrument(enable_commenter=True)
# Instruments incoming HTTP requests handled by Django/GunicornDjangoInstrumentor().instrument(is_sql_commentor_enabled=True)
server.log.info("OTel initialized in worker pid=%s", worker.pid)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi guys,
I'm using Django 4.2 and instrumented the Django and Psycopg2 Instrumentor in the Gunicorn config file, while I can get everything else, Django ORM spans are not present in traces.
Note: I went through this discussion as well, but didn't help - open-telemetry/opentelemetry-python#2989
I'm using Grafana Tempo and Grafana Alloy for traces and Otel collection. Please find other details below -
Libraries I'm using
Gunicorn Config
Thank you!!
Beta Was this translation helpful? Give feedback.
All reactions