Skip to content

Commit 4e781f8

Browse files
authored
Merge branch 'main' into feature
2 parents eea010b + 1a1163e commit 4e781f8

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
### Added
1717

18+
- Expand sqlalchemy pool.name to follow the semantic conventions
19+
([#1778](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1778))
1820
- Add `excluded_urls` functionality to `urllib` and `urllib3` instrumentations
1921
([#1733](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1733))
2022
- Make Django request span attributes available for `start_span`.

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,17 @@ def __init__(
118118
self._register_event_listener(engine, "checkin", self._pool_checkin)
119119
self._register_event_listener(engine, "checkout", self._pool_checkout)
120120

121+
def _get_connection_string(self):
122+
drivername = self.engine.url.drivername or ""
123+
host = self.engine.url.host or ""
124+
port = self.engine.url.port or ""
125+
database = self.engine.url.database or ""
126+
return f"{drivername}://{host}:{port}/{database}"
127+
121128
def _get_pool_name(self):
122-
return self.engine.pool.logging_name or ""
129+
if self.engine.pool.logging_name is not None:
130+
return self.engine.pool.logging_name
131+
return self._get_connection_string()
123132

124133
def _add_idle_to_connection_usage(self, value):
125134
self.connections_usage.add(

instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy_metrics.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,12 @@ def test_metrics_one_connection(self):
7070
)
7171

7272
def test_metrics_without_pool_name(self):
73-
pool_name = ""
73+
pool_name = "pool_test_name"
7474
engine = sqlalchemy.create_engine(
7575
"sqlite:///:memory:",
7676
pool_size=5,
7777
poolclass=QueuePool,
78+
pool_logging_name=pool_name,
7879
)
7980

8081
metrics = self.get_sorted_metrics()

tests/opentelemetry-docker-tests/tests/sqlalchemy_tests/test_postgres.py

+37
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,40 @@ class PostgresCreatorTestCase(PostgresTestCase):
9595
"url": "postgresql://",
9696
"creator": lambda: psycopg2.connect(**POSTGRES_CONFIG),
9797
}
98+
99+
100+
class PostgresMetricsTestCase(PostgresTestCase):
101+
__test__ = True
102+
103+
VENDOR = "postgresql"
104+
SQL_DB = "opentelemetry-tests"
105+
ENGINE_ARGS = {
106+
"url": "postgresql://%(user)s:%(password)s@%(host)s:%(port)s/%(dbname)s"
107+
% POSTGRES_CONFIG
108+
}
109+
110+
def test_metrics_pool_name(self):
111+
with self.connection() as conn:
112+
conn.execute("SELECT 1 + 1").fetchall()
113+
114+
pool_name = "{}://{}:{}/{}".format(
115+
self.VENDOR,
116+
POSTGRES_CONFIG["host"],
117+
POSTGRES_CONFIG["port"],
118+
self.SQL_DB,
119+
)
120+
metrics = self.get_sorted_metrics()
121+
self.assertEqual(len(metrics), 1)
122+
self.assert_metric_expected(
123+
metrics[0],
124+
[
125+
self.create_number_data_point(
126+
value=0,
127+
attributes={"pool.name": pool_name, "state": "idle"},
128+
),
129+
self.create_number_data_point(
130+
value=0,
131+
attributes={"pool.name": pool_name, "state": "used"},
132+
),
133+
],
134+
)

0 commit comments

Comments
 (0)