-
Notifications
You must be signed in to change notification settings - Fork 55
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: Added environment specific labels to client library when running in Cloud Run Jobs #877
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
98891ff
fix: Added environment specific labels to client library when running in
gkevinzheng 1400010
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 6681fad
Merge branch 'main' into cloud-run-job-labels
gkevinzheng d0a49a1
Removed unnecessary import
gkevinzheng 85ca7f2
Changed unit tests to pytest
gkevinzheng 55e9268
Renamed add_environmental_labels to add_resource_labels; cached porti…
gkevinzheng b9daf75
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 7053648
Updated comments and _get_environmental_labels
gkevinzheng e0c9c9a
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] ee28115
Merge branch 'main' into cloud-run-job-labels
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 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 |
---|---|---|
|
@@ -12,6 +12,8 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import functools | ||
import logging | ||
import os | ||
|
||
from google.cloud.logging_v2.resource import Resource | ||
|
@@ -67,6 +69,20 @@ | |
_PROJECT_NAME = "project/project-id" | ||
"""Attribute in metadata server when in GKE environment.""" | ||
|
||
_GAE_RESOURCE_TYPE = "gae_app" | ||
"""Resource type for App Engine environment.""" | ||
|
||
_CLOUD_RUN_JOB_RESOURCE_TYPE = "cloud_run_job" | ||
"""Resource type for Cloud Run Jobs.""" | ||
|
||
_GAE_TRACE_ID_LABEL = "appengine.googleapis.com/trace_id" | ||
"""Extra trace label to be added on App Engine environments""" | ||
|
||
_CLOUD_RUN_JOBS_EXECUTION_NAME_LABEL = "run.googleapis.com/execution_name" | ||
_CLOUD_RUN_JOBS_TASK_INDEX_LABEL = "run.googleapis.com/task_index" | ||
_CLOUD_RUN_JOBS_TASK_ATTEMPT_LABEL = "run.googleapis.com/task_attempt" | ||
"""Extra labels for Cloud Run environments to be recognized by Cloud Run Jobs web UI.""" | ||
|
||
|
||
def _create_functions_resource(): | ||
"""Create a standardized Cloud Functions resource. | ||
|
@@ -159,7 +175,7 @@ def _create_cloud_run_job_resource(): | |
region = retrieve_metadata_server(_REGION_ID) | ||
project = retrieve_metadata_server(_PROJECT_NAME) | ||
resource = Resource( | ||
type="cloud_run_job", | ||
type=_CLOUD_RUN_JOB_RESOURCE_TYPE, | ||
labels={ | ||
"project_id": project if project else "", | ||
"job_name": os.environ.get(_CLOUD_RUN_JOB_ID, ""), | ||
|
@@ -177,7 +193,7 @@ def _create_app_engine_resource(): | |
zone = retrieve_metadata_server(_ZONE_ID) | ||
project = retrieve_metadata_server(_PROJECT_NAME) | ||
resource = Resource( | ||
type="gae_app", | ||
type=_GAE_RESOURCE_TYPE, | ||
labels={ | ||
"project_id": project if project else "", | ||
"module_id": os.environ.get(_GAE_SERVICE_ENV, ""), | ||
|
@@ -233,3 +249,55 @@ def detect_resource(project=""): | |
else: | ||
# use generic global resource | ||
return _create_global_resource(project) | ||
|
||
|
||
@functools.lru_cache(maxsize=None) | ||
def _get_environmental_labels(resource_type): | ||
"""Builds a dictionary of labels to be inserted into a LogRecord of the given resource type. | ||
This function should only build a dict of items that are consistent across multiple logging statements | ||
of the same resource type, such as environment variables. | ||
|
||
Returns: | ||
dict: | ||
A dict representation of labels and the values of those labels | ||
""" | ||
labels = {} | ||
|
||
def set_item(key, environ_var): | ||
val = os.environ.get(environ_var, "") | ||
if val: | ||
labels[key] = val | ||
|
||
if resource_type == _CLOUD_RUN_JOB_RESOURCE_TYPE: | ||
set_item( | ||
_CLOUD_RUN_JOBS_EXECUTION_NAME_LABEL, | ||
_CLOUD_RUN_EXECUTION_ID, | ||
) | ||
set_item(_CLOUD_RUN_JOBS_TASK_INDEX_LABEL, _CLOUD_RUN_TASK_INDEX) | ||
set_item(_CLOUD_RUN_JOBS_TASK_ATTEMPT_LABEL, _CLOUD_RUN_TASK_ATTEMPT) | ||
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. nit: this works, but I find the indirection a bit harder to read than it probably needs to be. Maybe something like:
|
||
|
||
return labels | ||
|
||
|
||
def add_resource_labels(resource: Resource, record: logging.LogRecord): | ||
"""Returns additional labels to be appended on to a LogRecord object based on the | ||
local environment. Defaults to an empty dictionary if none apply. This is only to be | ||
used for CloudLoggingHandler, as the structured logging daemon already does this. | ||
|
||
Args: | ||
resource (google.cloud.logging.Resource): Resource based on the environment | ||
record (logging.LogRecord): A LogRecord object representing a log record | ||
Returns: | ||
Dict[str, str]: New labels to append to the labels of the LogRecord | ||
""" | ||
if not resource: | ||
return None | ||
|
||
# Get environmental labels from the resource type | ||
labels = _get_environmental_labels(resource.type) | ||
|
||
# Add labels from log record | ||
if resource.type == _GAE_RESOURCE_TYPE and record._trace is not None: | ||
labels[_GAE_TRACE_ID_LABEL] = record._trace | ||
|
||
return labels |
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
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.
nit: maybe add a comment about cache?