Update asyncpg to follow semantic conventions

This commit is contained in:
Srikanth Chekuri 2020-11-15 12:14:57 +05:30
parent 7513d7b961
commit b3234e8628
1 changed files with 29 additions and 11 deletions

View File

@ -49,11 +49,24 @@ _APPLIED = "_opentelemetry_tracer"
def _hydrate_span_from_args(connection, query, parameters) -> dict: def _hydrate_span_from_args(connection, query, parameters) -> dict:
span_attributes = {"db.type": "sql"} span_attributes = {"db.system": "postgresql"}
params = getattr(connection, "_params", None) params = getattr(connection, "_params", None)
span_attributes["db.instance"] = getattr(params, "database", None) dbname = getattr(params, "database", None)
span_attributes["db.user"] = getattr(params, "user", None) if dbname:
span_attributes["db.name"] = dbname
user = getattr(params, "user", None)
if user:
span_attributes["db.user"] = user
addr = getattr(connection, "_addr", None)
if isinstance(addr, tuple):
span_attributes["net.peer.name"] = addr[0]
span_attributes["net.peer.ip"] = addr[1]
span_attributes["net.transport"] = "IP.TCP"
elif isinstance(addr, str):
span_attributes["net.peer.name"] = addr
span_attributes["net.transport"] = "Unix"
if query is not None: if query is not None:
span_attributes["db.statement"] = query span_attributes["db.statement"] = query
@ -105,16 +118,21 @@ class AsyncPGInstrumentor(BaseInstrumentor):
tracer = getattr(asyncpg, _APPLIED) tracer = getattr(asyncpg, _APPLIED)
exception = None exception = None
span_attributes = _hydrate_span_from_args(
instance,
args[0],
args[1:] if self.capture_parameters else None,
)
name = ""
if args[0]:
name = args[0]
elif span_attributes.get("db.name"):
name = span_attributes["db.name"]
else:
name = "postgresql" # Does it ever happen?
with tracer.start_as_current_span( with tracer.start_as_current_span(name, kind=SpanKind.CLIENT) as span:
"postgresql", kind=SpanKind.CLIENT
) as span:
if span.is_recording(): if span.is_recording():
span_attributes = _hydrate_span_from_args(
instance,
args[0],
args[1:] if self.capture_parameters else None,
)
for attribute, value in span_attributes.items(): for attribute, value in span_attributes.items():
span.set_attribute(attribute, value) span.set_attribute(attribute, value)