Absent "database" attribute causes a defect when integrating with opentelemetry-instrumentation-dbapi #751
-
Describe the bugWe're currently implementing an OpenTelemetry instrumentation to our application which uses mysqlclient using opentelemetry-instrumentation-dbapi module. While it generally works, it's missing the DB name in our database spans generated by this module:
I've had a look at the instrumentation module's code to find out why it fails to get the database name.
How the module finds the DB name is that it looks up for the The
It seems to be a minor/non-critical issue, but here we're losing an opportunity to select/filter spans by the database name, which makes the troubleshooting and observability experience better. As it's basically one line of code assigning the database name from connection parameters to the object attribute, I decided to do add one and propose the change. Please take a look at my pull request #752 Environmentthis issue is environment-independent How did you install libmysqlclient libraries?No response What version of mysqlclient do you use?No response Docker command to start MySQL serverNo response Minimum but complete code to reproduceimport MySQLdb
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor, ConsoleSpanExporter
from opentelemetry.trace import set_tracer_provider
from opentelemetry.instrumentation.dbapi import trace_integration
DB_NAME = "test_db"
# Set up the Console exporter
console_exporter = ConsoleSpanExporter()
# Set up OpenTelemetry tracer provider
provider = TracerProvider()
set_tracer_provider(provider)
# Add Console Exporter
provider.add_span_processor(SimpleSpanProcessor(console_exporter))
# Instrument MySQLdb
trace_integration(
connect_module=MySQLdb,
connect_method_name="connect",
database_system="mysql"
)
# Get tracer instance
tracer = trace.get_tracer(__name__)
def main():
try:
with tracer.start_as_current_span("connect-to-database") as span:
# Connect to the MySQL database
connection = MySQLdb.connect(
host="127.0.0.1", # Use TCP to connect
port=3306,
user="root",
db=DB_NAME
)
span.add_event("Successfully connected to the database")
print("Connected to MySQL database")
with tracer.start_as_current_span("perform-database-operations") as span:
cursor = connection.cursor()
# Execute a query to check the connection
cursor.execute("SELECT DATABASE();")
record = cursor.fetchone()
print(f"You're connected to the database: {record[0]}")
# Insert a sample record
insert_query = "INSERT INTO sample_table (name, age) VALUES (%s, %s)"
cursor.execute(insert_query, ("Alice", 30))
connection.commit()
print("Inserted record into the database.")
# Query the data
cursor.execute("SELECT * FROM sample_table;")
rows = cursor.fetchall()
print("Data from the table:")
for row in rows:
print(row)
except MySQLdb.Error as e:
print(f"Error: {e}")
finally:
if "connection" in locals() and connection:
connection.close()
print("MySQL connection is closed.")
if __name__ == "__main__":
main() Schema and initial data required to reproduce.no initial data required to reproduce Commands, and any other step required to reproduce your issue.In [25]: import MySQLdb |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
closing this one, as #752 got merged |
Beta Was this translation helpful? Give feedback.
-
#752 didn't fix it. See #753. It supports db.name, db.user, and net.peer.name. |
Beta Was this translation helpful? Give feedback.
#752 didn't fix it. See #753. It supports db.name, db.user, and net.peer.name.