Azure / Azure/azure-sdk-for-python
azure.monitor.opentelemetry logs exception even when the exception is handled in a try/catch
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 3.4k
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 193
Description
- **Package Name**: azure.monitor.opentelemetry
- **Package Version**: 1.4.0
- **Operating System**: Debian Linux (docker image python:3.11-bookworm)
- **Python Version**: 3.11
**Describe the bug**
When using azure opentelemetry, a blob storage operation that errors inside a try/catch is still logged as an Exception
**To Reproduce**
Steps to reproduce the behavior:
Have a method like:
```
def main():
configure_azure_monitor(
logger_name="AzureTestLogger",
connection_string="InstrumentationKey=....",
credential=DefaultAzureCredential(),
)
logger = logging.getLogger("AzureTestLogger")
log_handler = logging.StreamHandler()
logger.addHandler(log_handler)
logger.setLevel("DEBUG")
logger.info(f"logger level is set to: {logging.getLevelName(logger.level)}")
blob_service_client = set_storage()
blob = blob_service_client.get_blob_client("temp", "blob_that_dont_exists.txt")
try:
blob.delete_blob()
except Exception:
print("That was fine, nothing to see here")
```
This logs an entry similar to:
"timestamp [UTC]",message,severityLevel,itemType,customDimensions,customMeasurements,"operation_Name","operation_Id","operation_ParentId","operation_SyntheticSource","session_Id","user_Id","user_AuthenticatedId","user_AccountId","application_Version","client_Type","client_Model","client_OS","client_IP","client_City","client_StateOrProvince","client_CountryOrRegion","client_Browser","cloud_RoleName","cloud_RoleInstance",appId,appName,iKey,sdkVersion,itemId,itemCount,"_ResourceId",problemId,handledAt,type,assembly,method,outerType,outerMessage,outerAssembly,outerMethod,innermostType,innermostMessage,innermostAssembly,innermostMethod,details
"10/7/2024, 2:14:05.173 PM",,,exception,"{""_MS.ResourceAttributeId"":""ce93da00-cb04-49ee-9f08-c54cc90c2c28""}",,,d81f7ad205600d5eaba48f98864ed236,f6decbb27f751b59,,,,,,,PC,Other,"#1 SMP Fri Mar 29 12:21:27 UTC 2024","0.0.0.0",Belper,Derbyshire,"United Kingdom",Other,"unknown_service",e28ae3742d83,"97337a28-7fba-4482-9a7f-847ca9fc4340","/subscriptions/ed5e47ec-141a-422f-abba-b69ef358c6fa/resourcegroups/dataplatform_uk_test_1/providers/microsoft.insights/components/appi-dataeng-test-ukwest-001","a3f4fd25-e33f-42df-a270-4bec07e84ce7","ulm_py3.11.9:otel1.24.0:ext1.0.0b24","73fc59f6-84b6-11ef-ac1d-000d3a86db37",1,"/subscriptions/ed5e47ec-141a-422f-abba-b69ef358c6fa/resourcegroups/dataplatform_uk_test_1/providers/microsoft.insights/components/appi-dataeng-test-ukwest-001",ResourceNotFoundError,,ResourceNotFoundError,Unknown,Unknown,ResourceNotFoundError,"The specified blob does not exist.
RequestId:af47a6b9-501e-004e-12c3-18e526000000
Time:2024-10-07T14:14:05.1863327Z
ErrorCode:BlobNotFound
Content: BlobNotFoundThe specified blob does not exist.
RequestId:af47a6b9-501e-004e-12c3-18e526000000
Time:2024-10-07T14:14:05.1863327Z",,,ResourceNotFoundError,"The specified blob does not exist.
RequestId:af47a6b9-501e-004e-12c3-18e526000000
Time:2024-10-07T14:14:05.1863327Z
ErrorCode:BlobNotFound
Content: BlobNotFoundThe specified blob does not exist.
RequestId:af47a6b9-501e-004e-12c3-18e526000000
Time:2024-10-07T14:14:05.1863327Z",,,"[{""outerId"":""0"",""message"":""The specified blob does not exist.\nRequestId:af47a6b9-501e-004e-12c3-18e526000000\nTime:2024-10-07T14:14:05.1863327Z\nErrorCode:BlobNotFound\nContent: BlobNotFoundThe specified blob does not exist.\nRequestId:af47a6b9-501e-004e-12c3-18e526000000\nTime:2024-10-07T14:14:05.1863327Z"",""type"":""ResourceNotFoundError"",""id"":""0"",""rawStack"":""Traceback (most recent call last):\n File \""/app/datalake_channel_extraction/.venv/lib/python3.11/site-packages/opentelemetry/trace/__init__.py\"", line 570, in use_span\n yield span\n File \""/app/datalake_channel_extraction/.venv/lib/python3.11/site-packages/azure/core/tracing/decorator.py\"", line 89, in wrapper_use_tracer\n return func(*args, **kwargs)\n ^^^^^^^^^^^^^^^^^^^^^\n File \""/app/datalake_channel_extraction/.venv/lib/python3.11/site-packages/azure/storage/blob/_blob_client.py\"", line 1211, in delete_blob\n process_storage_error(error)\n File \""/app/datalake_channel_extraction/.venv/lib/python3.11/site-packages/azure/storage/blob/_shared/response_handlers.py\"", line 184, in process_storage_error\n exec(\""raise error from None\"") # pylint: disable=exec-used # nosec\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \""\"", line 1, in \n File \""/app/datalake_channel_extraction/.venv/lib/python3.11/site-packages/azure/storage/blob/_blob_client.py\"", line 1209, in delete_blob\n self._client.blob.delete(**options)\n File \""/app/datalake_channel_extraction/.venv/lib/python3.11/site-packages/azure/core/tracing/decorator.py\"", line 89, in wrapper_use_tracer\n return func(*args, **kwargs)\n ^^^^^^^^^^^^^^^^^^^^^\n File \""/app/datalake_channel_extraction/.venv/lib/python3.11/site-packages/azure/storage/blob/_generated/operations/_blob_operations.py\"", line 2116, in delete\n map_error(status_code=response.status_code, response=response, error_map=error_map)\n File \""/app/datalake_channel_extraction/.venv/lib/python3.11/site-packages/azure/core/exceptions.py\"", line 164, in map_error\n raise error\nazure.core.exceptions.ResourceNotFoundError: The specified blob does not exist.\nRequestId:af47a6b9-501e-004e-12c3-18e526000000\nTime:2024-10-07T14:14:05.1863327Z\nErrorCode:BlobNotFound\nContent: BlobNotFoundThe specified blob does not exist.\nRequestId:af47a6b9-501e-004e-12c3-18e526000000\nTime:2024-10-07T14:14:05.1863327Z\n""}]"
**Expected behavior**
As the delete_blob have a try/catch and is handling the exception, I would expect it not to be logged in App insights.
As it is, those handled exceptions are triggering our exception alerts.
**Screenshots**
If applicable, add screenshots to help explain your problem.
**Additional context**
Contributor guide
Assessment
This issue has not been assessed yet.