snowflake-connector-python 3.16.0__cp313-cp313-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (246) hide show
  1. snowflake/connector/__init__.py +96 -0
  2. snowflake/connector/_query_context_cache.py +276 -0
  3. snowflake/connector/_sql_util.py +48 -0
  4. snowflake/connector/_utils.py +70 -0
  5. snowflake/connector/arrow_context.py +203 -0
  6. snowflake/connector/auth/__init__.py +53 -0
  7. snowflake/connector/auth/_auth.py +586 -0
  8. snowflake/connector/auth/_http_server.py +220 -0
  9. snowflake/connector/auth/_oauth_base.py +400 -0
  10. snowflake/connector/auth/by_plugin.py +219 -0
  11. snowflake/connector/auth/default.py +36 -0
  12. snowflake/connector/auth/idtoken.py +72 -0
  13. snowflake/connector/auth/keypair.py +222 -0
  14. snowflake/connector/auth/no_auth.py +39 -0
  15. snowflake/connector/auth/oauth.py +49 -0
  16. snowflake/connector/auth/oauth_code.py +479 -0
  17. snowflake/connector/auth/oauth_credentials.py +66 -0
  18. snowflake/connector/auth/okta.py +335 -0
  19. snowflake/connector/auth/pat.py +39 -0
  20. snowflake/connector/auth/usrpwdmfa.py +69 -0
  21. snowflake/connector/auth/webbrowser.py +502 -0
  22. snowflake/connector/auth/workload_identity.py +94 -0
  23. snowflake/connector/azure_storage_client.py +276 -0
  24. snowflake/connector/backoff_policies.py +141 -0
  25. snowflake/connector/bind_upload_agent.py +89 -0
  26. snowflake/connector/cache.py +696 -0
  27. snowflake/connector/compat.py +127 -0
  28. snowflake/connector/config_manager.py +496 -0
  29. snowflake/connector/connection.py +2273 -0
  30. snowflake/connector/connection_diagnostic.py +776 -0
  31. snowflake/connector/constants.py +442 -0
  32. snowflake/connector/converter.py +785 -0
  33. snowflake/connector/converter_issue23517.py +87 -0
  34. snowflake/connector/converter_null.py +14 -0
  35. snowflake/connector/converter_snowsql.py +205 -0
  36. snowflake/connector/cursor.py +1951 -0
  37. snowflake/connector/dbapi.py +53 -0
  38. snowflake/connector/description.py +19 -0
  39. snowflake/connector/direct_file_operation_utils.py +88 -0
  40. snowflake/connector/encryption_util.py +220 -0
  41. snowflake/connector/errorcode.py +91 -0
  42. snowflake/connector/errors.py +615 -0
  43. snowflake/connector/externals_utils/__init__.py +0 -0
  44. snowflake/connector/externals_utils/externals_setup.py +27 -0
  45. snowflake/connector/feature.py +4 -0
  46. snowflake/connector/file_compression_type.py +118 -0
  47. snowflake/connector/file_lock.py +72 -0
  48. snowflake/connector/file_transfer_agent.py +1215 -0
  49. snowflake/connector/file_util.py +153 -0
  50. snowflake/connector/gcs_storage_client.py +474 -0
  51. snowflake/connector/gzip_decoder.py +85 -0
  52. snowflake/connector/local_storage_client.py +90 -0
  53. snowflake/connector/log_configuration.py +60 -0
  54. snowflake/connector/logging_utils/__init__.py +0 -0
  55. snowflake/connector/logging_utils/filters.py +72 -0
  56. snowflake/connector/nanoarrow_arrow_iterator.cp313-win_amd64.pyd +0 -0
  57. snowflake/connector/nanoarrow_cpp/ArrowIterator/ArrayConverter.cpp +60 -0
  58. snowflake/connector/nanoarrow_cpp/ArrowIterator/ArrayConverter.hpp +29 -0
  59. snowflake/connector/nanoarrow_cpp/ArrowIterator/BinaryConverter.cpp +19 -0
  60. snowflake/connector/nanoarrow_cpp/ArrowIterator/BinaryConverter.hpp +26 -0
  61. snowflake/connector/nanoarrow_cpp/ArrowIterator/BooleanConverter.cpp +21 -0
  62. snowflake/connector/nanoarrow_cpp/ArrowIterator/BooleanConverter.hpp +23 -0
  63. snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowChunkIterator.cpp +557 -0
  64. snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowChunkIterator.hpp +98 -0
  65. snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowIterator.cpp +125 -0
  66. snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowIterator.hpp +115 -0
  67. snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowTableIterator.cpp +1005 -0
  68. snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowTableIterator.hpp +127 -0
  69. snowflake/connector/nanoarrow_cpp/ArrowIterator/DateConverter.cpp +47 -0
  70. snowflake/connector/nanoarrow_cpp/ArrowIterator/DateConverter.hpp +46 -0
  71. snowflake/connector/nanoarrow_cpp/ArrowIterator/DecFloatConverter.cpp +83 -0
  72. snowflake/connector/nanoarrow_cpp/ArrowIterator/DecFloatConverter.hpp +35 -0
  73. snowflake/connector/nanoarrow_cpp/ArrowIterator/DecimalConverter.cpp +97 -0
  74. snowflake/connector/nanoarrow_cpp/ArrowIterator/DecimalConverter.hpp +72 -0
  75. snowflake/connector/nanoarrow_cpp/ArrowIterator/FixedSizeListConverter.cpp +73 -0
  76. snowflake/connector/nanoarrow_cpp/ArrowIterator/FixedSizeListConverter.hpp +28 -0
  77. snowflake/connector/nanoarrow_cpp/ArrowIterator/FloatConverter.cpp +30 -0
  78. snowflake/connector/nanoarrow_cpp/ArrowIterator/FloatConverter.hpp +35 -0
  79. snowflake/connector/nanoarrow_cpp/ArrowIterator/IColumnConverter.hpp +17 -0
  80. snowflake/connector/nanoarrow_cpp/ArrowIterator/IntConverter.cpp +23 -0
  81. snowflake/connector/nanoarrow_cpp/ArrowIterator/IntConverter.hpp +45 -0
  82. snowflake/connector/nanoarrow_cpp/ArrowIterator/IntervalConverter.cpp +71 -0
  83. snowflake/connector/nanoarrow_cpp/ArrowIterator/IntervalConverter.hpp +56 -0
  84. snowflake/connector/nanoarrow_cpp/ArrowIterator/LICENSE.txt +209 -0
  85. snowflake/connector/nanoarrow_cpp/ArrowIterator/MapConverter.cpp +75 -0
  86. snowflake/connector/nanoarrow_cpp/ArrowIterator/MapConverter.hpp +30 -0
  87. snowflake/connector/nanoarrow_cpp/ArrowIterator/ObjectConverter.cpp +46 -0
  88. snowflake/connector/nanoarrow_cpp/ArrowIterator/ObjectConverter.hpp +29 -0
  89. snowflake/connector/nanoarrow_cpp/ArrowIterator/Python/Common.cpp +8 -0
  90. snowflake/connector/nanoarrow_cpp/ArrowIterator/Python/Common.hpp +95 -0
  91. snowflake/connector/nanoarrow_cpp/ArrowIterator/Python/Helpers.cpp +57 -0
  92. snowflake/connector/nanoarrow_cpp/ArrowIterator/Python/Helpers.hpp +36 -0
  93. snowflake/connector/nanoarrow_cpp/ArrowIterator/SnowflakeType.cpp +34 -0
  94. snowflake/connector/nanoarrow_cpp/ArrowIterator/SnowflakeType.hpp +48 -0
  95. snowflake/connector/nanoarrow_cpp/ArrowIterator/StringConverter.cpp +19 -0
  96. snowflake/connector/nanoarrow_cpp/ArrowIterator/StringConverter.hpp +26 -0
  97. snowflake/connector/nanoarrow_cpp/ArrowIterator/TimeConverter.cpp +36 -0
  98. snowflake/connector/nanoarrow_cpp/ArrowIterator/TimeConverter.hpp +31 -0
  99. snowflake/connector/nanoarrow_cpp/ArrowIterator/TimeStampConverter.cpp +346 -0
  100. snowflake/connector/nanoarrow_cpp/ArrowIterator/TimeStampConverter.hpp +145 -0
  101. snowflake/connector/nanoarrow_cpp/ArrowIterator/Util/macros.hpp +14 -0
  102. snowflake/connector/nanoarrow_cpp/ArrowIterator/Util/time.cpp +65 -0
  103. snowflake/connector/nanoarrow_cpp/ArrowIterator/Util/time.hpp +68 -0
  104. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_accessors.h +101 -0
  105. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_alloc.h +127 -0
  106. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_assert.h +45 -0
  107. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_builder.h +1908 -0
  108. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_emitter.h +215 -0
  109. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_endian.h +125 -0
  110. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_epilogue.h +7 -0
  111. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_flatbuffers.h +55 -0
  112. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_identifier.h +148 -0
  113. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_iov.h +31 -0
  114. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_prologue.h +8 -0
  115. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_refmap.h +144 -0
  116. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_rtconfig.h +162 -0
  117. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_types.h +97 -0
  118. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/flatcc_verifier.h +239 -0
  119. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/flatcc_portable.h +14 -0
  120. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/paligned_alloc.h +210 -0
  121. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pattributes.h +84 -0
  122. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pdiagnostic.h +84 -0
  123. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pdiagnostic_pop.h +20 -0
  124. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pdiagnostic_push.h +51 -0
  125. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pendian.h +206 -0
  126. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pendian_detect.h +118 -0
  127. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pinline.h +19 -0
  128. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pinttypes.h +52 -0
  129. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/portable.h +2 -0
  130. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/portable_basic.h +25 -0
  131. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pstatic_assert.h +67 -0
  132. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pstdalign.h +162 -0
  133. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pstdint.h +898 -0
  134. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/punaligned.h +190 -0
  135. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pversion.h +6 -0
  136. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc/portable/pwarnings.h +52 -0
  137. snowflake/connector/nanoarrow_cpp/ArrowIterator/flatcc.c +3204 -0
  138. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow.c +3217 -0
  139. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow.h +3618 -0
  140. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow.hpp +379 -0
  141. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow_arrow_iterator.pyx +256 -0
  142. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow_device.c +512 -0
  143. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow_device.h +350 -0
  144. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow_ipc.c +33273 -0
  145. snowflake/connector/nanoarrow_cpp/ArrowIterator/nanoarrow_ipc.h +397 -0
  146. snowflake/connector/nanoarrow_cpp/Logging/logging.cpp +100 -0
  147. snowflake/connector/nanoarrow_cpp/Logging/logging.hpp +49 -0
  148. snowflake/connector/network.py +1297 -0
  149. snowflake/connector/ocsp_asn1crypto.py +447 -0
  150. snowflake/connector/ocsp_snowflake.py +1883 -0
  151. snowflake/connector/options.py +131 -0
  152. snowflake/connector/pandas_tools.py +732 -0
  153. snowflake/connector/proxy.py +43 -0
  154. snowflake/connector/py.typed +0 -0
  155. snowflake/connector/result_batch.py +786 -0
  156. snowflake/connector/result_set.py +319 -0
  157. snowflake/connector/s3_storage_client.py +605 -0
  158. snowflake/connector/secret_detector.py +181 -0
  159. snowflake/connector/sf_dirs.py +64 -0
  160. snowflake/connector/sfbinaryformat.py +35 -0
  161. snowflake/connector/sfdatetime.py +345 -0
  162. snowflake/connector/snow_logging.py +144 -0
  163. snowflake/connector/sqlstate.py +9 -0
  164. snowflake/connector/ssd_internal_keys.py +30 -0
  165. snowflake/connector/ssl_wrap_socket.py +134 -0
  166. snowflake/connector/storage_client.py +478 -0
  167. snowflake/connector/telemetry.py +249 -0
  168. snowflake/connector/telemetry_oob.py +543 -0
  169. snowflake/connector/test_util.py +30 -0
  170. snowflake/connector/time_util.py +159 -0
  171. snowflake/connector/token_cache.py +402 -0
  172. snowflake/connector/tool/__init__.py +0 -0
  173. snowflake/connector/tool/dump_certs.py +57 -0
  174. snowflake/connector/tool/dump_ocsp_response.py +139 -0
  175. snowflake/connector/tool/dump_ocsp_response_cache.py +194 -0
  176. snowflake/connector/tool/probe_connection.py +69 -0
  177. snowflake/connector/url_util.py +49 -0
  178. snowflake/connector/util_text.py +301 -0
  179. snowflake/connector/vendored/__init__.py +3 -0
  180. snowflake/connector/vendored/requests/LICENSE +175 -0
  181. snowflake/connector/vendored/requests/__init__.py +169 -0
  182. snowflake/connector/vendored/requests/__version__.py +14 -0
  183. snowflake/connector/vendored/requests/_internal_utils.py +50 -0
  184. snowflake/connector/vendored/requests/adapters.py +537 -0
  185. snowflake/connector/vendored/requests/api.py +157 -0
  186. snowflake/connector/vendored/requests/auth.py +315 -0
  187. snowflake/connector/vendored/requests/certs.py +17 -0
  188. snowflake/connector/vendored/requests/compat.py +79 -0
  189. snowflake/connector/vendored/requests/cookies.py +561 -0
  190. snowflake/connector/vendored/requests/exceptions.py +140 -0
  191. snowflake/connector/vendored/requests/help.py +134 -0
  192. snowflake/connector/vendored/requests/hooks.py +33 -0
  193. snowflake/connector/vendored/requests/models.py +1033 -0
  194. snowflake/connector/vendored/requests/sessions.py +833 -0
  195. snowflake/connector/vendored/requests/status_codes.py +128 -0
  196. snowflake/connector/vendored/requests/structures.py +99 -0
  197. snowflake/connector/vendored/requests/utils.py +1093 -0
  198. snowflake/connector/vendored/urllib3/LICENSE.txt +21 -0
  199. snowflake/connector/vendored/urllib3/__init__.py +85 -0
  200. snowflake/connector/vendored/urllib3/_collections.py +355 -0
  201. snowflake/connector/vendored/urllib3/_version.py +2 -0
  202. snowflake/connector/vendored/urllib3/connection.py +572 -0
  203. snowflake/connector/vendored/urllib3/connectionpool.py +1137 -0
  204. snowflake/connector/vendored/urllib3/contrib/__init__.py +0 -0
  205. snowflake/connector/vendored/urllib3/contrib/_appengine_environ.py +36 -0
  206. snowflake/connector/vendored/urllib3/contrib/_securetransport/__init__.py +0 -0
  207. snowflake/connector/vendored/urllib3/contrib/_securetransport/bindings.py +519 -0
  208. snowflake/connector/vendored/urllib3/contrib/_securetransport/low_level.py +397 -0
  209. snowflake/connector/vendored/urllib3/contrib/appengine.py +314 -0
  210. snowflake/connector/vendored/urllib3/contrib/ntlmpool.py +130 -0
  211. snowflake/connector/vendored/urllib3/contrib/pyopenssl.py +509 -0
  212. snowflake/connector/vendored/urllib3/contrib/securetransport.py +920 -0
  213. snowflake/connector/vendored/urllib3/contrib/socks.py +216 -0
  214. snowflake/connector/vendored/urllib3/exceptions.py +323 -0
  215. snowflake/connector/vendored/urllib3/fields.py +274 -0
  216. snowflake/connector/vendored/urllib3/filepost.py +98 -0
  217. snowflake/connector/vendored/urllib3/packages/__init__.py +0 -0
  218. snowflake/connector/vendored/urllib3/packages/backports/__init__.py +0 -0
  219. snowflake/connector/vendored/urllib3/packages/backports/makefile.py +51 -0
  220. snowflake/connector/vendored/urllib3/packages/backports/weakref_finalize.py +155 -0
  221. snowflake/connector/vendored/urllib3/packages/six.py +1076 -0
  222. snowflake/connector/vendored/urllib3/poolmanager.py +540 -0
  223. snowflake/connector/vendored/urllib3/request.py +191 -0
  224. snowflake/connector/vendored/urllib3/response.py +885 -0
  225. snowflake/connector/vendored/urllib3/util/__init__.py +49 -0
  226. snowflake/connector/vendored/urllib3/util/connection.py +156 -0
  227. snowflake/connector/vendored/urllib3/util/proxy.py +57 -0
  228. snowflake/connector/vendored/urllib3/util/queue.py +22 -0
  229. snowflake/connector/vendored/urllib3/util/request.py +146 -0
  230. snowflake/connector/vendored/urllib3/util/response.py +107 -0
  231. snowflake/connector/vendored/urllib3/util/retry.py +620 -0
  232. snowflake/connector/vendored/urllib3/util/ssl_.py +495 -0
  233. snowflake/connector/vendored/urllib3/util/ssl_match_hostname.py +159 -0
  234. snowflake/connector/vendored/urllib3/util/ssltransport.py +221 -0
  235. snowflake/connector/vendored/urllib3/util/timeout.py +271 -0
  236. snowflake/connector/vendored/urllib3/util/url.py +435 -0
  237. snowflake/connector/vendored/urllib3/util/wait.py +152 -0
  238. snowflake/connector/version.py +3 -0
  239. snowflake/connector/wif_util.py +407 -0
  240. snowflake_connector_python-3.16.0.dist-info/METADATA +1475 -0
  241. snowflake_connector_python-3.16.0.dist-info/RECORD +246 -0
  242. snowflake_connector_python-3.16.0.dist-info/WHEEL +5 -0
  243. snowflake_connector_python-3.16.0.dist-info/entry_points.txt +4 -0
  244. snowflake_connector_python-3.16.0.dist-info/licenses/LICENSE.txt +202 -0
  245. snowflake_connector_python-3.16.0.dist-info/licenses/NOTICE +8 -0
  246. snowflake_connector_python-3.16.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,249 @@
