Skip to content

Commit 7096019

Browse files
committed
add attribute for db port
1 parent 803bb32 commit 7096019

File tree

2 files changed

+76
-71
lines changed

2 files changed

+76
-71
lines changed

instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi/__init__.py

Lines changed: 73 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@
6363

6464

6565
def trace_integration(
66-
connect_module: typing.Callable[..., typing.Any],
67-
connect_method_name: str,
68-
database_system: str,
69-
connection_attributes: typing.Dict = None,
70-
tracer_provider: typing.Optional[TracerProvider] = None,
71-
capture_parameters: bool = False,
72-
enable_commenter: bool = False,
73-
db_api_integration_factory=None,
66+
connect_module: typing.Callable[..., typing.Any],
67+
connect_method_name: str,
68+
database_system: str,
69+
connection_attributes: typing.Dict = None,
70+
tracer_provider: typing.Optional[TracerProvider] = None,
71+
capture_parameters: bool = False,
72+
enable_commenter: bool = False,
73+
db_api_integration_factory=None,
7474
):
7575
"""Integrate with DB API library.
7676
https://www.python.org/dev/peps/pep-0249/
@@ -104,17 +104,17 @@ def trace_integration(
104104

105105

106106
def wrap_connect(
107-
name: str,
108-
connect_module: typing.Callable[..., typing.Any],
109-
connect_method_name: str,
110-
database_system: str,
111-
connection_attributes: typing.Dict = None,
112-
version: str = "",
113-
tracer_provider: typing.Optional[TracerProvider] = None,
114-
capture_parameters: bool = False,
115-
enable_commenter: bool = False,
116-
db_api_integration_factory=None,
117-
commenter_options: dict = None,
107+
name: str,
108+
connect_module: typing.Callable[..., typing.Any],
109+
connect_method_name: str,
110+
database_system: str,
111+
connection_attributes: typing.Dict = None,
112+
version: str = "",
113+
tracer_provider: typing.Optional[TracerProvider] = None,
114+
capture_parameters: bool = False,
115+
enable_commenter: bool = False,
116+
db_api_integration_factory=None,
117+
commenter_options: dict = None,
118118
):
119119
"""Integrate with DB API library.
120120
https://www.python.org/dev/peps/pep-0249/
@@ -136,15 +136,15 @@ def wrap_connect(
136136
137137
"""
138138
db_api_integration_factory = (
139-
db_api_integration_factory or DatabaseApiIntegration
139+
db_api_integration_factory or DatabaseApiIntegration
140140
)
141141

142142
# pylint: disable=unused-argument
143143
def wrap_connect_(
144-
wrapped: typing.Callable[..., typing.Any],
145-
instance: typing.Any,
146-
args: typing.Tuple[typing.Any, typing.Any],
147-
kwargs: typing.Dict[typing.Any, typing.Any],
144+
wrapped: typing.Callable[..., typing.Any],
145+
instance: typing.Any,
146+
args: typing.Tuple[typing.Any, typing.Any],
147+
kwargs: typing.Dict[typing.Any, typing.Any],
148148
):
149149
db_integration = db_api_integration_factory(
150150
name,
@@ -168,7 +168,7 @@ def wrap_connect_(
168168

169169

170170
def unwrap_connect(
171-
connect_module: typing.Callable[..., typing.Any], connect_method_name: str
171+
connect_module: typing.Callable[..., typing.Any], connect_method_name: str
172172
):
173173
"""Disable integration with DB API library.
174174
https://www.python.org/dev/peps/pep-0249/
@@ -181,15 +181,15 @@ def unwrap_connect(
181181

182182

183183
def instrument_connection(
184-
name: str,
185-
connection,
186-
database_system: str,
187-
connection_attributes: typing.Dict = None,
188-
version: str = "",
189-
tracer_provider: typing.Optional[TracerProvider] = None,
190-
capture_parameters: bool = False,
191-
enable_commenter: bool = False,
192-
commenter_options: dict = None,
184+
name: str,
185+
connection,
186+
database_system: str,
187+
connection_attributes: typing.Dict = None,
188+
version: str = "",
189+
tracer_provider: typing.Optional[TracerProvider] = None,
190+
capture_parameters: bool = False,
191+
enable_commenter: bool = False,
192+
commenter_options: dict = None,
193193
):
194194
"""Enable instrumentation in a database connection.
195195
@@ -244,16 +244,16 @@ def uninstrument_connection(connection):
244244

245245
class DatabaseApiIntegration:
246246
def __init__(
247-
self,
248-
name: str,
249-
database_system: str,
250-
connection_attributes=None,
251-
version: str = "",
252-
tracer_provider: typing.Optional[TracerProvider] = None,
253-
capture_parameters: bool = False,
254-
enable_commenter: bool = False,
255-
commenter_options: dict = None,
256-
connect_module: typing.Callable[..., typing.Any] = None,
247+
self,
248+
name: str,
249+
database_system: str,
250+
connection_attributes=None,
251+
version: str = "",
252+
tracer_provider: typing.Optional[TracerProvider] = None,
253+
capture_parameters: bool = False,
254+
enable_commenter: bool = False,
255+
commenter_options: dict = None,
256+
connect_module: typing.Callable[..., typing.Any] = None,
257257
):
258258
self.connection_attributes = connection_attributes
259259
if self.connection_attributes is None:
@@ -283,8 +283,8 @@ def __init__(
283283
self.commenter_data = self.calculate_commenter_data()
284284

285285
def _get_db_version(
286-
self,
287-
db_driver,
286+
self,
287+
db_driver,
288288
):
289289
if db_driver in _DB_DRIVER_ALIASES:
290290
return util_version(_DB_DRIVER_ALIASES[db_driver])
@@ -296,7 +296,7 @@ def _get_db_version(
296296
return db_version
297297

298298
def calculate_commenter_data(
299-
self,
299+
self,
300300
):
301301
commenter_data = {}
302302
if not self.enable_commenter:
@@ -346,10 +346,10 @@ def calculate_commenter_data(
346346
return commenter_data
347347

348348
def wrapped_connection(
349-
self,
350-
connect_method: typing.Callable[..., typing.Any],
351-
args: typing.Tuple[typing.Any, typing.Any],
352-
kwargs: typing.Dict[typing.Any, typing.Any],
349+
self,
350+
connect_method: typing.Callable[..., typing.Any],
351+
args: typing.Tuple[typing.Any, typing.Any],
352+
kwargs: typing.Dict[typing.Any, typing.Any],
353353
):
354354
"""Add object proxy to connection object."""
355355
connection = connect_method(*args, **kwargs)
@@ -391,7 +391,7 @@ def get_connection_attributes(self, connection):
391391

392392

393393
def get_traced_connection_proxy(
394-
connection, db_api_integration, *args, **kwargs
394+
connection, db_api_integration, *args, **kwargs
395395
):
396396
# pylint: disable=abstract-method
397397
class TracedConnectionProxy(wrapt.ObjectProxy):
@@ -435,10 +435,10 @@ def __init__(self, db_api_integration: DatabaseApiIntegration) -> None:
435435
self._leading_comment_remover = re.compile(r"^/\*.*?\*/")
436436

437437
def _populate_span(
438-
self,
439-
span: trace_api.Span,
440-
cursor,
441-
*args: typing.Tuple[typing.Any, typing.Any],
438+
self,
439+
span: trace_api.Span,
440+
cursor,
441+
*args: typing.Tuple[typing.Any, typing.Any],
442442
):
443443
if not span.is_recording():
444444
return
@@ -451,9 +451,11 @@ def _populate_span(
451451
)
452452
span.set_attribute(SpanAttributes.DB_STATEMENT, statement)
453453

454+
span.set_attribute(SpanAttributes.DB_PORT, self._db_api_integration.connection_props.get("port"))
455+
454456
for (
455-
attribute_key,
456-
attribute_value,
457+
attribute_key,
458+
attribute_value,
457459
) in self._db_api_integration.span_attributes.items():
458460
span.set_attribute(attribute_key, attribute_value)
459461

@@ -475,11 +477,11 @@ def get_statement(self, cursor, args): # pylint: disable=no-self-use
475477
return statement
476478

477479
def traced_execution(
478-
self,
479-
cursor,
480-
query_method: typing.Callable[..., typing.Any],
481-
*args: typing.Tuple[typing.Any, typing.Any],
482-
**kwargs: typing.Dict[typing.Any, typing.Any],
480+
self,
481+
cursor,
482+
query_method: typing.Callable[..., typing.Any],
483+
*args: typing.Tuple[typing.Any, typing.Any],
484+
**kwargs: typing.Dict[typing.Any, typing.Any],
483485
):
484486
name = self.get_operation_name(cursor, args)
485487
if not name:
@@ -490,7 +492,7 @@ def traced_execution(
490492
)
491493

492494
with self._db_api_integration._tracer.start_as_current_span(
493-
name, kind=SpanKind.CLIENT
495+
name, kind=SpanKind.CLIENT
494496
) as span:
495497
self._populate_span(span, cursor, *args)
496498
if args and self._commenter_enabled:
@@ -499,12 +501,12 @@ def traced_execution(
499501

500502
# lazy capture of mysql-connector client version using cursor
501503
if (
502-
self._db_api_integration.database_system == "mysql"
503-
and self._db_api_integration.connect_module.__name__
504-
== "mysql.connector"
505-
and not self._db_api_integration.commenter_data[
506-
"mysql_client_version"
507-
]
504+
self._db_api_integration.database_system == "mysql"
505+
and self._db_api_integration.connect_module.__name__
506+
== "mysql.connector"
507+
and not self._db_api_integration.commenter_data[
508+
"mysql_client_version"
509+
]
508510
):
509511
self._db_api_integration.commenter_data[
510512
"mysql_client_version"
@@ -514,7 +516,7 @@ def traced_execution(
514516
self._db_api_integration.commenter_data
515517
)
516518
if self._commenter_options.get(
517-
"opentelemetry_values", True
519+
"opentelemetry_values", True
518520
):
519521
commenter_data.update(**_get_opentelemetry_values())
520522

instrumentation/opentelemetry-instrumentation-dbapi/tests/test_dbapi_integration.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ def test_span_succeeded(self):
6666
self.assertEqual(
6767
span.attributes[SpanAttributes.DB_STATEMENT], "Test query"
6868
)
69+
self.assertEqual(
70+
span.attributes[SpanAttributes.DB_PORT], 123
71+
)
6972
self.assertFalse("db.statement.parameters" in span.attributes)
7073
self.assertEqual(span.attributes[SpanAttributes.DB_USER], "testuser")
7174
self.assertEqual(

0 commit comments

Comments
 (0)