merdogan
merdogan

Reputation: 21

How to modify error logs in google cloud functions?

I am using google cloud functions with Python. I want to format all logs with some additional data, e.g. customer id. I achieved this without any problem using Stackdriver Logging library with CloudLoggingHandler. In the same manner, I also like to add this information to uncaught error logs and tracebacks.

I tried to modify sys.excepthook and sys.stderr but it did not work, probably they are handled exclusively by cloud functions.

Is there any way I can modify uncaught exceptions or modify handled errors, e.g. by using Stackdriver error reporting? Or do you have any alternative solution for this (without catching all exceptions)?

Upvotes: 1

Views: 1147

Answers (2)

Ian Carvalho
Ian Carvalho

Reputation: 11

Although you cannot override sys.excepthook, you might do the following:

To provide a little context, I organize my code structure similar to what is presented here: https://code.luasoftware.com/tutorials/google-cloud-functions/structure-for-google-cloud-functions-development-and-split-multiple-file/

Google stores your function entrypoint function in an env var called X_GOOGLE_ENTRY_POINT. You use Python language capabilities to override this function and wrap it similarly to a "decorator", basically wrapping it around a try/except block and then you can run whatever code you want there. I tried using sys.modules[\__name__] but it didn't work, so I went for locals().

I have the function code defined in test_logging.py

from app.functions.test_logging import *

and after importing I do the following

fn = os.getenv('X_GOOGLE_ENTRY_POINT')
lcl = locals()
def decorate(fn):
    def run(*args, **kwargs):
        try:
            fn(*args, **kwargs)
        except Exception as e:
            '''Do whatever you want to do here'''
    return run

lcl[fn] = decorate(lcl[fn])

It's hackish, but it's working for me and it's particularly useful because I keep my code base inside functions folder and I basically don't need to touch the main.py, making this very flexible. You can ever re-raise the error after handling the exception if you want to allow the GCP to know it has failed and maybe re-run

Upvotes: 1

Kolban
Kolban

Reputation: 15246

Cloud Functions provides the (currently) highest level abstraction for code execution. The philosophy is that your bring the code that implements your desired logic and Cloud Functions provides the highest level environment for execution. This has pluses and minuses.

Furthermore, the biggest plus is that you have the very least to concern yourself with in order to get the execution you desired.

On the other hand, you have very little in the way of operational control (the vision is that Cloud Functions provides the maximum in operational control).

As a consequence, if you want more control over the environment at the cost of you having to do more "work", I suggest you Cloud Run. In Cloud Run, you package your application logic as a Docker container and then ask it to take care of all execution of such logic. In your container, you can do anything you want ... including using technology such as Stackdriver logging and defining a CloudLoggingHandler. Cloud Run then takes care of your scaling and execution environment from there.

To sum up, the answer then becomes "No" you don't have control over error logs in Cloud Functions but you can achieve your desired outcome by leveraging Cloud Run instead.

Upvotes: 2

Related Questions