I'm using the google-cloud-logging Python client library with Cloud Functions. I'm following recommendations and using the standard Python logging library. v3+ of this library supports logging JSON payloads and setting custom metadata.
I wish to both set a custom label and log a JSON payload. The docs mention that sending a custom JSON payload can be achieved using the json_fields extra argument. Example:
import logging
data_dict = {"hello": "world"}
logging.info("message field", extra={"json_fields": data_dict})
With regard to sending a label the docs once again point to the extra argument. Example:
my_labels = {"foo": "bar"}
my_http = {"requestUrl": "localhost"}
my_trace = "01234"
logging.info(
"hello", extra={"labels": my_labels, "http_request": my_http, "trace": my_trace}
)
Following the guidance above I'm under the impression that this should work in achieving both:
image_metrics = {
'total_images_sourced': 10,
'total_images': 8
}
# Write custom metrics to Cloud Logging
logging.info("image_metrics", extra={
"labels": {"type": "image"},
"json_fields": image_metrics
})
However, the resulting logs only output a text payload. Neither the JSON payload nor the label is set.
How can I send both the JSON payload as well as the label?
Update 1: This seems to work correctly when testing locally via the Functions Framework but when I deploy the exact same codebase to update the Cloud Function and run again, it result in the text payload and no label.
