Search code examples
pythongoogle-cloud-platformstackdrivergoogle-cloud-stackdriver

Stackdriver python log RPC error


I am having a RPC issue with logging an error to GCP StackDriver. Following is the error message:

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

Here is the python code for logging:

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)

I run this code in my local computer connecting to my GCP account.

google-auth 1.2.0; google-cloud-logging 1.4.0

Solution

  • I have tried your code (slightly modified to adapt it to my project, and with a last line added in order to output logs to the Stackdriver Logging console) and it is working for me.

    Here I share the code I have used:

    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.')
    

    And after running the command python test.py in my local machine, this is the result I obtain:

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

    Then, if I move to the console link I provided before and filter using the Global resource type (resource.type="global"), this is what I see:

    enter image description here

    As stated by @A.Queue, I am also running google-auth version 1.3.0 and google-cloud-logging 1.4.0, so you should try upgrading google-auth, using the command:

    pip install --upgrade google-auth
    

    Try that and come back to us if the same error keeps coming up.