DD_DBM_PROPAGATION_MODE not working when databases are instrumented through Django
Summary of problem
I was playing recently with DBM-APM propagation and discovered that DBM propagation does not work (properly/at-all) when databases are instrumented using Django. I confirmed that DBM propagation works fine in case I let psycopg instrument database, but I can't really proceed with such setup given the fact I need to support multiple databases. I checked the code and looks like this is happening because dbm_propagator is actually never passed to cursor configuration:
File: contrib/django/patch.py:L142
# Each db alias will need its own config for dbapi
cfg = IntegrationConfig(
config.django.global_config, # global_config needed for analytics sample rate
"{}-{}".format("django", alias), # name not used but set anyway
_default_service=config.django._default_service,
_dbapi_span_name_prefix=prefix,
trace_fetch_methods=config.django.trace_fetch_methods,
analytics_enabled=config.django.analytics_enabled,
analytics_sample_rate=config.django.analytics_sample_rate,
)
return traced_cursor_cls(cursor, pin, cfg)
I think we could extend this configuration similarly to how psycopg configures it:
cfg = IntegrationConfig(
config.django.global_config, # global_config needed for analytics sample rate
"{}-{}".format("django", alias), # name not used but set anyway
_default_service=config.django._default_service,
_dbapi_span_name_prefix=prefix,
trace_fetch_methods=config.django.trace_fetch_methods,
analytics_enabled=config.django.analytics_enabled,
analytics_sample_rate=config.django.analytics_sample_rate,
_dbm_propagator=_DBM_Propagator(0, "query", sql_injector=sql_injector),
)
As sql_injector I believe we could either inject default injector or _psycopg_sql_injector in case psycopg is configured during cursor configuration.
Which version of dd-trace-py are you using?
2.4.0
Which version of pip are you using?
23.3.2
How can we reproduce your problem?
Try turning on DD_DBM_PROPAGATION_MODE=full in case your DB instrumentation is done through Django.
What is the result that you get?
No DBM propagation is available when databases instrumentation is achieved with Django patch.
What is the result that you expected?
Unless there's other way to configure psycopg to work with multiple databases out of the box (without manual configuration, as Django now takes care of service setup) I guess this extension should be quite straightforward to add.
Thanks for flagging this, @kbiedrzycki. Looks like it will be a straightforward change to make. We'll add it to our backlog, but please go ahead and pull request the change yourself if you're so inclined.
Are there any updates on this?
Any updates here?