File tree 4 files changed +51
-2
lines changed
instrumentation/opentelemetry-instrumentation-sqlalchemy
src/opentelemetry/instrumentation/sqlalchemy
tests/opentelemetry-docker-tests/tests/sqlalchemy_tests
4 files changed +51
-2
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
15
15
16
16
### Added
17
17
18
+ - Expand sqlalchemy pool.name to follow the semantic conventions
19
+ ([ #1778 ] ( https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1778 ) )
18
20
- Add ` excluded_urls ` functionality to ` urllib ` and ` urllib3 ` instrumentations
19
21
([ #1733 ] ( https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1733 ) )
20
22
- Make Django request span attributes available for ` start_span ` .
Original file line number Diff line number Diff line change @@ -118,8 +118,17 @@ def __init__(
118
118
self ._register_event_listener (engine , "checkin" , self ._pool_checkin )
119
119
self ._register_event_listener (engine , "checkout" , self ._pool_checkout )
120
120
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
+
121
128
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 ()
123
132
124
133
def _add_idle_to_connection_usage (self , value ):
125
134
self .connections_usage .add (
Original file line number Diff line number Diff line change @@ -70,11 +70,12 @@ def test_metrics_one_connection(self):
70
70
)
71
71
72
72
def test_metrics_without_pool_name (self ):
73
- pool_name = ""
73
+ pool_name = "pool_test_name "
74
74
engine = sqlalchemy .create_engine (
75
75
"sqlite:///:memory:" ,
76
76
pool_size = 5 ,
77
77
poolclass = QueuePool ,
78
+ pool_logging_name = pool_name ,
78
79
)
79
80
80
81
metrics = self .get_sorted_metrics ()
Original file line number Diff line number Diff line change @@ -95,3 +95,40 @@ class PostgresCreatorTestCase(PostgresTestCase):
95
95
"url" : "postgresql://" ,
96
96
"creator" : lambda : psycopg2 .connect (** POSTGRES_CONFIG ),
97
97
}
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
+ )
You can’t perform that action at this time.
0 commit comments