|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
| 15 | +import asyncio |
| 16 | + |
15 | 17 | import redis
|
| 18 | +import redis.asyncio |
16 | 19 |
|
17 | 20 | from opentelemetry import trace
|
18 | 21 | from opentelemetry.instrumentation.redis import RedisInstrumentor
|
@@ -121,6 +124,120 @@ def test_parent(self):
|
121 | 124 | self.assertEqual(child_span.name, "GET")
|
122 | 125 |
|
123 | 126 |
|
| 127 | +def async_call(coro): |
| 128 | + loop = asyncio.get_event_loop() |
| 129 | + return loop.run_until_complete(coro) |
| 130 | + |
| 131 | + |
| 132 | +class TestAsyncRedisInstrument(TestBase): |
| 133 | + def setUp(self): |
| 134 | + super().setUp() |
| 135 | + self.redis_client = redis.asyncio.Redis(port=6379) |
| 136 | + async_call(self.redis_client.flushall()) |
| 137 | + RedisInstrumentor().instrument(tracer_provider=self.tracer_provider) |
| 138 | + |
| 139 | + def tearDown(self): |
| 140 | + super().tearDown() |
| 141 | + RedisInstrumentor().uninstrument() |
| 142 | + |
| 143 | + def _check_span(self, span, name): |
| 144 | + self.assertEqual(span.name, name) |
| 145 | + self.assertIs(span.status.status_code, trace.StatusCode.UNSET) |
| 146 | + self.assertEqual(span.attributes.get(SpanAttributes.DB_NAME), 0) |
| 147 | + self.assertEqual( |
| 148 | + span.attributes[SpanAttributes.NET_PEER_NAME], "localhost" |
| 149 | + ) |
| 150 | + self.assertEqual(span.attributes[SpanAttributes.NET_PEER_PORT], 6379) |
| 151 | + |
| 152 | + def test_long_command(self): |
| 153 | + async_call(self.redis_client.mget(*range(1000))) |
| 154 | + |
| 155 | + spans = self.memory_exporter.get_finished_spans() |
| 156 | + self.assertEqual(len(spans), 1) |
| 157 | + span = spans[0] |
| 158 | + self._check_span(span, "MGET") |
| 159 | + self.assertTrue( |
| 160 | + span.attributes.get(SpanAttributes.DB_STATEMENT).startswith( |
| 161 | + "MGET 0 1 2 3" |
| 162 | + ) |
| 163 | + ) |
| 164 | + self.assertTrue( |
| 165 | + span.attributes.get(SpanAttributes.DB_STATEMENT).endswith("...") |
| 166 | + ) |
| 167 | + |
| 168 | + def test_basics(self): |
| 169 | + self.assertIsNone(async_call(self.redis_client.get("cheese"))) |
| 170 | + spans = self.memory_exporter.get_finished_spans() |
| 171 | + self.assertEqual(len(spans), 1) |
| 172 | + span = spans[0] |
| 173 | + self._check_span(span, "GET") |
| 174 | + self.assertEqual( |
| 175 | + span.attributes.get(SpanAttributes.DB_STATEMENT), "GET cheese" |
| 176 | + ) |
| 177 | + self.assertEqual(span.attributes.get("db.redis.args_length"), 2) |
| 178 | + |
| 179 | + def test_pipeline_traced(self): |
| 180 | + async def pipeline_simple(): |
| 181 | + async with self.redis_client.pipeline( |
| 182 | + transaction=False |
| 183 | + ) as pipeline: |
| 184 | + pipeline.set("blah", 32) |
| 185 | + pipeline.rpush("foo", "éé") |
| 186 | + pipeline.hgetall("xxx") |
| 187 | + await pipeline.execute() |
| 188 | + |
| 189 | + async_call(pipeline_simple()) |
| 190 | + |
| 191 | + spans = self.memory_exporter.get_finished_spans() |
| 192 | + self.assertEqual(len(spans), 1) |
| 193 | + span = spans[0] |
| 194 | + self._check_span(span, "SET RPUSH HGETALL") |
| 195 | + self.assertEqual( |
| 196 | + span.attributes.get(SpanAttributes.DB_STATEMENT), |
| 197 | + "SET blah 32\nRPUSH foo éé\nHGETALL xxx", |
| 198 | + ) |
| 199 | + self.assertEqual(span.attributes.get("db.redis.pipeline_length"), 3) |
| 200 | + |
| 201 | + def test_pipeline_immediate(self): |
| 202 | + async def pipeline_immediate(): |
| 203 | + async with self.redis_client.pipeline() as pipeline: |
| 204 | + pipeline.set("a", 1) |
| 205 | + await pipeline.immediate_execute_command("SET", "b", 2) |
| 206 | + await pipeline.execute() |
| 207 | + |
| 208 | + async_call(pipeline_immediate()) |
| 209 | + |
| 210 | + spans = self.memory_exporter.get_finished_spans() |
| 211 | + # expecting two separate spans here, rather than a |
| 212 | + # single span for the whole pipeline |
| 213 | + self.assertEqual(len(spans), 2) |
| 214 | + span = spans[0] |
| 215 | + self._check_span(span, "SET") |
| 216 | + self.assertEqual( |
| 217 | + span.attributes.get(SpanAttributes.DB_STATEMENT), "SET b 2" |
| 218 | + ) |
| 219 | + |
| 220 | + def test_parent(self): |
| 221 | + """Ensure OpenTelemetry works with redis.""" |
| 222 | + ot_tracer = trace.get_tracer("redis_svc") |
| 223 | + |
| 224 | + with ot_tracer.start_as_current_span("redis_get"): |
| 225 | + self.assertIsNone(async_call(self.redis_client.get("cheese"))) |
| 226 | + |
| 227 | + spans = self.memory_exporter.get_finished_spans() |
| 228 | + self.assertEqual(len(spans), 2) |
| 229 | + child_span, parent_span = spans[0], spans[1] |
| 230 | + |
| 231 | + # confirm the parenting |
| 232 | + self.assertIsNone(parent_span.parent) |
| 233 | + self.assertIs(child_span.parent, parent_span.get_span_context()) |
| 234 | + |
| 235 | + self.assertEqual(parent_span.name, "redis_get") |
| 236 | + self.assertEqual(parent_span.instrumentation_info.name, "redis_svc") |
| 237 | + |
| 238 | + self.assertEqual(child_span.name, "GET") |
| 239 | + |
| 240 | + |
124 | 241 | class TestRedisDBIndexInstrument(TestBase):
|
125 | 242 | def setUp(self):
|
126 | 243 | super().setUp()
|
|
0 commit comments