Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: Made write_entries raise ValueError on ParseErrors #958

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

Merged
merged 10 commits into from
Jan 22, 2025

Conversation

gkevinzheng
Copy link
Contributor

@gkevinzheng gkevinzheng commented Jan 6, 2025

Two fixes for #943:

  1. Made write_entries raise ValueError on invalid log entries.
  2. Added documentation clarifying the expected format of log_struct and clarifying the ValueError that gets raised.

@gkevinzheng gkevinzheng requested review from a team as code owners January 6, 2025 17:17
@gkevinzheng gkevinzheng requested a review from cindy-peng January 6, 2025 17:17
@product-auto-label product-auto-label bot added size: m Pull request size is medium. api: logging Issues related to the googleapis/python-logging API. labels Jan 6, 2025
@gkevinzheng gkevinzheng removed the request for review from cindy-peng January 6, 2025 17:17
try:
client.logging_api.write_entries(entries, partial_success=True)
except Exception:
_LOGGER.error("Failed to submit log message.", exc_info=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a good idea to just swallow all exceptions here. All grpc networking errors would now be ignored (ex bad IAM credentials), and users will have no idea anything is wrong unless they know to subscribe to the internal _LOGGER. And existing customers may have error handling logic in place that will now be bypassed, so this could be a breaking change hidden in a bug fix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just do json_format.ParseErrors instead?

Copy link
Contributor

@daniel-sanche daniel-sanche Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean swallowing the specific exception instead of all exceptions?

Honestly, I think just raising an exception here makes sense, so the user knows their input isn't handled. I'd do a raise ValueError(...) from e though, so we can give more context instead of raising the raw internal error. And then add a Raises: section in the docstring describing this situation

info (dict):
the log entry information. The dict must be serializable
to a Protobuf Struct (map from strings to Values, see
https://protobuf.dev/reference/protobuf/google.protobuf/#value),
Copy link
Contributor

@daniel-sanche daniel-sanche Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to lay out the specific types that are serializable?

Nested dicts and lists could make it complicated. But maybe we could do something like dict[str, float|bool|str|dict|list|None]

the log entry information. The dict must be serializable
to a Protobuf Struct (map from strings to Values, see
https://protobuf.dev/reference/protobuf/google.protobuf/#value),
otherwise the item will not be logged.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might be good to add more info to the main text too, instead of "Log a dictionary message"

# attempt to attach extra contex on which log caused error
self._append_context_to_error(e)
raise e
except Exception as e:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above: hiding exceptions by default seems like it could have unintentional consequences, and could be a breaking change

@gkevinzheng gkevinzheng changed the title fix: Log logging_api.write_entries errors internally rather than crashing fix: Made write_entries raise ValueError on ParseErrors Jan 14, 2025
@product-auto-label product-auto-label bot added size: s Pull request size is small. and removed size: m Pull request size is medium. labels Jan 14, 2025

See
https://cloud.google.com/logging/docs/reference/v2/rest/v2/entries/write

The dictionary entry must be able to be serializable to a Protobuf Struct
(see https://protobuf.dev/reference/protobuf/google.protobuf/#value for more
details), otherwise it will not be logged.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Otherwise a ValueError exception will be raised?"

Also, maybe include that this essentially means strings, floats, bools, lists, and dicts, and None?

@gkevinzheng gkevinzheng added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jan 15, 2025
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jan 15, 2025
@product-auto-label product-auto-label bot added size: m Pull request size is medium. and removed size: s Pull request size is small. labels Jan 16, 2025
@gkevinzheng gkevinzheng merged commit 5309478 into main Jan 22, 2025
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: logging Issues related to the googleapis/python-logging API. size: m Pull request size is medium.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants