Skip to content

Commit

Permalink
Separate instrument_connection() docstring block for mysql, mysqlclie…
Browse files Browse the repository at this point in the history
…nt, psycopg, psycopg2, pymysql, sqlite3
  • Loading branch information
beijiez committed Dec 18, 2024
1 parent 0cbd913 commit e11ec1a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import mysql.connector
from opentelemetry.instrumentation.mysql import MySQLInstrumentor
# Call instrument() to wrap all database connections
MySQLInstrumentor().instrument()
cnx = mysql.connector.connect(database="MySQL_Database")
Expand All @@ -34,11 +35,18 @@
cursor.close()
cnx.close()
cnx = MySQLInstrumentor().instrument_connection(cnx)
cursor = cnx.cursor()
.. code:: python
import mysql.connector
from opentelemetry.instrumentation.mysql import MySQLInstrumentor
# Alternatively, use instrument_connection for an individual connection
cnx = mysql.connector.connect(database="MySQL_Database")
instrumented_cnx = MySQLInstrumentor().instrument_connection(cnx)
cursor = instrumented_cnx.cursor()
cursor.execute("INSERT INTO test (testField) VALUES (123)")
cursor.close()
cnx.close()
instrumented_cnx.close()
API
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import MySQLdb
from opentelemetry.instrumentation.mysqlclient import MySQLClientInstrumentor
# Call instrument() to wrap all database connections
MySQLClientInstrumentor().instrument(enable_commenter=True, commenter_options={})
cnx = MySQLdb.connect(database="MySQL_Database")
Expand All @@ -56,6 +56,13 @@
cursor.close()
cnx.close()
.. code:: python
import MySQLdb
from opentelemetry.instrumentation.mysqlclient import MySQLClientInstrumentor
# Alternatively, use instrument_connection for an individual connection
cnx = MySQLdb.connect(database="MySQL_Database")
instrumented_cnx = MySQLClientInstrumentor.instrument_connection(
cnx,
enable_commenter=True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
import psycopg
from opentelemetry.instrumentation.psycopg import PsycopgInstrumentor
# Call instrument() to wrap all database connections
PsycopgInstrumentor().instrument()
cnx = psycopg.connect(database='Database')
Expand All @@ -98,6 +98,13 @@
cursor.close()
cnx.close()
.. code-block:: python
import psycopg
from opentelemetry.instrumentation.psycopg import PsycopgInstrumentor
# Alternatively, use instrument_connection for an individual connection
cnx = psycopg.connect(database='Database')
instrumented_cnx = instrument_connection(cnx)
cursor = instrumented_cnx.cursor()
cursor.execute("INSERT INTO test (testField) VALUES (123)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
import psycopg2
from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor
# Call instrument() to wrap all database connections
Psycopg2Instrumentor().instrument()
cnx = psycopg2.connect(database='Database')
Expand All @@ -98,6 +98,13 @@
cursor.close()
cnx.close()
.. code-block:: python
import psycopg2
from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor
# Alternatively, use instrument_connection for an individual connection
cnx = psycopg2.connect(database='Database')
instrumented_cnx = Psycopg2Instrumentor.instrument_connection(cnx)
cursor = instrumented_cnx.cursor()
cursor.execute("INSERT INTO test (testField) VALUES (123)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import pymysql
from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor
# Call instrument() to wrap all database connections
PyMySQLInstrumentor().instrument()
cnx = pymysql.connect(database="MySQL_Database")
Expand All @@ -35,6 +36,28 @@
cursor.close()
cnx.close()
.. code:: python
import pymysql
from opentelemetry.instrumentation.pymysql import PyMySQLInstrumentor
# Alternatively, use instrument_connection for an individual connection
cnx = pymysql.connect(database="MySQL_Database")
instrumented_cnx = PyMySQLInstrumentor().instrument_connection(
cnx,
enable_commenter=True,
commenter_options={
"db_driver": True,
"mysql_client_version": True
}
)
cursor = instrumented_cnx.cursor()
cursor.execute("INSERT INTO test (testField) VALUES (123)"
instrumented_cnx.commit()
cursor.close()
instrumented_cnx.close()
SQLCOMMENTER
*****************************************
You can optionally configure PyMySQL instrumentation to enable sqlcommenter which enriches
Expand All @@ -57,20 +80,6 @@
cursor.close()
cnx.close()
instrumented_cnx = PyMySQLInstrumentor().instrument_connection(
cnx,
enable_commenter=True,
commenter_options={
"db_driver": True,
"mysql_client_version": True
}
)
cursor = instrumented_cnx.cursor()
cursor.execute("INSERT INTO test (testField) VALUES (123)"
instrumented_cnx.commit()
cursor.close()
instrumented_cnx.close()
For example,
::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import sqlite3
from opentelemetry.instrumentation.sqlite3 import SQLite3Instrumentor
# Call instrument() to wrap all database connections
SQLite3Instrumentor().instrument()
cnx = sqlite3.connect(':memory:')
Expand All @@ -36,6 +36,13 @@
cursor.execute("INSERT INTO test (testField) VALUES (123)")
cursor.close()
cnx.close()
.. code:: python
import sqlite3
from opentelemetry.instrumentation.sqlite3 import SQLite3Instrumentor
# Alternatively, use instrument_connection for an individual connection
conn = sqlite3.connect(":memory:")
instrumented_connection = SQLite3Instrumentor.instrument_connection(conn)
cursor = instrumented_connection.cursor()
Expand Down

0 comments on commit e11ec1a

Please sign in to comment.