From 8b68e4dbcb28601bb4587daba3ebb7542305adbf Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Wed, 22 May 2024 10:51:24 +0200 Subject: [PATCH 1/2] Update pylint broad-except names The warning has been splitted into broad-exception-caught and broad-exception-raised. --- .../exporter/otlp/proto/common/_internal/__init__.py | 3 ++- .../opentelemetry-exporter-zipkin/tests/test_zipkin.py | 2 +- .../src/opentelemetry/baggage/propagation/__init__.py | 2 +- opentelemetry-api/src/opentelemetry/context/__init__.py | 4 ++-- opentelemetry-api/src/opentelemetry/propagate/__init__.py | 2 +- opentelemetry-api/src/opentelemetry/trace/__init__.py | 2 +- opentelemetry-api/src/opentelemetry/util/_providers.py | 2 +- opentelemetry-api/tests/metrics/test_observation.py | 2 +- .../src/opentelemetry/sdk/_configuration/__init__.py | 2 +- .../opentelemetry/sdk/_logs/_internal/export/__init__.py | 4 ++-- .../src/opentelemetry/sdk/error_handler/__init__.py | 2 +- .../src/opentelemetry/sdk/metrics/_internal/__init__.py | 8 ++++++-- .../opentelemetry/sdk/metrics/_internal/aggregation.py | 4 ++++ .../_internal/exponential_histogram/mapping/__init__.py | 2 ++ .../sdk/metrics/_internal/export/__init__.py | 5 +++-- .../src/opentelemetry/sdk/metrics/_internal/instrument.py | 6 +++++- .../src/opentelemetry/sdk/metrics/_internal/view.py | 3 ++- .../src/opentelemetry/sdk/resources/__init__.py | 2 +- .../src/opentelemetry/sdk/trace/export/__init__.py | 4 ++-- .../tests/error_handler/test_error_handler.py | 2 +- opentelemetry-sdk/tests/trace/test_trace.py | 4 ++-- shim/opentelemetry-opentracing-shim/tests/test_shim.py | 1 + tests/opentelemetry-test-utils/tests/test_utils.py | 6 +++--- 23 files changed, 46 insertions(+), 28 deletions(-) diff --git a/exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/__init__.py b/exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/__init__.py index 6593d89fd87..ba833c95bf1 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/__init__.py +++ b/exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/__init__.py @@ -106,9 +106,10 @@ def _encode_attributes( if attributes: pb2_attributes = [] for key, value in attributes.items(): + # pylint: disable=broad-exception-caught try: pb2_attributes.append(_encode_key_value(key, value)) - except Exception as error: # pylint: disable=broad-except + except Exception as error: _logger.exception(error) else: pb2_attributes = None diff --git a/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin.py b/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin.py index fa9c3ecf48f..d8231af21bb 100644 --- a/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin.py +++ b/exporter/opentelemetry-exporter-zipkin/tests/test_zipkin.py @@ -23,5 +23,5 @@ def test_constructors(self): try: json.ZipkinExporter() http.ZipkinExporter() - except Exception as exc: # pylint: disable=broad-except + except Exception as exc: # pylint: disable=broad-exception-caught self.assertIsNone(exc) diff --git a/opentelemetry-api/src/opentelemetry/baggage/propagation/__init__.py b/opentelemetry-api/src/opentelemetry/baggage/propagation/__init__.py index 91898d53ae3..49fb378eabd 100644 --- a/opentelemetry-api/src/opentelemetry/baggage/propagation/__init__.py +++ b/opentelemetry-api/src/opentelemetry/baggage/propagation/__init__.py @@ -83,7 +83,7 @@ def extract( continue try: name, value = entry.split("=", 1) - except Exception: # pylint: disable=broad-except + except Exception: # pylint: disable=broad-exception-caught _logger.warning( "Baggage list-member `%s` doesn't match the format", entry ) diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 0a2785ab607..a932c644a0e 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -50,7 +50,7 @@ def _load_runtime_context() -> _RuntimeContext: ) ) ).load()() - except Exception: # pylint: disable=broad-except + except Exception: # pylint: disable=broad-exception-caught logger.exception( "Failed to load context: %s, fallback to %s", configured_context, @@ -152,7 +152,7 @@ def detach(token: object) -> None: """ try: _RUNTIME_CONTEXT.detach(token) - except Exception: # pylint: disable=broad-except + except Exception: # pylint: disable=broad-exception-caught logger.exception("Failed to detach context") diff --git a/opentelemetry-api/src/opentelemetry/propagate/__init__.py b/opentelemetry-api/src/opentelemetry/propagate/__init__.py index a7006175fb8..622b8fff6fd 100644 --- a/opentelemetry-api/src/opentelemetry/propagate/__init__.py +++ b/opentelemetry-api/src/opentelemetry/propagate/__init__.py @@ -149,7 +149,7 @@ def inject( raise ValueError( f"Propagator {propagator} not found. It is either misspelled or not installed." ) - except Exception: # pylint: disable=broad-except + except Exception: # pylint: disable=broad-exception-caught logger.exception("Failed to load propagator: %s", propagator) raise diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index 3b6295e259d..28300f408c3 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -571,7 +571,7 @@ def use_span( finally: context_api.detach(token) - except Exception as exc: # pylint: disable=broad-except + except Exception as exc: # pylint: disable=broad-exception-caught if isinstance(span, Span) and span.is_recording(): # Record the exception as an event if record_exception: diff --git a/opentelemetry-api/src/opentelemetry/util/_providers.py b/opentelemetry-api/src/opentelemetry/util/_providers.py index 307650bb1d2..0a1f70f7fa7 100644 --- a/opentelemetry-api/src/opentelemetry/util/_providers.py +++ b/opentelemetry-api/src/opentelemetry/util/_providers.py @@ -49,6 +49,6 @@ def _load_provider( ) ).load()(), ) - except Exception: # pylint: disable=broad-except + except Exception: # pylint: disable=broad-exception-caught logger.exception("Failed to load configured provider %s", provider) raise diff --git a/opentelemetry-api/tests/metrics/test_observation.py b/opentelemetry-api/tests/metrics/test_observation.py index 0881f043b7b..a1a863fcd61 100644 --- a/opentelemetry-api/tests/metrics/test_observation.py +++ b/opentelemetry-api/tests/metrics/test_observation.py @@ -25,7 +25,7 @@ def test_measurement_init(self): # float Observation(321.321, {"hello": "world"}) - except Exception: # pylint: disable=broad-except + except Exception: # pylint: disable=broad-exception-caught self.fail( "Unexpected exception raised when instantiating Observation" ) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py index 14c560c41b0..ff7b04df4a4 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py @@ -334,7 +334,7 @@ def _import_sampler(sampler_name: str) -> Optional[Sampler]: _logger.warning(message) raise ValueError(message) return sampler - except Exception as exc: # pylint: disable=broad-except + except Exception as exc: # pylint: disable=broad-exception-caught _logger.warning( "Using default sampler. Failed to initialize sampler, %s: %s", sampler_name, diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/export/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/export/__init__.py index 597c55a6725..40b4969b9bd 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/export/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/export/__init__.py @@ -127,7 +127,7 @@ def emit(self, log_data: LogData): token = attach(set_value(_SUPPRESS_INSTRUMENTATION_KEY, True)) try: self._exporter.export((log_data,)) - except Exception: # pylint: disable=broad-except + except Exception: # pylint: disable=broad-exception-caught _logger.exception("Exception while exporting logs.") detach(token) @@ -309,7 +309,7 @@ def _export_batch(self) -> int: token = attach(set_value(_SUPPRESS_INSTRUMENTATION_KEY, True)) try: self._exporter.export(self._log_records[:idx]) # type: ignore - except Exception: # pylint: disable=broad-except + except Exception: # pylint: disable=broad-exception-caught _logger.exception("Exception while exporting logs.") detach(token) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py index 7b21d92d2af..728c773fa48 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/error_handler/__init__.py @@ -133,7 +133,7 @@ def __exit__(self, exc_type, exc_value, traceback): error_handler_class()._handle(exc_value) plugin_handled = True - # pylint: disable=broad-except + # pylint: disable=broad-exception-caught except Exception as error_handling_error: logger.exception( diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py index 908d8f81cf8..1e3a4528e37 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/__init__.py @@ -410,6 +410,7 @@ def __init__( with self._all_metric_readers_lock: if metric_reader in self._all_metric_readers: + # pylint: disable=broad-exception-raised raise Exception( f"MetricReader {metric_reader} has been registered " "already in other MeterProvider instance" @@ -437,7 +438,7 @@ def force_flush(self, timeout_millis: float = 10_000) -> bool: timeout_millis=(deadline_ns - current_ts) / 10**6 ) - # pylint: disable=broad-except + # pylint: disable=broad-exception-caught except Exception as error: metric_reader_error[metric_reader] = error @@ -451,6 +452,7 @@ def force_flush(self, timeout_millis: float = 10_000) -> bool: ] ) + # pylint: disable=broad-exception-raised raise Exception( "MeterProvider.force_flush failed because the following " "metric readers failed during collect:\n" @@ -476,6 +478,7 @@ def _shutdown(): current_ts = time_ns() try: if current_ts >= deadline_ns: + # pylint: disable=broad-exception-raised raise Exception( "Didn't get to execute, deadline already exceeded" ) @@ -483,7 +486,7 @@ def _shutdown(): timeout_millis=(deadline_ns - current_ts) / 10**6 ) - # pylint: disable=broad-except + # pylint: disable=broad-exception-caught except Exception as error: metric_reader_error[metric_reader] = error @@ -501,6 +504,7 @@ def _shutdown(): ] ) + # pylint: disable=broad-exception-raised raise Exception( ( "MeterProvider.shutdown failed because the following " diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py index 3f93c91fa24..9bec7de3e7b 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/aggregation.py @@ -988,6 +988,7 @@ def _downscale(change: int, positive, negative): return if change < 0: + # pylint: disable=broad-exception-raised raise Exception("Invalid change of scale") positive.downscale(change) @@ -1025,6 +1026,7 @@ def _merge( span = previous_buckets.index_end - index if span >= self._max_size: + # pylint: disable=broad-exception-raised raise Exception("Incorrect merge scale") if span >= len(previous_buckets.counts): @@ -1036,6 +1038,7 @@ def _merge( span = index - previous_buckets.index_start if span >= self._max_size: + # pylint: disable=broad-exception-raised raise Exception("Incorrect merge scale") if span >= len(previous_buckets.counts): @@ -1152,6 +1155,7 @@ def _create_aggregation( if isinstance(instrument, _Gauge): return _LastValueAggregation(attributes) + # pylint: disable=broad-exception-raised raise Exception(f"Invalid instrument type {type(instrument)} found") diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exponential_histogram/mapping/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exponential_histogram/mapping/__init__.py index d8c780cf404..5c9bd2d2a9a 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exponential_histogram/mapping/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exponential_histogram/mapping/__init__.py @@ -40,9 +40,11 @@ def _init(self, scale: int) -> None: # pylint: disable=attribute-defined-outside-init if scale > self._get_max_scale(): + # pylint: disable=broad-exception-raised raise Exception(f"scale is larger than {self._max_scale}") if scale < self._get_min_scale(): + # pylint: disable=broad-exception-raised raise Exception(f"scale is smaller than {self._min_scale}") # The size of the exponential histogram buckets is determined by a diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/export/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/export/__init__.py index 02dc2cf287a..53268fc749b 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/export/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/export/__init__.py @@ -174,7 +174,7 @@ def force_flush(self, timeout_millis: float = 10_000) -> bool: class MetricReader(ABC): - # pylint: disable=too-many-branches + # pylint: disable=too-many-branches,broad-exception-raised """ Base class for all metric readers @@ -529,12 +529,13 @@ def _receive_metrics( ) -> None: token = attach(set_value(_SUPPRESS_INSTRUMENTATION_KEY, True)) + # pylint: disable=broad-exception-caught,invalid-name try: with self._export_lock: self._exporter.export( metrics_data, timeout_millis=timeout_millis ) - except Exception as e: # pylint: disable=broad-except,invalid-name + except Exception as e: _logger.exception("Exception while exporting metrics %s", str(e)) detach(token) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/instrument.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/instrument.py index 11dd8499341..2b02e67fc3d 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/instrument.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/instrument.py @@ -54,9 +54,11 @@ def __init__( result = self._check_name_unit_description(name, unit, description) if result["name"] is None: + # pylint: disable=broad-exception-raised raise Exception(_ERROR_MESSAGE.format(name)) if result["unit"] is None: + # pylint: disable=broad-exception-raised raise Exception(_ERROR_MESSAGE.format(unit)) name = result["name"] @@ -85,9 +87,11 @@ def __init__( result = self._check_name_unit_description(name, unit, description) if result["name"] is None: + # pylint: disable=broad-exception-raised raise Exception(_ERROR_MESSAGE.format(name)) if result["unit"] is None: + # pylint: disable=broad-exception-raised raise Exception(_ERROR_MESSAGE.format(unit)) name = result["name"] @@ -136,7 +140,7 @@ def callback( instrument=self, attributes=api_measurement.attributes, ) - except Exception: # pylint: disable=broad-except + except Exception: # pylint: disable=broad-exception-caught _logger.exception( "Callback failed for instrument %s.", self.name ) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/view.py b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/view.py index 5b548a5e05c..9473acde4d4 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/view.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/view.py @@ -103,6 +103,7 @@ def __init__( is meter_schema_url is None ): + # pylint: disable=broad-exception-raised raise Exception( "Some instrument selection " f"criteria must be provided for View {name}" @@ -113,7 +114,7 @@ def __init__( and instrument_name is not None and ("*" in instrument_name or "?" in instrument_name) ): - + # pylint: disable=broad-exception-raised raise Exception( f"View {name} declared with wildcard " "characters in instrument_name" diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py index 20dd56862a0..0cdd70d97c2 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py @@ -400,7 +400,7 @@ def get_aggregated_resources( detector, timeout, ) - # pylint: disable=broad-except + # pylint: disable=broad-exception-caught except Exception as ex: if detector.raise_on_error: raise ex diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py index a4a9958343e..98f82f08b74 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py @@ -111,7 +111,7 @@ def on_end(self, span: ReadableSpan) -> None: token = attach(set_value(_SUPPRESS_INSTRUMENTATION_KEY, True)) try: self.span_exporter.export((span,)) - # pylint: disable=broad-except + # pylint: disable=broad-exception-caught except Exception: logger.exception("Exception while exporting Span.") detach(token) @@ -365,7 +365,7 @@ def _export_batch(self) -> int: # Ignore type b/c the Optional[None]+slicing is too "clever" # for mypy self.span_exporter.export(self.spans_list[:idx]) # type: ignore - except Exception: # pylint: disable=broad-except + except Exception: # pylint: disable=broad-exception-caught logger.exception("Exception while exporting Span batch.") detach(token) diff --git a/opentelemetry-sdk/tests/error_handler/test_error_handler.py b/opentelemetry-sdk/tests/error_handler/test_error_handler.py index 116771dc9a1..9dc1b67256c 100644 --- a/opentelemetry-sdk/tests/error_handler/test_error_handler.py +++ b/opentelemetry-sdk/tests/error_handler/test_error_handler.py @@ -11,7 +11,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# pylint: disable=broad-except from logging import ERROR from unittest import TestCase @@ -30,6 +29,7 @@ def test_default_error_handler(self, mock_entry_points): with self.assertLogs(logger, ERROR): with GlobalErrorHandler(): + # pylint: disable=broad-exception-raised raise Exception("some exception") # pylint: disable=no-self-use diff --git a/opentelemetry-sdk/tests/trace/test_trace.py b/opentelemetry-sdk/tests/trace/test_trace.py index 72c7095da70..f0334194cea 100644 --- a/opentelemetry-sdk/tests/trace/test_trace.py +++ b/opentelemetry-sdk/tests/trace/test_trace.py @@ -1969,7 +1969,7 @@ def test_parent_child_span_exception(self): ) as child_span: raise exception - except Exception: # pylint: disable=broad-except + except Exception: # pylint: disable=broad-exception-caught pass self.assertTrue(child_span.status.is_ok) @@ -2014,7 +2014,7 @@ def test_child_parent_span_exception(self): pass raise exception - except Exception: # pylint: disable=broad-except + except Exception: # pylint: disable=broad-exception-caught pass self.assertTrue(child_span.status.is_ok) diff --git a/shim/opentelemetry-opentracing-shim/tests/test_shim.py b/shim/opentelemetry-opentracing-shim/tests/test_shim.py index b75915d20fe..ac5fbeea4e6 100644 --- a/shim/opentelemetry-opentracing-shim/tests/test_shim.py +++ b/shim/opentelemetry-opentracing-shim/tests/test_shim.py @@ -496,6 +496,7 @@ def test_span_on_error(self): # Raise an exception while a span is active. with self.assertRaises(Exception) as exc_ctx: with self.shim.start_active_span("TestName") as scope: + # pylint: disable=broad-exception-raised raise Exception("bad thing") ex = exc_ctx.exception diff --git a/tests/opentelemetry-test-utils/tests/test_utils.py b/tests/opentelemetry-test-utils/tests/test_utils.py index ce97951f86d..cd0aa22f31e 100644 --- a/tests/opentelemetry-test-utils/tests/test_utils.py +++ b/tests/opentelemetry-test-utils/tests/test_utils.py @@ -23,7 +23,7 @@ def test_no_exception(self): with self.assertNotRaises(Exception): pass - except Exception as error: # pylint: disable=broad-except + except Exception as error: # pylint: disable=broad-exception-caught self.fail( # pylint: disable=no-member f"Unexpected exception {error} was raised" @@ -36,7 +36,7 @@ def test_no_specified_exception_single(self): with self.assertNotRaises(KeyError): 1 / 0 # pylint: disable=pointless-statement - except Exception as error: # pylint: disable=broad-except + except Exception as error: # pylint: disable=broad-exception-caught self.fail( # pylint: disable=no-member f"Unexpected exception {error} was raised" @@ -49,7 +49,7 @@ def test_no_specified_exception_multiple(self): with self.assertNotRaises(KeyError, IndexError): 1 / 0 # pylint: disable=pointless-statement - except Exception as error: # pylint: disable=broad-except + except Exception as error: # pylint: disable=broad-exception-caught self.fail( # pylint: disable=no-member f"Unexpected exception {error} was raised" From 5ae3444b9b2742b855cbe5a64217787cee327256 Mon Sep 17 00:00:00 2001 From: Riccardo Magliocchetti Date: Wed, 22 May 2024 10:53:14 +0200 Subject: [PATCH 2/2] pylint: re-enable broad-exception checks --- .pylintrc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.pylintrc b/.pylintrc index 4f37a7e10f0..976ef7f4730 100644 --- a/.pylintrc +++ b/.pylintrc @@ -76,7 +76,6 @@ disable=missing-docstring, unused-argument, # temp-pylint-upgrade redefined-builtin, cyclic-import, - broad-exception-raised, # Enable the message, report, category or checker with the given id(s). You can # either give multiple identifier separated by comma (,) or put this option @@ -481,3 +480,8 @@ max-statements=50 # Minimum number of public methods for a class (see R0903). min-public-methods=2 + +[EXCEPTIONS] + +# Exceptions that will emit a warning when being caught. +overgeneral-exceptions=builtins.Exception