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

Issue:
_clean_attribute shouldn't skip attributes
From attributes [spec](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/common/common.md#attributes)

>_This is required for map/dictionary structures represented as two arrays with indices that are kept in sync (e.g., two attributes header_keys and header_values, both containing an array of strings to represent a mapping header_keys[i] -> header_values[i])._

https://github.com/open-telemetry/opentelemetry-python/blob/653207dd2181db1a766a4a703dcda78fd7703bb2/opentelemetry-api/src/opentelemetry/attributes/__init__.py#L118-L123

https://github.com/open-telemetry/opentelemetry-python/blob/653207dd2181db1a766a4a703dcda78fd7703bb2/opentelemetry-api/src/opentelemetry/attributes/__init__.py#L66-L69

I think we shouldn't be continuing here when decode fails on byte value. Two options I can think of is reject everything by returning None or use empty value (""). What do you think?



Discussion:
I'm leaning towards using empty value. The spec says that we should accept null (None) and let exporters decide what to do with it so probably the empty value should be `None`, not `""`. 
Makes sense, empty string might be misinterpreted as successful operation.

Resolving patch:
diff --git a/opentelemetry-api/src/opentelemetry/attributes/__init__.py b/opentelemetry-api/src/opentelemetry/attributes/__init__.py
--- a/opentelemetry-api/src/opentelemetry/attributes/__init__.py
+++ b/opentelemetry-api/src/opentelemetry/attributes/__init__.py
@@ -59,13 +59,9 @@ def _clean_attribute(
         cleaned_seq = []
 
         for element in value:
-            # None is considered valid in any sequence
-            if element is None:
-                cleaned_seq.append(element)
-
             element = _clean_attribute_value(element, max_len)
-            # reject invalid elements
             if element is None:
+                cleaned_seq.append(element)
                 continue
 
             element_type = type(element)


Test patch:
diff --git a/opentelemetry-api/tests/attributes/test_attributes.py b/opentelemetry-api/tests/attributes/test_attributes.py
--- a/opentelemetry-api/tests/attributes/test_attributes.py
+++ b/opentelemetry-api/tests/attributes/test_attributes.py
@@ -69,6 +69,25 @@ def test_clean_attribute(self):
         self.assertInvalid("value", "")
         self.assertInvalid("value", None)
 
+    def test_sequence_attr_decode(self):
+        seq = [
+            None,
+            b"Content-Disposition",
+            b"Content-Type",
+            b"\x81",
+            b"Keep-Alive",
+        ]
+        expected = [
+            None,
+            "Content-Disposition",
+            "Content-Type",
+            None,
+            "Keep-Alive",
+        ]
+        self.assertEqual(
+            _clean_attribute("headers", seq, None), tuple(expected)
+        )
+
 
 class TestBoundedAttributes(unittest.TestCase):
     base = collections.OrderedDict(
Repository: open-telemetry/opentelemetry-python

Issue:
_OTEL_METRICS_EXPORTER env var should be OTEL_METRICS_EXPORTER
The environment variable `_OTEL_METRICS_EXPORTER` is prefixed with an underscore, but there's no need for it as that environment variable is marked as stable in the specification https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md#exporter-selection




Discussion:

Resolving patch:
diff --git a/opentelemetry-api/src/opentelemetry/environment_variables.py b/opentelemetry-api/src/opentelemetry/environment_variables.py
--- a/opentelemetry-api/src/opentelemetry/environment_variables.py
+++ b/opentelemetry-api/src/opentelemetry/environment_variables.py
@@ -12,6 +12,12 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+OTEL_METRICS_EXPORTER = "OTEL_METRICS_EXPORTER"
+"""
+.. envvar:: OTEL_METRICS_EXPORTER
+
+"""
+
 OTEL_PROPAGATORS = "OTEL_PROPAGATORS"
 """
 .. envvar:: OTEL_PROPAGATORS
@@ -41,9 +47,3 @@
 """
 .. envvar:: OTEL_PYTHON_METER_PROVIDER
 """
-
-_OTEL_METRICS_EXPORTER = "OTEL_METRICS_EXPORTER"
-"""
-.. envvar:: OTEL_METRICS_EXPORTER
-
-"""


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

Issue:
Add support for OTEL_LOGS_EXPORTER
As with metrics and tracing, the specification defines an environment variable for configuring logs exporters:

https://github.com/open-telemetry/opentelemetry-specification/pull/2196

We should add support for it. 


Discussion:

Resolving patch:
diff --git a/opentelemetry-api/src/opentelemetry/environment_variables.py b/opentelemetry-api/src/opentelemetry/environment_variables.py
--- a/opentelemetry-api/src/opentelemetry/environment_variables.py
+++ b/opentelemetry-api/src/opentelemetry/environment_variables.py
@@ -12,6 +12,12 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+OTEL_LOGS_EXPORTER = "OTEL_LOGS_EXPORTER"
+"""
+.. envvar:: OTEL_LOGS_EXPORTER
+
+"""
+
 OTEL_METRICS_EXPORTER = "OTEL_METRICS_EXPORTER"
 """
 .. envvar:: OTEL_METRICS_EXPORTER
diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py
--- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py
+++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py
@@ -25,6 +25,7 @@
 
 from opentelemetry import trace
 from opentelemetry.environment_variables import (
+    OTEL_LOGS_EXPORTER,
     OTEL_PYTHON_ID_GENERATOR,
     OTEL_TRACES_EXPORTER,
 )
@@ -45,9 +46,6 @@
 _RANDOM_ID_GENERATOR = "random"
 _DEFAULT_ID_GENERATOR = _RANDOM_ID_GENERATOR
 
-# TODO: add log exporter env variable
-_OTEL_LOGS_EXPORTER = "OTEL_LOGS_EXPORTER"
-
 
 def _get_id_generator() -> str:
     return environ.get(OTEL_PYTHON_ID_GENERATOR, _DEFAULT_ID_GENERATOR)
@@ -175,7 +173,7 @@ def _import_id_generator(id_generator_name: str) -> IdGenerator:
 def _initialize_components(auto_instrumentation_version):
     trace_exporters, log_exporters = _import_exporters(
         _get_exporter_names(environ.get(OTEL_TRACES_EXPORTER)),
-        _get_exporter_names(environ.get(_OTEL_LOGS_EXPORTER)),
+        _get_exporter_names(environ.get(OTEL_LOGS_EXPORTER)),
     )
     id_generator_name = _get_id_generator()
     id_generator = _import_id_generator(id_generator_name)


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

Issue:
Consider removing exporters from MeterProvider __init__ parameters
From [this comment](https://github.com/open-telemetry/opentelemetry-python/pull/2296#discussion_r765002669).


Discussion:
This would allow us to move `Metric` and `MetricData` into `_metrics/__init__.py` as mentioned in #2323, as it stands today, it causes a circular import.

Resolving patch:
diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py
--- a/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py
+++ b/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py
@@ -32,7 +32,6 @@
     ObservableUpDownCounter as APIObservableUpDownCounter,
 )
 from opentelemetry._metrics.instrument import UpDownCounter as APIUpDownCounter
-from opentelemetry.sdk._metrics.export import MetricExporter
 from opentelemetry.sdk._metrics.instrument import (
     Counter,
     Histogram,
@@ -100,7 +99,6 @@ class MeterProvider(APIMeterProvider):
 
     def __init__(
         self,
-        metric_exporters: Sequence[MetricExporter] = (),
         metric_readers: Sequence[MetricReader] = (),
         resource: Resource = Resource.create({}),
         shutdown_on_exit: bool = True,
@@ -116,8 +114,6 @@ def __init__(
         for metric_reader in self._metric_readers:
             metric_reader._register_meter_provider(self)
 
-        self._metric_exporters = metric_exporters
-
         self._resource = resource
         self._shutdown = False
 
@@ -126,7 +122,6 @@ def force_flush(self) -> bool:
         # FIXME implement a timeout
 
         metric_reader_result = True
-        metric_exporter_result = True
 
         for metric_reader in self._metric_readers:
             metric_reader_result = (
@@ -136,15 +131,7 @@ def force_flush(self) -> bool:
         if not metric_reader_result:
             _logger.warning("Unable to force flush all metric readers")
 
-        for metric_exporter in self._metric_exporters:
-            metric_exporter_result = (
-                metric_exporter_result and metric_exporter.force_flush()
-            )
-
-        if not metric_exporter_result:
-            _logger.warning("Unable to force flush all metric exporters")
-
-        return metric_reader_result and metric_exporter_result
+        return metric_reader_result
 
     def shutdown(self):
         # FIXME implement a timeout
@@ -161,12 +148,6 @@ def shutdown(self):
             if not result:
                 _logger.warning("A MetricReader failed to shutdown")
 
-        for metric_exporter in self._metric_exporters:
-            result = result and metric_exporter.shutdown()
-
-            if not result:
-                _logger.warning("A MetricExporter failed to shutdown")
-
         self._shutdown = True
 
         if self._atexit_handler is not None:


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

Issue:
Metric Reader function not being called
As reported by @aabmass :v:

There is a bug [here](https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py#L204). If a metric reader returns `False` in a shutdown, the next metric reader shutdown function won't be called.


Discussion:

Resolving patch:
diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py
--- a/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py
+++ b/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py
@@ -198,13 +198,17 @@ def shutdown(self):
             _logger.warning("shutdown can only be called once")
             return False
 
-        result = True
+        overall_result = True
 
         for metric_reader in self._sdk_config.metric_readers:
-            result = result and metric_reader.shutdown()
+            metric_reader_result = metric_reader.shutdown()
 
-            if not result:
-                _logger.warning("A MetricReader failed to shutdown")
+            if not metric_reader_result:
+                _logger.warning(
+                    "MetricReader {metric_reader} failed to shutdown"
+                )
+
+            overall_result = overall_result and metric_reader_result
 
         self._shutdown = True
 
@@ -212,7 +216,7 @@ def shutdown(self):
             unregister(self._atexit_handler)
             self._atexit_handler = None
 
-        return result
+        return overall_result
 
     def get_meter(
         self,


Test patch:
diff --git a/opentelemetry-sdk/tests/metrics/test_metrics.py b/opentelemetry-sdk/tests/metrics/test_metrics.py
--- a/opentelemetry-sdk/tests/metrics/test_metrics.py
+++ b/opentelemetry-sdk/tests/metrics/test_metrics.py
@@ -90,6 +90,30 @@ def test_get_meter_duplicate(self):
         self.assertIs(meter1, meter2)
         self.assertIsNot(meter1, meter3)
 
+    def test_shutdown(self):
+
+        mock_metric_reader_0 = Mock(**{"shutdown.return_value": False})
+        mock_metric_reader_1 = Mock(**{"shutdown.return_value": True})
+
+        meter_provider = MeterProvider(
+            metric_readers=[mock_metric_reader_0, mock_metric_reader_1]
+        )
+
+        self.assertFalse(meter_provider.shutdown())
+        mock_metric_reader_0.shutdown.assert_called_once()
+        mock_metric_reader_1.shutdown.assert_called_once()
+
+        mock_metric_reader_0 = Mock(**{"shutdown.return_value": True})
+        mock_metric_reader_1 = Mock(**{"shutdown.return_value": True})
+
+        meter_provider = MeterProvider(
+            metric_readers=[mock_metric_reader_0, mock_metric_reader_1]
+        )
+
+        self.assertTrue(meter_provider.shutdown())
+        mock_metric_reader_0.shutdown.assert_called_once()
+        mock_metric_reader_1.shutdown.assert_called_once()
+
     def test_shutdown_subsequent_calls(self):
         """
         No subsequent attempts to get a `Meter` are allowed after calling
Repository: open-telemetry/opentelemetry-python

Issue:
Did you mean to use f-string here?
Did you mean to use f-string here?

_Originally posted by @lonewolf3739 in https://github.com/open-telemetry/opentelemetry-python/pull/2405#discussion_r792096137_


Discussion:

Resolving patch:
diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py
--- a/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py
+++ b/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/__init__.py
@@ -205,7 +205,7 @@ def shutdown(self):
 
             if not metric_reader_result:
                 _logger.warning(
-                    "MetricReader {metric_reader} failed to shutdown"
+                    "MetricReader %s failed to shutdown", metric_reader
                 )
 
             overall_result = overall_result and metric_reader_result


Test patch:
diff --git a/opentelemetry-sdk/tests/metrics/test_metrics.py b/opentelemetry-sdk/tests/metrics/test_metrics.py
--- a/opentelemetry-sdk/tests/metrics/test_metrics.py
+++ b/opentelemetry-sdk/tests/metrics/test_metrics.py
@@ -15,7 +15,7 @@
 
 from logging import WARNING
 from unittest import TestCase
-from unittest.mock import Mock, patch
+from unittest.mock import MagicMock, Mock, patch
 
 from opentelemetry.sdk._metrics import Meter, MeterProvider
 from opentelemetry.sdk._metrics.instrument import (
@@ -92,14 +92,24 @@ def test_get_meter_duplicate(self):
 
     def test_shutdown(self):
 
-        mock_metric_reader_0 = Mock(**{"shutdown.return_value": False})
+        mock_metric_reader_0 = MagicMock(
+            **{
+                "shutdown.return_value": False,
+                "__str__.return_value": "mock_metric_reader_0",
+            }
+        )
         mock_metric_reader_1 = Mock(**{"shutdown.return_value": True})
 
         meter_provider = MeterProvider(
             metric_readers=[mock_metric_reader_0, mock_metric_reader_1]
         )
 
-        self.assertFalse(meter_provider.shutdown())
+        with self.assertLogs(level=WARNING) as log:
+            self.assertFalse(meter_provider.shutdown())
+            self.assertEqual(
+                log.records[0].getMessage(),
+                "MetricReader mock_metric_reader_0 failed to shutdown",
+            )
         mock_metric_reader_0.shutdown.assert_called_once()
         mock_metric_reader_1.shutdown.assert_called_once()
Repository: open-telemetry/opentelemetry-python

Issue:
OTLP Metric Exporter: Add sum to Histogram
The OTLP Histogram data point supports a sum field, the field in the aggregation is currently private. This should be changed in order for the exporter to have access to it.


Discussion:

Resolving patch:
diff --git a/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/_metric_exporter/__init__.py b/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/_metric_exporter/__init__.py
--- a/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/_metric_exporter/__init__.py
+++ b/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/_metric_exporter/__init__.py
@@ -137,6 +137,7 @@ def _translate_data(
                     time_unix_nano=metric.point.time_unix_nano,
                     start_time_unix_nano=metric.point.start_time_unix_nano,
                     count=sum(metric.point.bucket_counts),
+                    sum=metric.point.sum,
                     bucket_counts=metric.point.bucket_counts,
                     explicit_bounds=metric.point.explicit_bounds,
                 )


Test patch:
diff --git a/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/metrics/test_otlp_metrics_exporter.py b/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/metrics/test_otlp_metrics_exporter.py
--- a/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/metrics/test_otlp_metrics_exporter.py
+++ b/exporter/opentelemetry-exporter-otlp-proto-grpc/tests/metrics/test_otlp_metrics_exporter.py
@@ -589,6 +589,7 @@ def test_translate_histogram(self):
                                                 start_time_unix_nano=1641946016139533244,
                                                 time_unix_nano=1641946016139533244,
                                                 count=5,
+                                                sum=67,
                                                 bucket_counts=[1, 4],
                                                 explicit_bounds=[10.0, 20.0],
                                                 exemplars=[],
Repository: open-telemetry/opentelemetry-python

Issue:
Update docs to reflect intent of get_tracer/get_meter instrumenting_module_name argument
Currently the [documentation](https://opentelemetry-python.readthedocs.io/en/latest/api/trace.html?highlight=get_tracer#opentelemetry.trace.TracerProvider.get_tracer) for `get_tracer` says:

>The name of the instrumenting module (usually just __name__).
> This should not be the name of the module that is instrumented but the name of the module doing the instrumentation. E.g., instead of "requests", use "opentelemetry.instrumentation.requests".

This is confusing as using `__name__` in different files within the same instrumentation module would result in different tracer names. The documentation should recommend using a canonical identifier for the parameter.

This is related to the specification PR here: https://github.com/open-telemetry/opentelemetry-specification/pull/2276


Discussion:

Resolving patch:
diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py
--- a/opentelemetry-api/src/opentelemetry/trace/__init__.py
+++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py
@@ -202,8 +202,11 @@ def get_tracer(
         vs.  a functional tracer).
 
         Args:
-            instrumenting_module_name: The name of the instrumenting module
-                (usually just ``__name__``).
+            instrumenting_module_name: The name of the instrumenting module.
+                ``__name__`` may not be used as this can result in
+                different tracer names if the tracers are in different files.
+                It is better to use a fixed string that can be imported where
+                needed and used consistently as the name of the tracer.
 
                 This should *not* be the name of the module that is
                 instrumented but the name of the module doing the instrumentation.


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

Issue:
Add type annontations
please add type annotations here

_Originally posted by @aabmass in https://github.com/open-telemetry/opentelemetry-python/pull/2400#discussion_r809406486_


Discussion:

Resolving patch:
diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/_view_instrument_match.py b/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/_view_instrument_match.py
--- a/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/_view_instrument_match.py
+++ b/opentelemetry-sdk/src/opentelemetry/sdk/_metrics/_view_instrument_match.py
@@ -15,10 +15,12 @@
 
 from logging import getLogger
 from threading import Lock
-from typing import TYPE_CHECKING, Iterable
+from typing import TYPE_CHECKING, Dict, Iterable
 
 from opentelemetry.sdk._metrics.aggregation import (
+    _Aggregation,
     _convert_aggregation_temporality,
+    _PointVarT,
 )
 from opentelemetry.sdk._metrics.measurement import Measurement
 from opentelemetry.sdk._metrics.point import AggregationTemporality, Metric
@@ -41,8 +43,8 @@ def __init__(
         self._view = view
         self._instrument = instrument
         self._sdk_config = sdk_config
-        self._attributes_aggregation = {}
-        self._attributes_previous_point = {}
+        self._attributes_aggregation: Dict[frozenset, _Aggregation] = {}
+        self._attributes_previous_point: Dict[frozenset, _PointVarT] = {}
         self._lock = Lock()
 
     # pylint: disable=protected-access


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

Issue:
I re-read the specification. I think the requirement is to json encode the non-string attribute values not just the sequence type. For example `_check_value(True)` returns `'True'` which should actually be `'true'`.
I re-read the specification. I think the requirement is to json encode the non-string attribute values not just the sequence type. For example `_check_value(True)` returns `'True'` which should actually be `'true'`.

_Originally posted by @srikanthccv in https://github.com/open-telemetry/opentelemetry-python/pull/2642#discussion_r859218726_


Discussion:

Resolving patch:
diff --git a/exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py b/exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py
--- a/exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py
+++ b/exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py
@@ -211,6 +211,6 @@ def _sanitize(self, key: str) -> str:
     # pylint: disable=no-self-use
     def _check_value(self, value: Union[int, float, str, Sequence]) -> str:
         """Check the label value and return is appropriate representation"""
-        if not isinstance(value, str) and isinstance(value, Sequence):
+        if not isinstance(value, str):
             return dumps(value, default=str)
         return str(value)


Test patch:
diff --git a/exporter/opentelemetry-exporter-prometheus/tests/test_prometheus_exporter.py b/exporter/opentelemetry-exporter-prometheus/tests/test_prometheus_exporter.py
--- a/exporter/opentelemetry-exporter-prometheus/tests/test_prometheus_exporter.py
+++ b/exporter/opentelemetry-exporter-prometheus/tests/test_prometheus_exporter.py
@@ -205,3 +205,6 @@ def test_check_value(self):
         self.assertEqual(collector._check_value([1, 2]), "[1, 2]")
         self.assertEqual(collector._check_value((1, 2)), "[1, 2]")
         self.assertEqual(collector._check_value(["a", 2]), '["a", 2]')
+        self.assertEqual(collector._check_value(True), "true")
+        self.assertEqual(collector._check_value(False), "false")
+        self.assertEqual(collector._check_value(None), "null")