diff --git a/DESCRIPTION.md b/DESCRIPTION.md index 482c60239..9513fa71f 100644 --- a/DESCRIPTION.md +++ b/DESCRIPTION.md @@ -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. diff --git a/src/snowflake/connector/network.py b/src/snowflake/connector/network.py index a00cc6588..18b28e061 100644 --- a/src/snowflake/connector/network.py +++ b/src/snowflake/connector/network.py @@ -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": diff --git a/test/unit/test_connection.py b/test/unit/test_connection.py index 89398fd86..d3c0c3259 100644 --- a/test/unit/test_connection.py +++ b/test/unit/test_connection.py @@ -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