Can't add span processor in post_fork when using `initialize()`
Describe your environment
OS: Ubuntu Python version: Python 3.13.37 API version: "opentelemetry-distro[otlp]>=0.57b0"
What happened?
this is in my gunicorn post_fork:
def post_fork(server: Arbiter, worker: CanvasUvicornWorker) -> None:
from opentelemetry.instrumentation.auto_instrumentation import initialize
initialize()
PYROSCOPE_URL = os.getenv("PYROSCOPE_URL")
PYROSCOPE_USER = os.getenv("PYROSCOPE_USER")
PYROSCOPE_PASSWORD = os.getenv("PYROSCOPE_PASSWORD")
if PYROSCOPE_URL and PYROSCOPE_USER and PYROSCOPE_PASSWORD:
pyroscope.configure(
application_name="home-app",
server_address=PYROSCOPE_URL,
basic_auth_username=PYROSCOPE_USER,
basic_auth_password=PYROSCOPE_PASSWORD,
sample_rate=100, # default is 100
detect_subprocesses=True,
oncpu=True, # report cpu time only; default is True
gil_only=True,
enable_logging=True,
tags={
"customer": os.getenv("CUSTOMER_IDENTIFIER"),
},
)
tp = trace.get_tracer_provider()
tp.add_span_processor(PyroscopeSpanProcessor())
tr = tp.get_tracer("probe")
with tr.start_as_current_span("pyro-probe"):
pass
My pyro-probe span does have a pyroscope.span.id, but none of the spans generated from the auto-instrumentation do.
What is the correct way to add a span processor to the tracer provider that the initialize() method generates?
Steps to Reproduce
See above code.
Expected Result
All spans contain a pyroscope.span.id (this is specific to this span processor, but the general expected result is that the span processor runs for all spans, not just ones created in this block).
Actual Result
Any span generated from auto-instrumentation does not have the span processor added afterwards run.
Additional context
No response
Would you like to implement a fix?
None
Tip
React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.
I'm trying to reproduce your issue, but without more context, it seems to be working for me. Could you provide a more complete example that demonstrates the problem?
The code I have, using your example snippet, looks like this:
server.py
from opentelemetry import trace
import os
from random import randint
from flask import Flask
from opentelemetry.instrumentation.flask import FlaskInstrumentor
import logging
app = Flask(__name__)
FlaskInstrumentor().instrument_app(app)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Acquire a tracer
tracer = trace.get_tracer("diceroller.tracer")
@app.route("/rolldice")
def roll_dice():
# This creates a new span that's the child of the current one
with tracer.start_as_current_span("roll") as roll_span:
result = str(roll())
roll_span.set_attribute("roll.value", result)
logger.warn("Rolling the dice: %s", result)
return result
def roll():
return randint(1, 6)
gunicorn_conf.py
import pyroscope
from pyroscope.otel import PyroscopeSpanProcessor
from opentelemetry import trace
from gunicorn.app.base import Arbiter
from uvicorn.workers import UvicornWorker
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
def post_fork(server: Arbiter, worker: UvicornWorker) -> None:
print("Forked child, initializing pyroscope...")
from opentelemetry.instrumentation.auto_instrumentation import initialize
initialize()
PYROSCOPE_URL = 'http://localhost:4040'
PYROSCOPE_USER = 'pyroscope'
PYROSCOPE_PASSWORD = 'pyroscope'
if PYROSCOPE_URL and PYROSCOPE_USER and PYROSCOPE_PASSWORD:
pyroscope.configure(
application_name="home-app",
server_address=PYROSCOPE_URL,
basic_auth_username=PYROSCOPE_USER,
basic_auth_password=PYROSCOPE_PASSWORD,
sample_rate=100, # default is 100
detect_subprocesses=True,
oncpu=True, # report cpu time only; default is True
gil_only=True,
enable_logging=True,
tags={
"customer": "test_customer"
},
)
tp = trace.get_tracer_provider()
console_exporter = ConsoleSpanExporter()
tp.add_span_processor(SimpleSpanProcessor(console_exporter))
tp.add_span_processor(PyroscopeSpanProcessor())
tr = tp.get_tracer("probe")
with tr.start_as_current_span("pyro-probe"):
pass
I run the service like so:
export OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true
gunicorn -c gunicorn_config.py -w 4 service:app
And the output for an auto-instrumented span looks like so, with a pyroscope.profile.id attribute:
{
"name": "GET /rolldice",
"context": {
"trace_id": "0xdb6b9dea04d1508336ab2c75f4161f37",
"span_id": "0x3b7eeaba69e18279",
"trace_state": "[]"
},
"kind": "SpanKind.SERVER",
"parent_id": null,
"start_time": "xxxxxx",
"end_time": "xxxxxx",
"status": {
"status_code": "UNSET"
},
"attributes": {
"pyroscope.profile.id": "3b7eeaba69e18279",
"http.method": "GET",
"http.server_name": "127.0.0.1",