-
Notifications
You must be signed in to change notification settings - Fork 626
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
adding response_hook to redis instrumentor #669
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,8 +41,8 @@ | |
API | ||
--- | ||
""" | ||
|
||
from typing import Collection | ||
import typing | ||
from typing import Any, Collection | ||
|
||
import redis | ||
from wrapt import wrap_function_wrapper | ||
|
@@ -57,9 +57,14 @@ | |
from opentelemetry.instrumentation.redis.version import __version__ | ||
from opentelemetry.instrumentation.utils import unwrap | ||
from opentelemetry.semconv.trace import SpanAttributes | ||
from opentelemetry.trace import Span | ||
|
||
_DEFAULT_SERVICE = "redis" | ||
|
||
_ResponseHookT = typing.Optional[ | ||
typing.Callable[[Span, redis.connection.Connection, Any], None] | ||
] | ||
|
||
|
||
def _set_connection_attributes(span, conn): | ||
if not span.is_recording(): | ||
|
@@ -70,42 +75,64 @@ def _set_connection_attributes(span, conn): | |
span.set_attribute(key, value) | ||
|
||
|
||
def _traced_execute_command(func, instance, args, kwargs): | ||
tracer = getattr(redis, "_opentelemetry_tracer") | ||
query = _format_command_args(args) | ||
name = "" | ||
if len(args) > 0 and args[0]: | ||
name = args[0] | ||
else: | ||
name = instance.connection_pool.connection_kwargs.get("db", 0) | ||
with tracer.start_as_current_span( | ||
name, kind=trace.SpanKind.CLIENT | ||
) as span: | ||
if span.is_recording(): | ||
span.set_attribute(SpanAttributes.DB_STATEMENT, query) | ||
_set_connection_attributes(span, instance) | ||
span.set_attribute("db.redis.args_length", len(args)) | ||
return func(*args, **kwargs) | ||
|
||
|
||
def _traced_execute_pipeline(func, instance, args, kwargs): | ||
tracer = getattr(redis, "_opentelemetry_tracer") | ||
|
||
cmds = [_format_command_args(c) for c, _ in instance.command_stack] | ||
resource = "\n".join(cmds) | ||
|
||
span_name = " ".join([args[0] for args, _ in instance.command_stack]) | ||
|
||
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) | ||
span.set_attribute( | ||
"db.redis.pipeline_length", len(instance.command_stack) | ||
) | ||
return func(*args, **kwargs) | ||
def _instrument( | ||
tracer, response_hook: _ResponseHookT = None, | ||
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. Should there be a |
||
): | ||
def _traced_execute_command(func, instance, args, kwargs): | ||
query = _format_command_args(args) | ||
name = "" | ||
if len(args) > 0 and args[0]: | ||
name = args[0] | ||
else: | ||
name = instance.connection_pool.connection_kwargs.get("db", 0) | ||
with tracer.start_as_current_span( | ||
name, kind=trace.SpanKind.CLIENT | ||
) as span: | ||
if span.is_recording(): | ||
span.set_attribute(SpanAttributes.DB_STATEMENT, query) | ||
_set_connection_attributes(span, instance) | ||
span.set_attribute("db.redis.args_length", len(args)) | ||
response = func(*args, **kwargs) | ||
if callable(response_hook): | ||
response_hook(span, instance, response) | ||
return response | ||
|
||
def _traced_execute_pipeline(func, instance, args, kwargs): | ||
cmds = [_format_command_args(c) for c, _ in instance.command_stack] | ||
resource = "\n".join(cmds) | ||
|
||
span_name = " ".join([args[0] for args, _ in instance.command_stack]) | ||
|
||
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) | ||
span.set_attribute( | ||
"db.redis.pipeline_length", len(instance.command_stack) | ||
) | ||
response = func(*args, **kwargs) | ||
if callable(response_hook): | ||
response_hook(span, instance, response) | ||
return response | ||
|
||
pipeline_class = ( | ||
"BasePipeline" if redis.VERSION < (3, 0, 0) else "Pipeline" | ||
) | ||
redis_class = "StrictRedis" if redis.VERSION < (3, 0, 0) else "Redis" | ||
|
||
wrap_function_wrapper( | ||
"redis", f"{redis_class}.execute_command", _traced_execute_command | ||
) | ||
wrap_function_wrapper( | ||
"redis.client", f"{pipeline_class}.execute", _traced_execute_pipeline, | ||
) | ||
wrap_function_wrapper( | ||
"redis.client", | ||
f"{pipeline_class}.immediate_execute_command", | ||
_traced_execute_command, | ||
) | ||
|
||
|
||
class RedisInstrumentor(BaseInstrumentor): | ||
|
@@ -117,41 +144,18 @@ def instrumentation_dependencies(self) -> Collection[str]: | |
return _instruments | ||
|
||
def _instrument(self, **kwargs): | ||
"""Instruments the redis module | ||
|
||
Args: | ||
**kwargs: Optional arguments | ||
``tracer_provider``: a TracerProvider, defaults to global. | ||
``response_hook``: An optional callback which is invoked right before the span is finished processing a response. | ||
""" | ||
tracer_provider = kwargs.get("tracer_provider") | ||
setattr( | ||
redis, | ||
"_opentelemetry_tracer", | ||
trace.get_tracer( | ||
__name__, __version__, tracer_provider=tracer_provider, | ||
), | ||
tracer = trace.get_tracer( | ||
__name__, __version__, tracer_provider=tracer_provider | ||
) | ||
|
||
if redis.VERSION < (3, 0, 0): | ||
wrap_function_wrapper( | ||
"redis", "StrictRedis.execute_command", _traced_execute_command | ||
) | ||
wrap_function_wrapper( | ||
"redis.client", | ||
"BasePipeline.execute", | ||
_traced_execute_pipeline, | ||
) | ||
wrap_function_wrapper( | ||
"redis.client", | ||
"BasePipeline.immediate_execute_command", | ||
_traced_execute_command, | ||
) | ||
else: | ||
wrap_function_wrapper( | ||
"redis", "Redis.execute_command", _traced_execute_command | ||
) | ||
wrap_function_wrapper( | ||
"redis.client", "Pipeline.execute", _traced_execute_pipeline | ||
) | ||
wrap_function_wrapper( | ||
"redis.client", | ||
"Pipeline.immediate_execute_command", | ||
_traced_execute_command, | ||
) | ||
_instrument(tracer, response_hook=kwargs.get("response_hook")) | ||
|
||
def _uninstrument(self, **kwargs): | ||
if redis.VERSION < (3, 0, 0): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could you add an example usage of the hooks in the docstrings?
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.
done.