Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Google Cloud logging #496

Merged
merged 4 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions kcidb/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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}
Expand Down
9 changes: 9 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Google Cloud Functions for Kernel CI reporting"""

import os
import time
import atexit
import tempfile
import base64
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"functions-framework",
"google-cloud-firestore",
"google-cloud-secret-manager",
"google-cloud-logging",
"psycopg2",
"jsonschema[format]",
"requests",
Expand Down