Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type hints to SQLite3 #3057

Merged
merged 7 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
"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()
Loading