38
38
client = redis.StrictRedis(host="localhost", port=6379)
39
39
client.get("my-key")
40
40
41
+ The `instrument` method accepts the following keyword args:
42
+
43
+ tracer_provider (TracerProvider) - an optional tracer provider
44
+ request_hook (Callable) - a function with extra user-defined logic to be performed before performing the request
45
+ this function signature is:
46
+ def request_hook(span: Span, instance: redis.connection.Connection, args, kwargs) -> None
47
+ response_hook (Callable) - a function with extra user-defined logic to be performed after performing the request
48
+ this function signature is:
49
+ def response_hook(span: Span, instance: redis.connection.Connection, response) -> None
50
+
51
+ for example:
52
+
53
+ .. code: python
54
+
55
+ from opentelemetry.instrumentation.redis import RedisInstrumentor
56
+ import redis
57
+
58
+ def request_hook(span, instance, args, kwargs):
59
+ if span and span.is_recording():
60
+ span.set_attribute("custom_user_attribute_from_request_hook", "some-value")
61
+
62
+ def response_hook(span, instance, response):
63
+ if span and span.is_recording():
64
+ span.set_attribute("custom_user_attribute_from_response_hook", "some-value")
65
+
66
+ # Instrument redis with hooks
67
+ RedisInstrumentor().instrument(request_hook=request_hook, response_hook=response_hook)
68
+
69
+ # This will report a span with the default settings and the custom attributes added from the hooks
70
+ client = redis.StrictRedis(host="localhost", port=6379)
71
+ client.get("my-key")
72
+
41
73
API
42
74
---
43
75
"""
61
93
62
94
_DEFAULT_SERVICE = "redis"
63
95
96
+ _RequestHookT = typing .Optional [
97
+ typing .Callable [
98
+ [Span , redis .connection .Connection , typing .List , typing .Dict ], None
99
+ ]
100
+ ]
64
101
_ResponseHookT = typing .Optional [
65
102
typing .Callable [[Span , redis .connection .Connection , Any ], None ]
66
103
]
@@ -76,7 +113,9 @@ def _set_connection_attributes(span, conn):
76
113
77
114
78
115
def _instrument (
79
- tracer , response_hook : _ResponseHookT = None ,
116
+ tracer ,
117
+ request_hook : _RequestHookT = None ,
118
+ response_hook : _ResponseHookT = None ,
80
119
):
81
120
def _traced_execute_command (func , instance , args , kwargs ):
82
121
query = _format_command_args (args )
@@ -92,6 +131,8 @@ def _traced_execute_command(func, instance, args, kwargs):
92
131
span .set_attribute (SpanAttributes .DB_STATEMENT , query )
93
132
_set_connection_attributes (span , instance )
94
133
span .set_attribute ("db.redis.args_length" , len (args ))
134
+ if callable (request_hook ):
135
+ request_hook (span , instance , args , kwargs )
95
136
response = func (* args , ** kwargs )
96
137
if callable (response_hook ):
97
138
response_hook (span , instance , response )
@@ -155,7 +196,11 @@ def _instrument(self, **kwargs):
155
196
tracer = trace .get_tracer (
156
197
__name__ , __version__ , tracer_provider = tracer_provider
157
198
)
158
- _instrument (tracer , response_hook = kwargs .get ("response_hook" ))
199
+ _instrument (
200
+ tracer ,
201
+ request_hook = kwargs .get ("request_hook" ),
202
+ response_hook = kwargs .get ("response_hook" ),
203
+ )
159
204
160
205
def _uninstrument (self , ** kwargs ):
161
206
if redis .VERSION < (3 , 0 , 0 ):
0 commit comments