Closed
Description
Environment details
- OS type and version: Cloud Run Jobs
- Python version: 3.10
- pip version: Not relevant (in the container, I don't know the version!)
google-cloud-logging
version: Latest (no version mentioned in the requirements.txt)
Steps to reproduce
- Use this code for a working example in Cloud Run
Main.py
import os
from flask import Flask
app = Flask(__name__)
import google.cloud.logging
# Instantiates a client
client = google.cloud.logging.Client()
# Retrieves a Cloud Logging handler based on the environment
# you're running in and integrates the handler with the
# Python logging module. By default this captures all logs
# at INFO level and higher
client.setup_logging()
# [END logging_handler_setup]
# [START logging_handler_usage]
# Imports Python standard library logging
import logging
@app.route('/')
def call_function():
# The data to log
text = "Hello, world!"
# Emits the data using the standard logging module
logging.warning(text)
# [END logging_handler_usage]
print("Logged: {}".format(text))
return text
# For local execution
if __name__ == "__main__":
app.run(host='0.0.0.0',port=int(os.environ.get('PORT',8080)))
Requirements.txt
flask
google-cloud-logging
Dockerfile
FROM python:3.10-slim
ENV PYTHONUNBUFFERED True
WORKDIR /app
COPY requirements.txt .
RUN pip3 install --no-cache-dir -r requirements.txt
COPY . .
ENV PORT 8080
CMD python3 main.py
Deploy on cloud run and call the URL. The log is well mentioned in Cloud Logging, with that label
part
resource: {
labels: {
configuration_name: "log-format"
location: "us-central1"
project_id: "gdglyon-cloudrun"
revision_name: "log-format-00003-dof"
service_name: "log-format"
}
type: "cloud_run_revision"
}
severity: "WARNING"
Now, do the same thing with Cloud Run Jobs
Main.py (change only the main part)
import os
from flask import Flask
app = Flask(__name__)
import google.cloud.logging
# Instantiates a client
client = google.cloud.logging.Client()
# Retrieves a Cloud Logging handler based on the environment
# you're running in and integrates the handler with the
# Python logging module. By default this captures all logs
# at INFO level and higher
client.setup_logging()
# [END logging_handler_setup]
# [START logging_handler_usage]
# Imports Python standard library logging
import logging
@app.route('/')
def call_function():
# The data to log
text = "Hello, world!"
# Emits the data using the standard logging module
logging.warning(text)
# [END logging_handler_usage]
print("Logged: {}".format(text))
return text
# For local execution
if __name__ == "__main__":
call_function()
same Requitements.txt and same Dockerfile
This time the logs are present but not visible in the Cloud Run Jobs logs section. Why? Because the label is not correct:
resource: {
labels: {
instance_id: "001c9ea28b559541e3331ae5c4bef37c4b465860f0a985296835c80be9a949d42cb3505efcd10650f3848ad942b2b46bf120c93f4dfc15eef489ce8cd0db1012b3"
project_id: "gdglyon-cloudrun"
zone: "projects/751286965207/zones/us-central1-1"
}
type: "gce_instance"
}
severity: "WARNING"
The Cloud Logging Library doesn't correctly detect the Cloud Run Jobs runtime environment and don't put the correct values in the logs that cause a filtering issue in the Cloud Run Jobs UI.
(Cloud Run team is aware of that issue)
Metadata
Metadata
Assignees
Labels
Issues related to the googleapis/python-logging API.Issues related to the googleapis/python-logging API.opened by an external useropened by an external userDesirable enhancement or fix. May not be included in next release.Desirable enhancement or fix. May not be included in next release.‘Nice-to-have’ improvement, new feature or different behavior or design.‘Nice-to-have’ improvement, new feature or different behavior or design.