-
Notifications
You must be signed in to change notification settings - Fork 56
feat: Add support for library instrumentation #551
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
80 commits
Select commit
Hold shift + click to select a range
4979159
Add .python-version to .gitignore
arbrown 34058e9
Add initial class/test for instrumentation_source
arbrown 2c0668a
Add version and truncate logic
arbrown 18e67e7
Add instrumentation tests and severity info
arbrown 1868ed8
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 3ba423b
Add method to update and validate existing info
arbrown 0ba6769
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 1188002
Add .python-version to gitignore
arbrown 67f2343
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 8186234
Implement hook to add instrumentation for logger
arbrown 3c4b057
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 19ac45c
Add tests for logger instrumentation logic
arbrown c006efd
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] ce3f231
Update structured log handler to emit info
arbrown 1d80ff8
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] d354c45
Refactor structured log and add unit test
arbrown 8f92f68
Merge branch 'main' into instrumentation
arbrown 5b51050
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] b7b82e3
Merge branch 'instrumentation' of https://github.com/googleapis/pytho…
gcf-owl-bot[bot] 26016cf
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] aaa3675
Merge branch 'instrumentation' of https://github.com/googleapis/pytho…
gcf-owl-bot[bot] 4700b45
Add side effect to unit test
arbrown 385aeb6
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] d15abab
Update to environment submodule
arbrown 81a80c8
Fix linter errors
arbrown 0eef799
chore(main): release 3.1.0 (#479)
release-please[bot] f5a357c
docs: Change button in README to .png file (#554)
arbrown 738d9dc
chore(main): release 3.1.1 (#557)
release-please[bot] a64da5f
Update env-tests submodule
arbrown 028bdfe
Merge branch 'main' into instrumentation
arbrown dc6a797
Minor format update
arbrown f6d37d5
Fix system test to skip diagnostic log entry
arbrown 249a764
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 9be9332
Merge branch 'main' into instrumentation
arbrown 38fee9f
Update truncation logic based on feedback
arbrown c3b63ba
Update environment tests
arbrown 80ef001
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 61fe753
Merge branch 'instrumentation' of https://github.com/googleapis/pytho…
gcf-owl-bot[bot] b86a946
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] d010873
Merge branch 'instrumentation' of https://github.com/googleapis/pytho…
gcf-owl-bot[bot] ce5425b
Fix broken unit test
arbrown abfd16c
Fix broken unit test
arbrown cb1e6bf
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] cbd3835
Merge branch 'instrumentation' of https://github.com/googleapis/pytho…
gcf-owl-bot[bot] 5d0e702
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 9b7388d
Merge branch 'instrumentation' of https://github.com/googleapis/pytho…
gcf-owl-bot[bot] 5bff645
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 1157018
Merge branch 'instrumentation' of https://github.com/googleapis/pytho…
gcf-owl-bot[bot] cd78d79
Change default name/version
arbrown 4f81c5a
Refactor add_instrumentation
arbrown 4553d7f
Add more documentation to validation methods
arbrown 7d98aad
Refactor add_instrumentation to be more pythonic
arbrown 4962604
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 70e6233
Update environemnt tests
arbrown 05ae10e
Merge branch 'main' into instrumentation
arbrown 3c3b6bb
Refactor _is_valid and add test
arbrown f96772a
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 914c98d
Add more detail to method documentation
arbrown 27aa896
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 5d55bfd
Move methods to private
arbrown fc8ada3
Change instumentation_added to private
arbrown 7e77837
Fix some issues with validation method
arbrown ee4fc22
Fix bug in _add_instrumentation
arbrown 8fac555
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 55bfa06
Simplify string truncation
arbrown c939097
Merge branch 'main' into instrumentation
arbrown b5d8400
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] da3caa9
Merge branch 'instrumentation' of https://github.com/googleapis/pytho…
gcf-owl-bot[bot] bfda525
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] b5dfeef
Merge branch 'instrumentation' of https://github.com/googleapis/pytho…
gcf-owl-bot[bot] d62389a
Merge branch 'main' into instrumentation
daniel-sanche 572a04c
Remove unused import to fix lint
arbrown 6676621
Merge branch 'main' into instrumentation
arbrown 587367c
Remove validate_and_update_instrumentation
arbrown b60ae45
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] f84d303
Remove _is_valid code (no longer checked)
arbrown b7f5c41
Run nox blacken
arbrown 4066927
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 8acee94
Merge branch 'instrumentation' of https://github.com/googleapis/pytho…
gcf-owl-bot[bot] 7b85737
Remove extraneous unit test
arbrown 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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# 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. | ||
|
||
"""Add diagnostic instrumentation source information to logs""" | ||
from google.cloud.logging_v2.entries import StructEntry | ||
from google.cloud.logging_v2 import __version__ | ||
|
||
_DIAGNOSTIC_INFO_KEY = "logging.googleapis.com/diagnostic" | ||
_INSTRUMENTATION_SOURCE_KEY = "instrumentation_source" | ||
_PYTHON_LIBRARY_NAME = "python" | ||
|
||
_LIBRARY_VERSION = __version__ | ||
|
||
_MAX_NAME_LENGTH = 14 | ||
_MAX_VERSION_LENGTH = 14 | ||
_MAX_INSTRUMENTATION_ENTRIES = 3 | ||
|
||
|
||
def _add_instrumentation(entries, **kw): | ||
"""Add instrumentation information to a list of entries | ||
|
||
A new diagnostic entry is prepended to the list of | ||
entries. | ||
|
||
Args: | ||
entries (Sequence[Mapping[str, ...]]): sequence of mappings representing | ||
the log entry resources to log. | ||
|
||
Returns: | ||
Sequence[Mapping[str, ...]]: entries with instrumentation info added to | ||
the beginning of list. | ||
""" | ||
|
||
diagnostic_entry = _create_diagnostic_entry(**kw) | ||
entries.insert(0, diagnostic_entry.to_api_repr()) | ||
return entries | ||
|
||
|
||
def _create_diagnostic_entry(name=_PYTHON_LIBRARY_NAME, version=_LIBRARY_VERSION, **kw): | ||
"""Create a diagnostic log entry describing this library | ||
|
||
The diagnostic log consists of a list of library name and version objects | ||
that have handled a given log entry. If this library is the originator | ||
of the log entry, it will look like: | ||
{logging.googleapis.com/diagnostic: {instrumentation_source: [{name: "python", version: "3.0.0"}]}} | ||
|
||
Args: | ||
name(str): The name of this library (e.g. 'python') | ||
version(str) The version of this library (e.g. '3.0.0') | ||
|
||
Returns: | ||
google.cloud.logging_v2.LogEntry: Log entry with library information | ||
""" | ||
payload = { | ||
_DIAGNOSTIC_INFO_KEY: { | ||
_INSTRUMENTATION_SOURCE_KEY: [_get_instrumentation_source(name, version)] | ||
} | ||
} | ||
kw["severity"] = "INFO" | ||
daniel-sanche marked this conversation as resolved.
Show resolved
Hide resolved
|
||
entry = StructEntry(payload=payload, **kw) | ||
return entry | ||
|
||
|
||
def _get_instrumentation_source(name=_PYTHON_LIBRARY_NAME, version=_LIBRARY_VERSION): | ||
"""Gets a JSON representation of the instrumentation_source | ||
|
||
Args: | ||
name(str): The name of this library (e.g. 'python') | ||
version(str) The version of this library (e.g. '3.0.0') | ||
Returns: | ||
obj: JSON object with library information | ||
""" | ||
source = {"name": name, "version": version} | ||
# truncate strings to no more than _MAX_NAME_LENGTH characters | ||
for key, val in source.items(): | ||
source[key] = ( | ||
val if len(val) <= _MAX_NAME_LENGTH else f"{val[:_MAX_NAME_LENGTH]}*" | ||
) | ||
return source |
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
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Copyright 2022 Google LLC | ||
# | ||
# 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. | ||
|
||
import unittest | ||
import google.cloud.logging_v2._instrumentation as i | ||
|
||
|
||
class TestInstrumentation(unittest.TestCase): | ||
|
||
TEST_NAME = "python" | ||
# LONG_NAME > 14 characters | ||
LONG_NAME = TEST_NAME + "789ABCDEF" | ||
|
||
TEST_VERSION = "1.0.0" | ||
# LONG_VERSION > 16 characters | ||
LONG_VERSION = TEST_VERSION + "6789ABCDEF12" | ||
|
||
def _get_diagonstic_value(self, entry, key): | ||
return entry.payload[i._DIAGNOSTIC_INFO_KEY][i._INSTRUMENTATION_SOURCE_KEY][-1][ | ||
key | ||
] | ||
|
||
def test_default_diagnostic_info(self): | ||
entry = i._create_diagnostic_entry() | ||
self.assertEqual( | ||
i._PYTHON_LIBRARY_NAME, | ||
self._get_diagonstic_value(entry, "name"), | ||
) | ||
self.assertEqual( | ||
i._LIBRARY_VERSION, self._get_diagonstic_value(entry, "version") | ||
) | ||
|
||
def test_custom_diagnostic_info(self): | ||
entry = i._create_diagnostic_entry( | ||
name=self.TEST_NAME, version=self.TEST_VERSION | ||
) | ||
self.assertEqual( | ||
self.TEST_NAME, | ||
self._get_diagonstic_value(entry, "name"), | ||
) | ||
self.assertEqual( | ||
self.TEST_VERSION, self._get_diagonstic_value(entry, "version") | ||
) | ||
|
||
def test_truncate_long_values(self): | ||
entry = i._create_diagnostic_entry( | ||
name=self.LONG_NAME, version=self.LONG_VERSION | ||
) | ||
|
||
expected_name = self.LONG_NAME[: i._MAX_NAME_LENGTH] + "*" | ||
expected_version = self.LONG_VERSION[: i._MAX_VERSION_LENGTH] + "*" | ||
|
||
self.assertEqual(expected_name, self._get_diagonstic_value(entry, "name")) | ||
self.assertEqual(expected_version, self._get_diagonstic_value(entry, "version")) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.