Skip to content

Commit e2bf024

Browse files
sfc-gh-mkellersfc-gh-bdrutu
authored andcommitted
SNOW-1763555 update how HTTP headers are requested from OpenTelemetry (#2106)
Co-authored-by: Bogdan Drutu <[email protected]>
1 parent 449d668 commit e2bf024

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

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)