Skip to content

Commit

Permalink
SNOW-698806: add usage telemetry information (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-aling authored Dec 7, 2022
1 parent 4ff597d commit 7c8effd
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
4 changes: 4 additions & 0 deletions DESCRIPTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Source code is also available at:

# Release Notes

- v1.4.5(Dec 7, 2022)

- Updated the application name of driver connection `SnowflakeConnection` to `SnowflakeSQLAlchemy`.

- v1.4.4(Nov 16, 2022)

- Fixed a bug that percent signs in a non-compiled statement should not be interpolated with emtpy sequence when executed.
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ universal = 1

[metadata]
name = snowflake-sqlalchemy
version = 1.4.4
version = 1.4.5
description = Snowflake SQLAlchemy Dialect
long_description = file: DESCRIPTION.md
long_description_content_type = text/markdown
Expand Down
14 changes: 14 additions & 0 deletions src/snowflake/sqlalchemy/_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#
# Copyright (c) 2012-2022 Snowflake Computing Inc. All rights reserved.
#
import pkg_resources

# parameters needed for usage tracking
PARAM_APPLICATION = "application"
PARAM_INTERNAL_APPLICATION_NAME = "internal_application_name"
PARAM_INTERNAL_APPLICATION_VERSION = "internal_application_version"

APPLICATION_NAME = "SnowflakeSQLAlchemy"
SNOWFLAKE_SQLALCHEMY_VERSION = pkg_resources.get_distribution(
"snowflake-sqlalchemy"
).version
7 changes: 7 additions & 0 deletions src/snowflake/sqlalchemy/snowdialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
_CUSTOM_Float,
_CUSTOM_Time,
)
from .util import _update_connection_application_name

colspecs = {
Date: _CUSTOM_Date,
Expand Down Expand Up @@ -834,6 +835,12 @@ def get_table_comment(self, connection, table_name, schema=None, **kw):
else None
}

def connect(self, *cargs, **cparams):
snowflake_conn = super().connect(
*cargs, **_update_connection_application_name(**cparams)
)
return snowflake_conn


@sa_vnt.listens_for(Table, "before_create")
def check_table(table, connection, _ddl_runner, **kw):
Expand Down
19 changes: 19 additions & 0 deletions src/snowflake/sqlalchemy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
#

import re
from typing import Any
from urllib.parse import quote_plus

from sqlalchemy import exc

from snowflake.connector.compat import IS_STR
from snowflake.connector.connection import SnowflakeConnection

from ._constants import (
APPLICATION_NAME,
PARAM_APPLICATION,
PARAM_INTERNAL_APPLICATION_NAME,
PARAM_INTERNAL_APPLICATION_VERSION,
SNOWFLAKE_SQLALCHEMY_VERSION,
)


def _rfc_1738_quote(text):
return re.sub(r"[:@/]", lambda m: "%%%X" % ord(m.group(0)), text)
Expand Down Expand Up @@ -85,3 +94,13 @@ def _set_connection_interpolate_empty_sequences(
else:
# _dbapi_connection is a raw SnowflakeConnection
dbapi_connection._interpolate_empty_sequences = flag


def _update_connection_application_name(**conn_kwargs: Any) -> Any:
if PARAM_APPLICATION not in conn_kwargs:
conn_kwargs[PARAM_APPLICATION] = APPLICATION_NAME
if PARAM_INTERNAL_APPLICATION_NAME not in conn_kwargs:
conn_kwargs[PARAM_INTERNAL_APPLICATION_NAME] = APPLICATION_NAME
if PARAM_INTERNAL_APPLICATION_VERSION not in conn_kwargs:
conn_kwargs[PARAM_INTERNAL_APPLICATION_VERSION] = SNOWFLAKE_SQLALCHEMY_VERSION
return conn_kwargs
13 changes: 13 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@

from snowflake.connector import Error, ProgrammingError, connect
from snowflake.sqlalchemy import URL, MergeInto, dialect
from snowflake.sqlalchemy._constants import (
APPLICATION_NAME,
SNOWFLAKE_SQLALCHEMY_VERSION,
)
from snowflake.sqlalchemy.snowdialect import SnowflakeDialect

from .conftest import create_engine_with_future_flag as create_engine
Expand Down Expand Up @@ -98,6 +102,15 @@ def _create_users_addresses_tables_without_sequence(engine_testaccount, metadata
def verify_engine_connection(engine):
with engine.connect() as conn:
results = conn.execute(text("select current_version()")).fetchone()
assert conn.connection.driver_connection.application == APPLICATION_NAME
assert (
conn.connection.driver_connection._internal_application_name
== APPLICATION_NAME
)
assert (
conn.connection.driver_connection._internal_application_version
== SNOWFLAKE_SQLALCHEMY_VERSION
)
assert results is not None


Expand Down

0 comments on commit 7c8effd

Please sign in to comment.