Skip to content

Commit

Permalink
fix: writing bytes data to redis causes crash
Browse files Browse the repository at this point in the history
  • Loading branch information
sagivoululumigo committed Jul 4, 2024
1 parent 3527190 commit b10f760
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/lumigo_opentelemetry/instrumentations/redis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
2 changes: 2 additions & 0 deletions src/test/integration/redis/app/redis_set_and_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
19 changes: 17 additions & 2 deletions src/test/integration/redis/tests/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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(
Expand Down

0 comments on commit b10f760

Please sign in to comment.