Skip to content
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

feat: adds user agent parameters to two functions #1100

Merged
merged 8 commits into from
Aug 5, 2024
Merged

Conversation

chalmerlowe
Copy link
Collaborator

@chalmerlowe chalmerlowe commented Jul 31, 2024

This adds:

  • the ability for users to set a customer user agent (instead of the default user agent string that is currently hard coded in)
  • docstrings and type hinting for the functions that we touched
  • tests to test the new code path in google_client_info.
  • Add tests to check various states/edge cases (TBD)

Fixes #1082 🦕

Copy link

🤖 I detect that the PR title and the commit message differ and there's only one commit. To use the PR title for the commit history, you can use Github's automerge feature with squashing, or use automerge label. Good luck human!

-- conventional-commit-lint bot
https://conventionalcommits.org/

@chalmerlowe chalmerlowe self-assigned this Jul 31, 2024
@product-auto-label product-auto-label bot added the api: bigquery Issues related to the googleapis/python-bigquery-sqlalchemy API. label Aug 1, 2024
@product-auto-label product-auto-label bot added the size: m Pull request size is medium. label Aug 1, 2024
@chalmerlowe chalmerlowe added the owlbot:run Add this label to trigger the Owlbot post processor. label Aug 1, 2024
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Aug 1, 2024
@chalmerlowe chalmerlowe marked this pull request as ready for review August 1, 2024 17:34
@chalmerlowe chalmerlowe requested review from a team as code owners August 1, 2024 17:34
Default location for jobs / datasets / tables.
project_id (Optional[str]):
Project ID for the project which the client acts on behalf of.
user_agent (Optional[google.api_core.client_info.ClientInfo]):
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT:

Looks like it should be string?

Suggested change
user_agent (Optional[google.api_core.client_info.ClientInfo]):
user_agent (Optional[str]):

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This line is correct. The other line that had str was in error.

google_client_info returns a ClientInfo object which may include the user_agent string as well as several other attributes:

self.python_version = python_version
self.grpc_version = grpc_version
self.api_core_version = api_core_version
self.gapic_version = gapic_version
self.client_library_version = client_library_version
self.user_agent = user_agent
self.rest_version = rest_version

In addition, looking at the bigquery.Client object, it is expecting a ClientInfo object:

client_info (Optional[google.api_core.client_info.ClientInfo]):

Copy link
Contributor

Choose a reason for hiding this comment

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

Just looking at the code, user_agent is passed to google_client_info(), which is expecting a string. google.api_core.client_info.ClientInfo seems to be what google_client_info() returns instead?

@Linchin
Copy link
Contributor

Linchin commented Aug 2, 2024

LGTM. Just some thoughts: should we add a system test to verify that user agent is indeed successfully set up?

@Linchin Linchin self-requested a review August 2, 2024 17:36
@chalmerlowe
Copy link
Collaborator Author

chalmerlowe commented Aug 2, 2024

LGTM. Just some thoughts: should we add a system test to verify that user agent is indeed successfully set up?

I am not inclined to put the time/resources to add a system test to cover this minor update.

Right now, when the to_user_agent() method is called...the customer-supplied user agent string has a variety of elements appended to it. These elements are dynamic and change depending on the versions of certain components that are installed.

Are you picturing an end-to-end system test that accounts for each element of this string? If so, then that type of test feels like it may be brittle and would be dependent on exactly what sub-components are present in this function and might involve complex checks like regex. There might be ways to simplify the checks.

If you have alternate ideas on how to do this simply, easily, and in a way that is robust to change within the code, you are welcome to suggest something.

if self.grpc_version is not None:
    ua += "grpc/{grpc_version} "

if self.rest_version is not None:
    ua += "rest/{rest_version} "

    ua += "gax/{api_core_version} "

if self.gapic_version is not None:
    ua += "gapic/{gapic_version} "

if self.client_library_version is not None:
    ua += "gccl/{client_library_version} "

@Linchin
Copy link
Contributor

Linchin commented Aug 2, 2024

I will add a system test similar to the rest of tests in test_helpers.py. Sorry that I'm unable to suggest code change to locations unedited by this PR, apparently it's a feature request with github that hasn't been fulfilled. Feel free to modify as needed.

@chalmerlowe chalmerlowe merged commit f9324e3 into main Aug 5, 2024
17 checks passed
@chalmerlowe chalmerlowe deleted the add-user-agent branch August 5, 2024 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the googleapis/python-bigquery-sqlalchemy API. size: m Pull request size is medium.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ability to set custom User Agent
2 participants