Repository: googleapis/google-cloud-python

Issue:
BigQuery: Add user-agent info to Jupyter magics Client constructor(s).
In order to better prioritize jupyter features such as #8342 and #8143, it is important for Google to know how much these features are used to query BigQuery. This is done by populating the user-agent string with the corresponding "application name" and version information.

I propose we use the IPython version information in this string, as these magics are being used from an IPython kernel. Getting the IPython version string: https://stackoverflow.com/a/56265255/101923 Something like:

```
bigquery.Client(
    client_info=ClientInfo(
        user_agent="ipython-{}".format(IPython.__version__)
    )
)
```


Discussion:

Resolving patch:
diff --git a/bigquery/google/cloud/bigquery/magics.py b/bigquery/google/cloud/bigquery/magics.py
--- a/bigquery/google/cloud/bigquery/magics.py
+++ b/bigquery/google/cloud/bigquery/magics.py
@@ -144,6 +144,7 @@
 except ImportError:  # pragma: NO COVER
     bigquery_storage_v1beta1 = None
 
+from google.api_core import client_info
 import google.auth
 from google.cloud import bigquery
 from google.cloud.bigquery.dbapi import _helpers
@@ -398,6 +399,9 @@ def _cell_magic(line, query):
         project=project,
         credentials=context.credentials,
         default_query_job_config=context.default_query_job_config,
+        client_info=client_info.ClientInfo(
+            user_agent="ipython-{}".format(IPython.__version__)
+        ),
     )
     if context._connection:
         client._connection = context._connection


Test patch:
diff --git a/bigquery/tests/unit/test_client.py b/bigquery/tests/unit/test_client.py
--- a/bigquery/tests/unit/test_client.py
+++ b/bigquery/tests/unit/test_client.py
@@ -5353,10 +5353,10 @@ def test_load_table_from_dataframe_w_schema_wo_pyarrow(self):
                 dataframe, self.TABLE_REF, job_config=job_config, location=self.LOCATION
             )
 
-        assert len(warned) == 1
-        warning = warned[0]
-        assert warning.category is PendingDeprecationWarning
-        assert "pyarrow" in str(warning)
+        assert warned  # there should be at least one warning
+        for warning in warned:
+            assert "pyarrow" in str(warning)
+            assert warning.category in (DeprecationWarning, PendingDeprecationWarning)
 
         load_table_from_file.assert_called_once_with(
             client,
diff --git a/bigquery/tests/unit/test_magics.py b/bigquery/tests/unit/test_magics.py
--- a/bigquery/tests/unit/test_magics.py
+++ b/bigquery/tests/unit/test_magics.py
@@ -320,6 +320,31 @@ def test_bigquery_magic_without_optional_arguments(monkeypatch):
     assert list(return_value) == list(result)  # verify column names
 
 
+@pytest.mark.usefixtures("ipython_interactive")
+def test_bigquery_magic_default_connection_user_agent():
+    ip = IPython.get_ipython()
+    ip.extension_manager.load_extension("google.cloud.bigquery")
+    magics.context._connection = None
+
+    credentials_mock = mock.create_autospec(
+        google.auth.credentials.Credentials, instance=True
+    )
+    default_patch = mock.patch(
+        "google.auth.default", return_value=(credentials_mock, "general-project")
+    )
+    run_query_patch = mock.patch(
+        "google.cloud.bigquery.magics._run_query", autospec=True
+    )
+    conn_patch = mock.patch("google.cloud.bigquery.client.Connection", autospec=True)
+
+    with conn_patch as conn, run_query_patch, default_patch:
+        ip.run_cell_magic("bigquery", "", "SELECT 17 as num")
+
+    client_info_arg = conn.call_args.kwargs.get("client_info")
+    assert client_info_arg is not None
+    assert client_info_arg.user_agent == "ipython-" + IPython.__version__
+
+
 @pytest.mark.usefixtures("ipython_interactive")
 def test_bigquery_magic_with_legacy_sql():
     ip = IPython.get_ipython()