Skip to content

Commit

Permalink
feat: set user-agent string to distinguish SQLAlchemy requests (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Gurov committed Sep 8, 2021
1 parent c8a0f5e commit b5e1a21
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import pkg_resources
import re

from sqlalchemy import types, ForeignKeyConstraint
Expand Down Expand Up @@ -433,9 +434,10 @@ def create_connect_args(self, url):
),
url.database,
)
dist = pkg_resources.get_distribution("sqlalchemy-spanner")
return (
[match.group("instance"), match.group("database"), match.group("project")],
{},
{"user_agent": dist.project_name + "/" + dist.version},
)

@engine_to_connection
Expand Down
21 changes: 21 additions & 0 deletions test/test_suite.py
Expand Up @@ -18,6 +18,7 @@
import decimal
import operator
import os
import pkg_resources
import pytest
from unittest import mock

Expand Down Expand Up @@ -1525,3 +1526,23 @@ def test_interleave_on_delete_cascade(self):
with mock.patch("google.cloud.spanner_dbapi.cursor.Cursor.execute") as execute:
client.create(self._engine)
execute.assert_called_once_with(EXP_QUERY, [])


class UserAgentTest(fixtures.TestBase):
"""Check that SQLAlchemy dialect uses correct user agent."""

def setUp(self):
self._engine = create_engine(
"spanner:///projects/appdev-soda-spanner-staging/instances/"
"sqlalchemy-dialect-test/databases/compliance-test"
)
self._metadata = MetaData(bind=self._engine)

def test_user_agent(self):
dist = pkg_resources.get_distribution("sqlalchemy-spanner")

with self._engine.connect() as connection:
assert (
connection.connection.instance._client._client_info.user_agent
== dist.project_name + "/" + dist.version
)

0 comments on commit b5e1a21

Please sign in to comment.