Skip to content

Commit 10d8e26

Browse files
Fix sqlalchemy for postgres Unix sockets (#761)
* Fix sqlalchemy for postgres unix sockets The following bit of replaced code contained a type inconsistency: ```py attrs[SpanAttributes.NET_PEER_PORT] = int(data.get("port")) ``` `data.get` returns `Optional[str]` but `int(None)` throws a `TypeError`. When using postgresql via unix socket `dsn` looks something like this: ```py 'user=postgres host=/tmp/socket dbname=postgres' ``` The `parse_dsn` function returns this: ```py {'user': 'postgres', 'dbname': 'postgres', 'host': '/tmp/socket'} ``` * Update CHANGELOG * Conditionally set net.transport for psql tcp/unix * Use .value properties of enums * Improve postgresql attribute detection from cursor * Fix formatting Co-authored-by: Matt Oberle <[email protected]> Co-authored-by: Srikanth Chekuri <[email protected]>
1 parent 2dd9bd1 commit 10d8e26

File tree

2 files changed

+26
-11
lines changed
  • instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy

2 files changed

+26
-11
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4040

4141
## [1.6.2-0.25b2](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.6.2-0.25b2) - 2021-10-19
4242

43+
- `opentelemetry-instrumentation-sqlalchemy` Fix PostgreSQL instrumentation for Unix sockets
44+
([#761](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/761))
45+
4346
### Changed
4447

4548
- `opentelemetry-sdk-extension-aws` & `opentelemetry-propagator-aws` Release AWS Python SDK Extension as 2.0.1 and AWS Propagator as 1.0.1

instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py

+23-11
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import os
1415

1516
from sqlalchemy.event import listen # pylint: disable=no-name-in-module
1617

1718
from opentelemetry import trace
1819
from opentelemetry.instrumentation.sqlalchemy.version import __version__
19-
from opentelemetry.semconv.trace import SpanAttributes
20+
from opentelemetry.semconv.trace import NetTransportValues, SpanAttributes
2021
from opentelemetry.trace.status import Status, StatusCode
2122

2223

@@ -157,14 +158,25 @@ def _get_attributes_from_url(url):
157158
def _get_attributes_from_cursor(vendor, cursor, attrs):
158159
"""Attempt to set db connection attributes by introspecting the cursor."""
159160
if vendor == "postgresql":
160-
# pylint: disable=import-outside-toplevel
161-
from psycopg2.extensions import parse_dsn
162-
163-
if hasattr(cursor, "connection") and hasattr(cursor.connection, "dsn"):
164-
dsn = getattr(cursor.connection, "dsn", None)
165-
if dsn:
166-
data = parse_dsn(dsn)
167-
attrs[SpanAttributes.DB_NAME] = data.get("dbname")
168-
attrs[SpanAttributes.NET_PEER_NAME] = data.get("host")
169-
attrs[SpanAttributes.NET_PEER_PORT] = int(data.get("port"))
161+
info = getattr(getattr(cursor, "connection", None), "info", None)
162+
if not info:
163+
return attrs
164+
165+
attrs[SpanAttributes.DB_NAME] = info.dbname
166+
is_unix_socket = info.host and info.host.startswith("/")
167+
168+
if is_unix_socket:
169+
attrs[SpanAttributes.NET_TRANSPORT] = NetTransportValues.UNIX.value
170+
if info.port:
171+
# postgresql enforces this pattern on all socket names
172+
attrs[SpanAttributes.NET_PEER_NAME] = os.path.join(
173+
info.host, f".s.PGSQL.{info.port}"
174+
)
175+
else:
176+
attrs[
177+
SpanAttributes.NET_TRANSPORT
178+
] = NetTransportValues.IP_TCP.value
179+
attrs[SpanAttributes.NET_PEER_NAME] = info.host
180+
if info.port:
181+
attrs[SpanAttributes.NET_PEER_PORT] = int(info.port)
170182
return attrs

0 commit comments

Comments
 (0)