Skip to content

Commit

Permalink
SNOW-1763555 update how HTTP headers are requested from OpenTelemetry (
Browse files Browse the repository at this point in the history
…#2106)

Co-authored-by: Bogdan Drutu <bogdan.drutu@snowflake.com>
  • Loading branch information
sfc-gh-mkeller and sfc-gh-bdrutu authored Nov 15, 2024
1 parent 1e4d456 commit 9ddb205
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Source code is also available at: https://github.com/snowflakedb/snowflake-conne

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

- v3.12.3(October 25,2024)
- Improved the error message for SSL-related issues to provide clearer guidance when an SSL error occurs.
Expand Down
16 changes: 12 additions & 4 deletions src/snowflake/connector/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,11 +481,19 @@ def request(
HTTP_HEADER_USER_AGENT: PYTHON_CONNECTOR_USER_AGENT,
}
try:
from opentelemetry.propagate import inject
# SNOW-1763555: inject OpenTelemetry headers if available specifically in WC3 format
# into our request headers in case tracing is enabled. This should make sure that
# our requests are accounted for properly if OpenTelemetry is used by users.
from opentelemetry.trace.propagation.tracecontext import (
TraceContextTextMapPropagator,
)

inject(headers)
except ModuleNotFoundError as e:
logger.debug(f"Opentelemtry otel injection failed because of: {e}")
TraceContextTextMapPropagator().inject(headers)
except Exception:
logger.debug(
"Opentelemtry otel injection failed",
exc_info=True,
)
if self._connection.service_name:
headers[HTTP_HEADER_SERVICE_NAME] = self._connection.service_name
if method == "post":
Expand Down
16 changes: 16 additions & 0 deletions test/unit/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,3 +572,19 @@ def test_ssl_error_hint(caplog):
exc.value, OperationalError
)
assert "SSL error" in caplog.text and _CONNECTIVITY_ERR_MSG in caplog.text


def test_otel_error_message(caplog, mock_post_requests):
"""This test assumes that OpenTelemetry is not installed when tests are running."""
with mock.patch("snowflake.connector.network.SnowflakeRestful._post_request"):
with caplog.at_level(logging.DEBUG):
with fake_connector():
...
assert caplog.records
important_records = [
record
for record in caplog.records
if "Opentelemtry otel injection failed" in record.message
]
assert len(important_records) == 1
assert important_records[0].exc_text is not None

0 comments on commit 9ddb205

Please sign in to comment.