Skip to content

fix(logging): copy cached environmental labels to avoid trace_id leak on GAE (#17321) - #19266

Open
Roxen-AS wants to merge 2 commits into
googleapis:mainfrom
Roxen-AS:fix/logging-gae-trace-id-leak
Open

Roxen-AS wants to merge 2 commits into
googleapis:mainfrom
Roxen-AS:fix/logging-gae-trace-id-leak

Conversation

@Roxen-AS

Copy link
Copy Markdown

Fixes #17321

Summary of the issue

_get_environmental_labels in google/cloud/logging_v2/handlers/_monitored_resources.py is 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:

    labels = _get_environmental_labels(resource.type)
    if resource.type == _GAE_RESOURCE_TYPE and record._trace is not None:
        labels[_GAE_TRACE_ID_LABEL] = record._trace
    return labels

Consequently, once any log record with a trace was logged, the cached dictionary retained appengine.googleapis.com/trace_id for the lifetime of the process, leaking that trace ID into subsequent records without traces or with different traces.

Changes

  • In 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.
  • Added a unit test test_add_resource_labels_gae_trace_id_does_not_leak in packages/google-cloud-logging/tests/unit/handlers/test__monitored_resources.py verifying that subsequent log records with no trace or different traces do not retain previous trace IDs.

… on GAE (googleapis#17321)

Signed-off-by: Roxen-AS <akash.arjun12@gmail.com>
@Roxen-AS
Roxen-AS requested a review from a team as a code owner November 29, 2026 17:11

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 297 to 304
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

App Engine: trace_id label leaks across log records (mutation of lru_cache-cached dict)

1 participant