Skip to content

Commit 191ef9d

Browse files
committed
Add wrappers for redis.search methods create_index, search, aggregate
1 parent 919b2c2 commit 191ef9d

File tree

2 files changed

+96
-0
lines changed
  • instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis

2 files changed

+96
-0
lines changed

Diff for: instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/__init__.py

+82
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,17 @@ def response_hook(span, instance, response):
9494
from typing import Any, Collection
9595

9696
import redis
97+
import redis.commands
9798
from wrapt import wrap_function_wrapper
9899

99100
from opentelemetry import trace
100101
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
101102
from opentelemetry.instrumentation.redis.package import _instruments
102103
from opentelemetry.instrumentation.redis.util import (
104+
_args_or_none,
103105
_extract_conn_attributes,
104106
_format_command_args,
107+
_set_span_attribute,
105108
)
106109
from opentelemetry.instrumentation.redis.version import __version__
107110
from opentelemetry.instrumentation.utils import unwrap
@@ -217,6 +220,67 @@ def _traced_execute_pipeline(func, instance, args, kwargs):
217220
response_hook(span, instance, response)
218221
return response
219222

223+
def _traced_create_index(func, instance, args, kwargs):
224+
span_name = "redis.create_index"
225+
with tracer.start_as_current_span(span_name) as span:
226+
_set_span_attribute(
227+
span,
228+
"redis.create_index.fields",
229+
kwargs.get("fields").__str__(),
230+
)
231+
_set_span_attribute(
232+
span,
233+
"redis.create_index.definition",
234+
kwargs.get("definition").__str__(),
235+
)
236+
response = func(*args, **kwargs)
237+
return response
238+
239+
def _traced_search(func, instance, args, kwargs):
240+
span_name = "redis.search"
241+
with tracer.start_as_current_span(span_name) as span:
242+
query = kwargs.get("query") or _args_or_none(args, 0)
243+
_set_span_attribute(
244+
span,
245+
"redis.commands.search.query",
246+
query.query_string(),
247+
)
248+
response = func(*args, **kwargs)
249+
_set_span_attribute(
250+
span,
251+
"redis.commands.search.total",
252+
response.total
253+
)
254+
_set_span_attribute(
255+
span,
256+
"redis.commands.search.duration",
257+
response.duration
258+
)
259+
for index, doc in enumerate(response.docs):
260+
_set_span_attribute(
261+
span,
262+
f"redis.commands.search.xdoc_{index}",
263+
doc.__str__()
264+
)
265+
return response
266+
267+
def _traced_aggregate(func, instance, args, kwargs):
268+
span_name = "redis.aggregate"
269+
with tracer.start_as_current_span(span_name) as span:
270+
query = kwargs.get("query") or _args_or_none(args, 0)
271+
_set_span_attribute(
272+
span,
273+
"redis.commands.aggregate.query",
274+
query.query_string(),
275+
)
276+
response = func(*args, **kwargs)
277+
_set_span_attribute(
278+
span,
279+
"redis.commands.aggregate.results",
280+
str(response.rows)
281+
)
282+
return response
283+
220284
pipeline_class = (
221285
"BasePipeline" if redis.VERSION < (3, 0, 0) else "Pipeline"
222286
)
@@ -235,6 +299,21 @@ def _traced_execute_pipeline(func, instance, args, kwargs):
235299
f"{pipeline_class}.immediate_execute_command",
236300
_traced_execute_command,
237301
)
302+
wrap_function_wrapper(
303+
"redis.commands.search",
304+
"Search.create_index",
305+
_traced_create_index,
306+
)
307+
wrap_function_wrapper(
308+
"redis.commands.search",
309+
"Search.search",
310+
_traced_search,
311+
)
312+
wrap_function_wrapper(
313+
"redis.commands.search",
314+
"Search.aggregate",
315+
_traced_aggregate,
316+
)
238317
if redis.VERSION >= _REDIS_CLUSTER_VERSION:
239318
wrap_function_wrapper(
240319
"redis.cluster",
@@ -345,6 +424,9 @@ def _instrument(self, **kwargs):
345424
)
346425

347426
def _uninstrument(self, **kwargs):
427+
unwrap(redis.commands.search.Search, "create_index")
428+
unwrap(redis.commands.search.Search, "search")
429+
unwrap(redis.commands.search.Search, "aggregate")
348430
if redis.VERSION < (3, 0, 0):
349431
unwrap(redis.StrictRedis, "execute_command")
350432
unwrap(redis.StrictRedis, "pipeline")

Diff for: instrumentation/opentelemetry-instrumentation-redis/src/opentelemetry/instrumentation/redis/util.py

+14
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,17 @@ def _format_command_args(args):
6868
out_str = ""
6969

7070
return out_str
71+
72+
73+
def _set_span_attribute(span, name, value):
74+
if value is not None:
75+
if value != "":
76+
span.set_attribute(name, value)
77+
return
78+
79+
80+
def _args_or_none(args, n):
81+
try:
82+
return args[n]
83+
except IndexError:
84+
return None

0 commit comments

Comments
 (0)