DUDANF
DUDANF

Reputation: 2990

GCP: Creating Client() object locally

I have this class which is used for logging to StackDriver.

from google.cloud.logging.handlers import CloudLoggingHandler
from google.oauth2 import service_account

class GoogleLogger(CloudLoggingHandler):

    CREDS = google.cloud.logging.Client(
        project=PROJECT, credentials=service_account.Credentials.from_service_account_file("/path/to/creds"))

    def __init__(self, client=CREDS):
        super(GoogleLogger, self).__init__(client)

When run on the google cloud, this works seamlessly. However, when run locally it breaks at CREDS = google.cloud.logging.Client(project=PROJECT, credentials=service_account.Credentials.from_service_account_file("/path/to/creds"))

And my entire code breaks.

Question: Is there any way to skip instantiating this class if not on the cloud? Like a conditional class?

OR

Question: Is there any way to make this work locally? It should work. I give it the creds and the project and according to StackDriver docs if I give it the project + creds, it should work locally as well as in GCP?

When it breaks this is the traceback:

Traceback (most recent call last):
  File "/Users/daudn/Documents/clean_space/tgs_workflow/utils/logger.py", line 27, in <module>
    class GoogleLogger(CloudLoggingHandler):
  File "/Users/daudn/Documents/clean_space/tgs_workflow/utils/logger.py", line 36, in GoogleLogger
    project=PROJECT, credentials=service_account.Credentials.from_service_account_file(local_or_gcp()))
  File "/usr/local/lib/python3.7/site-packages/google/cloud/logging/client.py", line 123, in __init__
    self._connection = Connection(self, client_info=client_info)
  File "/usr/local/lib/python3.7/site-packages/google/cloud/logging/_http.py", line 39, in __init__
    super(Connection, self).__init__(client, client_info)
TypeError: __init__() takes 2 positional arguments but 3 were given

Upvotes: 1

Views: 212

Answers (1)

Micah Carrick
Micah Carrick

Reputation: 10187

Google added the client_info parameter in this change: https://github.com/googleapis/google-cloud-python/pull/7849/files#diff-340196d499e9d0eea25cd457f53bfa42L31

I suspect you are running a newer version of the Google Cloud Python SDK in your GCP environment and and older version on your local environment.

Upvotes: 2

Related Questions