prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  app_key not passed to aiohttp_jinja2 
  When using aiohttp_admin the app_key value for the templating module differs from the default one.
  
  This causes an error executing:
  https://github.com/DataDog/dd-trace-py/blob/ec191a4a71ae71017b70d26111bba4489e617ae5/ddtrace/contrib/aiohttp/template.py#L21
  
  As far as I understand this would solve the problem.
  `env = aiohttp_jinja2.get_env(request.app, app_key=kwargs["app_key"])`
  
  --- 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/aiohttp_jinja2/patch.py`
  Content:
  ```
  1 from ddtrace import Pin
  2 from ddtrace import config
  3 
  4 from ...ext import SpanTypes
  5 from ...internal.utils import get_argument_value
  6 from ..trace_utils import unwrap
  7 from ..trace_utils import with_traced_module
  8 from ..trace_utils import wrap
  9 
  10 
  11 config._add(
  12     "aiohttp_jinja2",
  13     dict(),
  14 )
  15 
  16 
  17 @with_traced_module
  18 def traced_render_template(aiohttp_jinja2, pin, func, instance, args, kwargs):
  19     # original signature:
  20     # render_template(template_name, request, context, *, app_key=APP_KEY, encoding='utf-8')
  21     template_name = get_argument_value(args, kwargs, 0, "template_name")
  22     request = get_argument_value(args, kwargs, 1, "request")
  23     env = aiohttp_jinja2.get_env(request.app)
  24 
  25     # the prefix is available only on PackageLoader
  26     template_prefix = getattr(env.loader, "package_path", "")
  27     template_meta = "%s/%s" % (template_prefix, template_name)
  28 
  29     with pin.tracer.trace("aiohttp.template", span_type=SpanTypes.TEMPLATE) as span:
  30         span.set_tag("aiohttp.template", template_meta)
  31         return func(*args, **kwargs)
  32 
  33 
  34 def _patch(aiohttp_jinja2):
  35     Pin().onto(aiohttp_jinja2)
  36     wrap("aiohttp_jinja2", "render_template", traced_render_template(aiohttp_jinja2))
  37 
  38 
  39 def patch():
  40     import aiohttp_jinja2
  41 
  42     if getattr(aiohttp_jinja2, "_datadog_patch", False):
  43         return
  44 
  45     _patch(aiohttp_jinja2)
  46 
  47     setattr(aiohttp_jinja2, "_datadog_patch", True)
  48 
  49 
  50 def _unpatch(aiohttp_jinja2):
  51     unwrap(aiohttp_jinja2, "render_template")
  52 
  53 
  54 def unpatch():
  55     import aiohttp_jinja2
  56 
  57     if not getattr(aiohttp_jinja2, "_datadog_patch", False):
  58         return
  59 
  60     _unpatch(aiohttp_jinja2)
  61 
  62     setattr(aiohttp_jinja2, "_datadog_patch", False)
  63
  ```
  --- 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/aiohttp_jinja2/patch.py b/ddtrace/contrib/aiohttp_jinja2/patch.py
  --- a/ddtrace/contrib/aiohttp_jinja2/patch.py
  +++ b/ddtrace/contrib/aiohttp_jinja2/patch.py
  @@ -20,7 +20,10 @@
       # render_template(template_name, request, context, *, app_key=APP_KEY, encoding='utf-8')
       template_name = get_argument_value(args, kwargs, 0, "template_name")
       request = get_argument_value(args, kwargs, 1, "request")
  -    env = aiohttp_jinja2.get_env(request.app)
  +    get_env_kwargs = {}
  +    if "app_key" in kwargs:
  +        get_env_kwargs["app_key"] = kwargs["app_key"]
  +    env = aiohttp_jinja2.get_env(request.app, **get_env_kwargs)
   
       # the prefix is available only on PackageLoader
       template_prefix = getattr(env.loader, "package_path", "")
  
verification_info: {"golden_diff": "diff --git a/ddtrace/contrib/aiohttp_jinja2/patch.py b/ddtrace/contrib/aiohttp_jinja2/patch.py\n--- a/ddtrace/contrib/aiohttp_jinja2/patch.py\n+++ b/ddtrace/contrib/aiohttp_jinja2/patch.py\n@@ -20,7 +20,10 @@\n     # render_template(template_name, request, context, *, app_key=APP_KEY, encoding='utf-8')\n     template_name = get_argument_value(args, kwargs, 0, \"template_name\")\n     request = get_argument_value(args, kwargs, 1, \"request\")\n-    env = aiohttp_jinja2.get_env(request.app)\n+    get_env_kwargs = {}\n+    if \"app_key\" in kwargs:\n+        get_env_kwargs[\"app_key\"] = kwargs[\"app_key\"]\n+    env = aiohttp_jinja2.get_env(request.app, **get_env_kwargs)\n \n     # the prefix is available only on PackageLoader\n     template_prefix = getattr(env.loader, \"package_path\", \"\")\n", "issue": "app_key not passed to aiohttp_jinja2 \nWhen using aiohttp_admin the app_key value for the templating module differs from the default one.\r\n\r\nThis causes an error executing:\r\nhttps://github.com/DataDog/dd-trace-py/blob/ec191a4a71ae71017b70d26111bba4489e617ae5/ddtrace/contrib/aiohttp/template.py#L21\r\n\r\nAs far as I understand this would solve the problem.\r\n`env = aiohttp_jinja2.get_env(request.app, app_key=kwargs[\"app_key\"])`\n", "before_files": [{"content": "from ddtrace import Pin\nfrom ddtrace import config\n\nfrom ...ext import SpanTypes\nfrom ...internal.utils import get_argument_value\nfrom ..trace_utils import unwrap\nfrom ..trace_utils import with_traced_module\nfrom ..trace_utils import wrap\n\n\nconfig._add(\n    \"aiohttp_jinja2\",\n    dict(),\n)\n\n\n@with_traced_module\ndef traced_render_template(aiohttp_jinja2, pin, func, instance, args, kwargs):\n    # original signature:\n    # render_template(template_name, request, context, *, app_key=APP_KEY, encoding='utf-8')\n    template_name = get_argument_value(args, kwargs, 0, \"template_name\")\n    request = get_argument_value(args, kwargs, 1, \"request\")\n    env = aiohttp_jinja2.get_env(request.app)\n\n    # the prefix is available only on PackageLoader\n    template_prefix = getattr(env.loader, \"package_path\", \"\")\n    template_meta = \"%s/%s\" % (template_prefix, template_name)\n\n    with pin.tracer.trace(\"aiohttp.template\", span_type=SpanTypes.TEMPLATE) as span:\n        span.set_tag(\"aiohttp.template\", template_meta)\n        return func(*args, **kwargs)\n\n\ndef _patch(aiohttp_jinja2):\n    Pin().onto(aiohttp_jinja2)\n    wrap(\"aiohttp_jinja2\", \"render_template\", traced_render_template(aiohttp_jinja2))\n\n\ndef patch():\n    import aiohttp_jinja2\n\n    if getattr(aiohttp_jinja2, \"_datadog_patch\", False):\n        return\n\n    _patch(aiohttp_jinja2)\n\n    setattr(aiohttp_jinja2, \"_datadog_patch\", True)\n\n\ndef _unpatch(aiohttp_jinja2):\n    unwrap(aiohttp_jinja2, \"render_template\")\n\n\ndef unpatch():\n    import aiohttp_jinja2\n\n    if not getattr(aiohttp_jinja2, \"_datadog_patch\", False):\n        return\n\n    _unpatch(aiohttp_jinja2)\n\n    setattr(aiohttp_jinja2, \"_datadog_patch\", False)\n", "path": "ddtrace/contrib/aiohttp_jinja2/patch.py"}], "after_files": [{"content": "from ddtrace import Pin\nfrom ddtrace import config\n\nfrom ...ext import SpanTypes\nfrom ...internal.utils import get_argument_value\nfrom ..trace_utils import unwrap\nfrom ..trace_utils import with_traced_module\nfrom ..trace_utils import wrap\n\n\nconfig._add(\n    \"aiohttp_jinja2\",\n    dict(),\n)\n\n\n@with_traced_module\ndef traced_render_template(aiohttp_jinja2, pin, func, instance, args, kwargs):\n    # original signature:\n    # render_template(template_name, request, context, *, app_key=APP_KEY, encoding='utf-8')\n    template_name = get_argument_value(args, kwargs, 0, \"template_name\")\n    request = get_argument_value(args, kwargs, 1, \"request\")\n    get_env_kwargs = {}\n    if \"app_key\" in kwargs:\n        get_env_kwargs[\"app_key\"] = kwargs[\"app_key\"]\n    env = aiohttp_jinja2.get_env(request.app, **get_env_kwargs)\n\n    # the prefix is available only on PackageLoader\n    template_prefix = getattr(env.loader, \"package_path\", \"\")\n    template_meta = \"%s/%s\" % (template_prefix, template_name)\n\n    with pin.tracer.trace(\"aiohttp.template\", span_type=SpanTypes.TEMPLATE) as span:\n        span.set_tag(\"aiohttp.template\", template_meta)\n        return func(*args, **kwargs)\n\n\ndef _patch(aiohttp_jinja2):\n    Pin().onto(aiohttp_jinja2)\n    wrap(\"aiohttp_jinja2\", \"render_template\", traced_render_template(aiohttp_jinja2))\n\n\ndef patch():\n    import aiohttp_jinja2\n\n    if getattr(aiohttp_jinja2, \"_datadog_patch\", False):\n        return\n\n    _patch(aiohttp_jinja2)\n\n    setattr(aiohttp_jinja2, \"_datadog_patch\", True)\n\n\ndef _unpatch(aiohttp_jinja2):\n    unwrap(aiohttp_jinja2, \"render_template\")\n\n\ndef unpatch():\n    import aiohttp_jinja2\n\n    if not getattr(aiohttp_jinja2, \"_datadog_patch\", False):\n        return\n\n    _unpatch(aiohttp_jinja2)\n\n    setattr(aiohttp_jinja2, \"_datadog_patch\", False)\n", "path": "ddtrace/contrib/aiohttp_jinja2/patch.py"}]}