diff --git a/kcidb/misc.py b/kcidb/misc.py index 204f571f..b0fab1c5 100644 --- a/kcidb/misc.py +++ b/kcidb/misc.py @@ -15,7 +15,7 @@ import dateutil import dateutil.relativedelta import dateutil.parser -from google.cloud import secretmanager +import google.cloud.secretmanager import jsonschema import jq import kcidb.io as io @@ -52,6 +52,10 @@ def logging_setup(level): """ assert isinstance(level, int) logging.getLogger().setLevel(level) + # Add timestamps to log messages + for handler in logging.getLogger().handlers: + handler.setFormatter(logging.Formatter( + '%(asctime)s:%(levelname)s:%(name)s:%(message)s')) # TODO Consider separate arguments for controlling the below logging.getLogger("urllib3").setLevel(LOGGING_LEVEL_MAP["NONE"]) logging.getLogger("google").setLevel(LOGGING_LEVEL_MAP["NONE"]) @@ -440,7 +444,7 @@ def get_secret(project_id, secret_id): """ assert isinstance(project_id, str) and project_id assert isinstance(secret_id, str) and secret_id - client = secretmanager.SecretManagerServiceClient() + client = google.cloud.secretmanager.SecretManagerServiceClient() path = client.secret_version_path(project_id, secret_id, "latest") return client.access_secret_version( request={"name": path} diff --git a/main.py b/main.py index fb7c667f..e2eea4e8 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ """Google Cloud Functions for Kernel CI reporting""" import os +import time import atexit import tempfile import base64 @@ -10,14 +11,22 @@ import smtplib from urllib.parse import unquote import functions_framework +import google.cloud.logging import kcidb # Name of the Google Cloud project we're deployed in PROJECT_ID = os.environ["GCP_PROJECT"] +# Setup Google Cloud logging, unless testing +if PROJECT_ID != "TEST_PROJECT": + google.cloud.logging.Client().setup_logging() + # Give logging some time to avoid occasional deployment failures + time.sleep(5) +# Setup KCIDB-specific logging kcidb.misc.logging_setup( kcidb.misc.LOGGING_LEVEL_MAP[os.environ.get("KCIDB_LOG_LEVEL", "NONE")] ) +# Get the module's logger LOGGER = logging.getLogger() # The subscriber object for the submission queue diff --git a/requirements.txt b/requirements.txt index b13dd20a..976e2cc6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,7 @@ google-cloud-pubsub google-cloud-firestore google-cloud-storage google-cloud-secret-manager +google-cloud-logging psycopg2 functions-framework jsonschema[format] diff --git a/setup.py b/setup.py index 20fb38b1..eb678f6b 100644 --- a/setup.py +++ b/setup.py @@ -49,6 +49,7 @@ "functions-framework", "google-cloud-firestore", "google-cloud-secret-manager", + "google-cloud-logging", "psycopg2", "jsonschema[format]", "requests",