mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-07-28 20:52:57 +08:00
Expand sqlalchemy pool.name to follow the semantic conventions (#1778)
This commit is contained in:
@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Added
|
||||
|
||||
- Expand sqlalchemy pool.name to follow the semantic conventions
|
||||
([#1778](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1778))
|
||||
- Add `excluded_urls` functionality to `urllib` and `urllib3` instrumentations
|
||||
([#1733](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1733))
|
||||
- Make Django request span attributes available for `start_span`.
|
||||
|
@ -118,8 +118,17 @@ class EngineTracer:
|
||||
self._register_event_listener(engine, "checkin", self._pool_checkin)
|
||||
self._register_event_listener(engine, "checkout", self._pool_checkout)
|
||||
|
||||
def _get_connection_string(self):
|
||||
drivername = self.engine.url.drivername or ""
|
||||
host = self.engine.url.host or ""
|
||||
port = self.engine.url.port or ""
|
||||
database = self.engine.url.database or ""
|
||||
return f"{drivername}://{host}:{port}/{database}"
|
||||
|
||||
def _get_pool_name(self):
|
||||
return self.engine.pool.logging_name or ""
|
||||
if self.engine.pool.logging_name is not None:
|
||||
return self.engine.pool.logging_name
|
||||
return self._get_connection_string()
|
||||
|
||||
def _add_idle_to_connection_usage(self, value):
|
||||
self.connections_usage.add(
|
||||
|
@ -70,11 +70,12 @@ class TestSqlalchemyMetricsInstrumentation(TestBase):
|
||||
)
|
||||
|
||||
def test_metrics_without_pool_name(self):
|
||||
pool_name = ""
|
||||
pool_name = "pool_test_name"
|
||||
engine = sqlalchemy.create_engine(
|
||||
"sqlite:///:memory:",
|
||||
pool_size=5,
|
||||
poolclass=QueuePool,
|
||||
pool_logging_name=pool_name,
|
||||
)
|
||||
|
||||
metrics = self.get_sorted_metrics()
|
||||
|
@ -95,3 +95,40 @@ class PostgresCreatorTestCase(PostgresTestCase):
|
||||
"url": "postgresql://",
|
||||
"creator": lambda: psycopg2.connect(**POSTGRES_CONFIG),
|
||||
}
|
||||
|
||||
|
||||
class PostgresMetricsTestCase(PostgresTestCase):
|
||||
__test__ = True
|
||||
|
||||
VENDOR = "postgresql"
|
||||
SQL_DB = "opentelemetry-tests"
|
||||
ENGINE_ARGS = {
|
||||
"url": "postgresql://%(user)s:%(password)s@%(host)s:%(port)s/%(dbname)s"
|
||||
% POSTGRES_CONFIG
|
||||
}
|
||||
|
||||
def test_metrics_pool_name(self):
|
||||
with self.connection() as conn:
|
||||
conn.execute("SELECT 1 + 1").fetchall()
|
||||
|
||||
pool_name = "{}://{}:{}/{}".format(
|
||||
self.VENDOR,
|
||||
POSTGRES_CONFIG["host"],
|
||||
POSTGRES_CONFIG["port"],
|
||||
self.SQL_DB,
|
||||
)
|
||||
metrics = self.get_sorted_metrics()
|
||||
self.assertEqual(len(metrics), 1)
|
||||
self.assert_metric_expected(
|
||||
metrics[0],
|
||||
[
|
||||
self.create_number_data_point(
|
||||
value=0,
|
||||
attributes={"pool.name": pool_name, "state": "idle"},
|
||||
),
|
||||
self.create_number_data_point(
|
||||
value=0,
|
||||
attributes={"pool.name": pool_name, "state": "used"},
|
||||
),
|
||||
],
|
||||
)
|
||||
|
Reference in New Issue
Block a user