internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: open-telemetry/opentelemetry-python-contrib

Issue:
Fix eachdist.py
`scripts/eachdist.py` is ignoring certain packages because it tries to get the package name by splitting with `split("/", maxsplit=1)`. This fails for a regular package path:

```python
In [1]: "/a/b/c/d".split("/", maxsplit=1)
Out[1]: ['', 'a/b/c/d']
```
This script also tries to split at a different character if the operating system is Windows.

Both problems can be fixed by using [`basename`](https://docs.python.org/3/library/os.path.html#os.path.basename) which is OS-portable:

```python
In [1]: from os.path import basename

In [2]: basename("/a/b/c/d")
Out[2]: 'd'
```


Discussion:

Resolving patch:
diff --git a/scripts/eachdist.py b/scripts/eachdist.py
--- a/scripts/eachdist.py
+++ b/scripts/eachdist.py
@@ -11,6 +11,7 @@
 from datetime import datetime
 from inspect import cleandoc
 from itertools import chain
+from os.path import basename
 from pathlib import Path, PurePath
 
 DEFAULT_ALLSEP = " "
@@ -631,9 +632,7 @@ def update_dependencies(targets, version, packages):
         if str(pkg) == "all":
             continue
         print(pkg)
-        package_name = str(pkg).split("/", maxsplit=1)[-1]
-        # Windows uses backslashes
-        package_name = str(pkg).split("\\", maxsplit=1)[-1]
+        package_name = basename(pkg)
         print(package_name)
 
         update_files(


Test patch:
Repository: open-telemetry/opentelemetry-python-contrib

Issue:
Use {} instead of dict()



Discussion:

Resolving patch:
diff --git a/instrumentation/opentelemetry-instrumentation-celery/src/opentelemetry/instrumentation/celery/utils.py b/instrumentation/opentelemetry-instrumentation-celery/src/opentelemetry/instrumentation/celery/utils.py
--- a/instrumentation/opentelemetry-instrumentation-celery/src/opentelemetry/instrumentation/celery/utils.py
+++ b/instrumentation/opentelemetry-instrumentation-celery/src/opentelemetry/instrumentation/celery/utils.py
@@ -131,7 +131,7 @@ def attach_span(task, task_id, span, is_publish=False):
     """
     span_dict = getattr(task, CTX_KEY, None)
     if span_dict is None:
-        span_dict = dict()
+        span_dict = {}
         setattr(task, CTX_KEY, span_dict)
 
     span_dict[(task_id, is_publish)] = span
diff --git a/instrumentation/opentelemetry-instrumentation-sklearn/src/opentelemetry/instrumentation/sklearn/__init__.py b/instrumentation/opentelemetry-instrumentation-sklearn/src/opentelemetry/instrumentation/sklearn/__init__.py
--- a/instrumentation/opentelemetry-instrumentation-sklearn/src/opentelemetry/instrumentation/sklearn/__init__.py
+++ b/instrumentation/opentelemetry-instrumentation-sklearn/src/opentelemetry/instrumentation/sklearn/__init__.py
@@ -194,7 +194,7 @@ def get_base_estimators(packages: List[str]) -> Dict[str, Type[BaseEstimator]]:
         A dictionary of qualnames and classes inheriting from
         ``BaseEstimator``.
     """
-    klasses = dict()
+    klasses = {}
     for package_name in packages:
         lib = import_module(package_name)
         package_dir = os.path.dirname(lib.__file__)


Test patch:
Repository: open-telemetry/opentelemetry-python-contrib

Issue:
Add support for PyMySQL v1.x series
pymysql instrumentation does not work with 1.x series as it lists <1.0 as the compatible versions. I've tested the instrumentatoin with a simple application that uses PyMySQL 1.0 and it seems to work. We should update the instrumentation to support <2.0.

original issue: https://github.com/open-telemetry/opentelemetry-python/issues/2259


Discussion:

Resolving patch:
diff --git a/instrumentation/opentelemetry-instrumentation-pymysql/src/opentelemetry/instrumentation/pymysql/package.py b/instrumentation/opentelemetry-instrumentation-pymysql/src/opentelemetry/instrumentation/pymysql/package.py
--- a/instrumentation/opentelemetry-instrumentation-pymysql/src/opentelemetry/instrumentation/pymysql/package.py
+++ b/instrumentation/opentelemetry-instrumentation-pymysql/src/opentelemetry/instrumentation/pymysql/package.py
@@ -13,4 +13,4 @@
 # limitations under the License.
 
 
-_instruments = ("PyMySQL ~= 0.10.1",)
+_instruments = ("PyMySQL < 2",)
diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py
--- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py
+++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/bootstrap_gen.py
@@ -97,7 +97,7 @@
         "instrumentation": "opentelemetry-instrumentation-pymongo==0.25b2",
     },
     "PyMySQL": {
-        "library": "PyMySQL ~= 0.10.1",
+        "library": "PyMySQL < 2",
         "instrumentation": "opentelemetry-instrumentation-pymysql==0.25b2",
     },
     "pyramid": {


Test patch:
Repository: open-telemetry/opentelemetry-python-contrib

Issue:
Increase the usage of augmented assignment statements
This issue corresponds with [this one](https://github.com/open-telemetry/opentelemetry-python/issues/2258).


Discussion:

Resolving patch:
diff --git a/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py b/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py
--- a/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py
+++ b/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py
@@ -160,7 +160,7 @@ def collect_request_attributes(scope):
     if query_string and http_url:
         if isinstance(query_string, bytes):
             query_string = query_string.decode("utf8")
-        http_url = http_url + ("?" + urllib.parse.unquote(query_string))
+        http_url += "?" + urllib.parse.unquote(query_string)
 
     result = {
         SpanAttributes.HTTP_SCHEME: scope.get("scheme"),


Test patch:
Repository: open-telemetry/opentelemetry-python-contrib

Issue:
opentelemetry-instrumentation-urllib



Discussion:
Hi I want to take this up

Resolving patch:
diff --git a/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py b/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py
--- a/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py
+++ b/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py
@@ -184,14 +184,11 @@ def _instrumented_open_call(
         }
 
         with tracer.start_as_current_span(
-            span_name, kind=SpanKind.CLIENT
+            span_name, kind=SpanKind.CLIENT, attributes=labels
         ) as span:
             exception = None
             if callable(request_hook):
                 request_hook(span, request)
-            if span.is_recording():
-                span.set_attribute(SpanAttributes.HTTP_METHOD, method)
-                span.set_attribute(SpanAttributes.HTTP_URL, url)
 
             headers = get_or_create_headers()
             inject(headers)


Test patch:
Repository: open-telemetry/opentelemetry-python-contrib

Issue:
opentelemetry-instrument command can cause recursive creation of subprocesses
**Describe your environment**

Python3.9, linux.

**Steps to reproduce**
Using `opentelemetry-instrument` with any exporter or instrumentation which invokes a python subprocess **during initialization**. For example, the `opentelemetry-exporter-gcp-trace` exporter may invoke the `gcloud` (written in python) command in a subprocess to get project information and authentication tokens. The subprocess will then try to autoinstrument, creating a recursive loop of subprocesses being created.

**What is the expected behavior?**
Auto-instrumentation should not apply to subprocesses created in the `initialize()` phase of auto-instrumentation. The `PYTHONPATH` environment variable should have the `sitecustomize.py` dirname stripped out at the beginning of `sitecustomize.py`. This would prevent subprocesses from being autoinstrumented during setup, which can cause a loop.

**What is the actual behavior?**
`PYTHONPATH` is correctly stripped later on to avoid this https://github.com/open-telemetry/opentelemetry-python-contrib/blob/e9f83e1292b0fe5f3478c9b23f3b5a5508481e68/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py#L120-L125

However, any subprocesses created in [these lines](https://github.com/open-telemetry/opentelemetry-python-contrib/blob/e9f83e1292b0fe5f3478c9b23f3b5a5508481e68/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py#L114-L117) will cause a loop.

**Additional context**
I can write a repro if necessary.


Discussion:

Resolving patch:
diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py
--- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py
+++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/auto_instrumentation/sitecustomize.py
@@ -110,6 +110,13 @@ def _load_configurators():
 
 
 def initialize():
+    # prevents auto-instrumentation of subprocesses if code execs another python process
+    environ["PYTHONPATH"] = sub(
+        rf"{dirname(abspath(__file__))}{pathsep}?",
+        "",
+        environ["PYTHONPATH"],
+    )
+
     try:
         distro = _load_distros()
         distro.configure()
@@ -117,12 +124,6 @@ def initialize():
         _load_instrumentors(distro)
     except Exception:  # pylint: disable=broad-except
         logger.exception("Failed to auto initialize opentelemetry")
-    finally:
-        environ["PYTHONPATH"] = sub(
-            rf"{dirname(abspath(__file__))}{pathsep}?",
-            "",
-            environ["PYTHONPATH"],
-        )
 
 
 initialize()


Test patch:
Repository: open-telemetry/opentelemetry-python-contrib

Issue:
opentelemetry-instrumentation-aiohttp-client



Discussion:
I would like to take this issue.

Resolving patch:
diff --git a/instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py b/instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py
--- a/instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py
+++ b/instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py
@@ -180,27 +180,24 @@ async def on_request_start(
 
         http_method = params.method.upper()
         request_span_name = f"HTTP {http_method}"
+        request_url = (
+            remove_url_credentials(trace_config_ctx.url_filter(params.url))
+            if callable(trace_config_ctx.url_filter)
+            else remove_url_credentials(str(params.url))
+        )
+
+        span_attributes = {
+            SpanAttributes.HTTP_METHOD: http_method,
+            SpanAttributes.HTTP_URL: request_url,
+        }
 
         trace_config_ctx.span = trace_config_ctx.tracer.start_span(
-            request_span_name,
-            kind=SpanKind.CLIENT,
+            request_span_name, kind=SpanKind.CLIENT, attributes=span_attributes
         )
 
         if callable(request_hook):
             request_hook(trace_config_ctx.span, params)
 
-        if trace_config_ctx.span.is_recording():
-            attributes = {
-                SpanAttributes.HTTP_METHOD: http_method,
-                SpanAttributes.HTTP_URL: remove_url_credentials(
-                    trace_config_ctx.url_filter(params.url)
-                )
-                if callable(trace_config_ctx.url_filter)
-                else remove_url_credentials(str(params.url)),
-            }
-            for key, value in attributes.items():
-                trace_config_ctx.span.set_attribute(key, value)
-
         trace_config_ctx.token = context_api.attach(
             trace.set_span_in_context(trace_config_ctx.span)
         )


Test patch:
Repository: open-telemetry/opentelemetry-python-contrib

Issue:
Wrong metric reader argument in system metrics example
The [system metrics example](https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py#L52) does not use a list of metric readers as an argument to `MeterProvider`.


Discussion:

Resolving patch:
diff --git a/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py b/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py
--- a/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py
+++ b/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py
@@ -49,7 +49,7 @@
 
     exporter = ConsoleMetricExporter()
 
-    set_meter_provider(MeterProvider(PeriodicExportingMetricReader(exporter)))
+    set_meter_provider(MeterProvider([PeriodicExportingMetricReader(exporter)]))
     SystemMetricsInstrumentor().instrument()
 
     # metrics are collected asynchronously


Test patch:
Repository: open-telemetry/opentelemetry-python-contrib

Issue:
Typos in gRPC instrumentation example codes
**Describe your environment**

* Platform: any
* Python version: any
* Library version: main HEAD as of Aug 24th

**Steps to reproduce**
See [documents for gRPC instrumentation](https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/grpc/grpc.html)

**What is the expected behavior?**
For client instrumentation example, I would see either of the followings:

1. `GrpcInstrumentorClient().instrument()` is called without return value assignment.
2. Get the return value of `GrpcInstrumentorClient()` and call `instrument()` method of it.

**What is the actual behavior?**

I see the following example codes.

In the [Usage Client](https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/grpc/grpc.html#usage-client) section:

```python
instrumentor = GrpcInstrumentorClient().instrument()
```

And in the [GrpcInstrumentorClient class document](https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/grpc/grpc.html#usage-client):

```python
grpc_client_instrumentor = GrpcInstrumentorClient()
grpc.client_instrumentor.instrument()
```

where 2nd line is calling non existing package. It should be typo of the variable defined in 1st line.

**Additional context**

I was trying implement the code to fix #373 and found this issue, so I created separate issue here. I'll create a PR for this shortly.


Discussion:

Resolving patch:
diff --git a/instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/__init__.py b/instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/__init__.py
--- a/instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/__init__.py
+++ b/instrumentation/opentelemetry-instrumentation-grpc/src/opentelemetry/instrumentation/grpc/__init__.py
@@ -43,7 +43,8 @@
         SimpleSpanProcessor(ConsoleSpanExporter())
     )
 
-    instrumentor = GrpcInstrumentorClient().instrument()
+    grpc_client_instrumentor = GrpcInstrumentorClient()
+    grpc_client_instrumentor.instrument()
 
     def run():
         with grpc.insecure_channel("localhost:50051") as channel:
@@ -180,7 +181,7 @@ class GrpcInstrumentorClient(BaseInstrumentor):
     Usage::
 
         grpc_client_instrumentor = GrpcInstrumentorClient()
-        grpc.client_instrumentor.instrument()
+        grpc_client_instrumentor.instrument()
 
     """
 


Test patch:
Repository: open-telemetry/opentelemetry-python-contrib

Issue:
is_instrumented_by_opentelemetry is not set in _InstrumentedFastAPI class
**Steps to reproduce**
If `instrument_app` function is called for already instrumented object of `_InstrumentedFastAPI` then it will add Opentelemtery middleware again in the `user_middleware` list as `is_instrumented_by_opentelemetry` is not set in the `__init__` function of `_InstrumentedFastAPI`.

**What is the expected behavior?**
If a fastapi app is already instrumented, i.e., it already has otel middleware in it then it shouldn't add another one.

**What is the actual behavior?**
Multiple otel middlewares are added.


Discussion:

Resolving patch:
diff --git a/instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py b/instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py
--- a/instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py
+++ b/instrumentation/opentelemetry-instrumentation-fastapi/src/opentelemetry/instrumentation/fastapi/__init__.py
@@ -247,6 +247,7 @@ def __init__(self, *args, **kwargs):
             client_response_hook=_InstrumentedFastAPI._client_response_hook,
             tracer_provider=_InstrumentedFastAPI._tracer_provider,
         )
+        self._is_instrumented_by_opentelemetry = True
 
 
 def _get_route_details(scope):


Test patch:
diff --git a/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py b/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py
--- a/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py
+++ b/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py
@@ -274,6 +274,14 @@ def test_request(self):
             self.assertEqual(span.resource.attributes["key1"], "value1")
             self.assertEqual(span.resource.attributes["key2"], "value2")
 
+    def test_mulitple_way_instrumentation(self):
+        self._instrumentor.instrument_app(self._app)
+        count = 0
+        for middleware in self._app.user_middleware:
+            if middleware.cls is OpenTelemetryMiddleware:
+                count += 1
+        self.assertEqual(count, 1)
+
     def tearDown(self):
         self._instrumentor.uninstrument()
         super().tearDown()
Repository: open-telemetry/opentelemetry-python-contrib

Issue:
Getting ZeroDivisionError  in Swap Utilization
**Describe your environment**
Python: 3.9
opentelemetry-instrumentation-psycopg2 = "^0.33b0"
opentelemetry-instrumentation-django = "^0.33b0"
opentelemetry-instrumentation-celery = "^0.33b0"
opentelemetry-exporter-otlp-proto-grpc = "^1.12.0"
Running on ECS (Docker)

The problem is with system metrics in this line, the when container is configured without swap memory
https://github.com/open-telemetry/opentelemetry-python-contrib/blob/76a565e8bde7f81a04315eae8431d21fc6e79383/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py#L503

**Steps to reproduce**
Create a container with 0 Swap memory and run with system metrics

**What is the expected behavior?**
Just work

**What is the actual behavior?**
Giving ZeroDivisionError





Discussion:

Resolving patch:
diff --git a/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py b/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py
--- a/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py
+++ b/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py
@@ -420,7 +420,9 @@ def _get_system_swap_utilization(
             if hasattr(system_swap, metric):
                 self._system_swap_utilization_labels["state"] = metric
                 yield Observation(
-                    getattr(system_swap, metric) / system_swap.total,
+                    getattr(system_swap, metric) / system_swap.total
+                    if system_swap.total
+                    else 0,
                     self._system_swap_utilization_labels.copy(),
                 )
 


Test patch:
Repository: open-telemetry/opentelemetry-python-contrib

Issue:
aiohttp-client: Allow overriding of span status
Version 0.34b0

In the aiohttp-client instrumentation, the `response_hook` is [called before setting the status and the status_code of the span](https://github.com/open-telemetry/opentelemetry-python-contrib/blob/75953f3b25c2ffb5427371aaf065671b441aca26/instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py#L235). This means the user cannot set a custom status on the span, as it will be immediately overridden by the default value. 

In my specific usecase I am making requests as a cache, where a cache-miss returns a 404 response. These spans are than tagged with the `StatusCode.ERROR`, which is a bit annoying to me as these responses are completely expected and accepted. Therefore I would like to override the StatusCode on those requests in the response_hook.

I am happy to provide a fix for this.

**What is the expected behavior?**
`reponse_hook` should be called after setting the span status, to allow for overriding it.

**What is the actual behavior?**
`reponse_hook` is called before setting the span status

**Additional context**
I compared this with the httpx and requests instrumentation. In those instrumentations, overriding the status in the hook is possible.


Discussion:

Resolving patch:
diff --git a/instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py b/instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py
--- a/instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py
+++ b/instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py
@@ -232,12 +232,13 @@ async def on_request_exception(
         if trace_config_ctx.span is None:
             return
 
-        if callable(response_hook):
-            response_hook(trace_config_ctx.span, params)
-
         if trace_config_ctx.span.is_recording() and params.exception:
             trace_config_ctx.span.set_status(Status(StatusCode.ERROR))
             trace_config_ctx.span.record_exception(params.exception)
+
+        if callable(response_hook):
+            response_hook(trace_config_ctx.span, params)
+
         _end_trace(trace_config_ctx)
 
     def _trace_config_ctx_factory(**kwargs):


Test patch: