Skip to content
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

fix: Log logging_api.write_entries errors internally rather than crashing #958

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

gkevinzheng
Copy link
Contributor

Two fixes for #943:

  1. Added documentation clarifying the expected format of log_struct
  2. Logger._do_log and Batch.commit will log errors internally rather than reraising an Exception which could propagate to user code when calling logging_api.write_entries

@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

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.

3 participants