adding response_hook to redis instrumentor (#669)

This commit is contained in:
ItayGibel-heliosphere
2021-09-14 23:47:12 +03:00
committed by GitHub
parent 291e50813a
commit db636a462c
3 changed files with 181 additions and 67 deletions

View File

@ -80,3 +80,64 @@ class TestRedis(TestBase):
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
def test_response_hook(self):
redis_client = redis.Redis()
connection = redis.connection.Connection()
redis_client.connection = connection
response_attribute_name = "db.redis.response"
def response_hook(span, conn, response):
span.set_attribute(response_attribute_name, response)
RedisInstrumentor().uninstrument()
RedisInstrumentor().instrument(
tracer_provider=self.tracer_provider, response_hook=response_hook
)
test_value = "test_value"
with mock.patch.object(connection, "send_command"):
with mock.patch.object(
redis_client, "parse_response", return_value=test_value
):
redis_client.get("key")
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
self.assertEqual(
span.attributes.get(response_attribute_name), test_value
)
def test_request_hook(self):
redis_client = redis.Redis()
connection = redis.connection.Connection()
redis_client.connection = connection
custom_attribute_name = "my.request.attribute"
def request_hook(span, conn, args, kwargs):
if span and span.is_recording():
span.set_attribute(custom_attribute_name, args[0])
RedisInstrumentor().uninstrument()
RedisInstrumentor().instrument(
tracer_provider=self.tracer_provider, request_hook=request_hook
)
test_value = "test_value"
with mock.patch.object(connection, "send_command"):
with mock.patch.object(
redis_client, "parse_response", return_value=test_value
):
redis_client.get("key")
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)
span = spans[0]
self.assertEqual(span.attributes.get(custom_attribute_name), "GET")