使用opencensus-python时路径不存在或无法访问错误

问题描述

当我尝试在我的 dockerized Django 项目中集成 opencensus-python 时遇到以下错误

ERROR: Path /tmp/opencensus-python-<hash-code>/2021-07-26T070705.948749-147e2037.blob@2021-07-26T070828.122386.lock does not exist or is inaccessible. 
ERROR: Path /tmp/opencensus-python-<hash-code>/2021-07-26T085046.693213-b80f5ca1.blob.tmp does not exist or is inaccessible.                                               

我进行了一些搜索,发现 _check_storage_size 中返回了此错误,但这是什么意思,我该如何解决

解决方法

这是 OSError 由于找不到路径。确保您以正确的格式分配路径并且文件/文件夹具有读/写权限。还要确保在导出器配置中增加“'storage_max_size'”的值。

您可以参考这个 Python 代码来检查 test_check_storage_size_error

     def put(self,data,lease_period=0):
            if not self._check_storage_size():
                return None
            blob = LocalFileBlob(os.path.join(
                self.path,'{}-{}.blob'.format(
                    _fmt(_now()),'{:08x}'.format(random.getrandbits(32)),# thread-safe random
                ),))
            return blob.put(data,lease_period=lease_period)
    
    def _check_storage_size(self):
            size = 0
            for dirpath,dirnames,filenames in os.walk(self.path):
                for f in filenames:
                    fp = os.path.join(dirpath,f)
                    # skip if it is symbolic link
                    if not os.path.islink(fp):
                        try:
                            size += os.path.getsize(fp)
                        except OSError:
                            logger.error(
                                "Path %s does not exist or is inaccessible.",fp
                            )
                            continue
                        if size >= self.max_size:
                            logger.warning(
                                "Persistent storage max capacity has been "
                                "reached. Currently at %fKB. Telemetry will be "
                                "lost. Please consider increasing the value of "
                                "'storage_max_size' in exporter config.",format(size/1024)
                            )
                            return False
            return True

def test_check_storage_size_error(self):
            input = (1,2,3)
            with LocalFileStorage(os.path.join(TEST_FOLDER,'asd5'),1) as stor:
                with mock.patch('os.path.getsize',side_effect=throw(OSError)):
                    stor.put(input)
                    with mock.patch('os.path.islink') as os_mock:
                        os_mock.return_value = True
                    self.assertTrue(stor._check_storage_size())

您可以参考opencesus-python-storage.pyopencesus-python-test_storage.py