lanternfish 0.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,296 @@
1
+ # redis.py
2
+ # Görev: Redis client metotlarını trace etmek (semantic memory tracking için)
3
+ # Yapmaz: Kendi başına instrument çağırmaz, default tracer_provider kullanır.
4
+
5
+ """
6
+ Lanternfish Redis Instrumentation
7
+
8
+ redis-py kütüphanesini otomatik olarak izler.
9
+ Agent'ın cache işlemlerini (GET, SET, HGET, pipeline, vb.) yakalar.
10
+
11
+ Kullanım:
12
+ from lanternfish import instrument
13
+ import redis
14
+
15
+ instrument(api_key="your-api-key", endpoint="http://localhost:8000")
16
+
17
+ r = redis.Redis()
18
+ r.set("key", "value") # Otomatik izlenir
19
+ r.get("key") # Otomatik izlenir (hit/miss detection)
20
+ """
21
+
22
+ import threading
23
+ from typing import Any, Dict, Tuple, Optional
24
+ from opentelemetry import trace
25
+ from opentelemetry.sdk.trace import TracerProvider
26
+
27
+
28
+ def instrument_redis(tracer_provider: TracerProvider) -> None:
29
+ """Redis kütüphanelerini (redis-py sync + async) izler.
30
+
31
+ Args:
32
+ tracer_provider: Span'lerin gönderileceği TracerProvider.
33
+ """
34
+ _instrument_redis_sync(tracer_provider)
35
+ _instrument_redis_async(tracer_provider)
36
+
37
+
38
+ def _truncate_value(value: Any, max_length: int = 500) -> str:
39
+ """Value'yu string'e çevir ve kısalt."""
40
+ if value is None:
41
+ return ""
42
+
43
+ try:
44
+ if isinstance(value, bytes):
45
+ value_str = value.decode('utf-8', errors='replace')
46
+ else:
47
+ value_str = str(value)
48
+ except Exception:
49
+ value_str = "<binary>"
50
+
51
+ if len(value_str) > max_length:
52
+ value_str = value_str[:max_length] + f"... [truncated, {len(value_str)} chars total]"
53
+
54
+ return value_str
55
+
56
+
57
+ def _extract_command_info(args: Tuple[Any, ...]) -> Dict[str, Any]:
58
+ """Redis command args'larından bilgi çıkar.
59
+
60
+ Redis komutları genelde: (COMMAND, key, *args) formatında.
61
+ """
62
+ if not args:
63
+ return {"command": "UNKNOWN", "key": None, "args_count": 0}
64
+
65
+ command = str(args[0]).upper() if args[0] else "UNKNOWN"
66
+ key = args[1] if len(args) > 1 else None
67
+
68
+ # Key'i string'e çevir
69
+ if isinstance(key, bytes):
70
+ try:
71
+ key = key.decode('utf-8', errors='replace')
72
+ except Exception:
73
+ key = "<binary>"
74
+ elif key is not None:
75
+ key = str(key)
76
+
77
+ return {
78
+ "command": command,
79
+ "key": key,
80
+ "args_count": len(args) - 2 if len(args) > 2 else 0,
81
+ }
82
+
83
+
84
+ # Cache hit/miss detection için thread-local storage
85
+ _redis_state = threading.local()
86
+
87
+
88
+ def _instrument_redis_sync(tracer_provider: TracerProvider) -> None:
89
+ """redis-py sync client'ı izler."""
90
+ try:
91
+ import redis
92
+ from redis.client import Redis
93
+ except ImportError:
94
+ return
95
+
96
+ tracer = trace.get_tracer("lanternfish.redis.sync", tracer_provider=tracer_provider)
97
+
98
+ # Orijinal execute_command'ı kaydet
99
+ original_execute_command = Redis.execute_command
100
+
101
+ def traced_execute_command(self: Any, *args: Any, **kwargs: Any) -> Any:
102
+ """Redis execute_command izleme (tüm komutlar buradan geçer)."""
103
+ cmd_info = _extract_command_info(args)
104
+ command = cmd_info["command"]
105
+ key = cmd_info["key"]
106
+
107
+ span_name = f"redis.command {command}"
108
+
109
+ with tracer.start_as_current_span(record_exception=False, set_status_on_exception=False, name=span_name) as span:
110
+ span.set_attribute("redis.system", "redis-py")
111
+ span.set_attribute("redis.command", command)
112
+
113
+ if key:
114
+ span.set_attribute("redis.key", key[:200]) # Key'i kısalt
115
+
116
+ # Database index
117
+ try:
118
+ db_index = self.connection_pool.connection_kwargs.get('db', 0)
119
+ span.set_attribute("redis.db", db_index)
120
+ except Exception:
121
+ pass
122
+
123
+ # Host info
124
+ try:
125
+ host = self.connection_pool.connection_kwargs.get('host', 'localhost')
126
+ port = self.connection_pool.connection_kwargs.get('port', 6379)
127
+ span.set_attribute("redis.host", f"{host}:{port}")
128
+ except Exception:
129
+ pass
130
+
131
+ # Args count
132
+ if cmd_info["args_count"] > 0:
133
+ span.set_attribute("redis.args.count", cmd_info["args_count"])
134
+
135
+ # Value (SET commands için)
136
+ if command in ["SET", "SETEX", "PSETEX", "SETNX", "HSET", "HMSET"]:
137
+ if len(args) > 2:
138
+ value = args[2]
139
+ value_str = _truncate_value(value)
140
+ span.set_attribute("redis.value.size", len(value_str))
141
+
142
+ try:
143
+ result = original_execute_command(self, *args, **kwargs)
144
+
145
+ # Cache hit/miss detection (GET commands için)
146
+ if command in ["GET", "HGET", "MGET"]:
147
+ if result is None:
148
+ span.set_attribute("redis.hit", False)
149
+ span.set_attribute("redis.cache.status", "miss")
150
+ else:
151
+ span.set_attribute("redis.hit", True)
152
+ span.set_attribute("redis.cache.status", "hit")
153
+
154
+ # Result size
155
+ result_str = _truncate_value(result)
156
+ span.set_attribute("redis.result.size", len(result_str))
157
+
158
+ # EXISTS command için
159
+ elif command == "EXISTS":
160
+ span.set_attribute("redis.exists", bool(result))
161
+
162
+ # DEL command için
163
+ elif command == "DEL":
164
+ span.set_attribute("redis.deleted.count", int(result) if result else 0)
165
+
166
+ # KEYS/SCAN command için
167
+ elif command in ["KEYS", "SMEMBERS", "HKEYS", "HVALS"]:
168
+ if isinstance(result, list):
169
+ span.set_attribute("redis.result.count", len(result))
170
+
171
+ # TTL command için
172
+ elif command == "TTL":
173
+ span.set_attribute("redis.ttl", int(result) if result is not None else -1)
174
+
175
+ span.set_status(trace.StatusCode.OK)
176
+ return result
177
+
178
+ except Exception as e:
179
+ span.set_status(trace.StatusCode.ERROR, type(e).__name__)
180
+ span.set_attribute("redis.error.type", type(e).__name__)
181
+ raise
182
+
183
+ # Uygula
184
+ Redis.execute_command = traced_execute_command
185
+
186
+ # Pipeline desteği
187
+ _instrument_redis_pipeline(tracer)
188
+
189
+
190
+ def _instrument_redis_pipeline(tracer: trace.Tracer) -> None:
191
+ """Redis pipeline izleme."""
192
+ try:
193
+ from redis.client import Pipeline
194
+ except ImportError:
195
+ return
196
+
197
+ original_execute = Pipeline.execute
198
+
199
+ def traced_execute(self: Any, raise_on_error: bool = True) -> Any:
200
+ """Pipeline execute izleme."""
201
+ command_count = len(self.command_stack)
202
+
203
+ span_name = f"redis.pipeline ({command_count} commands)"
204
+
205
+ with tracer.start_as_current_span(record_exception=False, set_status_on_exception=False, name=span_name) as span:
206
+ span.set_attribute("redis.system", "redis-py")
207
+ span.set_attribute("redis.operation", "pipeline")
208
+ span.set_attribute("redis.pipeline.command_count", command_count)
209
+
210
+ # İlk birkaç komutu listele (max 5)
211
+ commands = []
212
+ for cmd_args in self.command_stack[:5]:
213
+ if cmd_args and cmd_args[0]:
214
+ commands.append(cmd_args[0].upper() if isinstance(cmd_args[0], str) else str(cmd_args[0]))
215
+
216
+ if commands:
217
+ span.set_attribute("redis.pipeline.commands", ", ".join(commands))
218
+
219
+ if command_count > 5:
220
+ span.set_attribute("redis.pipeline.more", command_count - 5)
221
+
222
+ try:
223
+ result = original_execute(self, raise_on_error=raise_on_error)
224
+ span.set_status(trace.StatusCode.OK)
225
+ return result
226
+ except Exception as e:
227
+ span.set_status(trace.StatusCode.ERROR, type(e).__name__)
228
+ span.set_attribute("redis.error.type", type(e).__name__)
229
+ raise
230
+
231
+ Pipeline.execute = traced_execute
232
+
233
+
234
+ def _instrument_redis_async(tracer_provider: TracerProvider) -> None:
235
+ """redis-py async client'ı izler (redis.asyncio)."""
236
+ try:
237
+ from redis.asyncio import Redis as AsyncRedis
238
+ except ImportError:
239
+ return
240
+
241
+ tracer = trace.get_tracer("lanternfish.redis.async", tracer_provider=tracer_provider)
242
+
243
+ # Orijinal execute_command'ı kaydet
244
+ original_execute_command = AsyncRedis.execute_command
245
+
246
+ async def traced_execute_command(self: Any, *args: Any, **kwargs: Any) -> Any:
247
+ """Async Redis execute_command izleme."""
248
+ cmd_info = _extract_command_info(args)
249
+ command = cmd_info["command"]
250
+ key = cmd_info["key"]
251
+
252
+ span_name = f"redis.command {command}"
253
+
254
+ with tracer.start_as_current_span(record_exception=False, set_status_on_exception=False, name=span_name) as span:
255
+ span.set_attribute("redis.system", "redis-py.async")
256
+ span.set_attribute("redis.command", command)
257
+
258
+ if key:
259
+ span.set_attribute("redis.key", key[:200])
260
+
261
+ # Database index
262
+ try:
263
+ db_index = self.connection_pool.connection_kwargs.get('db', 0)
264
+ span.set_attribute("redis.db", db_index)
265
+ except Exception:
266
+ pass
267
+
268
+ # Host info
269
+ try:
270
+ host = self.connection_pool.connection_kwargs.get('host', 'localhost')
271
+ port = self.connection_pool.connection_kwargs.get('port', 6379)
272
+ span.set_attribute("redis.host", f"{host}:{port}")
273
+ except Exception:
274
+ pass
275
+
276
+ try:
277
+ result = await original_execute_command(self, *args, **kwargs)
278
+
279
+ # Cache hit/miss detection
280
+ if command in ["GET", "HGET"]:
281
+ if result is None:
282
+ span.set_attribute("redis.hit", False)
283
+ span.set_attribute("redis.cache.status", "miss")
284
+ else:
285
+ span.set_attribute("redis.hit", True)
286
+ span.set_attribute("redis.cache.status", "hit")
287
+
288
+ span.set_status(trace.StatusCode.OK)
289
+ return result
290
+
291
+ except Exception as e:
292
+ span.set_status(trace.StatusCode.ERROR, type(e).__name__)
293
+ span.set_attribute("redis.error.type", type(e).__name__)
294
+ raise
295
+
296
+ AsyncRedis.execute_command = traced_execute_command
@@ -0,0 +1,300 @@
1
+ # sql.py
2
+ # Gorev: SQLAlchemy, psycopg2 ve sqlite3 SQL sorgularini OpenTelemetry ile izlemek
3
+ # Yapmaz: TracerProvider veya exporter olusturmaz; veritabanina baglanmaz
4
+ #
5
+ # Gercek API:
6
+ # SQLAlchemy: before_cursor_execute / after_cursor_execute / handle_error event'leri
7
+ # psycopg2 : cursor.execute() ve cursor.executemany() monkey-patch
8
+ # sqlite3 : sqlite3.Cursor.execute() ve sqlite3.Cursor.executemany() monkey-patch
9
+ #
10
+ # Strateji:
11
+ # SQLAlchemy: engine event listener (before/after/error) ile span lifecycle yonetimi.
12
+ # thread-local stack ile iç içe sorgu destegi saglanir.
13
+ # psycopg2 : cursor.execute() patch'i, context manager span kullanimi.
14
+ # sqlite3 : ayni sekilde Cursor.execute() patch'i; her zaman mevcut (built-in).
15
+ #
16
+ # Guvenlik:
17
+ # _sanitize_query(): string ve numeric literal'lari '?' ile maskeler.
18
+ # Credentials (parola, email vb.) span'lere yazilmaz.
19
+
20
+ import re
21
+ import threading
22
+ from typing import Any
23
+
24
+ from opentelemetry import context as otel_context
25
+ from opentelemetry import trace as otel_trace
26
+ from opentelemetry.sdk.trace import TracerProvider
27
+ from opentelemetry.trace import StatusCode
28
+
29
+
30
+ # ── Yardimci fonksiyonlar ────────────────────────────────────────────────────
31
+
32
+ def _sanitize_query(query: str, max_length: int = 500) -> str:
33
+ """SQL sorgusundaki hassas verileri maskele ve uzun sorgulari kisalt.
34
+
35
+ String literal'lari '?' ile, numeric literal'lari ? ile degistirir.
36
+ Boylece parola, email gibi degerler span'e yazilmaz.
37
+ """
38
+ if not query:
39
+ return ""
40
+ sanitized = re.sub(r"'[^']*'", "'?'", query)
41
+ sanitized = re.sub(r"\b\d+\b", "?", sanitized)
42
+ sanitized = re.sub(r"\s+", " ", sanitized).strip()
43
+ if len(sanitized) > max_length:
44
+ sanitized = sanitized[:max_length] + "... [truncated]"
45
+ return sanitized
46
+
47
+
48
+ def _extract_query_type(query: str) -> str:
49
+ """SQL sorgusunun tipini dondur (SELECT, INSERT, UPDATE, DELETE, vb.)."""
50
+ if not query:
51
+ return "UNKNOWN"
52
+ first = query.strip().upper().split()[0] if query.strip() else ""
53
+ known = {"SELECT", "INSERT", "UPDATE", "DELETE", "CREATE", "DROP", "ALTER",
54
+ "BEGIN", "COMMIT", "ROLLBACK", "TRUNCATE", "EXPLAIN"}
55
+ return first if first in known else ("OTHER" if first else "UNKNOWN")
56
+
57
+
58
+ # ── SQLAlchemy ───────────────────────────────────────────────────────────────
59
+
60
+ def _instrument_sqlalchemy(tracer_provider: TracerProvider) -> None:
61
+ """SQLAlchemy Engine event listener'lari ile SQL span'leri olusturur."""
62
+ try:
63
+ from sqlalchemy import event
64
+ from sqlalchemy.engine import Engine
65
+ except ImportError:
66
+ return
67
+
68
+ tracer = otel_trace.get_tracer("lanternfish.sql.sqlalchemy", tracer_provider=tracer_provider)
69
+ _local = threading.local()
70
+
71
+ @event.listens_for(Engine, "before_cursor_execute")
72
+ def _before(conn, cursor, statement, parameters, context, executemany):
73
+ query_type = _extract_query_type(statement)
74
+ db_type = conn.engine.name or "unknown"
75
+ host = getattr(conn.engine.url, "host", None) or "localhost"
76
+
77
+ span = tracer.start_span(
78
+ f"sql.query {query_type} ({db_type})",
79
+ context=otel_context.get_current(),
80
+ )
81
+ span.set_attribute("sql.system", "sqlalchemy")
82
+ span.set_attribute("sql.database", db_type)
83
+ span.set_attribute("sql.host", host)
84
+ span.set_attribute("sql.query", _sanitize_query(statement))
85
+ span.set_attribute("sql.query.type", query_type)
86
+ span.set_attribute("sql.query.original_length", len(statement))
87
+
88
+ if parameters:
89
+ count = len(parameters) if isinstance(parameters, (list, tuple, dict)) else 1
90
+ span.set_attribute("sql.parameters.count", count)
91
+ if executemany:
92
+ span.set_attribute("sql.executemany", True)
93
+
94
+ token = otel_context.attach(otel_trace.set_span_in_context(span))
95
+ if not hasattr(_local, "stack"):
96
+ _local.stack = []
97
+ _local.stack.append((span, token))
98
+
99
+ @event.listens_for(Engine, "after_cursor_execute")
100
+ def _after(conn, cursor, statement, parameters, context, executemany):
101
+ if not getattr(_local, "stack", None):
102
+ return
103
+ span, token = _local.stack.pop()
104
+ try:
105
+ if cursor.rowcount >= 0:
106
+ span.set_attribute("sql.row_count", cursor.rowcount)
107
+ except Exception:
108
+ pass
109
+ span.set_status(StatusCode.OK)
110
+ span.end()
111
+ otel_context.detach(token)
112
+
113
+ @event.listens_for(Engine, "handle_error")
114
+ def _error(exception_context):
115
+ if not getattr(_local, "stack", None):
116
+ return
117
+ span, token = _local.stack.pop()
118
+ exc = exception_context.original_exception
119
+ span.set_status(StatusCode.ERROR, type(exc).__name__)
120
+ span.set_attribute("sql.error.type", type(exc).__name__)
121
+ span.end()
122
+ otel_context.detach(token)
123
+
124
+
125
+ # ── psycopg2 ─────────────────────────────────────────────────────────────────
126
+
127
+ def _instrument_psycopg2(tracer_provider: TracerProvider) -> None:
128
+ """psycopg2 connect() patch'i — cursor_factory subclass ile execute izleme.
129
+
130
+ psycopg2.extensions.cursor C extension tipi oldugu icin dogrudan patch'lenemez.
131
+ Bunun yerine psycopg2.connect() sarmalanir ve her baglanti icin traced cursor
132
+ subclass'i inject edilir.
133
+ """
134
+ try:
135
+ import psycopg2
136
+ import psycopg2.extensions
137
+ except ImportError:
138
+ return
139
+
140
+ tracer = otel_trace.get_tracer("lanternfish.sql.psycopg2", tracer_provider=tracer_provider)
141
+
142
+ class TracedCursor(psycopg2.extensions.cursor):
143
+ def _span_name(self, query: Any) -> str:
144
+ q = query.decode("utf-8", errors="replace") if isinstance(query, bytes) else str(query)
145
+ return f"sql.query {_extract_query_type(q)} (postgresql)", q
146
+
147
+ def execute(self, query: Any, vars: Any = None) -> None:
148
+ name, q = self._span_name(query)
149
+ with tracer.start_as_current_span(record_exception=False, set_status_on_exception=False, name=name) as span:
150
+ span.set_attribute("sql.system", "psycopg2")
151
+ span.set_attribute("sql.database", "postgresql")
152
+ span.set_attribute("sql.query", _sanitize_query(q))
153
+ span.set_attribute("sql.query.type", _extract_query_type(q))
154
+ try:
155
+ conn = self.connection
156
+ if conn and hasattr(conn, "info") and conn.info:
157
+ span.set_attribute("sql.host", conn.info.host or "localhost")
158
+ span.set_attribute("sql.dbname", conn.info.dbname or "unknown")
159
+ except Exception:
160
+ pass
161
+ if vars is not None:
162
+ count = len(vars) if isinstance(vars, (list, tuple, dict)) else 1
163
+ span.set_attribute("sql.parameters.count", count)
164
+ try:
165
+ result = super().execute(query, vars)
166
+ if self.rowcount >= 0:
167
+ span.set_attribute("sql.row_count", self.rowcount)
168
+ span.set_status(StatusCode.OK)
169
+ return result
170
+ except Exception as exc:
171
+ span.set_status(StatusCode.ERROR, type(exc).__name__)
172
+ span.set_attribute("sql.error.type", type(exc).__name__)
173
+ raise
174
+
175
+ def executemany(self, query: Any, vars_list: Any) -> None:
176
+ name, q = self._span_name(query)
177
+ name = name + " [executemany]"
178
+ with tracer.start_as_current_span(record_exception=False, set_status_on_exception=False, name=name) as span:
179
+ span.set_attribute("sql.system", "psycopg2")
180
+ span.set_attribute("sql.database", "postgresql")
181
+ span.set_attribute("sql.query", _sanitize_query(q))
182
+ span.set_attribute("sql.query.type", _extract_query_type(q))
183
+ span.set_attribute("sql.executemany", True)
184
+ if vars_list is not None:
185
+ span.set_attribute("sql.executemany.count", len(vars_list))
186
+ try:
187
+ result = super().executemany(query, vars_list)
188
+ if self.rowcount >= 0:
189
+ span.set_attribute("sql.row_count", self.rowcount)
190
+ span.set_status(StatusCode.OK)
191
+ return result
192
+ except Exception as exc:
193
+ span.set_status(StatusCode.ERROR, type(exc).__name__)
194
+ raise
195
+
196
+ original_connect = psycopg2.connect
197
+
198
+ def traced_connect(*args: Any, **kwargs: Any) -> Any:
199
+ kwargs.setdefault("cursor_factory", TracedCursor)
200
+ return original_connect(*args, **kwargs)
201
+
202
+ psycopg2.connect = traced_connect
203
+
204
+
205
+ # ── sqlite3 ──────────────────────────────────────────────────────────────────
206
+
207
+ def _instrument_sqlite3(tracer_provider: TracerProvider) -> None:
208
+ """sqlite3.connect() patch'i — TracedCursor + TracedConnection subclass ile.
209
+
210
+ sqlite3.Cursor C extension tipi oldugu icin dogrudan patch'lenemez.
211
+ sqlite3.connect() sarmalanir ve her baglanti icin TracedConnection
212
+ (cursor() -> TracedCursor) inject edilir.
213
+ """
214
+ import sqlite3
215
+
216
+ tracer = otel_trace.get_tracer("lanternfish.sql.sqlite3", tracer_provider=tracer_provider)
217
+
218
+ class TracedCursor(sqlite3.Cursor):
219
+ def execute(self, sql: str, parameters: Any = ()) -> "TracedCursor":
220
+ query_type = _extract_query_type(sql)
221
+ with tracer.start_as_current_span(record_exception=False, set_status_on_exception=False, name=f"sql.query {query_type} (sqlite)") as span:
222
+ span.set_attribute("sql.system", "sqlite3")
223
+ span.set_attribute("sql.database", "sqlite")
224
+ span.set_attribute("sql.query", _sanitize_query(sql))
225
+ span.set_attribute("sql.query.type", query_type)
226
+ span.set_attribute("sql.host", "localhost")
227
+ if parameters:
228
+ count = len(parameters) if isinstance(parameters, (list, tuple, dict)) else 1
229
+ span.set_attribute("sql.parameters.count", count)
230
+ try:
231
+ result = super().execute(sql, parameters)
232
+ if self.rowcount >= 0:
233
+ span.set_attribute("sql.row_count", self.rowcount)
234
+ span.set_status(StatusCode.OK)
235
+ return result
236
+ except Exception as exc:
237
+ span.set_status(StatusCode.ERROR, type(exc).__name__)
238
+ span.set_attribute("sql.error.type", type(exc).__name__)
239
+ raise
240
+
241
+ def executemany(self, sql: str, seq_of_parameters: Any) -> "TracedCursor":
242
+ query_type = _extract_query_type(sql)
243
+ with tracer.start_as_current_span(record_exception=False, set_status_on_exception=False, name=
244
+ f"sql.query {query_type} (sqlite) [executemany]"
245
+ ) as span:
246
+ span.set_attribute("sql.system", "sqlite3")
247
+ span.set_attribute("sql.database", "sqlite")
248
+ span.set_attribute("sql.query", _sanitize_query(sql))
249
+ span.set_attribute("sql.query.type", query_type)
250
+ span.set_attribute("sql.executemany", True)
251
+ if seq_of_parameters is not None:
252
+ span.set_attribute("sql.executemany.count", len(seq_of_parameters))
253
+ try:
254
+ result = super().executemany(sql, seq_of_parameters)
255
+ if self.rowcount >= 0:
256
+ span.set_attribute("sql.row_count", self.rowcount)
257
+ span.set_status(StatusCode.OK)
258
+ return result
259
+ except Exception as exc:
260
+ span.set_status(StatusCode.ERROR, type(exc).__name__)
261
+ raise
262
+
263
+ class TracedConnection(sqlite3.Connection):
264
+ def cursor(self, factory: Any = None) -> TracedCursor:
265
+ return super().cursor(factory or TracedCursor)
266
+
267
+ # C-level Connection.execute() Python cursor() metodunu bypass eder;
268
+ # Python-level override ile TracedCursor'a yonlendiriyoruz.
269
+ def execute(self, sql: str, parameters: Any = ()) -> TracedCursor:
270
+ return self.cursor().execute(sql, parameters)
271
+
272
+ def executemany(self, sql: str, seq_of_parameters: Any) -> TracedCursor:
273
+ return self.cursor().executemany(sql, seq_of_parameters)
274
+
275
+ original_connect = sqlite3.connect
276
+
277
+ def traced_connect(database: Any, *args: Any, **kwargs: Any) -> TracedConnection:
278
+ kwargs.setdefault("factory", TracedConnection)
279
+ return original_connect(database, *args, **kwargs)
280
+
281
+ sqlite3.connect = traced_connect
282
+
283
+
284
+ # ── Ana fonksiyon ────────────────────────────────────────────────────────────
285
+
286
+ def instrument_sql(tracer_provider: TracerProvider) -> None:
287
+ """SQL kütüphanelerini (SQLAlchemy, psycopg2, sqlite3) OpenTelemetry ile izler.
288
+
289
+ Her kütüphane icin merkezi sorgu noktasini patch'ler. Paket yuklu degilse
290
+ o kütüphane sessizce atlanir. sqlite3 her zaman mevcuttur (built-in Python).
291
+
292
+ Yakalananlar: query type, sanitized query, host, database, row count,
293
+ parameter count, executemany count, error type/message.
294
+
295
+ Args:
296
+ tracer_provider: Span'lerin gonderilecegi TracerProvider.
297
+ """
298
+ _instrument_sqlalchemy(tracer_provider)
299
+ _instrument_psycopg2(tracer_provider)
300
+ _instrument_sqlite3(tracer_provider)