Skip to content

Commit 7fabfcf

Browse files
Mv dbapi base class to proxy.py
1 parent b3c3aae commit 7fabfcf

File tree

2 files changed

+79
-58
lines changed
  • instrumentation/opentelemetry-instrumentation-dbapi/src/opentelemetry/instrumentation/dbapi

2 files changed

+79
-58
lines changed

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

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,14 @@
4141
import logging
4242
import re
4343
import typing
44-
from abc import ABC, abstractmethod
4544

4645
import wrapt
4746

4847
from opentelemetry import trace as trace_api
48+
from opentelemetry.instrumentation.dbapi.proxy import (
49+
BaseTracedConnectionProxy,
50+
BaseTracedCursorProxy,
51+
)
4952
from opentelemetry.instrumentation.dbapi.version import __version__
5053
from opentelemetry.instrumentation.sqlcommenter_utils import _add_sql_comment
5154
from opentelemetry.instrumentation.utils import (
@@ -391,31 +394,6 @@ def get_connection_attributes(self, connection):
391394
self.span_attributes[SpanAttributes.NET_PEER_PORT] = port
392395

393396

394-
class BaseTracedConnectionProxy(ABC, wrapt.ObjectProxy):
395-
# pylint: disable=unused-argument
396-
def __init__(self, connection, *args, **kwargs):
397-
wrapt.ObjectProxy.__init__(self, connection)
398-
399-
def __getattribute__(self, name):
400-
if object.__getattribute__(self, name):
401-
return object.__getattribute__(self, name)
402-
403-
return object.__getattribute__(
404-
object.__getattribute__(self, "_connection"), name
405-
)
406-
407-
@abstractmethod
408-
def cursor(self, *args, **kwargs):
409-
"""Returns instrumented database query cursor"""
410-
411-
def __enter__(self):
412-
self.__wrapped__.__enter__()
413-
return self
414-
415-
def __exit__(self, *args, **kwargs):
416-
self.__wrapped__.__exit__(*args, **kwargs)
417-
418-
419397
def get_traced_connection_proxy(
420398
connection, db_api_integration, *args, **kwargs
421399
):
@@ -550,38 +528,6 @@ def traced_execution(
550528
return query_method(*args, **kwargs)
551529

552530

553-
# pylint: disable=abstract-method
554-
class BaseTracedCursorProxy(ABC, wrapt.ObjectProxy):
555-
# pylint: disable=unused-argument
556-
@abstractmethod
557-
def __init__(self, cursor, *args, **kwargs):
558-
"""Wrap db client cursor for tracing"""
559-
wrapt.ObjectProxy.__init__(self, cursor)
560-
self._cursor_tracer = None
561-
562-
def callproc(self, *args, **kwargs):
563-
return self._cursor_tracer.traced_execution(
564-
self.__wrapped__, self.__wrapped__.callproc, *args, **kwargs
565-
)
566-
567-
def execute(self, *args, **kwargs):
568-
return self._cursor_tracer.traced_execution(
569-
self.__wrapped__, self.__wrapped__.execute, *args, **kwargs
570-
)
571-
572-
def executemany(self, *args, **kwargs):
573-
return self._cursor_tracer.traced_execution(
574-
self.__wrapped__, self.__wrapped__.executemany, *args, **kwargs
575-
)
576-
577-
def __enter__(self):
578-
self.__wrapped__.__enter__()
579-
return self
580-
581-
def __exit__(self, *args, **kwargs):
582-
self.__wrapped__.__exit__(*args, **kwargs)
583-
584-
585531
def get_traced_cursor_proxy(cursor, db_api_integration, *args, **kwargs):
586532
class TracedCursorProxy(BaseTracedCursorProxy):
587533
def __init__(self, *args, **kwargs):
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
from abc import ABC, abstractmethod
17+
18+
import wrapt
19+
20+
21+
class BaseTracedConnectionProxy(ABC, wrapt.ObjectProxy):
22+
# pylint: disable=unused-argument
23+
def __init__(self, connection, *args, **kwargs):
24+
wrapt.ObjectProxy.__init__(self, connection)
25+
26+
def __getattribute__(self, name):
27+
if object.__getattribute__(self, name):
28+
return object.__getattribute__(self, name)
29+
30+
return object.__getattribute__(
31+
object.__getattribute__(self, "_connection"), name
32+
)
33+
34+
@abstractmethod
35+
def cursor(self, *args, **kwargs):
36+
"""Returns instrumented database query cursor"""
37+
38+
def __enter__(self):
39+
self.__wrapped__.__enter__()
40+
return self
41+
42+
def __exit__(self, *args, **kwargs):
43+
self.__wrapped__.__exit__(*args, **kwargs)
44+
45+
46+
# pylint: disable=abstract-method
47+
class BaseTracedCursorProxy(ABC, wrapt.ObjectProxy):
48+
# pylint: disable=unused-argument
49+
@abstractmethod
50+
def __init__(self, cursor, *args, **kwargs):
51+
"""Wrap db client cursor for tracing"""
52+
wrapt.ObjectProxy.__init__(self, cursor)
53+
self._cursor_tracer = None
54+
55+
def callproc(self, *args, **kwargs):
56+
return self._cursor_tracer.traced_execution(
57+
self.__wrapped__, self.__wrapped__.callproc, *args, **kwargs
58+
)
59+
60+
def execute(self, *args, **kwargs):
61+
return self._cursor_tracer.traced_execution(
62+
self.__wrapped__, self.__wrapped__.execute, *args, **kwargs
63+
)
64+
65+
def executemany(self, *args, **kwargs):
66+
return self._cursor_tracer.traced_execution(
67+
self.__wrapped__, self.__wrapped__.executemany, *args, **kwargs
68+
)
69+
70+
def __enter__(self):
71+
self.__wrapped__.__enter__()
72+
return self
73+
74+
def __exit__(self, *args, **kwargs):
75+
self.__wrapped__.__exit__(*args, **kwargs)

0 commit comments

Comments
 (0)