Repository navigation
Conversation
… on GAE (googleapis#17321) Signed-off-by: Roxen-AS <akash.arjun12@gmail.com>
There was a problem hiding this comment.
Code Review
This pull request prevents the GAE trace ID from polluting the cached environmental labels dictionary by copying the labels and safely accessing the _trace attribute using getattr. A unit test was also added to verify this fix. The reviewer suggests optimizing performance by only copying the labels dictionary when mutation is actually required, avoiding unnecessary allocations on every log record.
| labels = _get_environmental_labels(resource.type).copy() | ||
|
|
||
| # Add labels from log record | ||
| if resource.type == _GAE_RESOURCE_TYPE and record._trace is not None: | ||
| if ( | ||
| resource.type == _GAE_RESOURCE_TYPE | ||
| and getattr(record, "_trace", None) is not None | ||
| ): | ||
| labels[_GAE_TRACE_ID_LABEL] = record._trace |
There was a problem hiding this comment.
Unconditionally calling .copy() on the dictionary returned by _get_environmental_labels introduces unnecessary overhead (allocation and copying) for every single log record, even when the resource type is not GAE or when there is no trace ID. Since logging is a performance-critical hot path, we should only copy the dictionary when we actually need to mutate it.
| labels = _get_environmental_labels(resource.type).copy() | |
| # Add labels from log record | |
| if resource.type == _GAE_RESOURCE_TYPE and record._trace is not None: | |
| if ( | |
| resource.type == _GAE_RESOURCE_TYPE | |
| and getattr(record, "_trace", None) is not None | |
| ): | |
| labels[_GAE_TRACE_ID_LABEL] = record._trace | |
| labels = _get_environmental_labels(resource.type) | |
| # Add labels from log record | |
| if ( | |
| resource.type == _GAE_RESOURCE_TYPE | |
| and getattr(record, "_trace", None) is not None | |
| ): | |
| labels = labels.copy() | |
| labels[_GAE_TRACE_ID_LABEL] = record._trace |
There was a problem hiding this comment.
Good catch! Updated to only copy the dictionary when mutating on GAE with a trace ID in commit 8845e61.
… path (googleapis#17321) Signed-off-by: Roxen-AS <akash.arjun12@gmail.com>
Fixes #17321
Summary of the issue
_get_environmental_labelsingoogle/cloud/logging_v2/handlers/_monitored_resources.pyis memoized via@functools.lru_cache(maxsize=None)and returns a cached dictionary.When running on App Engine (
_GAE_RESOURCE_TYPE),add_resource_labels(resource, record)mutated that cached dictionary directly in place:Consequently, once any log record with a trace was logged, the cached dictionary retained
appengine.googleapis.com/trace_idfor the lifetime of the process, leaking that trace ID into subsequent records without traces or with different traces.Changes
google/cloud/logging_v2/handlers/_monitored_resources.py, call.copy()on the dictionary returned by_get_environmental_labels(resource.type)so that record-specific labels do not mutate the cached dictionary.test_add_resource_labels_gae_trace_id_does_not_leakinpackages/google-cloud-logging/tests/unit/handlers/test__monitored_resources.pyverifying that subsequent log records with no trace or different traces do not retain previous trace IDs.