internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: DataDog/dd-trace-py

Issue:
profiling/line2def does not handle empty filenames
### Which version of dd-trace-py are you using?
We're not running dd-trace - we're running the profiler by importing `ddtrace.profiling.auto`.

### Which version of the libraries are you using?
ddtrace: 0.40.0
datadog: 0.38.0

You can copy/paste the output of `pip freeze` here.

### How can we reproduce your problem?
I'm unsure - this appears to happen sporadically.

### What is the result that you get?
First, ddtrace runs into a KeyError in `_to_Location`, line 90:
```
def _to_Location(self, filename, lineno, funcname=None):
        try:
            return self._locations[(filename, lineno, funcname)]
```
`filename` is '', `lineno` is 1, `funcname` is None.

Next, in `filename_and_lineno_to_def`, line 63, we get an IndexError:
```
def filename_and_lineno_to_def(filename, lineno):
    if filename[0] == "<" and filename[-1] == ">":
        return default_def(filename, lineno)
```
Since the filename is an empty string, this complains.



### What is the result that you expected?
Not an error.

If you need more information, please let me know!


Discussion:

Resolving patch:
diff --git a/ddtrace/profiling/_line2def.py b/ddtrace/profiling/_line2def.py
--- a/ddtrace/profiling/_line2def.py
+++ b/ddtrace/profiling/_line2def.py
@@ -55,12 +55,12 @@ def file_to_tree(filename):
 
 
 def default_def(filename, lineno):
-    return filename + ":" + str(lineno)
+    return str(filename) + ":" + str(lineno)
 
 
 @lru_cache(maxsize=8192)
 def filename_and_lineno_to_def(filename, lineno):
-    if filename[0] == "<" and filename[-1] == ">":
+    if not filename or (filename[0] == "<" and filename[-1] == ">"):
         return default_def(filename, lineno)
 
     try:


Test patch:
diff --git a/tests/profiling/test_line2def.py b/tests/profiling/test_line2def.py
--- a/tests/profiling/test_line2def.py
+++ b/tests/profiling/test_line2def.py
@@ -67,3 +67,12 @@ def test_filename_and_lineno_to_def_oserror():
 def test_filename_and_lineno_to_def_wrong_syntax():
     filename = os.path.join(os.path.dirname(__file__), "_wrong_file")
     assert _line2def.filename_and_lineno_to_def(filename, 8) == ("%s:8" % filename)
+
+
+def test_empty_filename_to_def():
+    assert _line2def.filename_and_lineno_to_def("", 1) == ":1"
+
+
+def test_bracket_filename_to_def():
+    assert _line2def.filename_and_lineno_to_def("<input>", 2) == "<input>:2"
+    assert _line2def.filename_and_lineno_to_def("<>", 2) == "<>:2"
Repository: DataDog/dd-trace-py

Issue:
Flask support doesn't call store_request_headers
Flask integration doesn't store request/response headers on the span. Other integrations such as Falcon do.


Discussion:

Resolving patch:
diff --git a/ddtrace/contrib/flask/middleware.py b/ddtrace/contrib/flask/middleware.py
--- a/ddtrace/contrib/flask/middleware.py
+++ b/ddtrace/contrib/flask/middleware.py
@@ -3,6 +3,8 @@
 from ...internal.logger import get_logger
 from ...propagation.http import HTTPPropagator
 from ...utils.deprecation import deprecated
+from ddtrace.http import store_request_headers
+from ddtrace import config
 
 import flask.templating
 from flask import g, request, signals
@@ -155,6 +157,7 @@ def _finish_span(self, span, exception=None):
         endpoint = ''
         url = ''
         if request:
+            store_request_headers(request.headers, span, config.flask)
             method = request.method
             endpoint = request.endpoint or code
             url = request.base_url or ''


Test patch:
diff --git a/tests/contrib/flask/test_middleware.py b/tests/contrib/flask/test_middleware.py
--- a/tests/contrib/flask/test_middleware.py
+++ b/tests/contrib/flask/test_middleware.py
@@ -7,6 +7,7 @@
 from ddtrace.contrib.flask import TraceMiddleware
 from ddtrace.constants import SAMPLING_PRIORITY_KEY
 from ddtrace.ext import http, errors
+from ddtrace import config
 
 from tests.opentracer.utils import init_tracer
 from .web import create_app
@@ -385,3 +386,17 @@ def test_success_200_ot(self):
         assert dd_span.error == 0
         assert_span_http_status_code(dd_span, 200)
         assert dd_span.meta.get(http.METHOD) == 'GET'
