DEV Community

Cover image for How to Fix Cloud Run Jobs Logging
Marcelo Costa
Marcelo Costa

Posted on • Edited on

How to Fix Cloud Run Jobs Logging

Google Cloud Run is a great product and became even better after allowing you to use it to run background Jobs:

Image description

Recently it became even more flexible allowing users to override a bunch execution args:

Image description

But at the time this blog post was written, it lacks a bit on some developer tooling, for instance automatically showing logs in Google Cloud Logging.

If you set up your application and instrument it to run in Google Cloud Run, the logs are automatically set as a gce_instance resource type. Then if you go in Google Cloud Console, and jump into the Logs of a Cloud Run Job, you see nothing... because it expects it to live under a cloud_run_job resource type.

This most likely happens since this feature is kinda new, so I'd imagine this will be fixed, but in meanwhile, here's some Python sample code that fixes this behavior:

import google.cloud.logging
from google.cloud.logging.handlers import CloudLoggingHandler
from google.cloud.logging_v2.handlers import setup_logging
from google.cloud.logging_v2.resource import Resource
from google.cloud.logging_v2.handlers._monitored_resources import retrieve_metadata_server, _REGION_ID, _PROJECT_NAME

client = google.cloud.logging.Client()

cloud_run_job = os.environ.get("CLOUD_RUN_JOB")
if cloud_run_job:
    region = retrieve_metadata_server(_REGION_ID)
    project = retrieve_metadata_server(_PROJECT_NAME)

    # build a manual resource object
    cr_job_resource = Resource(
        type="cloud_run_job",
        labels={
            "job_name": cloud_run_job,
            "location": region.split("/")[-1] if region else "",
            "project_id": project,
        },
    )
    labels = {"run.googleapis.com/execution_name": os.environ.get("CLOUD_RUN_EXECUTION")}
    handler = CloudLoggingHandler(client, resource=cr_job_resource, labels=labels)
    setup_logging(handler)
Enter fullscreen mode Exit fullscreen mode

Hope it helps!

Top comments (0)