Stackdriver python 记录 RPC 错误

Stackdriver python log RPC error

我在向 GCP StackDriver 记录错误时遇到 RPC 问题。以下是错误信息:

grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.DEADLINE_EXCEEDED, Deadline Exceeded)>

这是用于记录的 python 代码:

import logging
import logging.handlers
import os
import config
import google.cloud.logging as gcp_logging
from google.oauth2 import service_account

logger = logging.getLogger('my_logger')
## using Google Stackdriver logging
#client = gcp_logging.Client(project=config.project, credentials=config.credentials_gcp_ml)
#client = gcp_logging.Client.from_service_account_json('./cred.json')
cred = service_account.Credentials.from_service_account_file('./cred.json')
client = gcp_logging.Client(project = config.project, credentials=cred)
hdlr = client.get_default_handler()
logger = logging.getLogger('cloudLogger')

formatter = logging.Formatter('%(asctime)s  %(levelname)s   %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)

我 运行 在连接到我的 GCP 帐户的本地计算机中使用此代码。

google-auth 1.2.0; google-cloud-logging 1.4.0

我已经尝试了您的代码(稍微修改以适应我的项目,并添加了最后一行以便将日志输出到 Stackdriver Logging console)并且它对我有用。

这里分享一下我用过的代码:

import logging
import logging.handlers
import os
import google.cloud.logging as gcp_logging
from google.oauth2 import service_account

logger = logging.getLogger('my_logger')
cred = service_account.Credentials.from_service_account_file('./private-key.json')
client = gcp_logging.Client(project = "<YOUR_PROJECT_ID>", credentials=cred)
hdlr = client.get_default_handler()
logger = logging.getLogger('cloudLogger')

formatter = logging.Formatter('%(asctime)s  %(levelname)s   %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
logger.info('This is a Logging Test.')

在我的本地计算机中 运行 命令 python test.py 之后,这是我获得的结果:

Program shutting down, attempting to send 1 queued log entries to Stackdriver Logging...
Waiting up to 5 seconds.
Sent all pending logs.

然后,如果我转到我之前提供的控制台 link 并使用全局资源类型 (resource.type="global") 进行过滤,这就是我所看到的:

正如@A.Queue所述,我也是运行google-auth版本1.3.0google-cloud-logging1.4.0,所以你应该试试升级 google-auth,使用命令:

pip install --upgrade google-auth

尝试一下,如果同样的错误不断出现,请返回给我们。