-
Notifications
You must be signed in to change notification settings - Fork 599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Instrument RedisCluster clients #1177
Changes from 7 commits
acff3f4
aeeaaa3
fa68ff7
7f001e5
08e5a50
23939bd
913a69d
3beeb18
09987a0
1a2b694
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,6 +122,9 @@ def response_hook(span, instance, response): | |
if redis.VERSION >= _REDIS_ASYNCIO_VERSION: | ||
import redis.asyncio | ||
|
||
_REDIS_CLUSTER_VERSION = (4, 1, 0) | ||
_REDIS_ASYNCIO_CLUSTER_VERSION = (4, 3, 0) | ||
|
||
|
||
def _set_connection_attributes(span, conn): | ||
if not span.is_recording(): | ||
|
@@ -149,7 +152,8 @@ def _traced_execute_command(func, instance, args, kwargs): | |
) as span: | ||
if span.is_recording(): | ||
span.set_attribute(SpanAttributes.DB_STATEMENT, query) | ||
_set_connection_attributes(span, instance) | ||
if hasattr(instance, "connection_pool"): | ||
_set_connection_attributes(span, instance) | ||
span.set_attribute("db.redis.args_length", len(args)) | ||
if callable(request_hook): | ||
request_hook(span, instance, args, kwargs) | ||
|
@@ -159,19 +163,37 @@ def _traced_execute_command(func, instance, args, kwargs): | |
return response | ||
|
||
def _traced_execute_pipeline(func, instance, args, kwargs): | ||
cmds = [_format_command_args(c) for c, _ in instance.command_stack] | ||
try: | ||
command_stack = ( | ||
instance.command_stack | ||
if hasattr(instance, "command_stack") | ||
else instance._command_stack | ||
) | ||
except AttributeError: | ||
command_stack = [] | ||
|
||
cmds = [ | ||
_format_command_args(c.args if hasattr(c, "args") else c[0]) | ||
for c in command_stack | ||
] | ||
resource = "\n".join(cmds) | ||
|
||
span_name = " ".join([args[0] for args, _ in instance.command_stack]) | ||
span_name = " ".join( | ||
[ | ||
(c.args[0] if hasattr(c, "args") else c[0][0]) | ||
for c in command_stack | ||
] | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also suggest to safe guard this code for potential There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated to handle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated to handle |
||
|
||
with tracer.start_as_current_span( | ||
span_name, kind=trace.SpanKind.CLIENT | ||
) as span: | ||
if span.is_recording(): | ||
span.set_attribute(SpanAttributes.DB_STATEMENT, resource) | ||
_set_connection_attributes(span, instance) | ||
if hasattr(instance, "connection_pool"): | ||
_set_connection_attributes(span, instance) | ||
span.set_attribute( | ||
"db.redis.pipeline_length", len(instance.command_stack) | ||
"db.redis.pipeline_length", len(command_stack) | ||
) | ||
response = func(*args, **kwargs) | ||
if callable(response_hook): | ||
|
@@ -196,6 +218,17 @@ def _traced_execute_pipeline(func, instance, args, kwargs): | |
f"{pipeline_class}.immediate_execute_command", | ||
_traced_execute_command, | ||
) | ||
if redis.VERSION >= _REDIS_CLUSTER_VERSION: | ||
wrap_function_wrapper( | ||
"redis.cluster", | ||
"RedisCluster.execute_command", | ||
_traced_execute_command, | ||
) | ||
wrap_function_wrapper( | ||
"redis.cluster", | ||
"ClusterPipeline.execute", | ||
_traced_execute_pipeline, | ||
) | ||
if redis.VERSION >= _REDIS_ASYNCIO_VERSION: | ||
wrap_function_wrapper( | ||
"redis.asyncio", | ||
|
@@ -212,6 +245,17 @@ def _traced_execute_pipeline(func, instance, args, kwargs): | |
f"{pipeline_class}.immediate_execute_command", | ||
_traced_execute_command, | ||
) | ||
if redis.VERSION >= _REDIS_ASYNCIO_CLUSTER_VERSION: | ||
wrap_function_wrapper( | ||
"redis.asyncio.cluster", | ||
"RedisCluster.execute_command", | ||
_traced_execute_command, | ||
) | ||
wrap_function_wrapper( | ||
"redis.asyncio.cluster", | ||
"ClusterPipeline.execute", | ||
_traced_execute_pipeline, | ||
) | ||
|
||
|
||
class RedisInstrumentor(BaseInstrumentor): | ||
|
@@ -258,8 +302,14 @@ def _uninstrument(self, **kwargs): | |
unwrap(redis.Redis, "pipeline") | ||
unwrap(redis.client.Pipeline, "execute") | ||
unwrap(redis.client.Pipeline, "immediate_execute_command") | ||
if redis.VERSION >= _REDIS_CLUSTER_VERSION: | ||
unwrap(redis.cluster.RedisCluster, "execute_command") | ||
unwrap(redis.cluster.ClusterPipeline, "execute") | ||
if redis.VERSION >= _REDIS_ASYNCIO_VERSION: | ||
unwrap(redis.asyncio.Redis, "execute_command") | ||
unwrap(redis.asyncio.Redis, "pipeline") | ||
unwrap(redis.asyncio.client.Pipeline, "execute") | ||
unwrap(redis.asyncio.client.Pipeline, "immediate_execute_command") | ||
if redis.VERSION >= _REDIS_ASYNCIO_CLUSTER_VERSION: | ||
unwrap(redis.asyncio.cluster.RedisCluster, "execute_command") | ||
unwrap(redis.asyncio.cluster.ClusterPipeline, "execute") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add the
hasattr
check in the_set_connection_attributes
function?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved
hasattr
check to the_set_connection_attributes
function.