diff --git a/newrelic/hooks/external_botocore.py b/newrelic/hooks/external_botocore.py index d481ce845..12dd4153f 100644 --- a/newrelic/hooks/external_botocore.py +++ b/newrelic/hooks/external_botocore.py @@ -900,25 +900,40 @@ def _wrap_bedrock_runtime_converse(wrapped, instance, args, kwargs): def extract_bedrock_converse_attrs(kwargs, response, response_headers, model, span_id, trace_id): input_message_list = [] - # If a system message is supplied, it is under its own key in kwargs rather than with the other input messages - if "system" in kwargs.keys(): - input_message_list.extend({"role": "system", "content": result["text"]} for result in kwargs.get("system", [])) - - # kwargs["messages"] can hold multiple requests and responses to maintain conversation history - # We grab the last message (the newest request) in the list each time, so we don't duplicate recorded data - _input_messages = kwargs.get("messages", []) - _input_messages = _input_messages and (_input_messages[-1] or {}) - _input_messages = _input_messages.get("content", []) - input_message_list.extend( - [{"role": "user", "content": result["text"]} for result in _input_messages if "text" in result] - ) + try: + # If a system message is supplied, it is under its own key in kwargs rather than with the other input messages + if "system" in kwargs.keys(): + input_message_list.extend( + {"role": "system", "content": result["text"]} for result in kwargs.get("system", []) if "text" in result + ) + + # kwargs["messages"] can hold multiple requests and responses to maintain conversation history + # We grab the last message (the newest request) in the list each time, so we don't duplicate recorded data + _input_messages = kwargs.get("messages", []) + _input_messages = _input_messages and (_input_messages[-1] or {}) + _input_messages = _input_messages.get("content", []) + input_message_list.extend( + [{"role": "user", "content": result["text"]} for result in _input_messages if "text" in result] + ) + except Exception: + _logger.warning( + "Exception occurred in botocore instrumentation for AWS Bedrock: Failed to extract input messages from Converse request. Report this issue to New Relic Support.", + exc_info=True, + ) output_message_list = None - if "output" in response: - output_message_list = [ - {"role": "assistant", "content": result["text"]} - for result in response.get("output").get("message").get("content", []) - ] + try: + if "output" in response: + output_message_list = [ + {"role": "assistant", "content": result["text"]} + for result in response.get("output").get("message").get("content", []) + if "text" in result + ] + except Exception: + _logger.warning( + "Exception occurred in botocore instrumentation for AWS Bedrock: Failed to extract output messages from onverse response. Report this issue to New Relic Support.", + exc_info=True, + ) bedrock_attrs = { "request_id": response_headers.get("x-amzn-requestid"),