mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-07-30 05:32:30 +08:00
Update sqlalchemy to follow semantic conventions
This commit is contained in:
@ -24,10 +24,9 @@ _HOST = "net.peer.name"
|
|||||||
_PORT = "net.peer.port"
|
_PORT = "net.peer.port"
|
||||||
# Database semantic conventions here:
|
# Database semantic conventions here:
|
||||||
# https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/database.md
|
# https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/database.md
|
||||||
_ROWS = "sql.rows" # number of rows returned by a query
|
|
||||||
_STMT = "db.statement"
|
_STMT = "db.statement"
|
||||||
_DB = "db.type"
|
_DB = "db.name"
|
||||||
_URL = "db.url"
|
_USER = "db.user"
|
||||||
|
|
||||||
|
|
||||||
def _normalize_vendor(vendor):
|
def _normalize_vendor(vendor):
|
||||||
@ -39,7 +38,7 @@ def _normalize_vendor(vendor):
|
|||||||
return "sqlite"
|
return "sqlite"
|
||||||
|
|
||||||
if "postgres" in vendor or vendor == "psycopg2":
|
if "postgres" in vendor or vendor == "psycopg2":
|
||||||
return "postgres"
|
return "postgresql"
|
||||||
|
|
||||||
return vendor
|
return vendor
|
||||||
|
|
||||||
@ -58,17 +57,15 @@ def _wrap_create_engine(func, module, args, kwargs):
|
|||||||
object that will listen to SQLAlchemy events.
|
object that will listen to SQLAlchemy events.
|
||||||
"""
|
"""
|
||||||
engine = func(*args, **kwargs)
|
engine = func(*args, **kwargs)
|
||||||
EngineTracer(_get_tracer(engine), None, engine)
|
EngineTracer(_get_tracer(engine), engine)
|
||||||
return engine
|
return engine
|
||||||
|
|
||||||
|
|
||||||
class EngineTracer:
|
class EngineTracer:
|
||||||
def __init__(self, tracer, service, engine):
|
def __init__(self, tracer, engine):
|
||||||
self.tracer = tracer
|
self.tracer = tracer
|
||||||
self.engine = engine
|
self.engine = engine
|
||||||
self.vendor = _normalize_vendor(engine.name)
|
self.vendor = _normalize_vendor(engine.name)
|
||||||
self.service = service or self.vendor
|
|
||||||
self.name = "%s.query" % self.vendor
|
|
||||||
self.current_span = None
|
self.current_span = None
|
||||||
|
|
||||||
listen(engine, "before_cursor_execute", self._before_cur_exec)
|
listen(engine, "before_cursor_execute", self._before_cur_exec)
|
||||||
@ -77,11 +74,11 @@ class EngineTracer:
|
|||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def _before_cur_exec(self, conn, cursor, statement, *args):
|
def _before_cur_exec(self, conn, cursor, statement, *args):
|
||||||
self.current_span = self.tracer.start_span(self.name)
|
self.current_span = self.tracer.start_span(statement)
|
||||||
with self.tracer.use_span(self.current_span, end_on_exit=False):
|
with self.tracer.use_span(self.current_span, end_on_exit=False):
|
||||||
if self.current_span.is_recording():
|
if self.current_span.is_recording():
|
||||||
self.current_span.set_attribute("service", self.vendor)
|
|
||||||
self.current_span.set_attribute(_STMT, statement)
|
self.current_span.set_attribute(_STMT, statement)
|
||||||
|
self.current_span.set_attribute("db.system", self.vendor)
|
||||||
|
|
||||||
if not _set_attributes_from_url(
|
if not _set_attributes_from_url(
|
||||||
self.current_span, conn.engine.url
|
self.current_span, conn.engine.url
|
||||||
@ -94,16 +91,7 @@ class EngineTracer:
|
|||||||
def _after_cur_exec(self, conn, cursor, statement, *args):
|
def _after_cur_exec(self, conn, cursor, statement, *args):
|
||||||
if self.current_span is None:
|
if self.current_span is None:
|
||||||
return
|
return
|
||||||
|
self.current_span.end()
|
||||||
try:
|
|
||||||
if (
|
|
||||||
cursor
|
|
||||||
and cursor.rowcount >= 0
|
|
||||||
and self.current_span.is_recording()
|
|
||||||
):
|
|
||||||
self.current_span.set_attribute(_ROWS, cursor.rowcount)
|
|
||||||
finally:
|
|
||||||
self.current_span.end()
|
|
||||||
|
|
||||||
def _handle_error(self, context):
|
def _handle_error(self, context):
|
||||||
if self.current_span is None:
|
if self.current_span is None:
|
||||||
@ -127,6 +115,8 @@ def _set_attributes_from_url(span: trace.Span, url):
|
|||||||
span.set_attribute(_PORT, url.port)
|
span.set_attribute(_PORT, url.port)
|
||||||
if url.database:
|
if url.database:
|
||||||
span.set_attribute(_DB, url.database)
|
span.set_attribute(_DB, url.database)
|
||||||
|
if url.username:
|
||||||
|
span.set_attribute(_USER, url.username)
|
||||||
|
|
||||||
return bool(url.host)
|
return bool(url.host)
|
||||||
|
|
||||||
@ -135,7 +125,7 @@ def _set_attributes_from_cursor(span: trace.Span, vendor, cursor):
|
|||||||
"""Attempt to set db connection attributes by introspecting the cursor."""
|
"""Attempt to set db connection attributes by introspecting the cursor."""
|
||||||
if not span.is_recording():
|
if not span.is_recording():
|
||||||
return
|
return
|
||||||
if vendor == "postgres":
|
if vendor == "postgresql":
|
||||||
# pylint: disable=import-outside-toplevel
|
# pylint: disable=import-outside-toplevel
|
||||||
from psycopg2.extensions import parse_dsn
|
from psycopg2.extensions import parse_dsn
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user