Azure App Service中Python应用因OpenTelemetry SDK权限问题无法启动
Azure App Service部署Python应用时Azure Monitor OpenTelemetry SDK权限问题
我在Azure App Service上部署基于Python的Web应用,应用使用从Azure容器注册表拉取的Docker镜像。问题源于Azure Monitor OpenTelemetry SDK:该SDK会在Linux版Web应用的/var/log/applicationinsights/目录创建诊断日志文件,且此路径为硬编码不可配置,相关核心代码如下:
if not exists(_DIAGNOSTIC_LOG_PATH): makedirs(_DIAGNOSTIC_LOG_PATH) f_handler = logging.FileHandler( join( _DIAGNOSTIC_LOG_PATH, _DIAGNOSTIC_LOGGER_FILE_NAME ) )
我使用app_user启动应用,该用户仅能访问容器中应用相关卷,无上述目录的写入权限,导致应用启动失败,抛出如下堆栈信息:
2024-10-25T04:33:34.877284066Z File "/usr/src/app/api/main.py", line 19, in <module> 2024-10-25T04:33:34.877309466Z configure_azure_telemetry(option="basic") 2024-10-25T04:33:34.877361967Z File "/usr/src/app/api/dependencies/instrumentation.py", line 11, in configure_azure_telemetry 2024-10-25T04:33:34.877378367Z configure_basic_azure_monitor() 2024-10-25T04:33:34.877383767Z File "/usr/src/app/doc_generator_api/dependencies/instrumentation.py", line 25, in configure_basic_azure_monitor 2024-10-25T04:33:34.878324584Z configure_azure_monitor( 2024-10-25T04:33:34.879013896Z File "/agents/python/azure/monitor/opentelemetry/_configure.py", line 98, in configure_azure_monitor 2024-10-25T04:33:34.879018996Z _send_attach_warning() 2024-10-25T04:33:34.879022896Z File "/agents/python/azure/monitor/opentelemetry/_configure.py", line 222, in _send_attach_warning 2024-10-25T04:33:34.879026897Z AzureDiagnosticLogging.warning( 2024-10-25T04:33:34.879030297Z File "/agents/python/azure/monitor/opentelemetry/_diagnostics/diagnostic_logging.py", line 87, in warning 2024-10-25T04:33:34.879034197Z AzureDiagnosticLogging._initialize() 2024-10-25T04:33:34.879037997Z File "/agents/python/azure/monitor/opentelemetry/_diagnostics/diagnostic_logging.py", line 68, in _initialize 2024-10-25T04:33:34.879041897Z f_handler = logging.FileHandler( 2024-10-25T04:33:34.879883312Z ^^^^^^^^^^^^^^^^^^^^ 2024-10-25T04:33:34.879896112Z File "/opt/conda/lib/python3.11/logging/__init__.py", line 1181, in __init__ 2024-10-25T04:33:34.879905312Z StreamHandler.__init__(self, self._open()) 2024-10-25T04:33:34.879910112Z ^^^^^^^^^^^^ 2024-10-25T04:33:34.879914512Z File "/opt/conda/lib/python3.11/logging/__init__.py", line 1213, in _open 2024-10-25T04:33:34.879919112Z return open_func(self.baseFilename, self.mode, 2024-10-25T04:33:34.879923612Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2024-10-25T04:33:34.879928513Z PermissionError: [Errno 13] Permission denied: '/var/log/applicationinsights/applicationinsights-extension.log'
我已尝试在Dockerfile中修改该目录权限:
RUN mkdir -p /var/log/applicationinsights # Set the permissions to allow writing RUN chown -R ${APP_UID}:${APP_GID} /var/log/applicationinsights RUN chmod -R 755 /var/log/applicationinsights
但部署至Azure App Service后,/var/log/applicationinsights/目录仍归root所有,问题未解决。
内容的提问来源于stack exchange,提问作者Sumit Kumar Mahto
相关产品推荐
相关产品推荐

