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
feat: record source locations #254
Merged
Merged
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
dc2e4a8
added structured log handler
daniel-sanche 0e37217
Merge branch 'master' into structured-log-handler-2
daniel-sanche 3a32c3d
improved structured log handler
daniel-sanche e454c38
refactored GCPFilter
daniel-sanche 761eccd
moved and renamed filter
daniel-sanche 4495cf5
fixed up broken filter
daniel-sanche 6a3a595
blackened
daniel-sanche 3c1af26
fixed typo
daniel-sanche cb8b57b
fixed lint
daniel-sanche f25da46
expanded on comment
daniel-sanche b686173
updated environment tests
daniel-sanche dc85dd3
Merge branch 'master' into structured-log-handler-2
daniel-sanche 133c584
moved location of stdout log template
daniel-sanche 637f95c
added new test for structured log class
daniel-sanche 570bc2d
added a test with http_request data
daniel-sanche 3aa5baa
improved structured log tests
daniel-sanche d1014f2
export CloudLoggingFilter in __init__ file
daniel-sanche 5bec0ea
added more tests
daniel-sanche 7392bd2
added a test for user overrides
daniel-sanche cf20b69
fixed lint issues
daniel-sanche 12d2d37
removed timestamp from structured log output
daniel-sanche 237e68c
added source location to filter
daniel-sanche bd84214
remove appengine tests
daniel-sanche ed7b070
reverted span naming scheme
daniel-sanche 4ae78d4
removed unused timestamp field
daniel-sanche 636dc3c
Merge branch 'structured-log-handler-2' into add-source-locations
daniel-sanche 614d1bf
remove unneeded imports
daniel-sanche c861b12
Merge branch 'structured-log-handler-2' into add-source-locations
daniel-sanche dcf0fed
Merge branch 'v2_update_2' into add-source-locations
daniel-sanche File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright 2021 Google LLC All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Logging handler for printing formatted structured logs to standard output. | ||
""" | ||
|
||
import logging.handlers | ||
|
||
from google.cloud.logging_v2.handlers.handlers import CloudLoggingFilter | ||
|
||
GCP_FORMAT = '{"message": "%(message)s", "severity": "%(levelname)s", "logging.googleapis.com/trace": "%(trace)s", "logging.googleapis.com/sourceLocation": { "file": "%(pathname)s", "line": "%(lineno)d", "function": "%(funcName)s"}, "httpRequest": {"requestMethod": "%(request_method)s", "requestUrl": "%(request_url)s", "userAgent": "%(user_agent)s", "protocol": "%(protocol)s"} }' | ||
|
||
|
||
class StructuredLogHandler(logging.StreamHandler): | ||
"""Handler to format logs into the Cloud Logging structured log format, | ||
and write them to standard output | ||
""" | ||
|
||
def __init__(self, *, name=None, stream=None, project=None): | ||
""" | ||
Args: | ||
name (Optional[str]): The name of the custom log in Cloud Logging. | ||
stream (Optional[IO]): Stream to be used by the handler. | ||
""" | ||
super(StructuredLogHandler, self).__init__(stream=stream) | ||
self.name = name | ||
self.project_id = project | ||
|
||
# add extra keys to log record | ||
self.addFilter(CloudLoggingFilter(project)) | ||
|
||
# make logs appear in GCP structured logging format | ||
self.formatter = logging.Formatter(GCP_FORMAT) | ||
|
||
def format(self, record): | ||
"""Format the message into structured log JSON. | ||
Args: | ||
record (logging.LogRecord): The log record. | ||
Returns: | ||
str: A JSON string formatted for GKE fluentd. | ||
""" | ||
|
||
payload = self.formatter.format(record) | ||
return payload |
Submodule environment
updated
16 files
+1 −0 | deployable/go/.dockerignore | |
+2 −2 | deployable/go/Dockerfile | |
+4 −0 | deployable/nodejs/.dockerignore | |
+4 −0 | deployable/nodejs/.gitignore | |
+35 −0 | deployable/nodejs/Dockerfile | |
+68 −29 | deployable/nodejs/app.js | |
+2 −2 | deployable/nodejs/package.json | |
+31 −0 | deployable/nodejs/tests.js | |
+6 −0 | deployable/python/snippets.py | |
+105 −0 | envctl/env_scripts/nodejs/cloudrun.sh | |
+1 −0 | envctl/env_scripts/nodejs/functions.sh | |
+3 −2 | tests/common/common.py | |
+47 −0 | tests/common/python.py | |
+38 −0 | tests/nodejs/test_cloudrun.py | |
+21 −0 | tests/python/test_cloudrun.py | |
+21 −1 | tests/python/test_functions.py |
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.
Can some of the 3 be empty? For example, code that's not part of any function.
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.
No, these are all part of the official
record
object spec, so the attributes will all exist.They could be
None
, but that would be caught by this check