This library provides automatic instrumentation for the Python `sqlite3` module, enabling distributed tracing for database operations within applications. It's part of the `opentelemetry-python-contrib` repository, which follows a frequent release cadence, often aligning with core OpenTelemetry Python releases, with the current version being 0.62b0.
pip install opentelemetry-instrumentation-sqlite3Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up the OpenTelemetry SDK with a console exporter and then enable SQLite3 instrumentation. It's crucial to call `SQLite3Instrumentor().instrument()` before any `sqlite3.connect()` calls or module interactions to ensure all operations are traced. The example performs basic table creation, data insertion, and selection.
Refer to release notes for potential breaking changes when upgrading beta versions.
Call `SQLite3Instrumentor().instrument()` at the very beginning of your application's lifecycle, typically alongside other OpenTelemetry SDK configurations.
Ensure you initialize a `TracerProvider`, add at least one `SpanProcessor` (e.g., `SimpleSpanProcessor` or `BatchSpanProcessor`), and configure an `SpanExporter` (e.g., `ConsoleSpanExporter`, `OTLPSpanExporter`) as part of your application's setup.
Always obtain a cursor object using `connection.cursor()` and perform operations via the cursor: `cursor.execute(...)`.
Install the necessary system-level development packages for SQLite (e.g., `sqlite-devel` on CentOS/RHEL, `libsqlite3-dev` on Debian/Ubuntu, `build-base` and `sqlite-dev` on Alpine) and ensure Python is built or reinstalled with these dependencies available. For example: `sudo apt-get install libsqlite3-dev` and then potentially reinstalling Python or the `sqlite3` module if it was custom-built.
Ensure `SQLite3Instrumentor().instrument()` is called early in your application's lifecycle to patch all `sqlite3` connections, or explicitly instrument individual connections using `instrumented_connection = SQLite3Instrumentor().instrument_connection(conn)`. When using the instrumented connection, ensure you obtain cursors from it, e.g., `cursor = instrumented_connection.cursor()`.
Update all `opentelemetry` related packages to their latest compatible versions. Ensure consistency across `opentelemetry-api`, `opentelemetry-sdk`, and all `opentelemetry-instrumentation-*` packages being used. It's often best to upgrade them simultaneously.
Verify that `SQLite3Instrumentor().instrument()` is called. If instrumenting individual connections, ensure the `instrument_connection` method is used and that cursors are created from the *instrumented* connection. Additionally, confirm that a `TracerProvider` is configured and an exporter is set up to send telemetry data.