+
+    def test_http_request_header_tracing(self):
+        config.flask.http.trace_headers(['Host', 'my-header'])
+        self.app.get('/', headers={
+            'my-header': 'my_value',
+        })
+        traces = self.tracer.writer.pop_traces()
+
+        assert len(traces) == 1
+        assert len(traces[0]) == 1
+        span = traces[0][0]
+
+        assert span.get_tag('http.request.headers.my-header') == 'my_value'
+        assert span.get_tag('http.request.headers.host') == 'localhost'
Repository: DataDog/dd-trace-py

Issue:
RuntimeError: Set changed size during iteration
Python Version: 3.7.7

Error:
```
RuntimeError: Set changed size during iteration
  File "django/core/handlers/wsgi.py", line 141, in __call__
    response = self.get_response(request)
  File "ddtrace/contrib/django/patch.py", line 82, in wrapper
    return func(mod, pin, wrapped, instance, args, kwargs)
  File "ddtrace/contrib/django/patch.py", line 406, in traced_get_response
    span_type=SpanTypes.HTTP,
  File "ddtrace/tracer.py", line 638, in trace
    span_type=span_type,
  File "ddtrace/tracer.py", line 389, in start_span
    new_ctx = self._check_new_process()
  File "ddtrace/tracer.py", line 570, in _check_new_process
    self._update_dogstatsd_constant_tags()
  File "ddtrace/tracer.py", line 525, in _update_dogstatsd_constant_tags
    for k, v in RuntimeTags()
  File "ddtrace/tracer.py", line 524, in <listcomp>
    '{}:{}'.format(k, v)
  File "ddtrace/internal/runtime/runtime_metrics.py", line 29, in <genexpr>
    collected = (collector.collect(self._enabled) for collector in self._collectors)
  File "ddtrace/internal/runtime/collector.py", line 67, in collect
    self.value = self.collect_fn(keys)
  File "ddtrace/internal/runtime/tag_collectors.py", line 25, in collect_fn
    tags = [(SERVICE, service) for service in ddtrace.tracer._services]
  File "ddtrace/internal/runtime/tag_collectors.py", line 25, in <listcomp>
    tags = [(SERVICE, service) for service in ddtrace.tracer._services]
```

### Which version of dd-trace-py are you using?
```
0.42.0
```

### Which version of the libraries are you using?
```
django==2.2.14
ddtrace==0.42.0
gunicorn==20.0.4
```

### How can we reproduce your problem?
It's not clear if there's anything specific about the WSGI requests that triggers this condition. 

### What is the result that you get?
-

### What is the result that you expected?
-


Discussion:
Thank you for reporting this @YukSeungChan. We will work on a fix for this and update this issue.

Resolving patch:
diff --git a/ddtrace/internal/runtime/tag_collectors.py b/ddtrace/internal/runtime/tag_collectors.py
--- a/ddtrace/internal/runtime/tag_collectors.py
+++ b/ddtrace/internal/runtime/tag_collectors.py
@@ -21,7 +21,8 @@ class TracerTagCollector(RuntimeTagCollector):
 
     def collect_fn(self, keys):
         ddtrace = self.modules.get("ddtrace")
-        tags = [(SERVICE, service) for service in ddtrace.tracer._services]
+        # make sure to copy _services to avoid RuntimeError: Set changed size during iteration
+        tags = [(SERVICE, service) for service in list(ddtrace.tracer._services)]
         if ENV_KEY in ddtrace.tracer.tags:
             tags.append((ENV_KEY, ddtrace.tracer.tags[ENV_KEY]))
         return tags


Test patch:
Repository: DataDog/dd-trace-py

Issue:
Add dogpile.cache to support libraries
We need to update https://github.com/DataDog/dd-trace-py/blob/master/docs/index.rst after #1123 


Discussion:

