From b10f760ead716f64b4702de65f4e97f2634c6556 Mon Sep 17 00:00:00 2001 From: Sagiv Oulu Date: Thu, 4 Jul 2024 15:04:41 +0300 Subject: [PATCH] fix: writing bytes data to redis causes crash --- .../instrumentations/redis/__init__.py | 4 +++- .../redis/app/redis_set_and_get.py | 2 ++ .../integration/redis/tests/test_redis.py | 19 +++++++++++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/lumigo_opentelemetry/instrumentations/redis/__init__.py b/src/lumigo_opentelemetry/instrumentations/redis/__init__.py index 6c398be1..c95c542d 100644 --- a/src/lumigo_opentelemetry/instrumentations/redis/__init__.py +++ b/src/lumigo_opentelemetry/instrumentations/redis/__init__.py @@ -27,7 +27,9 @@ def request_hook( # a db.statement attribute is automatically added by the RedisInstrumentor # when this hook is called, but only includes the command name for some # versions so we need to set it ourselves. - add_body_attribute(span, " ".join(args), "db.statement") + add_body_attribute( + span, " ".join(str(arg) for arg in args), "db.statement" + ) def response_hook(span: Span, instance: Connection, response: Any) -> None: with lumigo_safe_execute("redis_response_hook"): diff --git a/src/test/integration/redis/app/redis_set_and_get.py b/src/test/integration/redis/app/redis_set_and_get.py index f813f743..de586b80 100644 --- a/src/test/integration/redis/app/redis_set_and_get.py +++ b/src/test/integration/redis/app/redis_set_and_get.py @@ -5,3 +5,5 @@ client = redis.StrictRedis(host=os.getenv("REDIS_HOST"), port=os.getenv("REDIS_PORT")) client.set("my-key", "my-value") client.get("my-key") +client.set("my-bytes-key", b"my-bytes-value") +client.get("my-bytes-key") diff --git a/src/test/integration/redis/tests/test_redis.py b/src/test/integration/redis/tests/test_redis.py index dee8c976..56e05c9a 100644 --- a/src/test/integration/redis/tests/test_redis.py +++ b/src/test/integration/redis/tests/test_redis.py @@ -45,8 +45,8 @@ def test_redis_set_and_get(self): spans_container = SpansContainer.parse_spans_from_file() - self.assertGreaterEqual(len(spans_container.spans), 2) - set_span, get_span = spans_container.spans + self.assertGreaterEqual(len(spans_container.spans), 4) + set_span, get_span, set_bytes_span, get_bytes_span = spans_container.spans self.assertEqual(set_span[ATTRIBUTES][DB_SYSTEM], "redis") self.assertEqual( set_span[ATTRIBUTES][DB_STATEMENT], @@ -58,6 +58,21 @@ def test_redis_set_and_get(self): self.assertEqual(get_span[ATTRIBUTES][DB_STATEMENT], "GET my-key") self.assertEqual(get_span[ATTRIBUTES][REDIS_RESPONSE_BODY], "my-value") + self.assertEqual(set_bytes_span[ATTRIBUTES][DB_SYSTEM], "redis") + self.assertEqual( + set_bytes_span[ATTRIBUTES][DB_STATEMENT], + "SET my-bytes-key b'my-bytes-value'", + ) + self.assertEqual(set_bytes_span[ATTRIBUTES][REDIS_RESPONSE_BODY], "True") + + self.assertEqual(get_bytes_span[ATTRIBUTES][DB_SYSTEM], "redis") + self.assertEqual( + get_bytes_span[ATTRIBUTES][DB_STATEMENT], "GET my-bytes-key" + ) + self.assertEqual( + get_bytes_span[ATTRIBUTES][REDIS_RESPONSE_BODY], "my-bytes-value" + ) + def test_redis_hash(self): with RedisContainer("redis:latest") as redis_server: run_redis_sample(