1
+ #!/usr/bin/env python
2
+ from __future__ import annotations
3
+
4
+ import logging
5
+ from enum import Enum, unique
6
+ from threading import Lock
7
+ from typing import TYPE_CHECKING, Any
8
+
9
+ from .description import CLIENT_NAME, SNOWFLAKE_CONNECTOR_VERSION
10
+ from .secret_detector import SecretDetector
11
+ from .test_util import ENABLE_TELEMETRY_LOG, rt_plain_logger
12
+
13
+ if TYPE_CHECKING:
14
+ from .connection import SnowflakeConnection
15
+ from .network import SnowflakeRestful
16
+
17
+ logger = logging.getLogger(__name__)
18
+
19
+
20
+ @unique
21
+ class TelemetryField(Enum):
22
+ # Fields which can be logged to telemetry
23
+ TIME_CONSUME_FIRST_RESULT = "client_time_consume_first_result"
24
+ TIME_CONSUME_LAST_RESULT = "client_time_consume_last_result"
25
+ TIME_DOWNLOADING_CHUNKS = "client_time_downloading_chunks"
26
+ TIME_PARSING_CHUNKS = "client_time_parsing_chunks"
27
+ SQL_EXCEPTION = "client_sql_exception"
28
+ GET_PARTITIONS_USED = "client_get_partitions_used"
29
+ EMPTY_SEQ_INTERPOLATION = "client_pyformat_empty_seq_interpolation"
30
+ # fetch_pandas_* usage
31
+ PANDAS_FETCH_ALL = "client_fetch_pandas_all"
32
+ PANDAS_FETCH_BATCHES = "client_fetch_pandas_batches"
33
+ # fetch_arrow_* usage
34
+ ARROW_FETCH_ALL = "client_fetch_arrow_all"
35
+ ARROW_FETCH_BATCHES = "client_fetch_arrow_batches"
36
+ # write_pandas usage
37
+ PANDAS_WRITE = "client_write_pandas"
38
+ # imported packages along with client
39
+ IMPORTED_PACKAGES = "client_imported_packages"
40
+ # multi-statement usage
41
+ MULTI_STATEMENT = "client_multi_statement_query"
42
+ # Keys for telemetry data sent through either in-band or out-of-band telemetry
43
+ KEY_TYPE = "type"
44
+ KEY_SOURCE = "source"
45
+ KEY_SFQID = "query_id"
46
+ KEY_SQLSTATE = "sql_state"
47
+ KEY_DRIVER_TYPE = "driver_type"
48
+ KEY_DRIVER_VERSION = "driver_version"
49
+ KEY_REASON = "reason"
50
+ KEY_VALUE = "value"
51
+ KEY_EXCEPTION = "exception"
52
+ # Reserved UpperCamelName keys
53
+ KEY_ERROR_NUMBER = "ErrorNumber"
54
+ KEY_ERROR_MESSAGE = "ErrorMessage"
55
+ KEY_STACKTRACE = "Stacktrace"
56
+ # OOB camelName keys
57
+ KEY_OOB_DRIVER = "driver"
58
+ KEY_OOB_VERSION = "version"
59
+ KEY_OOB_TELEMETRY_SERVER_DEPLOYMENT = "telemetryServerDeployment"
60
+ KEY_OOB_CONNECTION_STRING = "connectionString"
61
+ KEY_OOB_EXCEPTION_MESSAGE = "exceptionMessage"
62
+ KEY_OOB_ERROR_MESSAGE = "errorMessage"
63
+ KEY_OOB_EXCEPTION_STACK_TRACE = "exceptionStackTrace"
64
+ KEY_OOB_EVENT_TYPE = "eventType"
65
+ KEY_OOB_ERROR_CODE = "errorCode"
66
+ KEY_OOB_SQL_STATE = "sqlState"
67
+ KEY_OOB_REQUEST = "request"
68
+ KEY_OOB_RESPONSE = "response"
69
+ KEY_OOB_RESPONSE_STATUS_LINE = "responseStatusLine"
70
+ KEY_OOB_RESPONSE_STATUS_CODE = "responseStatusCode"
71
+ KEY_OOB_RETRY_TIMEOUT = "retryTimeout"
72
+ KEY_OOB_RETRY_COUNT = "retryCount"
73
+ KEY_OOB_EVENT_SUB_TYPE = "eventSubType"
74
+ KEY_OOB_SFC_PEER_HOST = "sfcPeerHost"
75
+ KEY_OOB_CERT_ID = "certId"
76
+ KEY_OOB_OCSP_REQUEST_BASE64 = "ocspRequestBase64"
77
+ KEY_OOB_OCSP_RESPONDER_URL = "ocspResponderURL"
78
+ KEY_OOB_INSECURE_MODE = "insecureMode"
79
+ KEY_OOB_FAIL_OPEN = "failOpen"
80
+ KEY_OOB_CACHE_ENABLED = "cacheEnabled"
81
+ KEY_OOB_CACHE_HIT = "cacheHit"
82
+
83
+
84
+ class TelemetryData:
85
+ """An instance of telemetry data which can be sent to the server."""
86
+
87
+ TRUE = 1
88
+ FALSE = 0
89
+
90
+ def __init__(self, message, timestamp) -> None:
91
+ self.message = message
92
+ self.timestamp = timestamp
93
+
94
+ @classmethod
95
+ def from_telemetry_data_dict(
96
+ cls,
97
+ from_dict: dict,
98
+ timestamp: int,
99
+ connection: SnowflakeConnection | None = None,
100
+ is_oob_telemetry: bool = False,
101
+ ):
102
+ """
103
+ Generate telemetry data with driver info from given dict and timestamp.
104
+ It takes an optional connection object to read data from.
105
+ It also takes a boolean is_oob_telemetry to indicate whether it's for out-of-band telemetry, as
106
+ naming of keys for driver and version is different from the ones of in-band telemetry.
107
+ """
108
+ return cls(
109
+ generate_telemetry_data_dict(
110
+ from_dict=(from_dict or {}),
111
+ connection=connection,
112
+ is_oob_telemetry=is_oob_telemetry,
113
+ ),
114
+ timestamp,
115
+ )
116
+
117
+ def to_dict(self) -> dict[str, Any]:
118
+ return {"message": self.message, "timestamp": str(self.timestamp)}
119
+
120
+ def __repr__(self) -> str:
121
+ return str(self.to_dict())
122
+
123
+
124
+ class TelemetryClient:
125
+ """Client to enqueue and send metrics to the telemetry endpoint in batch."""
126
+
127
+ SF_PATH_TELEMETRY = "/telemetry/send"
128
+ DEFAULT_FORCE_FLUSH_SIZE = 100
129
+
130
+ def __init__(self, rest: SnowflakeRestful, flush_size=None) -> None:
131
+ self._rest: SnowflakeRestful | None = rest
132
+ self._log_batch = []
133
+ self._flush_size = flush_size or TelemetryClient.DEFAULT_FORCE_FLUSH_SIZE
134
+ self._lock = Lock()
135
+ self._enabled = True
136
+
137
+ def add_log_to_batch(self, telemetry_data: TelemetryData) -> None:
138
+ if self.is_closed:
139
+ raise Exception("Attempted to add log when TelemetryClient is closed")
140
+ elif not self._enabled:
141
+ logger.debug("TelemetryClient disabled. Ignoring log.")
142
+ return
143
+
144
+ with self._lock:
145
+ self._log_batch.append(telemetry_data)
146
+
147
+ if len(self._log_batch) >= self._flush_size:
148
+ self.send_batch()
149
+
150
+ def try_add_log_to_batch(self, telemetry_data: TelemetryData) -> None:
151
+ try:
152
+ self.add_log_to_batch(telemetry_data)
153
+ except Exception:
154
+ logger.warning("Failed to add log to telemetry.", exc_info=True)
155
+
156
+ def send_batch(self) -> None:
157
+ if self.is_closed:
158
+ raise Exception("Attempted to send batch when TelemetryClient is closed")
159
+ elif not self._enabled:
160
+ logger.debug("TelemetryClient disabled. Not sending logs.")
161
+ return
162
+
163
+ with self._lock:
164
+ to_send = self._log_batch
165
+ self._log_batch = []
166
+
167
+ if not to_send:
168
+ logger.debug("Nothing to send to telemetry.")
169
+ return
170
+
171
+ body = {"logs": [x.to_dict() for x in to_send]}
172
+ logger.debug(
173
+ "Sending %d logs to telemetry. Data is %s.",
174
+ len(body),
175
+ SecretDetector.mask_secrets(str(body))[1],
176
+ )
177
+ if ENABLE_TELEMETRY_LOG:
178
+ # This logger guarantees the payload won't be masked. Testing purpose.
179
+ rt_plain_logger.debug(f"Inband telemetry data being sent is {body}")
180
+ try:
181
+ ret = self._rest.request(
182
+ TelemetryClient.SF_PATH_TELEMETRY,
183
+ body=body,
184
+ method="post",
185
+ client=None,
186
+ timeout=5,
187
+ )
188
+ if not ret["success"]:
189
+ logger.info(
190
+ "Non-success response from telemetry server: %s. "
191
+ "Disabling telemetry.",
192
+ str(ret),
193
+ )
194
+ self._enabled = False
195
+ else:
196
+ logger.debug("Successfully uploading metrics to telemetry.")
197
+ except Exception:
198
+ self._enabled = False
199
+ logger.debug("Failed to upload metrics to telemetry.", exc_info=True)
200
+
201
+ @property
202
+ def is_closed(self) -> bool:
203
+ return self._rest is None
204
+
205
+ def close(self, send_on_close: bool = True) -> None:
206
+ if not self.is_closed:
207
+ logger.debug("Closing telemetry client.")
208
+ if send_on_close:
209
+ self.send_batch()
210
+ self._rest = None
211
+
212
+ def disable(self) -> None:
213
+ self._enabled = False
214
+
215
+ def is_enabled(self):
216
+ return self._enabled
217
+
218
+ def buffer_size(self):
219
+ return len(self._log_batch)
220
+
221
+
222
+ def generate_telemetry_data_dict(
223
+ from_dict: dict | None = None,
224
+ connection: SnowflakeConnection | None = None,
225
+ is_oob_telemetry: bool = False,
226
+ ) -> dict[str, Any]:
227
+ """
228
+ Generate telemetry data with driver info.
229
+ The method also takes an optional dict to update from and optional connection object to read data from.
230
+ It also takes a boolean is_oob_telemetry to indicate whether it's for out-of-band telemetry, as
231
+ naming of keys for driver and version is different from the ones of in-band telemetry.
232
+ """
233
+ from_dict = from_dict or {}
234
+ return (
235
+ {
236
+ TelemetryField.KEY_DRIVER_TYPE.value: CLIENT_NAME,
237
+ TelemetryField.KEY_DRIVER_VERSION.value: SNOWFLAKE_CONNECTOR_VERSION,
238
+ TelemetryField.KEY_SOURCE.value: (
239
+ connection.application if connection else CLIENT_NAME
240
+ ),
241
+ **from_dict,
242
+ }
243
+ if not is_oob_telemetry
244
+ else {
245
+ TelemetryField.KEY_OOB_DRIVER.value: CLIENT_NAME,
246
+ TelemetryField.KEY_OOB_VERSION.value: SNOWFLAKE_CONNECTOR_VERSION,
247
+ **from_dict,
248
+ }
249
+ )