Resolving patch:
diff --git a/ddtrace/contrib/dogpile_cache/__init__.py b/ddtrace/contrib/dogpile_cache/__init__.py
--- a/ddtrace/contrib/dogpile_cache/__init__.py
+++ b/ddtrace/contrib/dogpile_cache/__init__.py
@@ -1,12 +1,12 @@
 """
 Instrument dogpile.cache__ to report all cached lookups.
 
-This will add spans around the calls to your cache backend (eg. redis, memory,
+This will add spans around the calls to your cache backend (e.g. redis, memory,
 etc). The spans will also include the following tags:
 
 - key/keys: The key(s) dogpile passed to your backend. Note that this will be
   the output of the region's ``function_key_generator``, but before any key
-  mangling is applied (ie. the region's ``key_mangler``).
+  mangling is applied (i.e. the region's ``key_mangler``).
 - region: Name of the region.
 - backend: Name of the backend class.
 - hit: If the key was found in the cache.
diff --git a/ddtrace/monkey.py b/ddtrace/monkey.py
--- a/ddtrace/monkey.py
+++ b/ddtrace/monkey.py
@@ -68,6 +68,7 @@
     "pynamodb": True,
     "pyodbc": True,
     "fastapi": True,
+    "dogpile_cache": True,
 }
 
 _LOCK = threading.Lock()


Test patch:
Repository: DataDog/dd-trace-py

Issue:
Documentation site code uses incorrect class from ddtrace/contrib/wsgi/wsgi.py
### Which version of dd-trace-py are you using?
0.48.1

### What libraries am I using:
Python: 3.8

### How can we reproduce your problem?
1. Copy the example code from https://ddtrace.readthedocs.io/en/stable/integrations.html#wsgi
2. Example code cites `DDTraceMiddleware` class
2. Run any wsgi application with said code

### What is the result that you get?
```
ImportError: cannot import name 'DDTraceMiddleware' from 'ddtrace.contrib.wsgi' (/usr/local/lib/python3.8/site-packages/ddtrace/contrib/wsgi/__init__.py)
```

### What is the result that you expected?
Example code in readthedocs site should properly cite the import for the `DDWSGIMiddleware` class: https://github.com/DataDog/dd-trace-py/blob/master/ddtrace/contrib/wsgi/wsgi.py#L83



Discussion:

Resolving patch:
diff --git a/ddtrace/contrib/wsgi/__init__.py b/ddtrace/contrib/wsgi/__init__.py
--- a/ddtrace/contrib/wsgi/__init__.py
+++ b/ddtrace/contrib/wsgi/__init__.py
@@ -8,10 +8,10 @@
 The middleware can be used manually via the following command::
 
 
-    from ddtrace.contrib.wsgi import DDTraceMiddleware
+    from ddtrace.contrib.wsgi import DDWSGIMiddleware
 
     # application is a WSGI application
-    application = DDTraceMiddleware(application)
+    application = DDWSGIMiddleware(application)
 
 
 Global Configuration


Test patch:
Repository: DataDog/dd-trace-py

Issue:
mypy stub files
mypy throws an error when ddtrace is in use:

![image](https://user-images.githubusercontent.com/284476/58525411-a7ff0c00-8199-11e9-8bcb-770aeeeda3fb.png)


It would be nice if this library could ship with stub files or be put on https://github.com/python/typeshed. I'm not sure how to do that yet but maybe someone here is.

More info from mypy side here https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports


Discussion:
@jasonkuhrt thank you for opening the request, this has been on my personal todo list for awhile.

I also do not know how to do it, but would love to try and find the time to do this.
@brettlangdon fwiw this is what we did locally in our project to satisfy mypy (we're only stubbing the parts we use, not complete, of course)

![image](https://user-images.githubusercontent.com/284476/58559945-24253e00-81f2-11e9-96a4-90ac9ea762e4.png)

Nice! Thanks for sharing.
I ended up creating a compat module so that I could use `ddtrace.tracer.wrap` without getting warnings:

```python
from typing import Any, Callable, TypeVar

import ddtrace

# This allows us to use a type annotated ddtrace.tracer.wrap.
# First, we assign the name "trace" to ddtrace.tracer.wrap.
# Then, we assign a type:
# "trace" is a callable that takes arguments, and returns a function.
# That function takes another function as an argument and returns it.
F = TypeVar("F", bound=Callable[..., Any])
trace: Callable[..., Callable[[F], F]] = ddtrace.tracer.wrap
```

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -157,8 +157,10 @@ def get_exts_for(name):
     long_description_content_type="text/markdown",
     license="BSD",
     packages=find_packages(exclude=["tests*"]),
+    package_data={"ddtrace": ["py.typed"]},
     py_modules=["ddtrace_gevent_check"],
     python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*",
+    zip_safe=False,
     # enum34 is an enum backport for earlier versions of python
     # funcsigs backport required for vendored debtcollector
     install_requires=[


Test patch:
Repository: DataDog/dd-trace-py

Issue:
Incorrect types for patch() and patch_all() keyword arguments
### Which version of dd-trace-py are you using?
0.51.1

### Which version of pip are you using?

20.2.3

### Which version of the libraries are you using?

fastapi==0.63.0

### How can we reproduce your problem?

I have this line of code: `    ddtrace.patch(fastapi=True)`

And then in the command line I run `mypy project_dir`

### What is the result that you get?

`error: Argument "fastapi" to "patch" has incompatible type "bool"; expected "Dict[str, bool]"`

### What is the result that you expected?

No type errors


I believe this is because the type of the patch_modules parameter should be just `bool` and not `Dict[str, bool]` because it is a keyword arguments parameter (declared with **) for these two functions. See: https://www.python.org/dev/peps/pep-0484/#arbitrary-argument-lists-and-default-argument-values 


Discussion:
https://ddtrace.readthedocs.io/en/stable/advanced_usage.html#patch

I see `Dict[str, bool]`. Seems like just `bool`, I agree.

Resolving patch:
diff --git a/ddtrace/monkey.py b/ddtrace/monkey.py
--- a/ddtrace/monkey.py
+++ b/ddtrace/monkey.py
@@ -12,7 +12,6 @@
 import threading
 from typing import Any
 from typing import Callable
-from typing import Dict
 from typing import List
 
 from ddtrace.vendor.wrapt.importer import when_imported
@@ -136,7 +135,7 @@ def on_import(hook):
 
 
 def patch_all(**patch_modules):
-    # type: (Dict[str, bool]) -> None
+    # type: (bool) -> None
     """Automatically patches all available modules.
 
     In addition to ``patch_modules``, an override can be specified via an
