Skip to content

Commit

Permalink
Add type hints to SQLite3 (#3057)
Browse files Browse the repository at this point in the history
* Add type hints to SQLite3

* Apply suggestions from code review

* Update instrumentation/opentelemetry-instrumentation-sqlite3/src/opentelemetry/instrumentation/sqlite3/package.py

* This is a type alias, pylint is dumb

---------

Co-authored-by: Emídio Neto <[email protected]>
Co-authored-by: Aaron Abbott <[email protected]>
  • Loading branch information
3 people authored Dec 4, 2024
1 parent 9cf2683 commit f393546
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,35 @@
---
"""

from __future__ import annotations

import sqlite3
from sqlite3 import dbapi2
from typing import Collection
from typing import Any, Collection, TypeVar, Union

from opentelemetry.instrumentation import dbapi
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.sqlite3.package import _instruments
from opentelemetry.instrumentation.sqlite3.version import __version__
from opentelemetry.trace import TracerProvider

# No useful attributes of sqlite3 connection object
_CONNECTION_ATTRIBUTES = {}

_DATABASE_SYSTEM = "sqlite"

SQLite3Connection = TypeVar( # pylint: disable=invalid-name
"SQLite3Connection", bound=Union[sqlite3.Connection, None]
)


class SQLite3Instrumentor(BaseInstrumentor):
_TO_WRAP = [sqlite3, dbapi2]

def instrumentation_dependencies(self) -> Collection[str]:
return _instruments

def _instrument(self, **kwargs):
def _instrument(self, **kwargs: Any) -> None:
"""Integrate with SQLite3 Python library.
https://docs.python.org/3/library/sqlite3.html
"""
Expand All @@ -77,13 +84,16 @@ def _instrument(self, **kwargs):
tracer_provider=tracer_provider,
)

def _uninstrument(self, **kwargs):
def _uninstrument(self, **kwargs: Any) -> None:
""" "Disable SQLite3 instrumentation"""
for module in self._TO_WRAP:
dbapi.unwrap_connect(module, "connect")

@staticmethod
def instrument_connection(connection, tracer_provider=None):
def instrument_connection(
connection: SQLite3Connection,
tracer_provider: TracerProvider | None = None,
) -> SQLite3Connection:
"""Enable instrumentation in a SQLite connection.
Args:
Expand All @@ -105,7 +115,9 @@ def instrument_connection(connection, tracer_provider=None):
)

@staticmethod
def uninstrument_connection(connection):
def uninstrument_connection(
connection: SQLite3Connection,
) -> SQLite3Connection:
"""Disable instrumentation in a SQLite connection.
Args:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations


_instruments = tuple()
_instruments: tuple[str, ...] = tuple()

0 comments on commit f393546

Please sign in to comment.