-
Notifications
You must be signed in to change notification settings - Fork 57
feat: Added flushes/close functionality to logging handlers #917
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9f85e57
feat: Added flushes/close functionality to logging handlers
gkevinzheng d725eff
Fixed unit test issues.
gkevinzheng 9ee7bae
linting
gkevinzheng ae3ece3
more linting
gkevinzheng b46779e
Addressed code review feedback
gkevinzheng 1b037b6
Refactored _close
gkevinzheng c1d1f3f
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] da74511
Added system tests
gkevinzheng cc32e9b
make transport_open private
gkevinzheng 72a8ff6
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] e04a188
Merge branch 'main' into cloud-logging-handler-flush
gkevinzheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -188,7 +188,10 @@ def __init__( | |
resource = detect_resource(client.project) | ||
self.name = name | ||
self.client = client | ||
client._handlers.add(self) | ||
self.transport = transport(client, name, resource=resource) | ||
self._transport_open = True | ||
self._transport_cls = transport | ||
self.project_id = client.project | ||
self.resource = resource | ||
self.labels = labels | ||
|
@@ -213,6 +216,12 @@ def emit(self, record): | |
labels = {**add_resource_labels(resource, record), **(labels or {})} or None | ||
|
||
# send off request | ||
if not self._transport_open: | ||
self.transport = self._transport_cls( | ||
self.client, self.name, resource=self.resource | ||
) | ||
self._transport_open = True | ||
|
||
self.transport.send( | ||
record, | ||
message, | ||
|
@@ -225,6 +234,21 @@ def emit(self, record): | |
source_location=record._source_location, | ||
) | ||
|
||
def flush(self): | ||
"""Forces the Transport object to submit any pending log records. | ||
|
||
For SyncTransport, this is a no-op. | ||
""" | ||
super(CloudLoggingHandler, self).flush() | ||
if self._transport_open: | ||
self.transport.flush() | ||
|
||
def close(self): | ||
"""Closes the log handler and cleans up all Transport objects used.""" | ||
self.transport.close() | ||
self.transport = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like the idea of using Can't we use a new flag for this? |
||
self._transport_open = False | ||
|
||
|
||
def _format_and_parse_message(record, formatter_handler): | ||
""" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't this mean the handler has been closed? Why not just raise an error, instead of re-creating the transport?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I based this off of some logging handlers I saw in the Python standard library, which will reopen a closed connection if that connection is closed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I think this adds extra complexity, so I'd prefer to keep them permanently closed if we can get away with that
But if you think this behavior would help us integrate with the standard library, it makes sense to keep it.