0

尝试使用google logging 客户端库将日志写入 gcloud,具体来说,我有兴趣编写将附加到托管资源的日志,在本例中为Vertex AI端点:

代码示例:

import logging
from google.api_core.client_options import ClientOptions
import google.cloud.logging_v2 as logging_v2
from google.oauth2 import service_account


def init_module_logger(module_name: str) -> logging.Logger:

    module_logger = logging.getLogger(module_name)
    module_logger.setLevel(settings.LOG_LEVEL)
    credentials= service_account.Credentials.from_service_account_info(json.loads(SA_KEY_JSON))
    client = logging_v2.client.Client(
        credentials=credentials,
        client_options=ClientOptions(api_endpoint="us-east1-aiplatform.googleapis.com"),
    )
    handler = client.get_default_handler(
        resource=Resource(
            type="aiplatform.googleapis.com/Endpoint",
            labels={"endpoint_id": "ENDPOINT_NUMBER_ID", 
            "location": "us-east1"},
        )
    )
    #Assume we have the formatter
    handler.setFormatter(ENRICHED_FORMATTER)
    module_logger.addHandler(handler)
    return module_logger


logger = init_module_logger(__name__)
logger.info("This Fails with 501")

我得到:

google.api_core.exceptions.MethodNotImplemented: 501 服务器上没有实现GRPC目标,主机:us-east1-aiplatform.googleapis.com,方法:/google.logging.v2.LoggingServiceV2/WriteLogEntries。发送所有待处理的日志。

我认为我们需要启用 api 并被告知它已启用,并且我们有:https://www.googleapis.com/auth/logging.write 可能导致错误的原因是什么?

4

1 回答 1

1

正如@DazWilkin在评论中提到的那样,错误是因为 API 端点us-east1-aiplatform.googleapis.com没有名为WriteLogEntries.

上述端点用于向 Vertex AI 服务发送请求,而不是向 Cloud Logging。要使用的 API 端点logging.googleapis.com如方法中所示entries.write。有关详细信息,请参阅此文档

ClientOptions()函数应logging.googleapis.com作为api_endpoint参数。如果client_options未指定参数,logging.googleapis.com则默认使用。

更改 api_endpoint 参数后,我能够成功写入日志条目。ClientOptions() 如下:

client = logging_v2.client.Client(
        credentials=credentials,
        client_options=ClientOptions(api_endpoint="logging.googleapis.com"),
    )
于 2021-11-22T18:03:54.847 回答