11# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22# SPDX-License-Identifier: Apache-2.0
33# Modifications Copyright The OpenTelemetry Authors. Licensed under the Apache License 2.0 License.
4+
5+ # pylint: disable=too-many-lines
6+
47import logging
58import os
69import re
@@ -413,7 +416,8 @@ def _customize_sampler(sampler: Sampler) -> Sampler:
413416
414417 try :
415418 parsed_config = _parse_config_string (config )
416- except ValueError as error :
419+ # pylint: disable=broad-exception-caught
420+ except Exception as error :
417421 _logger .warning ("Failed to parse adaptive sampling configuration: %s" , str (error ))
418422
419423 if parsed_config is not None :
@@ -924,7 +928,7 @@ def _create_aws_otlp_exporter(endpoint: str, service: str, region: str):
924928 _logger .error ("Failed to create AWS OTLP exporter: %s" , errors )
925929 return None
926930
927-
931+ # pylint: disable=too-many-return-statements,too-many-branches
928932def _parse_config_string (config : str ) -> Optional [_AWSXRayAdaptiveSamplingConfig ]:
929933 if config is None :
930934 return None
@@ -935,54 +939,71 @@ def _parse_config_string(config: str) -> Optional[_AWSXRayAdaptiveSamplingConfig
935939 try :
936940 config = path .read_text (encoding = "utf-8" )
937941 except IOError as err :
938- raise ValueError (f"Failed to read adaptive sampling configuration file: { err } " ) from err
942+ _logger .warning ("Failed to read adaptive sampling configuration file: %s" , err )
943+ return None
939944 elif config .endswith (".yml" ) or config .endswith (".yaml" ):
940- raise ValueError ("Adaptive sampling configuration file must be a YAML file" )
945+ _logger .warning ("Adaptive sampling configuration file must be a YAML file" )
946+ return None
941947 else :
942948 _logger .debug ("Adaptive sampling configuration is not a file path, assuming it's a YAML string" )
943949
944950 # Parse YAML config
945- config_map = yaml .safe_load (config )
951+ config_map = None
952+ try :
953+ config_map = yaml .safe_load (config )
954+ # pylint: disable=broad-exception-caught
955+ except Exception as exception :
956+ _logger .warning ("Adaptive sampling configuration must be a valid YAML mapping: %s" , exception )
946957 if not isinstance (config_map , dict ):
947- raise ValueError ("Adaptive sampling configuration must be a valid YAML mapping" )
958+ _logger .warning ("Adaptive sampling configuration must be a valid YAML mapping" )
959+ return None
948960
949961 # Ensure only relevant data is in the YAML configuration
950962 for key in config_map :
951963 if key not in ["version" , "anomalyConditions" , "anomalyCaptureLimit" ]:
952- raise ValueError (f"Invalid key in adaptive sampling configuration: { key } " )
964+ _logger .warning ("Invalid key in adaptive sampling configuration: %s" , key )
965+ return None
953966
954967 version_obj = config_map .get ("version" )
955968 if version_obj is None :
956- raise ValueError ("Missing required 'version' field in adaptive sampling configuration" )
969+ _logger .warning ("Missing required 'version' field in adaptive sampling configuration" )
970+ return None
957971
958- config_version = float (version_obj )
959- if config_version < 1.0 or config_version >= 2.0 :
960- raise ValueError (
961- f"Incompatible adaptive sampling config version: { config_version } . "
962- "This version of the AWS X-Ray remote sampler only supports version 1.X."
963- )
972+ try :
973+ config_version = float (version_obj )
974+ if config_version < 1.0 or config_version >= 2.0 :
975+ _logger .warning (
976+ "Incompatible adaptive sampling config version: %s. "
977+ "This version of the AWS X-Ray remote sampler only supports version 1.X." ,
978+ config_version ,
979+ )
980+ return None
964981
965- # Parse anomaly conditions
966- anomaly_conditions = None
967- if "anomalyConditions" in config_map :
968- anomaly_conditions = [
969- _AnomalyConditions (
970- error_code_regex = cond .get ("errorCodeRegex" ),
971- operations = cond .get ("operations" ),
972- high_latency_ms = cond .get ("highLatencyMs" ),
973- usage = _UsageType (cond ["usage" ]) if "usage" in cond else None ,
982+ # Parse anomaly conditions
983+ anomaly_conditions = None
984+ if "anomalyConditions" in config_map :
985+ anomaly_conditions = [
986+ _AnomalyConditions (
987+ error_code_regex = cond .get ("errorCodeRegex" ),
988+ operations = cond .get ("operations" ),
989+ high_latency_ms = cond .get ("highLatencyMs" ),
990+ usage = _UsageType (cond ["usage" ]) if "usage" in cond else None ,
991+ )
992+ for cond in config_map ["anomalyConditions" ]
993+ ]
994+
995+ # Parse anomaly capture limit
996+ anomaly_capture_limit = None
997+ if "anomalyCaptureLimit" in config_map :
998+ anomaly_capture_limit_data = config_map ["anomalyCaptureLimit" ]
999+ anomaly_capture_limit = _AnomalyCaptureLimit (
1000+ anomaly_traces_per_second = anomaly_capture_limit_data ["anomalyTracesPerSecond" ]
9741001 )
975- for cond in config_map ["anomalyConditions" ]
976- ]
9771002
978- # Parse anomaly capture limit
979- anomaly_capture_limit = None
980- if "anomalyCaptureLimit" in config_map :
981- anomaly_capture_limit_data = config_map ["anomalyCaptureLimit" ]
982- anomaly_capture_limit = _AnomalyCaptureLimit (
983- anomaly_traces_per_second = anomaly_capture_limit_data ["anomalyTracesPerSecond" ]
1003+ return _AWSXRayAdaptiveSamplingConfig (
1004+ version = config_version , anomaly_conditions = anomaly_conditions , anomaly_capture_limit = anomaly_capture_limit
9841005 )
1006+ except ValueError as err :
1007+ _logger .warning ("Failed to load AWS X-Ray adaptive sampling configuration: %s" , err )
9851008
986- return _AWSXRayAdaptiveSamplingConfig (
987- version = config_version , anomaly_conditions = anomaly_conditions , anomaly_capture_limit = anomaly_capture_limit
988- )
1009+ return None
0 commit comments