Skip to content

Commit 9ddb205

Browse files
SNOW-1763555 update how HTTP headers are requested from OpenTelemetry (#2106)
Co-authored-by: Bogdan Drutu <[email protected]>
1 parent 1e4d456 commit 9ddb205

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

DESCRIPTION.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Source code is also available at: https://github.com/snowflakedb/snowflake-conne
1010

1111
- v3.12.4(TBD)
1212
- Fixed a bug where multipart uploads to Azure would be missing their MD5 hashes.
13+
- Fixed a bug where OpenTelemetry header injection would sometimes cause Exceptions to be thrown.
1314

1415
- v3.12.3(October 25,2024)
1516
- Improved the error message for SSL-related issues to provide clearer guidance when an SSL error occurs.

src/snowflake/connector/network.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,11 +481,19 @@ def request(
481481
HTTP_HEADER_USER_AGENT: PYTHON_CONNECTOR_USER_AGENT,
482482
}
483483
try:
484-
from opentelemetry.propagate import inject
484+
# SNOW-1763555: inject OpenTelemetry headers if available specifically in WC3 format
485+
# into our request headers in case tracing is enabled. This should make sure that
486+
# our requests are accounted for properly if OpenTelemetry is used by users.
487+
from opentelemetry.trace.propagation.tracecontext import (
488+
TraceContextTextMapPropagator,
489+
)
485490

486-
inject(headers)
487-
except ModuleNotFoundError as e:
488-
logger.debug(f"Opentelemtry otel injection failed because of: {e}")
491+
TraceContextTextMapPropagator().inject(headers)
492+
except Exception:
493+
logger.debug(
494+
"Opentelemtry otel injection failed",
495+
exc_info=True,
496+
)
489497
if self._connection.service_name:
490498
headers[HTTP_HEADER_SERVICE_NAME] = self._connection.service_name
491499
if method == "post":

test/unit/test_connection.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,19 @@ def test_ssl_error_hint(caplog):
572572
exc.value, OperationalError
573573
)
574574
assert "SSL error" in caplog.text and _CONNECTIVITY_ERR_MSG in caplog.text
575+
576+
577+
def test_otel_error_message(caplog, mock_post_requests):
578+
"""This test assumes that OpenTelemetry is not installed when tests are running."""
579+
with mock.patch("snowflake.connector.network.SnowflakeRestful._post_request"):
580+
with caplog.at_level(logging.DEBUG):
581+
with fake_connector():
582+
...
583+
assert caplog.records
584+
important_records = [
585+
record
586+
for record in caplog.records
587+
if "Opentelemtry otel injection failed" in record.message
588+
]
589+
assert len(important_records) == 1
590+
assert important_records[0].exc_text is not None

0 commit comments

Comments
 (0)