Skip to content

fix: deduplicate x-goog-api-client headers - #17616

Open
daniel-sanche wants to merge 21 commits into
mainfrom
deduplicate_headers
Open

fix: deduplicate x-goog-api-client headers#17616
daniel-sanche wants to merge 21 commits into
mainfrom
deduplicate_headers

Conversation

@daniel-sanche

@daniel-sanche daniel-sanche commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

We currently populate the x-goog-api-client header multiple times, in different places in the stack (api-core, google-auth, user-provided, etc). While some backend systems are able to handle the duplicate headers, it can cause issues for others

This PR explores a potential fix:

  • the GapicCallable wrapper stores this header value separately at init time, and appends the combines if the user passes in a new copy of the header at runtime, instead of passing through multiple copies
  • The google-auth AuthMetadataPlugin class has a new suppress_metrics_header argument. When enabled, it won't append its own x-goog-api-client header, and allow the gapic wrapper to be the only writer (this header should only be required for auth-spefici HTTP calls, not standard gapic rpcs)

b/477429588

@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 introduces changes to handle and merge duplicate x-goog-api-client metadata headers in google-api-core and allows suppressing these metrics headers in google-auth's AuthMetadataPlugin. Feedback on the changes includes a recommendation to copy self._arbitrary_metadata to prevent shared state mutation, and a suggestion to simplify the _extract_metrics_header function into a single-pass loop for better readability and compatibility with general iterables.

Comment thread packages/google-api-core/google/api_core/gapic_v1/method.py Outdated
Comment thread packages/google-api-core/google/api_core/gapic_v1/method.py Outdated
@daniel-sanche
daniel-sanche marked this pull request as ready for review August 3, 2026 16:19
@daniel-sanche
daniel-sanche requested review from a team as code owners August 3, 2026 16:19
default_host=default_host,
suppress_metrics_header=True,
)
except TypeError:

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.

Just double checking it is intended/desirable to catch/handle ALL TypeErrors that could be raised?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

TypeError is what Python raises when an invalid kwarg argument is passed, so this is as specific as we can be here. If something else is causing the error, we would expect to see it on the next line

Given one or more metadata payload strings, create a combined
string with deduplicated tokens, while preserving token order.

Inputs are expected contain a set of metadata tokens separated by spaces

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.

nit: "expected to contain" (missing "to").

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch!

return func


def _deduplicate_metadata_tokens(*headers: str) -> str:

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.

Should this be "*headers: Optional[str]" since it seems like None is handled

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Returns:
A tuple containing:
- A sequence of remaining metadata tuples.
- a string representing the header value.

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.

This return type tuple doc string seems to be reversed from the actual return tuple which is the string first and then the metadata

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, fixed

@daniel-sanche daniel-sanche changed the title [DRAFT] fix: deduplicate x-goog-api-client headers fix: deduplicate x-goog-api-client headers Aug 3, 2026

@chalmerlowe chalmerlowe 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.

BLOCKER: minor spelling error.
Otherwise LGTM.
Approved.

Given one or more metadata payload strings, create a combined
string with deduplicated tokens, while preserving token order.

Inputs are expecte to contain a set of metadata tokens separated by spaces

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.

Suggested change
Inputs are expecte to contain a set of metadata tokens separated by spaces
Inputs are expected to contain a set of metadata tokens separated by spaces

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.

4 participants