@@ -166,7 +165,7 @@ def patch_all(**patch_modules):
 
 
 def patch(raise_errors=True, **patch_modules):
-    # type: (bool, Dict[str, bool]) -> None
+    # type: (bool, bool) -> None
     """Patch only a set of given modules.
 
     :param bool raise_errors: Raise error if one patch fail.


Test patch:
Repository: DataDog/dd-trace-py

Issue:
Incorrect types for patch() and patch_all() keyword arguments
### Which version of dd-trace-py are you using?
0.51.1

### Which version of pip are you using?

20.2.3

### Which version of the libraries are you using?

fastapi==0.63.0

### How can we reproduce your problem?

I have this line of code: `    ddtrace.patch(fastapi=True)`

And then in the command line I run `mypy project_dir`

### What is the result that you get?

`error: Argument "fastapi" to "patch" has incompatible type "bool"; expected "Dict[str, bool]"`

### What is the result that you expected?

No type errors


I believe this is because the type of the patch_modules parameter should be just `bool` and not `Dict[str, bool]` because it is a keyword arguments parameter (declared with **) for these two functions. See: https://www.python.org/dev/peps/pep-0484/#arbitrary-argument-lists-and-default-argument-values 


Discussion:
https://ddtrace.readthedocs.io/en/stable/advanced_usage.html#patch

I see `Dict[str, bool]`. Seems like just `bool`, I agree.
@mscheifer and @gitpushdashf thanks! Great catch. I have opened up #2744 to address this issue.

Resolving patch:
diff --git a/ddtrace/monkey.py b/ddtrace/monkey.py
--- a/ddtrace/monkey.py
+++ b/ddtrace/monkey.py
@@ -12,7 +12,6 @@
 import threading
 from typing import Any
 from typing import Callable
-from typing import Dict
 from typing import List
 
 from ddtrace.vendor.wrapt.importer import when_imported
@@ -135,7 +134,7 @@ def on_import(hook):
 
 
 def patch_all(**patch_modules):
-    # type: (Dict[str, bool]) -> None
+    # type: (bool) -> None
     """Automatically patches all available modules.
 
     In addition to ``patch_modules``, an override can be specified via an
@@ -165,7 +164,7 @@ def patch_all(**patch_modules):
 
 
 def patch(raise_errors=True, **patch_modules):
