Registry / observability / opentelemetry-instrumentation-pymysql

opentelemetry-instrumentation-pymysql

JSON →
library0.65b0pypypi✓ verified 23d ago

This library provides automatic instrumentation for PyMySQL, allowing OpenTelemetry to trace database operations performed via PyMySQL. It is part of the `opentelemetry-python-contrib` repository, which releases frequently (often several times a month) with new features, bug fixes, and instrumentations. The current version is 0.62b0, indicating a beta release.

pip install opentelemetry-instrumentation-pymysql
INSTALL
IMPORT
SIG · OPENTELEMETRY-INST
O
opentelemetry-instrumentation-pymysql
observabilitypythonv0.65b0
Install
2.1s avg
Import
Disk
43MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.65b0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 65.9MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.1s · import 0.000s · 23MB
43MB installed
● package 43MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

PyMySQLInstrumentor
from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor
from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor

This quickstart demonstrates how to set up the OpenTelemetry Python SDK and enable PyMySQL instrumentation. It configures a simple ConsoleSpanExporter to print traces to the console. When `PyMySQLInstrumentor().enable()` is called, subsequent `pymysql` operations will automatically generate spans. Remember to replace placeholder database credentials with actual ones for a functional setup.

import os from opentelemetry import trace from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor import pymysql # 1. Configure OpenTelemetry SDK resource = Resource.create({"service.name": "pymysql-example"}) tracer_provider = TracerProvider(resource=resource) tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter())) trace.set_tracer_provider(tracer_provider) # 2. Enable PyMySQL Instrumentation PyMySQLInstrumentor().enable() # 3. Use PyMySQL (this will be traced) # For a real application, replace with actual database credentials try: # Use os.environ.get for sensitive info or mock connection conn = pymysql.connect( host=os.environ.get('MYSQL_HOST', 'localhost'), user=os.environ.get('MYSQL_USER', 'root'), password=os.environ.get('MYSQL_PASSWORD', 'testpass'), database=os.environ.get('MYSQL_DATABASE', 'testdb') ) cursor = conn.cursor() cursor.execute("SELECT 1 + 1") result = cursor.fetchone() print(f"PyMySQL query result: {result}") cursor.close() conn.close() except Exception as e: print(f"Could not connect to PyMySQL or execute query: {e}") print("Make sure a MySQL/MariaDB server is running and accessible with correct credentials.") print("Traces should be printed to console above.")
Debug
Known issues
gotchaThe `opentelemetry-instrumentation-pymysql` package only provides the instrumentation. You must explicitly install `pymysql` yourself, as it is a peer dependency.
fix
Ensure `pymysql` is installed: `pip install pymysql`.
affects: All versions
gotchaOpenTelemetry instrumentations require the OpenTelemetry SDK (TracerProvider, SpanProcessor, Exporter) to be configured *before* the instrumentor is enabled, otherwise no traces will be generated or exported. This is a common oversight.
fix
Configure `trace.set_tracer_provider()` and `add_span_processor()` with an appropriate exporter (e.g., `ConsoleSpanExporter`, `OTLPSpanExporter`) before calling `PyMySQLInstrumentor().enable()`.
affects: All versions
deprecatedAs of OpenTelemetry Python SDK 1.0.0, manual calls to `set_tracer_provider` and `set_meter_provider` are generally discouraged in favor of `opentelemetry-sdk-extension-aws`'s `AwsLambdaInstrumentor` for serverless environments. For general applications, consider using environment variables (e.g., `OTEL_TRACER_PROVIDER`) or `opentelemetry-sdk-extension-auto`.
fix
While `set_tracer_provider` still works, for more robust and production-ready setups, especially in serverless or containerized environments, consider using automatic instrumentation or environment variables. This specific PyMySQL instrumentation is typically enabled manually via `PyMySQLInstrumentor().enable()`.
affects: 1.0.0 and above for `opentelemetry-sdk`
gotchaThe package version `0.62b0` indicates a beta release. While generally stable, minor API changes or behavioral adjustments might occur in future beta or stable releases of the `opentelemetry-python-contrib` repository.
fix
Periodically check the official OpenTelemetry Python Contrib GitHub repository and documentation for updates and release notes, especially when upgrading between beta versions.
affects: All `0.x.yBETA` versions
Upgrade
Version history
0.65b0latest on PyPI · released Jul 16, 2026
Audit
Dependencies
pymysqlrequiredThis instrumentation wraps the PyMySQL library, which must be installed separately.
opentelemetry-sdkrequiredRequired for configuring the OpenTelemetry SDK (tracer provider, span processor, exporter) to send traces.
Agent activity
20 hits · last 30 days
node
18
OpenAI (training)
1
Resources
opentelemetry-instrumentation-pymysql — pip install opentelemetry-instrumentation-pymysql · libregistry