-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathtest_sqlite3.py
151 lines (138 loc) · 6.18 KB
/
test_sqlite3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import importlib
import sqlite3
from unittest import mock
import pytest
from inline_snapshot import snapshot
from opentelemetry.instrumentation.sqlite3 import SQLite3Instrumentor
import logfire
import logfire._internal.integrations.httpx
import logfire._internal.integrations.sqlite3
from logfire.testing import TestExporter
def test_sqlite3_instrumentation(exporter: TestExporter):
logfire.instrument_sqlite3()
with sqlite3.connect(':memory:') as conn:
cur = conn.cursor()
cur.execute('DROP TABLE IF EXISTS test')
cur.execute('CREATE TABLE test (id INT PRIMARY KEY, name VARCHAR(255))')
cur.execute('INSERT INTO test (id, name) VALUES (1, "test")')
values = cur.execute('SELECT * FROM test').fetchall()
assert values == [(1, 'test')]
assert exporter.exported_spans_as_dict() == 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': 'sqlite',
'db.name': '',
'db.statement': 'DROP TABLE IF EXISTS test',
},
},
{
'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': 'sqlite',
'db.name': '',
'db.statement': 'CREATE TABLE test (id INT PRIMARY KEY, name VARCHAR(255))',
},
},
{
'name': 'INSERT',
'context': {'trace_id': 3, 'span_id': 5, 'is_remote': False},
'parent': None,
'start_time': 5000000000,
'end_time': 6000000000,
'attributes': {
'logfire.span_type': 'span',
'logfire.msg': 'INSERT INTO test (id, name) VALUES (1, "test")',
'db.system': 'sqlite',
'db.name': '',
'db.statement': 'INSERT INTO test (id, name) VALUES (1, "test")',
},
},
{
'name': 'SELECT',
'context': {'trace_id': 4, 'span_id': 7, 'is_remote': False},
'parent': None,
'start_time': 7000000000,
'end_time': 8000000000,
'attributes': {
'logfire.span_type': 'span',
'logfire.msg': 'SELECT * FROM test',
'db.system': 'sqlite',
'db.name': '',
'db.statement': 'SELECT * FROM test',
},
},
]
)
SQLite3Instrumentor().uninstrument()
def test_instrument_sqlite3_connection(exporter: TestExporter):
with sqlite3.connect(':memory:') as conn:
cur = conn.cursor()
cur.execute('DROP TABLE IF EXISTS test')
cur.execute('CREATE TABLE test (id INT PRIMARY KEY, name VARCHAR(255))')
conn = logfire.instrument_sqlite3(conn)
cur = conn.cursor()
cur.execute('INSERT INTO test (id, name) VALUES (1, "test")')
values = cur.execute('SELECT * FROM test').fetchall()
assert values == [(1, 'test')]
assert exporter.exported_spans_as_dict() == 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': 'sqlite',
'db.name': '',
'db.statement': 'INSERT INTO test (id, name) VALUES (1, "test")',
},
},
{
'name': 'SELECT',
'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': 'SELECT * FROM test',
'db.system': 'sqlite',
'db.name': '',
'db.statement': 'SELECT * FROM test',
},
},
]
)
spans_before_uninstrument = len(exporter.exported_spans_as_dict())
conn: sqlite3.Connection = SQLite3Instrumentor().uninstrument_connection(conn)
cur = conn.cursor()
cur.execute('INSERT INTO test (id, name) VALUES (2, "test-2")')
assert len(exporter.exported_spans_as_dict()) == spans_before_uninstrument
values = cur.execute('SELECT * FROM test').fetchall()
assert values == [(1, 'test'), (2, 'test-2')]
def test_missing_opentelemetry_dependency() -> None:
with mock.patch.dict('sys.modules', {'opentelemetry.instrumentation.sqlite3': None}):
with pytest.raises(RuntimeError) as exc_info:
importlib.reload(logfire._internal.integrations.sqlite3)
assert str(exc_info.value) == snapshot("""\
`logfire.instrument_sqlite3()` requires the `opentelemetry-instrumentation-sqlite3` package.
You can install this with:
pip install 'logfire[sqlite3]'\
""")