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 :
@@ -925,6 +929,7 @@ def _create_aws_otlp_exporter(endpoint: str, service: str, region: str):
925929 return None
926930
927931
932+ # pylint: disable=too-many-return-statements,too-many-branches
928933def _parse_config_string (config : str ) -> Optional [_AWSXRayAdaptiveSamplingConfig ]:
929934 if config is None :
930935 return None
@@ -935,54 +940,71 @@ def _parse_config_string(config: str) -> Optional[_AWSXRayAdaptiveSamplingConfig
935940 try :
936941 config = path .read_text (encoding = "utf-8" )
937942 except IOError as err :
938- raise ValueError (f"Failed to read adaptive sampling configuration file: { err } " ) from err
943+ _logger .warning ("Failed to read adaptive sampling configuration file: %s" , err )
944+ return None
939945 elif config .endswith (".yml" ) or config .endswith (".yaml" ):
940- raise ValueError ("Adaptive sampling configuration file must be a YAML file" )
946+ _logger .warning ("Adaptive sampling configuration file must be a YAML file" )
947+ return None
941948 else :
942949 _logger .debug ("Adaptive sampling configuration is not a file path, assuming it's a YAML string" )
943950
944951 # Parse YAML config
945- config_map = yaml .safe_load (config )
952+ config_map = None
953+ try :
954+ config_map = yaml .safe_load (config )
955+ # pylint: disable=broad-exception-caught
956+ except Exception as exception :
957+ _logger .warning ("Adaptive sampling configuration must be a valid YAML mapping: %s" , exception )
946958 if not isinstance (config_map , dict ):
947- raise ValueError ("Adaptive sampling configuration must be a valid YAML mapping" )
959+ _logger .warning ("Adaptive sampling configuration must be a valid YAML mapping" )
960+ return None
948961
949962 # Ensure only relevant data is in the YAML configuration
950963 for key in config_map :
951964 if key not in ["version" , "anomalyConditions" , "anomalyCaptureLimit" ]:
952- raise ValueError (f"Invalid key in adaptive sampling configuration: { key } " )
965+ _logger .warning ("Invalid key in adaptive sampling configuration: %s" , key )
966+ return None
953967
954968 version_obj = config_map .get ("version" )
955969 if version_obj is None :
956- raise ValueError ("Missing required 'version' field in adaptive sampling configuration" )
970+ _logger .warning ("Missing required 'version' field in adaptive sampling configuration" )
971+ return None
957972
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- )
973+ try :
974+ config_version = float (version_obj )
975+ if config_version < 1.0 or config_version >= 2.0 :
976+ _logger .warning (
977+ "Incompatible adaptive sampling config version: %s. "
978+ "This version of the AWS X-Ray remote sampler only supports version 1.X." ,
979+ config_version ,
980+ )
981+ return None
964982
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 ,
983+ # Parse anomaly conditions
984+ anomaly_conditions = None
985+ if "anomalyConditions" in config_map :
986+ anomaly_conditions = [
987+ _AnomalyConditions (
988+ error_code_regex = cond .get ("errorCodeRegex" ),
989+ operations = cond .get ("operations" ),
990+ high_latency_ms = cond .get ("highLatencyMs" ),
991+ usage = _UsageType (cond ["usage" ]) if "usage" in cond else None ,
992+ )
993+ for cond in config_map ["anomalyConditions" ]
994+ ]
995+
996+ # Parse anomaly capture limit
997+ anomaly_capture_limit = None
998+ if "anomalyCaptureLimit" in config_map :
999+ anomaly_capture_limit_data = config_map ["anomalyCaptureLimit" ]
1000+ anomaly_capture_limit = _AnomalyCaptureLimit (
1001+ anomaly_traces_per_second = anomaly_capture_limit_data ["anomalyTracesPerSecond" ]
9741002 )
975- for cond in config_map ["anomalyConditions" ]
976- ]
9771003
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" ]
1004+ return _AWSXRayAdaptiveSamplingConfig (
1005+ version = config_version , anomaly_conditions = anomaly_conditions , anomaly_capture_limit = anomaly_capture_limit
9841006 )
1007+ except ValueError as err :
1008+ _logger .warning ("Failed to load AWS X-Ray adaptive sampling configuration: %s" , err )
9851009
986- return _AWSXRayAdaptiveSamplingConfig (
987- version = config_version , anomaly_conditions = anomaly_conditions , anomaly_capture_limit = anomaly_capture_limit
988- )
1010+ return None
0 commit comments