|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 | import asyncio
|
15 |
| -from unittest import mock |
| 15 | +from unittest import IsolatedAsyncioTestCase, mock |
16 | 16 | from unittest.mock import AsyncMock
|
17 | 17 |
|
| 18 | +import fakeredis |
| 19 | +import pytest |
18 | 20 | import redis
|
19 | 21 | import redis.asyncio
|
| 22 | +from fakeredis.aioredis import FakeRedis |
| 23 | +from redis.exceptions import ConnectionError as redis_ConnectionError |
| 24 | +from redis.exceptions import WatchError |
20 | 25 |
|
21 | 26 | from opentelemetry import trace
|
22 | 27 | from opentelemetry.instrumentation.redis import RedisInstrumentor
|
@@ -311,3 +316,113 @@ def test_attributes_unix_socket(self):
|
311 | 316 | span.attributes[SpanAttributes.NET_TRANSPORT],
|
312 | 317 | NetTransportValues.OTHER.value,
|
313 | 318 | )
|
| 319 | + |
| 320 | + def test_connection_error(self): |
| 321 | + server = fakeredis.FakeServer() |
| 322 | + server.connected = False |
| 323 | + redis_client = fakeredis.FakeStrictRedis(server=server) |
| 324 | + try: |
| 325 | + redis_client.set("foo", "bar") |
| 326 | + except redis_ConnectionError: |
| 327 | + pass |
| 328 | + |
| 329 | + spans = self.memory_exporter.get_finished_spans() |
| 330 | + self.assertEqual(len(spans), 1) |
| 331 | + span = spans[0] |
| 332 | + |
| 333 | + self.assertEqual(span.name, "SET") |
| 334 | + self.assertEqual(span.kind, SpanKind.CLIENT) |
| 335 | + self.assertEqual(span.status.status_code, trace.StatusCode.ERROR) |
| 336 | + |
| 337 | + def test_response_error(self): |
| 338 | + redis_client = fakeredis.FakeStrictRedis() |
| 339 | + redis_client.lpush("mylist", "value") |
| 340 | + try: |
| 341 | + redis_client.incr( |
| 342 | + "mylist" |
| 343 | + ) # Trying to increment a list, which is invalid |
| 344 | + except redis.ResponseError: |
| 345 | + pass |
| 346 | + |
| 347 | + spans = self.memory_exporter.get_finished_spans() |
| 348 | + self.assertEqual(len(spans), 2) |
| 349 | + |
| 350 | + span = spans[0] |
| 351 | + self.assertEqual(span.name, "LPUSH") |
| 352 | + self.assertEqual(span.kind, SpanKind.CLIENT) |
| 353 | + self.assertEqual(span.status.status_code, trace.StatusCode.UNSET) |
| 354 | + |
| 355 | + span = spans[1] |
| 356 | + self.assertEqual(span.name, "INCRBY") |
| 357 | + self.assertEqual(span.kind, SpanKind.CLIENT) |
| 358 | + self.assertEqual(span.status.status_code, trace.StatusCode.ERROR) |
| 359 | + |
| 360 | + def test_watch_error_sync(self): |
| 361 | + def redis_operations(): |
| 362 | + try: |
| 363 | + redis_client = fakeredis.FakeStrictRedis() |
| 364 | + pipe = redis_client.pipeline(transaction=True) |
| 365 | + pipe.watch("a") |
| 366 | + redis_client.set("a", "bad") # This will cause the WatchError |
| 367 | + pipe.multi() |
| 368 | + pipe.set("a", "1") |
| 369 | + pipe.execute() |
| 370 | + except WatchError: |
| 371 | + pass |
| 372 | + |
| 373 | + redis_operations() |
| 374 | + |
| 375 | + spans = self.memory_exporter.get_finished_spans() |
| 376 | + self.assertEqual(len(spans), 3) |
| 377 | + |
| 378 | + # there should be 3 tests, we start watch operation and have 2 set operation on same key |
| 379 | + self.assertEqual(len(spans), 3) |
| 380 | + |
| 381 | + self.assertEqual(spans[0].attributes.get("db.statement"), "WATCH ?") |
| 382 | + self.assertEqual(spans[0].kind, SpanKind.CLIENT) |
| 383 | + self.assertEqual(spans[0].status.status_code, trace.StatusCode.UNSET) |
| 384 | + |
| 385 | + for span in spans[1:]: |
| 386 | + self.assertEqual(span.attributes.get("db.statement"), "SET ? ?") |
| 387 | + self.assertEqual(span.kind, SpanKind.CLIENT) |
| 388 | + self.assertEqual(span.status.status_code, trace.StatusCode.UNSET) |
| 389 | + |
| 390 | + |
| 391 | +class TestRedisAsync(TestBase, IsolatedAsyncioTestCase): |
| 392 | + def setUp(self): |
| 393 | + super().setUp() |
| 394 | + RedisInstrumentor().instrument(tracer_provider=self.tracer_provider) |
| 395 | + |
| 396 | + def tearDown(self): |
| 397 | + super().tearDown() |
| 398 | + RedisInstrumentor().uninstrument() |
| 399 | + |
| 400 | + @pytest.mark.asyncio |
| 401 | + async def test_watch_error_async(self): |
| 402 | + async def redis_operations(): |
| 403 | + try: |
| 404 | + redis_client = FakeRedis() |
| 405 | + async with redis_client.pipeline(transaction=False) as pipe: |
| 406 | + await pipe.watch("a") |
| 407 | + await redis_client.set("a", "bad") |
| 408 | + pipe.multi() |
| 409 | + await pipe.set("a", "1") |
| 410 | + await pipe.execute() |
| 411 | + except WatchError: |
| 412 | + pass |
| 413 | + |
| 414 | + await redis_operations() |
| 415 | + |
| 416 | + spans = self.memory_exporter.get_finished_spans() |
| 417 | + |
| 418 | + # there should be 3 tests, we start watch operation and have 2 set operation on same key |
| 419 | + self.assertEqual(len(spans), 3) |
| 420 | + |
| 421 | + self.assertEqual(spans[0].attributes.get("db.statement"), "WATCH ?") |
| 422 | + self.assertEqual(spans[0].kind, SpanKind.CLIENT) |
| 423 | + self.assertEqual(spans[0].status.status_code, trace.StatusCode.UNSET) |
| 424 | + |
| 425 | + for span in spans[1:]: |
| 426 | + self.assertEqual(span.attributes.get("db.statement"), "SET ? ?") |
| 427 | + self.assertEqual(span.kind, SpanKind.CLIENT) |
| 428 | + self.assertEqual(span.status.status_code, trace.StatusCode.UNSET) |
0 commit comments