|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import redis |
| 16 | + |
| 17 | +from opentelemetry import trace |
| 18 | +from opentelemetry.ext.redis import RedisInstrumentor |
| 19 | +from opentelemetry.test.test_base import TestBase |
| 20 | + |
| 21 | + |
| 22 | +class TestRedisInstrument(TestBase): |
| 23 | + |
| 24 | + test_service = "redis" |
| 25 | + |
| 26 | + def setUp(self): |
| 27 | + super().setUp() |
| 28 | + self.redis_client = redis.Redis(port=6379) |
| 29 | + self.redis_client.flushall() |
| 30 | + RedisInstrumentor().instrument(tracer_provider=self.tracer_provider) |
| 31 | + |
| 32 | + def tearDown(self): |
| 33 | + super().tearDown() |
| 34 | + RedisInstrumentor().uninstrument() |
| 35 | + |
| 36 | + def _check_span(self, span): |
| 37 | + self.assertEqual(span.attributes["service"], self.test_service) |
| 38 | + self.assertEqual(span.name, "redis.command") |
| 39 | + self.assertIs( |
| 40 | + span.status.canonical_code, trace.status.StatusCanonicalCode.OK |
| 41 | + ) |
| 42 | + self.assertEqual(span.attributes.get("db.instance"), 0) |
| 43 | + self.assertEqual( |
| 44 | + span.attributes.get("db.url"), "redis://localhost:6379" |
| 45 | + ) |
| 46 | + |
| 47 | + def test_long_command(self): |
| 48 | + self.redis_client.mget(*range(1000)) |
| 49 | + |
| 50 | + spans = self.memory_exporter.get_finished_spans() |
| 51 | + self.assertEqual(len(spans), 1) |
| 52 | + span = spans[0] |
| 53 | + self._check_span(span) |
| 54 | + self.assertTrue( |
| 55 | + span.attributes.get("db.statement").startswith("MGET 0 1 2 3") |
| 56 | + ) |
| 57 | + self.assertTrue(span.attributes.get("db.statement").endswith("...")) |
| 58 | + |
| 59 | + def test_basics(self): |
| 60 | + self.assertIsNone(self.redis_client.get("cheese")) |
| 61 | + spans = self.memory_exporter.get_finished_spans() |
| 62 | + self.assertEqual(len(spans), 1) |
| 63 | + span = spans[0] |
| 64 | + self._check_span(span) |
| 65 | + self.assertEqual(span.attributes.get("db.statement"), "GET cheese") |
| 66 | + self.assertEqual(span.attributes.get("redis.args_length"), 2) |
| 67 | + |
| 68 | + def test_pipeline_traced(self): |
| 69 | + with self.redis_client.pipeline(transaction=False) as pipeline: |
| 70 | + pipeline.set("blah", 32) |
| 71 | + pipeline.rpush("foo", "éé") |
| 72 | + pipeline.hgetall("xxx") |
| 73 | + pipeline.execute() |
| 74 | + |
| 75 | + spans = self.memory_exporter.get_finished_spans() |
| 76 | + self.assertEqual(len(spans), 1) |
| 77 | + span = spans[0] |
| 78 | + self._check_span(span) |
| 79 | + self.assertEqual( |
| 80 | + span.attributes.get("db.statement"), |
| 81 | + "SET blah 32\nRPUSH foo éé\nHGETALL xxx", |
| 82 | + ) |
| 83 | + self.assertEqual(span.attributes.get("redis.pipeline_length"), 3) |
| 84 | + |
| 85 | + def test_pipeline_immediate(self): |
| 86 | + with self.redis_client.pipeline() as pipeline: |
| 87 | + pipeline.set("a", 1) |
| 88 | + pipeline.immediate_execute_command("SET", "b", 2) |
| 89 | + pipeline.execute() |
| 90 | + |
| 91 | + spans = self.memory_exporter.get_finished_spans() |
| 92 | + # expecting two separate spans here, rather than a |
| 93 | + # single span for the whole pipeline |
| 94 | + self.assertEqual(len(spans), 2) |
| 95 | + span = spans[0] |
| 96 | + self._check_span(span) |
| 97 | + self.assertEqual(span.attributes.get("db.statement"), "SET b 2") |
| 98 | + |
| 99 | + def test_parent(self): |
| 100 | + """Ensure OpenTelemetry works with redis.""" |
| 101 | + ot_tracer = trace.get_tracer("redis_svc") |
| 102 | + |
| 103 | + with ot_tracer.start_as_current_span("redis_get"): |
| 104 | + self.assertIsNone(self.redis_client.get("cheese")) |
| 105 | + |
| 106 | + spans = self.memory_exporter.get_finished_spans() |
| 107 | + self.assertEqual(len(spans), 2) |
| 108 | + child_span, parent_span = spans[0], spans[1] |
| 109 | + |
| 110 | + # confirm the parenting |
| 111 | + self.assertIsNone(parent_span.parent) |
| 112 | + self.assertIs(child_span.parent, parent_span.get_context()) |
| 113 | + |
| 114 | + self.assertEqual(parent_span.name, "redis_get") |
| 115 | + self.assertEqual(parent_span.instrumentation_info.name, "redis_svc") |
| 116 | + |
| 117 | + self.assertEqual( |
| 118 | + child_span.attributes.get("service"), self.test_service |
| 119 | + ) |
| 120 | + self.assertEqual(child_span.name, "redis.command") |
0 commit comments