Skip to content

Commit 090b4ef

Browse files
Apply #2106 to async code
1 parent e2bf024 commit 090b4ef

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/snowflake/connector/aio/_network.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,19 @@ async def request(
217217
HTTP_HEADER_USER_AGENT: PYTHON_CONNECTOR_USER_AGENT,
218218
}
219219
try:
220-
from opentelemetry.propagate import inject
220+
# SNOW-1763555: inject OpenTelemetry headers if available specifically in WC3 format
221+
# into our request headers in case tracing is enabled. This should make sure that
222+
# our requests are accounted for properly if OpenTelemetry is used by users.
223+
from opentelemetry.trace.propagation.tracecontext import (
224+
TraceContextTextMapPropagator,
225+
)
221226

222-
inject(headers)
223-
except ModuleNotFoundError as e:
224-
logger.debug(f"Opentelemtry otel injection failed because of: {e}")
227+
TraceContextTextMapPropagator().inject(headers)
228+
except Exception:
229+
logger.debug(
230+
"Opentelemtry otel injection failed",
231+
exc_info=True,
232+
)
225233
if self._connection.service_name:
226234
headers[HTTP_HEADER_SERVICE_NAME] = self._connection.service_name
227235
if method == "post":

test/unit/aio/test_connection_async_unit.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,3 +551,19 @@ async def test_ssl_error_hint(caplog):
551551
exc.value, OperationalError
552552
)
553553
assert "SSL error" in caplog.text and _CONNECTIVITY_ERR_MSG in caplog.text
554+
555+
556+
async def test_otel_error_message_async(caplog, mock_post_requests):
557+
"""This test assumes that OpenTelemetry is not installed when tests are running."""
558+
with mock.patch("snowflake.connector.aio._network.SnowflakeRestful._post_request"):
559+
with caplog.at_level(logging.DEBUG):
560+
async with fake_db_conn():
561+
...
562+
assert caplog.records
563+
important_records = [
564+
record
565+
for record in caplog.records
566+
if "Opentelemtry otel injection failed" in record.message
567+
]
568+
assert len(important_records) == 1
569+
assert important_records[0].exc_text is not None

0 commit comments

Comments
 (0)