Azure/azure-sdk-for-python

Exception while exporting metrics invalid literal for int() with base 10: 'None'

Guiforge opened this issue · 2 comments

  • Package Name: azure-monitor-opentelemetry
  • Package Version:
  • Operating System:
  • Python Version:

Describe the bug
Crash during export metrics


2024-05-15T07:38:51.619185568Z [2024-05-15 07:38:51,618: ERROR/MainProcess] Exception while exporting metrics invalid literal for int() with base 10: 'None'
2024-05-15T07:38:51.619240368Z Traceback (most recent call last):
2024-05-15T07:38:51.619246768Z   File "/app/.venv/lib/python3.12/site-packages/opentelemetry/sdk/metrics/_internal/export/__init__.py", line 534, in _receive_metrics
2024-05-15T07:38:51.619251068Z     self._exporter.export(
2024-05-15T07:38:51.619255468Z   File "/app/.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/exporter/export/metrics/_exporter.py", line 96, in export
2024-05-15T07:38:51.619260069Z     envelope = self._point_to_envelope(
2024-05-15T07:38:51.619264069Z                ^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-15T07:38:51.619267569Z   File "/app/.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/exporter/export/metrics/_exporter.py", line 144, in _point_to_envelope
2024-05-15T07:38:51.619271769Z     envelope = _handle_std_metric_envelope(envelope, name, point.attributes) # type: ignore
2024-05-15T07:38:51.619276269Z                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-15T07:38:51.619280569Z   File "/app/.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/exporter/export/metrics/_exporter.py", line 236, in _handle_std_metric_envelope
2024-05-15T07:38:51.619285469Z     properties["Dependency.Success"] = str(_is_status_code_success(status_code, 400)) # type: ignore
2024-05-15T07:38:51.619289469Z                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-15T07:38:51.619293769Z   File "/app/.venv/lib/python3.12/site-packages/azure/monitor/opentelemetry/exporter/export/metrics/_exporter.py", line 277, in _is_status_code_success
2024-05-15T07:38:51.619298469Z     return status_code is not None and int(status_code) < threshold

As you can see the Exception is here:
https://github.com/Azure/azure-sdk-for-python/blob/ac47fa8cf5e5976e5e7308a0e76ee4adf06a31db/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/metrics/_exporter.py#L276C1-L277C68

def _is_status_code_success(status_code: Optional[str], threshold: int) -> bool:
    return status_code is not None and int(status_code) < threshold

The exception is due to status_code can be 'None'

In urllib the status code is pack in str
https://github.com/open-telemetry/opentelemetry-python-contrib/blob/460fc335836c395db8472ecf464e7ecd94c08925/instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py#L247C1-L248C69

The status code can be None when urllib open File. (In my case weasyprint + file:///style.css)

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @jeremydvoss @lzchen.