Tsvi Sabo
Tsvi Sabo

Reputation: 693

Writing log to gcloud Vertex AI Endpoint using gcloud client fails with google.api_core.exceptions.MethodNotImplemented: 501

Trying to use google logging client library for writing logs into gcloud, specifically, i'm interested in writing logs that will be attached to a managed resource, in this case, a Vertex AI endpoint:

Code sample:

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")

And i am getting:

google.api_core.exceptions.MethodNotImplemented: 501 The GRPC target is not implemented on the server, host: us-east1-aiplatform.googleapis.com, method: /google.logging.v2.LoggingServiceV2/WriteLogEntries. Sent all pending logs.

I thought we need to enable api and was told it's enabled, and that we have: https://www.googleapis.com/auth/logging.write what could be causing the error?

Upvotes: 0

Views: 428

Answers (1)

Kabilan Mohanraj
Kabilan Mohanraj

Reputation: 1916

As mentioned by @DazWilkin in the comment, the error is because the API endpoint us-east1-aiplatform.googleapis.com does not have a method called WriteLogEntries.

The above endpoint is used to send requests to Vertex AI services and not to Cloud Logging. The API endpoint to be used is the logging.googleapis.com as shown in the entries.write method. Refer to this documentation for more info.

The ClientOptions() function should have logging.googleapis.com as the api_endpoint parameter. If the client_options parameter is not specified, logging.googleapis.com is used by default.

After changing the api_endpoint parameter, I was able to successfully write the log entries. The ClientOptions() is as follows:

client = logging_v2.client.Client(
        credentials=credentials,
        client_options=ClientOptions(api_endpoint="logging.googleapis.com"),
    )

Upvotes: 1

Related Questions