prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Disabling intrument_middleware via code not working in Django
  ### Which version of dd-trace-py are you using?
  
  1.1.2
  
  ### Which version of pip are you using?
  
  22.0.4
  
  ### Which version of the libraries are you using?
  
  /
  
  ### How can we reproduce your problem?
  
  Disable `instrument_middleware` via config code in Django integration does not work:
  
  Example: (neither of these work):
  
  ```python
  ddtrace.config.django.instrument_middleware = False
  ddtrace.config.django["instrument_middleware"] = False
  ```
  
  
  Configuring this via env variable (`DD_DJANGO_INSTRUMENT_MIDDLEWARE`) works.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `ddtrace/contrib/django/__init__.py`
  Content:
  ```
  1 """
  2 The Django__ integration traces requests, views, template renderers, database
  3 and cache calls in a Django application.
  4 
  5 
  6 Enable Django tracing automatically via ``ddtrace-run``::
  7 
  8     ddtrace-run python manage.py runserver
  9 
  10 
  11 Django tracing can also be enabled manually::
  12 
  13     from ddtrace import patch_all
  14     patch_all()
  15 
  16 
  17 To have Django capture the tracer logs, ensure the ``LOGGING`` variable in
  18 ``settings.py`` looks similar to::
  19 
  20     LOGGING = {
  21         'loggers': {
  22             'ddtrace': {
  23                 'handlers': ['console'],
  24                 'level': 'WARNING',
  25             },
  26         },
  27     }
  28 
  29 
  30 Configuration
  31 ~~~~~~~~~~~~~
  32 .. py:data:: ddtrace.config.django['distributed_tracing_enabled']
  33 
  34    Whether or not to parse distributed tracing headers from requests received by your Django app.
  35 
  36    Default: ``True``
  37 
  38 .. py:data:: ddtrace.config.django['service_name']
  39 
  40    The service name reported for your Django app.
  41 
  42    Can also be configured via the ``DD_SERVICE`` environment variable.
  43 
  44    Default: ``'django'``
  45 
  46 .. py:data:: ddtrace.config.django['cache_service_name']
  47 
  48    The service name reported for your Django app cache layer.
  49 
  50    Can also be configured via the ``DD_DJANGO_CACHE_SERVICE_NAME`` environment variable.
  51 
  52    Default: ``'django'``
  53 
  54 .. py:data:: ddtrace.config.django['database_service_name']
  55 
  56    A string reported as the service name of the Django app database layer.
  57 
  58    Can also be configured via the ``DD_DJANGO_DATABASE_SERVICE_NAME`` environment variable.
  59 
  60    Takes precedence over database_service_name_prefix.
  61 
  62    Default: ``''``
  63 
  64 .. py:data:: ddtrace.config.django['database_service_name_prefix']
  65 
  66    A string to be prepended to the service name reported for your Django app database layer.
  67 
  68    Can also be configured via the ``DD_DJANGO_DATABASE_SERVICE_NAME_PREFIX`` environment variable.
  69 
  70    The database service name is the name of the database appended with 'db'. Has a lower precedence than database_service_name.
  71 
  72    Default: ``''``
  73 
  74 .. py:data:: ddtrace.config.django["trace_fetch_methods"]
  75 
  76    Whether or not to trace fetch methods.
  77 
  78    Can also configured via the ``DD_DJANGO_TRACE_FETCH_METHODS`` environment variable.
  79 
  80    Default: ``False``
  81 
  82 .. py:data:: ddtrace.config.django['instrument_middleware']
  83 
  84    Whether or not to instrument middleware.
  85 
  86    Can also be enabled with the ``DD_DJANGO_INSTRUMENT_MIDDLEWARE`` environment variable.
  87 
  88    Default: ``True``
  89 
  90 .. py:data:: ddtrace.config.django['instrument_templates']
  91 
  92    Whether or not to instrument template rendering.
  93 
  94    Can also be enabled with the ``DD_DJANGO_INSTRUMENT_TEMPLATES`` environment variable.
  95 
  96    Default: ``True``
  97 
  98 .. py:data:: ddtrace.config.django['instrument_databases']
  99 
  100    Whether or not to instrument databases.
  101 
  102    Can also be enabled with the ``DD_DJANGO_INSTRUMENT_DATABASES`` environment variable.
  103 
  104    Default: ``True``
  105 
  106 .. py:data:: ddtrace.config.django['instrument_caches']
  107 
  108    Whether or not to instrument caches.
  109 
  110     Can also be enabled with the ``DD_DJANGO_INSTRUMENT_CACHES`` environment variable.
  111 
  112    Default: ``True``
  113 
  114 .. py:data:: ddtrace.config.django['trace_query_string']
  115 
  116    Whether or not to include the query string as a tag.
  117 
  118    Default: ``False``
  119 
  120 .. py:data:: ddtrace.config.django['include_user_name']
  121 
  122    Whether or not to include the authenticated user's username as a tag on the root request span.
  123 
  124    Default: ``True``
  125 
  126 .. py:data:: ddtrace.config.django['use_handler_resource_format']
  127 
  128    Whether or not to use the resource format `"{method} {handler}"`. Can also be
  129    enabled with the ``DD_DJANGO_USE_HANDLER_RESOURCE_FORMAT`` environment
  130    variable.
  131 
  132    The default resource format for Django >= 2.2.0 is otherwise `"{method} {urlpattern}"`.
  133 
  134    Default: ``False``
  135 
  136 .. py:data:: ddtrace.config.django['use_legacy_resource_format']
  137 
  138    Whether or not to use the legacy resource format `"{handler}"`. Can also be
  139    enabled with the ``DD_DJANGO_USE_LEGACY_RESOURCE_FORMAT`` environment
  140    variable.
  141 
  142    The default resource format for Django >= 2.2.0 is otherwise `"{method} {urlpattern}"`.
  143 
  144    Default: ``False``
  145 
  146 Example::
  147 
  148     from ddtrace import config
  149 
  150     # Enable distributed tracing
  151     config.django['distributed_tracing_enabled'] = True
  152 
  153     # Override service name
  154     config.django['service_name'] = 'custom-service-name'
  155 
  156 
  157 :ref:`Headers tracing <http-headers-tracing>` is supported for this integration.
  158 
  159 .. __: https://www.djangoproject.com/
  160 """  # noqa: E501
  161 from ...internal.utils.importlib import require_modules
  162 
  163 
  164 required_modules = ["django"]
  165 
  166 with require_modules(required_modules) as missing_modules:
  167     if not missing_modules:
  168         from . import patch as _patch
  169         from .patch import patch
  170         from .patch import unpatch
  171 
  172         __all__ = ["patch", "unpatch", "_patch"]
  173
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/ddtrace/contrib/django/__init__.py b/ddtrace/contrib/django/__init__.py
  --- a/ddtrace/contrib/django/__init__.py
  +++ b/ddtrace/contrib/django/__init__.py
  @@ -29,6 +29,13 @@
   
   Configuration
   ~~~~~~~~~~~~~
  +
  +.. important::
  +
  +    Note that the in-code configuration must be run before Django is instrumented. This means that in-code configuration
  +    will not work with ``ddtrace-run`` and before a call to ``patch`` or ``patch_all``.
  +
  +
   .. py:data:: ddtrace.config.django['distributed_tracing_enabled']
   
      Whether or not to parse distributed tracing headers from requests received by your Django app.
  
verification_info: {"golden_diff": "diff --git a/ddtrace/contrib/django/__init__.py b/ddtrace/contrib/django/__init__.py\n--- a/ddtrace/contrib/django/__init__.py\n+++ b/ddtrace/contrib/django/__init__.py\n@@ -29,6 +29,13 @@\n \n Configuration\n ~~~~~~~~~~~~~\n+\n+.. important::\n+\n+    Note that the in-code configuration must be run before Django is instrumented. This means that in-code configuration\n+    will not work with ``ddtrace-run`` and before a call to ``patch`` or ``patch_all``.\n+\n+\n .. py:data:: ddtrace.config.django['distributed_tracing_enabled']\n \n    Whether or not to parse distributed tracing headers from requests received by your Django app.\n", "issue": "Disabling intrument_middleware via code not working in Django\n### Which version of dd-trace-py are you using?\r\n\r\n1.1.2\r\n\r\n### Which version of pip are you using?\r\n\r\n22.0.4\r\n\r\n### Which version of the libraries are you using?\r\n\r\n/\r\n\r\n### How can we reproduce your problem?\r\n\r\nDisable `instrument_middleware` via config code in Django integration does not work:\r\n\r\nExample: (neither of these work):\r\n\r\n```python\r\nddtrace.config.django.instrument_middleware = False\r\nddtrace.config.django[\"instrument_middleware\"] = False\r\n```\r\n\r\n\r\nConfiguring this via env variable (`DD_DJANGO_INSTRUMENT_MIDDLEWARE`) works.\n", "before_files": [{"content": "\"\"\"\nThe Django__ integration traces requests, views, template renderers, database\nand cache calls in a Django application.\n\n\nEnable Django tracing automatically via ``ddtrace-run``::\n\n    ddtrace-run python manage.py runserver\n\n\nDjango tracing can also be enabled manually::\n\n    from ddtrace import patch_all\n    patch_all()\n\n\nTo have Django capture the tracer logs, ensure the ``LOGGING`` variable in\n``settings.py`` looks similar to::\n\n    LOGGING = {\n        'loggers': {\n            'ddtrace': {\n                'handlers': ['console'],\n                'level': 'WARNING',\n            },\n        },\n    }\n\n\nConfiguration\n~~~~~~~~~~~~~\n.. py:data:: ddtrace.config.django['distributed_tracing_enabled']\n\n   Whether or not to parse distributed tracing headers from requests received by your Django app.\n\n   Default: ``True``\n\n.. py:data:: ddtrace.config.django['service_name']\n\n   The service name reported for your Django app.\n\n   Can also be configured via the ``DD_SERVICE`` environment variable.\n\n   Default: ``'django'``\n\n.. py:data:: ddtrace.config.django['cache_service_name']\n\n   The service name reported for your Django app cache layer.\n\n   Can also be configured via the ``DD_DJANGO_CACHE_SERVICE_NAME`` environment variable.\n\n   Default: ``'django'``\n\n.. py:data:: ddtrace.config.django['database_service_name']\n\n   A string reported as the service name of the Django app database layer.\n\n   Can also be configured via the ``DD_DJANGO_DATABASE_SERVICE_NAME`` environment variable.\n\n   Takes precedence over database_service_name_prefix.\n\n   Default: ``''``\n\n.. py:data:: ddtrace.config.django['database_service_name_prefix']\n\n   A string to be prepended to the service name reported for your Django app database layer.\n\n   Can also be configured via the ``DD_DJANGO_DATABASE_SERVICE_NAME_PREFIX`` environment variable.\n\n   The database service name is the name of the database appended with 'db'. Has a lower precedence than database_service_name.\n\n   Default: ``''``\n\n.. py:data:: ddtrace.config.django[\"trace_fetch_methods\"]\n\n   Whether or not to trace fetch methods.\n\n   Can also configured via the ``DD_DJANGO_TRACE_FETCH_METHODS`` environment variable.\n\n   Default: ``False``\n\n.. py:data:: ddtrace.config.django['instrument_middleware']\n\n   Whether or not to instrument middleware.\n\n   Can also be enabled with the ``DD_DJANGO_INSTRUMENT_MIDDLEWARE`` environment variable.\n\n   Default: ``True``\n\n.. py:data:: ddtrace.config.django['instrument_templates']\n\n   Whether or not to instrument template rendering.\n\n   Can also be enabled with the ``DD_DJANGO_INSTRUMENT_TEMPLATES`` environment variable.\n\n   Default: ``True``\n\n.. py:data:: ddtrace.config.django['instrument_databases']\n\n   Whether or not to instrument databases.\n\n   Can also be enabled with the ``DD_DJANGO_INSTRUMENT_DATABASES`` environment variable.\n\n   Default: ``True``\n\n.. py:data:: ddtrace.config.django['instrument_caches']\n\n   Whether or not to instrument caches.\n\n    Can also be enabled with the ``DD_DJANGO_INSTRUMENT_CACHES`` environment variable.\n\n   Default: ``True``\n\n.. py:data:: ddtrace.config.django['trace_query_string']\n\n   Whether or not to include the query string as a tag.\n\n   Default: ``False``\n\n.. py:data:: ddtrace.config.django['include_user_name']\n\n   Whether or not to include the authenticated user's username as a tag on the root request span.\n\n   Default: ``True``\n\n.. py:data:: ddtrace.config.django['use_handler_resource_format']\n\n   Whether or not to use the resource format `\"{method} {handler}\"`. Can also be\n   enabled with the ``DD_DJANGO_USE_HANDLER_RESOURCE_FORMAT`` environment\n   variable.\n\n   The default resource format for Django >= 2.2.0 is otherwise `\"{method} {urlpattern}\"`.\n\n   Default: ``False``\n\n.. py:data:: ddtrace.config.django['use_legacy_resource_format']\n\n   Whether or not to use the legacy resource format `\"{handler}\"`. Can also be\n   enabled with the ``DD_DJANGO_USE_LEGACY_RESOURCE_FORMAT`` environment\n   variable.\n\n   The default resource format for Django >= 2.2.0 is otherwise `\"{method} {urlpattern}\"`.\n\n   Default: ``False``\n\nExample::\n\n    from ddtrace import config\n\n    # Enable distributed tracing\n    config.django['distributed_tracing_enabled'] = True\n\n    # Override service name\n    config.django['service_name'] = 'custom-service-name'\n\n\n:ref:`Headers tracing <http-headers-tracing>` is supported for this integration.\n\n.. __: https://www.djangoproject.com/\n\"\"\"  # noqa: E501\nfrom ...internal.utils.importlib import require_modules\n\n\nrequired_modules = [\"django\"]\n\nwith require_modules(required_modules) as missing_modules:\n    if not missing_modules:\n        from . import patch as _patch\n        from .patch import patch\n        from .patch import unpatch\n\n        __all__ = [\"patch\", \"unpatch\", \"_patch\"]\n", "path": "ddtrace/contrib/django/__init__.py"}], "after_files": [{"content": "\"\"\"\nThe Django__ integration traces requests, views, template renderers, database\nand cache calls in a Django application.\n\n\nEnable Django tracing automatically via ``ddtrace-run``::\n\n    ddtrace-run python manage.py runserver\n\n\nDjango tracing can also be enabled manually::\n\n    from ddtrace import patch_all\n    patch_all()\n\n\nTo have Django capture the tracer logs, ensure the ``LOGGING`` variable in\n``settings.py`` looks similar to::\n\n    LOGGING = {\n        'loggers': {\n            'ddtrace': {\n                'handlers': ['console'],\n                'level': 'WARNING',\n            },\n        },\n    }\n\n\nConfiguration\n~~~~~~~~~~~~~\n\n.. important::\n\n    Note that the in-code configuration must be run before Django is instrumented. This means that in-code configuration\n    will not work with ``ddtrace-run`` and before a call to ``patch`` or ``patch_all``.\n\n\n.. py:data:: ddtrace.config.django['distributed_tracing_enabled']\n\n   Whether or not to parse distributed tracing headers from requests received by your Django app.\n\n   Default: ``True``\n\n.. py:data:: ddtrace.config.django['service_name']\n\n   The service name reported for your Django app.\n\n   Can also be configured via the ``DD_SERVICE`` environment variable.\n\n   Default: ``'django'``\n\n.. py:data:: ddtrace.config.django['cache_service_name']\n\n   The service name reported for your Django app cache layer.\n\n   Can also be configured via the ``DD_DJANGO_CACHE_SERVICE_NAME`` environment variable.\n\n   Default: ``'django'``\n\n.. py:data:: ddtrace.config.django['database_service_name']\n\n   A string reported as the service name of the Django app database layer.\n\n   Can also be configured via the ``DD_DJANGO_DATABASE_SERVICE_NAME`` environment variable.\n\n   Takes precedence over database_service_name_prefix.\n\n   Default: ``''``\n\n.. py:data:: ddtrace.config.django['database_service_name_prefix']\n\n   A string to be prepended to the service name reported for your Django app database layer.\n\n   Can also be configured via the ``DD_DJANGO_DATABASE_SERVICE_NAME_PREFIX`` environment variable.\n\n   The database service name is the name of the database appended with 'db'. Has a lower precedence than database_service_name.\n\n   Default: ``''``\n\n.. py:data:: ddtrace.config.django[\"trace_fetch_methods\"]\n\n   Whether or not to trace fetch methods.\n\n   Can also configured via the ``DD_DJANGO_TRACE_FETCH_METHODS`` environment variable.\n\n   Default: ``False``\n\n.. py:data:: ddtrace.config.django['instrument_middleware']\n\n   Whether or not to instrument middleware.\n\n   Can also be enabled with the ``DD_DJANGO_INSTRUMENT_MIDDLEWARE`` environment variable.\n\n   Default: ``True``\n\n.. py:data:: ddtrace.config.django['instrument_templates']\n\n   Whether or not to instrument template rendering.\n\n   Can also be enabled with the ``DD_DJANGO_INSTRUMENT_TEMPLATES`` environment variable.\n\n   Default: ``True``\n\n.. py:data:: ddtrace.config.django['instrument_databases']\n\n   Whether or not to instrument databases.\n\n   Can also be enabled with the ``DD_DJANGO_INSTRUMENT_DATABASES`` environment variable.\n\n   Default: ``True``\n\n.. py:data:: ddtrace.config.django['instrument_caches']\n\n   Whether or not to instrument caches.\n\n    Can also be enabled with the ``DD_DJANGO_INSTRUMENT_CACHES`` environment variable.\n\n   Default: ``True``\n\n.. py:data:: ddtrace.config.django['trace_query_string']\n\n   Whether or not to include the query string as a tag.\n\n   Default: ``False``\n\n.. py:data:: ddtrace.config.django['include_user_name']\n\n   Whether or not to include the authenticated user's username as a tag on the root request span.\n\n   Default: ``True``\n\n.. py:data:: ddtrace.config.django['use_handler_resource_format']\n\n   Whether or not to use the resource format `\"{method} {handler}\"`. Can also be\n   enabled with the ``DD_DJANGO_USE_HANDLER_RESOURCE_FORMAT`` environment\n   variable.\n\n   The default resource format for Django >= 2.2.0 is otherwise `\"{method} {urlpattern}\"`.\n\n   Default: ``False``\n\n.. py:data:: ddtrace.config.django['use_legacy_resource_format']\n\n   Whether or not to use the legacy resource format `\"{handler}\"`. Can also be\n   enabled with the ``DD_DJANGO_USE_LEGACY_RESOURCE_FORMAT`` environment\n   variable.\n\n   The default resource format for Django >= 2.2.0 is otherwise `\"{method} {urlpattern}\"`.\n\n   Default: ``False``\n\nExample::\n\n    from ddtrace import config\n\n    # Enable distributed tracing\n    config.django['distributed_tracing_enabled'] = True\n\n    # Override service name\n    config.django['service_name'] = 'custom-service-name'\n\n\n:ref:`Headers tracing <http-headers-tracing>` is supported for this integration.\n\n.. __: https://www.djangoproject.com/\n\"\"\"  # noqa: E501\nfrom ...internal.utils.importlib import require_modules\n\n\nrequired_modules = [\"django\"]\n\nwith require_modules(required_modules) as missing_modules:\n    if not missing_modules:\n        from . import patch as _patch\n        from .patch import patch\n        from .patch import unpatch\n\n        __all__ = [\"patch\", \"unpatch\", \"_patch\"]\n", "path": "ddtrace/contrib/django/__init__.py"}]}