|
35 | 35 | from snowflake.connector.errors import Error, ForbiddenError |
36 | 36 | from snowflake.connector.network import APPLICATION_SNOWSQL, ReauthenticationRequest |
37 | 37 | from snowflake.connector.sqlstate import SQLSTATE_FEATURE_NOT_SUPPORTED |
| 38 | +from snowflake.connector.telemetry import TelemetryField |
38 | 39 |
|
39 | 40 | try: # pragma: no cover |
40 | 41 | from parameters import CONNECTION_PARAMETERS_ADMIN |
@@ -1107,3 +1108,80 @@ def test_ocsp_cache_working(conn_cnx): |
1107 | 1108 | with conn_cnx() as cnx: |
1108 | 1109 | assert cnx |
1109 | 1110 | assert OCSP_CACHE.telemetry["hit"] + OCSP_CACHE.telemetry["miss"] > original_count |
| 1111 | + |
| 1112 | + |
| 1113 | +@pytest.mark.skipolddriver |
| 1114 | +def test_imported_packages_telemetry(conn_cnx, capture_sf_telemetry, db_parameters): |
| 1115 | + # these imports are not used but for testing |
| 1116 | + import html.parser # noqa: F401 |
| 1117 | + import json # noqa: F401 |
| 1118 | + import multiprocessing as mp # noqa: F401 |
| 1119 | + from datetime import date # noqa: F401 |
| 1120 | + from math import sqrt # noqa: F401 |
| 1121 | + |
| 1122 | + def check_packages(message: str, expected_packages: list[str]) -> bool: |
| 1123 | + return ( |
| 1124 | + all([package in message for package in expected_packages]) |
| 1125 | + and "__main__" not in message |
| 1126 | + ) |
| 1127 | + |
| 1128 | + packages = [ |
| 1129 | + "pytest", |
| 1130 | + "unittest", |
| 1131 | + "json", |
| 1132 | + "multiprocessing", |
| 1133 | + "html", |
| 1134 | + "datetime", |
| 1135 | + "math", |
| 1136 | + ] |
| 1137 | + |
| 1138 | + with conn_cnx() as conn, capture_sf_telemetry.patch_connection( |
| 1139 | + conn, False |
| 1140 | + ) as telemetry_test: |
| 1141 | + conn._log_telemetry_imported_packages() |
| 1142 | + assert len(telemetry_test.records) > 0 |
| 1143 | + assert any( |
| 1144 | + [ |
| 1145 | + t.message[TelemetryField.KEY_TYPE.value] |
| 1146 | + == TelemetryField.IMPORTED_PACKAGES.value |
| 1147 | + and CLIENT_NAME == t.message[TelemetryField.KEY_SOURCE.value] |
| 1148 | + and check_packages(t.message["value"], packages) |
| 1149 | + for t in telemetry_test.records |
| 1150 | + ] |
| 1151 | + ) |
| 1152 | + |
| 1153 | + # test different application |
| 1154 | + new_application_name = "PythonSnowpark" |
| 1155 | + config = { |
| 1156 | + "user": db_parameters["user"], |
| 1157 | + "password": db_parameters["password"], |
| 1158 | + "host": db_parameters["host"], |
| 1159 | + "port": db_parameters["port"], |
| 1160 | + "account": db_parameters["account"], |
| 1161 | + "schema": db_parameters["schema"], |
| 1162 | + "database": db_parameters["database"], |
| 1163 | + "protocol": db_parameters["protocol"], |
| 1164 | + "timezone": "UTC", |
| 1165 | + "application": new_application_name, |
| 1166 | + } |
| 1167 | + with snowflake.connector.connect( |
| 1168 | + **config |
| 1169 | + ) as conn, capture_sf_telemetry.patch_connection(conn, False) as telemetry_test: |
| 1170 | + conn._log_telemetry_imported_packages() |
| 1171 | + assert len(telemetry_test.records) > 0 |
| 1172 | + assert any( |
| 1173 | + [ |
| 1174 | + t.message[TelemetryField.KEY_TYPE.value] |
| 1175 | + == TelemetryField.IMPORTED_PACKAGES.value |
| 1176 | + and new_application_name == t.message[TelemetryField.KEY_SOURCE.value] |
| 1177 | + for t in telemetry_test.records |
| 1178 | + ] |
| 1179 | + ) |
| 1180 | + |
| 1181 | + # test opt out |
| 1182 | + config["log_imported_packages_in_telemetry"] = False |
| 1183 | + with snowflake.connector.connect( |
| 1184 | + **config |
| 1185 | + ) as conn, capture_sf_telemetry.patch_connection(conn, False) as telemetry_test: |
| 1186 | + conn._log_telemetry_imported_packages() |
| 1187 | + assert len(telemetry_test.records) == 0 |
0 commit comments