Title: Crash with google.protobuf.json_format.ParseError: Value '2024-09-23' has unexpected type
Open Graph Title: Crash with google.protobuf.json_format.ParseError: Value '2024-09-23' has unexpected type
X Title: Crash with google.protobuf.json_format.ParseError: Value '2024-09-23' has unexpected type
Description: Environment details OS type and version: python:3.11.9-bookworm base image Python version: 3.11.9 pip version: pip 24.0 from /usr/local/lib/python3.11/site-packages/pip (python 3.11) google-cloud-logging version: 3.11.2 Steps to reproduc...
Open Graph Description: Environment details OS type and version: python:3.11.9-bookworm base image Python version: 3.11.9 pip version: pip 24.0 from /usr/local/lib/python3.11/site-packages/pip (python 3.11) google-cloud-l...
X Description: Environment details OS type and version: python:3.11.9-bookworm base image Python version: 3.11.9 pip version: pip 24.0 from /usr/local/lib/python3.11/site-packages/pip (python 3.11) google-cloud-l...
Opengraph URL: https://github.com/googleapis/python-logging/issues/943
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Crash with google.protobuf.json_format.ParseError: Value '2024-09-23' has unexpected type \u003cclass 'datetime.date'\u003e","articleBody":"#### Environment details\r\n\r\n - OS type and version: `python:3.11.9-bookworm` base image\r\n - Python version: `3.11.9`\r\n - pip version: `pip 24.0 from /usr/local/lib/python3.11/site-packages/pip (python 3.11)`\r\n - `google-cloud-logging` version: `3.11.2`\r\n\r\n#### Steps to reproduce\r\n\r\nTrying to send down a dict that has a DateTime property blows the library up with a `google.protobuf.json_format.ParseError`\r\n\r\nI guess I could work around it manually and convert it to a string myself, but the bigger issue is that I wouldn't expect a logging library to crash my application.\r\n\r\nIdeally the library should handle anything thrown at it with a best effort string conversion (even if it's just a useless class instance identity or anything), but at the very least have a way to log its own error rather than throwing an unhandled exception? Or are end users expected to wrap it in try / except and handle certain errors? Or is there a way to give the library a custom JSON parser?\r\n\r\n#### Code example\r\n\r\n```python\r\nfrom datetime import datetime\r\nfrom google.cloud import logging\r\n\r\nlogging_client = logging.Client()\r\nlogging_client.setup_logging()\r\n\r\nmy_dict = {\"my_date\": datetime.now()}\r\n\r\nlogging_client.logger(\"default\").log_struct({\"message\": \"message\", \"my_dict\": my_dict}, severity=\"INFO\")\r\n```\r\n\r\nI thought maybe using the `logging` std lib might make a difference but that also crashes:\r\n\r\n```python\r\nimport logging\r\nfrom datetime import datetime\r\nfrom google.cloud import logging as gcloud_logging\r\n\r\nlogging_client = gcloud_logging.Client()\r\nlogging_client.setup_logging()\r\n\r\ndata_dict = {\"my_date\": datetime.now()}\r\nlogging.info(\"message field\", extra={\"json_fields\": data_dict})\r\n\r\n\u003e\u003e\u003e Failed to submit 1 logs.\r\nTraceback (most recent call last):\r\n File \"/usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py\", line 600, in _ConvertFieldValuePair\r\n self.ConvertMessage(value, sub_message, '{0}.{1}'.format(path, name))\r\n File \"/usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py\", line 485, in ConvertMessage\r\n methodcaller(_WKTJSONMETHODS[full_name][1], value, message, path)(self)\r\n File \"/usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py\", line 704, in _ConvertStructMessage\r\n self._ConvertValueMessage(value[key], message.fields[key],\r\n File \"/usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py\", line 682, in _ConvertValueMessage\r\n raise ParseError('Value {0} has unexpected type {1} at {2}'.format(\r\ngoogle.protobuf.json_format.ParseError: Value 2024-09-23 17:55:34.185216 has unexpected type \u003cclass 'datetime.datetime'\u003e at LogEntry.jsonPayload.my_date\r\n\r\nThe above exception was the direct cause of the following exception:\r\n\r\nTraceback (most recent call last):\r\n File \"/usr/local/lib/python3.11/site-packages/google/cloud/logging_v2/handlers/transports/background_thread.py\", line 115, in _safely_commit_batch\r\n batch.commit()\r\n File \"/usr/local/lib/python3.11/site-packages/google/cloud/logging_v2/logger.py\", line 468, in commit\r\n client.logging_api.write_entries(\r\n File \"/usr/local/lib/python3.11/site-packages/google/cloud/logging_v2/_gapic.py\", line 154, in write_entries\r\n log_entry_pbs = [_log_entry_mapping_to_pb(entry) for entry in entries]\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"/usr/local/lib/python3.11/site-packages/google/cloud/logging_v2/_gapic.py\", line 154, in \u003clistcomp\u003e\r\n log_entry_pbs = [_log_entry_mapping_to_pb(entry) for entry in entries]\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"/usr/local/lib/python3.11/site-packages/google/cloud/logging_v2/_gapic.py\", line 557, in _log_entry_mapping_to_pb\r\n ParseDict(mapping, entry_pb)\r\n File \"/usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py\", line 446, in ParseDict\r\n parser.ConvertMessage(js_dict, message, '')\r\n File \"/usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py\", line 487, in ConvertMessage\r\n self._ConvertFieldValuePair(value, message, path)\r\n File \"/usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py\", line 616, in _ConvertFieldValuePair\r\n raise ParseError(str(e)) from e\r\ngoogle.protobuf.json_format.ParseError: Value 2024-09-23 17:55:34.185216 has unexpected type \u003cclass 'datetime.datetime'\u003e at LogEntry.jsonPayload.my_date\r\n```\r\n\r\n#### Stack trace\r\n\r\n```\r\nTraceback (most recent call last):\r\n File \"/usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py\", line 600, in _ConvertFieldValuePair\r\n self.ConvertMessage(value, sub_message, '{0}.{1}'.format(path, name))\r\n File \"/usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py\", line 485, in ConvertMessage\r\n methodcaller(_WKTJSONMETHODS[full_name][1], value, message, path)(self)\r\n File \"/usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py\", line 704, in _ConvertStructMessage\r\n self._ConvertValueMessage(value[key], message.fields[key],\r\n File \"/usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py\", line 670, in _ConvertValueMessage\r\n self._ConvertStructMessage(value, message.struct_value, path)\r\n File \"/usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py\", line 704, in _ConvertStructMessage\r\n self._ConvertValueMessage(value[key], message.fields[key],\r\n File \"/usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py\", line 682, in _ConvertValueMessage\r\n raise ParseError('Value {0} has unexpected type {1} at {2}'.format(\r\ngoogle.protobuf.json_format.ParseError: Value 2024-09-23 17:24:50.706366 has unexpected type \u003cclass 'datetime.datetime'\u003e at LogEntry.jsonPayload.my_dict.my_date\r\n\r\nThe above exception was the direct cause of the following exception:\r\n\r\nTraceback (most recent call last):\r\n File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\r\n File \"/usr/local/lib/python3.11/site-packages/google/cloud/logging_v2/logger.py\", line 220, in log_struct\r\n self._do_log(client, StructEntry, info, **kw)\r\n File \"/usr/local/lib/python3.11/site-packages/google/cloud/logging_v2/logger.py\", line 169, in _do_log\r\n client.logging_api.write_entries(entries, partial_success=True)\r\n File \"/usr/local/lib/python3.11/site-packages/google/cloud/logging_v2/_gapic.py\", line 154, in write_entries\r\n log_entry_pbs = [_log_entry_mapping_to_pb(entry) for entry in entries]\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"/usr/local/lib/python3.11/site-packages/google/cloud/logging_v2/_gapic.py\", line 154, in \u003clistcomp\u003e\r\n log_entry_pbs = [_log_entry_mapping_to_pb(entry) for entry in entries]\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"/usr/local/lib/python3.11/site-packages/google/cloud/logging_v2/_gapic.py\", line 557, in _log_entry_mapping_to_pb\r\n ParseDict(mapping, entry_pb)\r\n File \"/usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py\", line 446, in ParseDict\r\n parser.ConvertMessage(js_dict, message, '')\r\n File \"/usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py\", line 487, in ConvertMessage\r\n self._ConvertFieldValuePair(value, message, path)\r\n File \"/usr/local/lib/python3.11/site-packages/google/protobuf/json_format.py\", line 616, in _ConvertFieldValuePair\r\n raise ParseError(str(e)) from e\r\ngoogle.protobuf.json_format.ParseError: Value 2024-09-23 17:24:50.706366 has unexpected type \u003cclass 'datetime.datetime'\u003e at LogEntry.jsonPayload.my_dict.my_date```\r\n","author":{"url":"https://github.com/daaain","@type":"Person","name":"daaain"},"datePublished":"2024-09-23T17:34:38.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/943/python-logging/issues/943"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:a43f3f25-c354-04d8-bd3e-34506ca6e173 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8522:1E2E76:D81372:1275A73:6A4C5F4C |
| html-safe-nonce | 7a1596bc845c9dee4b374811848045e613c5fe26361a140c33dec5ed01a7c5b8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NTIyOjFFMkU3NjpEODEzNzI6MTI3NUE3Mzo2QTRDNUY0QyIsInZpc2l0b3JfaWQiOiIxNjM2Mjk0OTI4NTIxNTE4OTI0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 1a11255a5e3669c6210b4ae28a0903cc39847073bed120e44b0716bd5efa2f89 |
| hovercard-subject-tag | issue:2543252752 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/googleapis/python-logging/943/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3175bbe7f40dd216fac860ee6fc93330e63fb4925668f0b25838a35da08cbfb0/googleapis/python-logging/issues/943 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3175bbe7f40dd216fac860ee6fc93330e63fb4925668f0b25838a35da08cbfb0/googleapis/python-logging/issues/943 |
| og:image:alt | Environment details OS type and version: python:3.11.9-bookworm base image Python version: 3.11.9 pip version: pip 24.0 from /usr/local/lib/python3.11/site-packages/pip (python 3.11) google-cloud-l... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | daaain |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3d11bb817438277de2a940854450e83a7d32b6aeb5014e9e6b00a6423900251c |
| turbo-cache-control | no-preview |
| go-import | github.com/googleapis/python-logging git https://github.com/googleapis/python-logging.git |
| octolytics-dimension-user_id | 16785467 |
| octolytics-dimension-user_login | googleapis |
| octolytics-dimension-repository_id | 226992562 |
| octolytics-dimension-repository_nwo | googleapis/python-logging |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 226992562 |
| octolytics-dimension-repository_network_root_nwo | googleapis/python-logging |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 14099438da5379150f15a2892474c7c7e6c0e55e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width