11#!/usr/bin/env python
22from __future__ import annotations
33
4+ import inspect
45import logging
56import os
67import re
1415from .time_util import get_time_millis
1516
1617if TYPE_CHECKING : # pragma: no cover
18+ from .aio ._connection import SnowflakeConnection as AsyncSnowflakeConnection
19+ from .aio ._cursor import SnowflakeCursor as AsyncSnowflakeCursor
1720 from .connection import SnowflakeConnection
1821 from .cursor import SnowflakeCursor
1922
@@ -35,8 +38,8 @@ def __init__(
3538 sfqid : str | None = None ,
3639 query : str | None = None ,
3740 done_format_msg : bool | None = None ,
38- connection : SnowflakeConnection | None = None ,
39- cursor : SnowflakeCursor | None = None ,
41+ connection : SnowflakeConnection | AsyncSnowflakeConnection | None = None ,
42+ cursor : SnowflakeCursor | AsyncSnowflakeCursor | None = None ,
4043 errtype : TelemetryField = TelemetryField .SQL_EXCEPTION ,
4144 send_telemetry : bool = True ,
4245 ) -> None :
@@ -145,11 +148,10 @@ def generate_telemetry_exception_data(
145148
146149 def send_exception_telemetry (
147150 self ,
148- connection : SnowflakeConnection | None ,
151+ connection : SnowflakeConnection | AsyncSnowflakeConnection | None ,
149152 telemetry_data : dict [str , Any ],
150153 ) -> None :
151154 """Send telemetry data by in-band telemetry if it is enabled, otherwise send through out-of-band telemetry."""
152-
153155 if (
154156 connection is not None
155157 and connection .telemetry_enabled
@@ -159,21 +161,34 @@ def send_exception_telemetry(
159161 telemetry_data [TelemetryField .KEY_TYPE .value ] = self .errtype .value
160162 telemetry_data [TelemetryField .KEY_SOURCE .value ] = connection .application
161163 telemetry_data [TelemetryField .KEY_EXCEPTION .value ] = self .__class__ .__name__
164+ telemetry_data [TelemetryField .KEY_USES_AIO .value ] = str (
165+ self ._is_aio_connection (connection )
166+ ).lower ()
162167 ts = get_time_millis ()
163168 try :
164- connection ._log_telemetry (
169+ result = connection ._log_telemetry (
165170 TelemetryData .from_telemetry_data_dict (
166171 from_dict = telemetry_data , timestamp = ts , connection = connection
167172 )
168173 )
174+ if inspect .isawaitable (result ):
175+ try :
176+ import asyncio
177+
178+ asyncio .get_running_loop ().create_task (result )
179+ except Exception :
180+ logger .debug (
181+ "Failed to schedule async telemetry logging." ,
182+ exc_info = True ,
183+ )
169184 except AttributeError :
170185 logger .debug ("Cursor failed to log to telemetry." , exc_info = True )
171186
172187 def exception_telemetry (
173188 self ,
174189 msg : str ,
175- cursor : SnowflakeCursor | None ,
176- connection : SnowflakeConnection | None ,
190+ cursor : SnowflakeCursor | AsyncSnowflakeCursor | None ,
191+ connection : SnowflakeConnection | AsyncSnowflakeConnection | None ,
177192 ) -> None :
178193 """Main method to generate and send telemetry data for exceptions."""
179194 try :
@@ -370,6 +385,18 @@ def errorhandler_make_exception(
370385 )
371386 return error_class (error_value )
372387
388+ @staticmethod
389+ def _is_aio_connection (
390+ connection : SnowflakeConnection | AsyncSnowflakeConnection ,
391+ ) -> bool :
392+ try :
393+ # Try import async connection. The import may fail if aio is not installed.
394+ from .aio ._connection import SnowflakeConnection as AsyncSnowflakeConnection
395+
396+ return isinstance (connection , AsyncSnowflakeConnection )
397+ except ImportError :
398+ return False
399+
373400
374401class _Warning (Exception ):
375402 """Exception for important warnings."""
0 commit comments