-    # type: (bool, Dict[str, bool]) -> None
+    # type: (bool, bool) -> None
     """Patch only a set of given modules.
 
     :param bool raise_errors: Raise error if one patch fail.


Test patch:
Repository: DataDog/dd-trace-py

Issue:
tuple index out of range of threading.py
After upgrading from ddtrace==0.46.0 to version ddtrace==0.55.4 my service crash with IndexError.
```
Traceback (most recent call last):
  File "/my_service/services/base_service.py", line 105, in run
    futures.append(executor.submit(fn=self._single_entry_point_run, entry_point=entry_point))
  File "/my_service/venv/lib/python3.7/site-packages/ddtrace/contrib/futures/threading.py", line 26, in _wrap_submit
    fn = args[0]
IndexError: tuple index out of range
```

I'm facing this issue even when setting futures=False.
`patch_all(celery=True, django=True, psycopg2=True, redis=True, futures=True)`



Discussion:

Resolving patch:
diff --git a/ddtrace/contrib/futures/threading.py b/ddtrace/contrib/futures/threading.py
--- a/ddtrace/contrib/futures/threading.py
+++ b/ddtrace/contrib/futures/threading.py
@@ -21,10 +21,12 @@ def _wrap_submit(func, instance, args, kwargs):
     if ddtrace.tracer.context_provider._has_active_context():
         current_ctx = ddtrace.tracer.context_provider.active()
 
-    # extract the target function that must be executed in
-    # a new thread and the `target` arguments
-    fn = args[0]
-    fn_args = args[1:]
+    # The target function can be provided as a kwarg argument "fn" or the first positional argument
+    if "fn" in kwargs:
+        fn = kwargs.pop("fn")
+        fn_args = args
+    else:
+        fn, fn_args = args[0], args[1:]
     return func(_wrap_execution, current_ctx, fn, fn_args, kwargs)
 
 


Test patch:
diff --git a/tests/contrib/futures/test_propagation.py b/tests/contrib/futures/test_propagation.py
--- a/tests/contrib/futures/test_propagation.py
+++ b/tests/contrib/futures/test_propagation.py
@@ -68,6 +68,28 @@ def fn(value, key=None):
             (dict(name="executor.thread"),),
         )
 
+    def test_propagation_with_kwargs(self):
+        # instrumentation must work if only kwargs are provided
+
+        def fn(value, key=None):
+            with self.tracer.trace("executor.thread"):
+                return value, key
+
+        with self.override_global_tracer():
+            with self.tracer.trace("main.thread"):
+                with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
+                    future = executor.submit(fn=fn, value=42, key="CheeseShop")
+                    value, key = future.result()
+                    # assert the right result
+                    self.assertEqual(value, 42)
+                    self.assertEqual(key, "CheeseShop")
+
+        # the trace must be completed
+        self.assert_structure(
+            dict(name="main.thread"),
+            (dict(name="executor.thread"),),
+        )
+
     def test_disabled_instrumentation(self):
         # it must not propagate if the module is disabled
         unpatch()
Repository: DataDog/dd-trace-py

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"])`


Discussion:
@manuher your proposal makes sense. Could you start a PR that includes a test for supporting passing along the `app_key` argument from `render_template` to `get_env`?

Resolving patch:
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 @@ def traced_render_template(aiohttp_jinja2, pin, func, instance, args, kwargs):
     # 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", "")


Test patch:
Repository: DataDog/dd-trace-py

Issue:
pytest plugin starts tracer when plugin is disabled
### Which version of dd-trace-py are you using?

1.1.1

### Which version of pip are you using?

```
# pip --version
pip 22.0.4 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)
```

### Which version of the libraries are you using?

```
# pip freeze
attrs==21.4.0
ddsketch==2.0.1
ddtrace==1.1.1
iniconfig==1.1.1
packaging==21.3
pluggy==1.0.0
protobuf==3.20.1
py==1.11.0
pyparsing==3.0.8
pytest==7.1.2
six==1.16.0
tenacity==8.0.1
tomli==2.0.1
```

### How can we reproduce your problem?

Check out https://gist.github.com/majorgreys/f799970f731fdf70d865d18325e1c34d and run:

```
docker run -it --rm (docker build -q .)
```

### What is the result that you get?

`ddtrace` modules are imported by the Python interpreter. Importing any of these modules starts the global tracer.

The workaround for this is to disable the `ddtrace` plugin with `-p no:ddtrace` in the command.

### What is the result that you expected?

`ddtrace` should not be the modules imported by the Python interpreter.





Discussion:

Resolving patch:
diff --git a/ddtrace/contrib/pytest/__init__.py b/ddtrace/contrib/pytest/__init__.py
--- a/ddtrace/contrib/pytest/__init__.py
+++ b/ddtrace/contrib/pytest/__init__.py
@@ -18,6 +18,22 @@
     ddtrace = 1
     ddtrace-patch-all = 1
 
+
+.. note::
+   The ddtrace plugin for pytest has the side effect of importing the ddtrace
+   package and starting a global tracer.
+
+   If this is causing issues for your pytest runs where traced execution of
+   tests is not enabled, you can deactivate the plugin::
+
+     [pytest]
+     addopts = -p no:ddtrace
+
+   See the `pytest documentation
+   <https://docs.pytest.org/en/7.1.x/how-to/plugins.html#deactivating-unregistering-a-plugin-by-name>`_
+   for more details.
+
+
 Global Configuration
 ~~~~~~~~~~~~~~~~~~~~
 


Test patch: