Buckets:

download
raw
5.64 kB
from __future__ import annotations
import importlib
from unittest import mock
import mysql.connector
import pytest
from dirty_equals import IsInt
from inline_snapshot import snapshot
from opentelemetry.instrumentation.mysql import MySQLInstrumentor
from testcontainers.mysql import MySqlContainer
import logfire
import logfire._internal.integrations.mysql
from logfire.testing import TestExporter
@pytest.fixture(scope='module')
def mysql_container():
with MySqlContainer() as mysql_container:
yield mysql_container
def get_mysql_connection(mysql_container: MySqlContainer):
host = mysql_container.get_container_host_ip()
port = mysql_container.get_exposed_port(3306)
connection = mysql.connector.connect(host=host, port=port, user='test', password='test', database='test')
return connection
def test_mysql_instrumentation(exporter: TestExporter, mysql_container: MySqlContainer):
# The instrumentation's declared upper bound (mysql-connector-python < 10.0) rejects the
# server-version-aligned 26.x releases even though they're functionally unchanged:
# https://github.com/open-telemetry/opentelemetry-python-contrib/issues/4921
logfire.instrument_mysql(skip_dep_check=True)
with get_mysql_connection(mysql_container) as conn:
with conn.cursor() as cursor:
cursor.execute('DROP TABLE IF EXISTS test')
cursor.execute('CREATE TABLE test (id INT PRIMARY KEY, name VARCHAR(255))')
assert exporter.exported_spans_as_dict(parse_json_attributes=True) == snapshot(
[
{
'name': 'DROP',
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
'parent': None,
'start_time': 1000000000,
'end_time': 2000000000,
'attributes': {
'logfire.span_type': 'span',
'logfire.msg': 'DROP TABLE IF EXISTS test',
'db.system': 'mysql',
'db.name': 'test',
'db.statement': 'DROP TABLE IF EXISTS test',
'db.user': 'test',
'net.peer.name': 'localhost',
'server.address': 'localhost',
'net.peer.port': IsInt(),
'server.port': IsInt(),
},
},
{
'name': 'CREATE',
'context': {'trace_id': 2, 'span_id': 3, 'is_remote': False},
'parent': None,
'start_time': 3000000000,
'end_time': 4000000000,
'attributes': {
'logfire.span_type': 'span',
'logfire.msg': 'CREATE TABLE test (id INT PRIMARY KEY, name VARCHAR(255))',
'db.system': 'mysql',
'db.name': 'test',
'db.statement': 'CREATE TABLE test (id INT PRIMARY KEY, name VARCHAR(255))',
'db.user': 'test',
'net.peer.name': 'localhost',
'server.address': 'localhost',
'net.peer.port': IsInt(),
'server.port': IsInt(),
},
},
]
)
MySQLInstrumentor().uninstrument()
def test_instrument_mysql_connection(exporter: TestExporter, mysql_container: MySqlContainer):
with get_mysql_connection(mysql_container) as conn:
with conn.cursor() as cursor:
cursor.execute('DROP TABLE IF EXISTS test')
cursor.execute('CREATE TABLE test (id INT PRIMARY KEY, name VARCHAR(255))')
assert exporter.exported_spans_as_dict(parse_json_attributes=True) == []
conn = logfire.instrument_mysql(conn)
with conn.cursor() as cursor:
cursor.execute('INSERT INTO test (id, name) VALUES (1, "test")')
assert exporter.exported_spans_as_dict(parse_json_attributes=True) == snapshot(
[
{
'name': 'INSERT',
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
'parent': None,
'start_time': 1000000000,
'end_time': 2000000000,
'attributes': {
'logfire.span_type': 'span',
'logfire.msg': 'INSERT INTO test (id, name) VALUES (1, "test")',
'db.system': 'mysql',
'db.name': 'test',
'db.statement': 'INSERT INTO test (id, name) VALUES (1, "test")',
'db.user': 'test',
'net.peer.name': 'localhost',
'server.address': 'localhost',
'net.peer.port': IsInt(),
'server.port': IsInt(),
},
}
]
)
conn = MySQLInstrumentor().uninstrument_connection(conn) # type: ignore
with conn.cursor() as cursor:
cursor.execute('INSERT INTO test (id, name) VALUES (2, "test-2")')
assert len(exporter.exported_spans_as_dict(parse_json_attributes=True)) == 1
def test_missing_opentelemetry_dependency() -> None:
with mock.patch.dict('sys.modules', {'opentelemetry.instrumentation.mysql': None}):
with pytest.raises(RuntimeError) as exc_info:
importlib.reload(logfire._internal.integrations.mysql)
assert str(exc_info.value) == snapshot("""\
`logfire.instrument_mysql()` requires the `opentelemetry-instrumentation-mysql` package.
You can install this with:
pip install 'logfire[mysql]'\
""")

Xet Storage Details

Size:
5.64 kB
·
Xet hash:
de46451fb8ea54d656a780e48fb94dcd7347e579534fa2cb87605f5acb66854f

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.