Skip to content

Commit

Permalink
Use is_recording flag in aiopg, asyncpg, dbapi, psycopg2, pymemcache,…
Browse files Browse the repository at this point in the history
… pymongo, redis, sqlalchemy instrumentations (#1212)
  • Loading branch information
lzchen authored and codeboten committed Oct 22, 2020
1 parent a9e4305 commit dfb0621
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@


def _set_connection_attributes(span, conn):
if not span.is_recording():
return
for key, value in _extract_conn_attributes(
conn.connection_pool.connection_kwargs
).items():
Expand All @@ -75,10 +77,11 @@ def _traced_execute_command(func, instance, args, kwargs):
with tracer.start_as_current_span(
_CMD, kind=trace.SpanKind.CLIENT
) as span:
span.set_attribute("service", tracer.instrumentation_info.name)
span.set_attribute(_RAWCMD, query)
_set_connection_attributes(span, instance)
span.set_attribute("redis.args_length", len(args))
if span.is_recording():
span.set_attribute("service", tracer.instrumentation_info.name)
span.set_attribute(_RAWCMD, query)
_set_connection_attributes(span, instance)
span.set_attribute("redis.args_length", len(args))
return func(*args, **kwargs)


Expand All @@ -91,12 +94,13 @@ def _traced_execute_pipeline(func, instance, args, kwargs):
with tracer.start_as_current_span(
_CMD, kind=trace.SpanKind.CLIENT
) as span:
span.set_attribute("service", tracer.instrumentation_info.name)
span.set_attribute(_RAWCMD, resource)
_set_connection_attributes(span, instance)
span.set_attribute(
"redis.pipeline_length", len(instance.command_stack)
)
if span.is_recording():
span.set_attribute("service", tracer.instrumentation_info.name)
span.set_attribute(_RAWCMD, resource)
_set_connection_attributes(span, instance)
span.set_attribute(
"redis.pipeline_length", len(instance.command_stack)
)
return func(*args, **kwargs)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ def test_span_properties(self):
self.assertEqual(span.name, "redis.command")
self.assertEqual(span.kind, SpanKind.CLIENT)

def test_not_recording(self):
redis_client = redis.Redis()
RedisInstrumentor().instrument(tracer_provider=self.tracer_provider)

mock_tracer = mock.Mock()
mock_span = mock.Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = True
with mock.patch("opentelemetry.trace.get_tracer") as tracer:
with mock.patch.object(redis_client, "connection"):
tracer.return_value = mock_tracer
redis_client.get("key")
self.assertFalse(mock_span.is_recording())
self.assertTrue(mock_span.is_recording.called)
self.assertFalse(mock_span.set_attribute.called)
self.assertFalse(mock_span.set_status.called)

def test_instrument_uninstrument(self):
redis_client = redis.Redis()
RedisInstrumentor().instrument(tracer_provider=self.tracer_provider)
Expand Down

0 comments on commit dfb0621

Please sign in to comment.