Update redis instrumentation to follow semantic conventions

This commit is contained in:
Srikanth Chekuri
2020-11-15 02:10:34 +05:30
parent 7513d7b961
commit ee90c7e50c
3 changed files with 13 additions and 10 deletions

View File

@ -59,7 +59,6 @@ from opentelemetry.instrumentation.utils import unwrap
_DEFAULT_SERVICE = "redis" _DEFAULT_SERVICE = "redis"
_RAWCMD = "db.statement" _RAWCMD = "db.statement"
_CMD = "redis.command"
def _set_connection_attributes(span, conn): def _set_connection_attributes(span, conn):
@ -75,7 +74,7 @@ def _traced_execute_command(func, instance, args, kwargs):
tracer = getattr(redis, "_opentelemetry_tracer") tracer = getattr(redis, "_opentelemetry_tracer")
query = _format_command_args(args) query = _format_command_args(args)
with tracer.start_as_current_span( with tracer.start_as_current_span(
_CMD, kind=trace.SpanKind.CLIENT args[0], kind=trace.SpanKind.CLIENT
) as span: ) as span:
if span.is_recording(): if span.is_recording():
span.set_attribute("service", tracer.instrumentation_info.name) span.set_attribute("service", tracer.instrumentation_info.name)
@ -91,8 +90,11 @@ def _traced_execute_pipeline(func, instance, args, kwargs):
cmds = [_format_command_args(c) for c, _ in instance.command_stack] cmds = [_format_command_args(c) for c, _ in instance.command_stack]
resource = "\n".join(cmds) resource = "\n".join(cmds)
pipeline_cmnds = " ".join([args[0] for args, _ in instance.command_stack])
span_name = "PIPELINE" + " " + pipeline_cmnds
with tracer.start_as_current_span( with tracer.start_as_current_span(
_CMD, kind=trace.SpanKind.CLIENT span_name, kind=trace.SpanKind.CLIENT
) as span: ) as span:
if span.is_recording(): if span.is_recording():
span.set_attribute("service", tracer.instrumentation_info.name) span.set_attribute("service", tracer.instrumentation_info.name)

View File

@ -20,15 +20,16 @@ Some utils used by the redis integration
def _extract_conn_attributes(conn_kwargs): def _extract_conn_attributes(conn_kwargs):
""" Transform redis conn info into dict """ """ Transform redis conn info into dict """
attributes = { attributes = {
"db.type": "redis", "db.system": "redis",
"db.instance": conn_kwargs.get("db", 0), "db.name": conn_kwargs.get("db", 0),
} }
try: try:
attributes["db.url"] = "redis://{}:{}".format( attributes["net.peer.name"] = conn_kwargs["host"]
conn_kwargs["host"], conn_kwargs["port"] attributes["net.peer.ip"] = conn_kwargs["port"]
) attributes["net.transport"] = "IP.TCP"
except KeyError: except KeyError:
pass # don't include url attribute attributes["net.peer.name"] = conn_kwargs["path"]
attributes["net.transport"] = "Unix"
return attributes return attributes

View File

@ -31,7 +31,7 @@ class TestRedis(TestBase):
spans = self.memory_exporter.get_finished_spans() spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1) self.assertEqual(len(spans), 1)
span = spans[0] span = spans[0]
self.assertEqual(span.name, "redis.command") self.assertEqual(span.name, "GET")
self.assertEqual(span.kind, SpanKind.CLIENT) self.assertEqual(span.kind, SpanKind.CLIENT)
def test_not_recording(self): def test_not_recording(self):