Skip to content

Commit 7c8effd

Browse files
authored
SNOW-698806: add usage telemetry information (#362)
1 parent 4ff597d commit 7c8effd

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

DESCRIPTION.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Source code is also available at:
99

1010
# Release Notes
1111

12+
- v1.4.5(Dec 7, 2022)
13+
14+
- Updated the application name of driver connection `SnowflakeConnection` to `SnowflakeSQLAlchemy`.
15+
1216
- v1.4.4(Nov 16, 2022)
1317

1418
- Fixed a bug that percent signs in a non-compiled statement should not be interpolated with emtpy sequence when executed.

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ universal = 1
33

44
[metadata]
55
name = snowflake-sqlalchemy
6-
version = 1.4.4
6+
version = 1.4.5
77
description = Snowflake SQLAlchemy Dialect
88
long_description = file: DESCRIPTION.md
99
long_description_content_type = text/markdown
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#
2+
# Copyright (c) 2012-2022 Snowflake Computing Inc. All rights reserved.
3+
#
4+
import pkg_resources
5+
6+
# parameters needed for usage tracking
7+
PARAM_APPLICATION = "application"
8+
PARAM_INTERNAL_APPLICATION_NAME = "internal_application_name"
9+
PARAM_INTERNAL_APPLICATION_VERSION = "internal_application_version"
10+
11+
APPLICATION_NAME = "SnowflakeSQLAlchemy"
12+
SNOWFLAKE_SQLALCHEMY_VERSION = pkg_resources.get_distribution(
13+
"snowflake-sqlalchemy"
14+
).version

src/snowflake/sqlalchemy/snowdialect.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
_CUSTOM_Float,
6262
_CUSTOM_Time,
6363
)
64+
from .util import _update_connection_application_name
6465

6566
colspecs = {
6667
Date: _CUSTOM_Date,
@@ -834,6 +835,12 @@ def get_table_comment(self, connection, table_name, schema=None, **kw):
834835
else None
835836
}
836837

838+
def connect(self, *cargs, **cparams):
839+
snowflake_conn = super().connect(
840+
*cargs, **_update_connection_application_name(**cparams)
841+
)
842+
return snowflake_conn
843+
837844

838845
@sa_vnt.listens_for(Table, "before_create")
839846
def check_table(table, connection, _ddl_runner, **kw):

src/snowflake/sqlalchemy/util.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@
33
#
44

55
import re
6+
from typing import Any
67
from urllib.parse import quote_plus
78

89
from sqlalchemy import exc
910

1011
from snowflake.connector.compat import IS_STR
1112
from snowflake.connector.connection import SnowflakeConnection
1213

14+
from ._constants import (
15+
APPLICATION_NAME,
16+
PARAM_APPLICATION,
17+
PARAM_INTERNAL_APPLICATION_NAME,
18+
PARAM_INTERNAL_APPLICATION_VERSION,
19+
SNOWFLAKE_SQLALCHEMY_VERSION,
20+
)
21+
1322

1423
def _rfc_1738_quote(text):
1524
return re.sub(r"[:@/]", lambda m: "%%%X" % ord(m.group(0)), text)
@@ -85,3 +94,13 @@ def _set_connection_interpolate_empty_sequences(
8594
else:
8695
# _dbapi_connection is a raw SnowflakeConnection
8796
dbapi_connection._interpolate_empty_sequences = flag
97+
98+
99+
def _update_connection_application_name(**conn_kwargs: Any) -> Any:
100+
if PARAM_APPLICATION not in conn_kwargs:
101+
conn_kwargs[PARAM_APPLICATION] = APPLICATION_NAME
102+
if PARAM_INTERNAL_APPLICATION_NAME not in conn_kwargs:
103+
conn_kwargs[PARAM_INTERNAL_APPLICATION_NAME] = APPLICATION_NAME
104+
if PARAM_INTERNAL_APPLICATION_VERSION not in conn_kwargs:
105+
conn_kwargs[PARAM_INTERNAL_APPLICATION_VERSION] = SNOWFLAKE_SQLALCHEMY_VERSION
106+
return conn_kwargs

tests/test_core.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838

3939
from snowflake.connector import Error, ProgrammingError, connect
4040
from snowflake.sqlalchemy import URL, MergeInto, dialect
41+
from snowflake.sqlalchemy._constants import (
42+
APPLICATION_NAME,
43+
SNOWFLAKE_SQLALCHEMY_VERSION,
44+
)
4145
from snowflake.sqlalchemy.snowdialect import SnowflakeDialect
4246

4347
from .conftest import create_engine_with_future_flag as create_engine
@@ -98,6 +102,15 @@ def _create_users_addresses_tables_without_sequence(engine_testaccount, metadata
98102
def verify_engine_connection(engine):
99103
with engine.connect() as conn:
100104
results = conn.execute(text("select current_version()")).fetchone()
105+
assert conn.connection.driver_connection.application == APPLICATION_NAME
106+
assert (
107+
conn.connection.driver_connection._internal_application_name
108+
== APPLICATION_NAME
109+
)
110+
assert (
111+
conn.connection.driver_connection._internal_application_version
112+
== SNOWFLAKE_SQLALCHEMY_VERSION
113+
)
101114
assert results is not None
102115

103116

0 commit comments

Comments
 (0)