tib
tib

Reputation: 23

Google cloud functions missing logs issue

I have a small python CF conencted to a PubSub topic that should send out some emails using the sendgrid API.

The CF can dynamically load & run functions based on a env var (CF_FUNCTION_NAME) provided (monorepo architecture):

# main.py
import logging
import os
from importlib import import_module

def get_function(function_name):
    return getattr(import_module(f"functions.{function_name}"), function_name)

def do_nothing(*args):
    return "no function"

cf_function_name = os.getenv("CF_FUNCTION_NAME", False)
disable_logging = os.getenv("CF_DISABLE_LOGGING", False)

def run(*args):
    if not disable_logging and cf_function_name:
        import google.cloud.logging
        client = google.cloud.logging.Client()
        client.get_default_handler()
        client.setup_logging()
        print("Logging enabled")
    cf = get_function(cf_function_name) if cf_function_name else do_nothing
    return cf(*args)

This works fine, except for some issues related to Stackdriver logging:

Screenshot of the stackdriver logs of multiple subsequent executions: stackdriver screenshot

Is there something I'm missing here? Is my dynamic loading of funcitons somehow messing witht the logging?

Thanks.

Upvotes: 2

Views: 1303

Answers (2)

chengineer
chengineer

Reputation: 61

It seems like there is an issue with Cloud Functions and Python for a month now, where errors do not get logged automatically with tracebacks and categorized correctly as "Error": GCP Cloud Functions no longer categorizes errors correctly with tracebacks

Upvotes: 1

guillaume blaquiere
guillaume blaquiere

Reputation: 75715

I don't see any issue here. When you load your function for the first time, one instance is created and the logging is enabled (your logging trace). Then, the instance stay up until its eviction (unpredictable!).

If you want to see several trace, perform 2 calls in the same time. Cloud Function instance can handle only one request at the same time. 2 calls in parallel imply the creation of another instance and thus, a new logging initialisation.

About the exception, same things. If you don't catch and print it, nothing will be logged. Simply catch them!

Upvotes: 1

Related Questions