-
Notifications
You must be signed in to change notification settings - Fork 10
Rd 3762 redis #154
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
Merged
Merged
Rd 3762 redis #154
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
60bc2ec
retry in case of error with edge
saartochner-lumigo e471345
split event and parsers
saartochner-lumigo 151c500
Merge branch 'master' of github.com:lumigo-io/python_tracer into RD-3…
saartochner-lumigo 8383ea1
split sync_hook to tracer and http hooks
saartochner-lumigo f56b826
refactor http hooks to be a wrapper
saartochner-lumigo 613ef65
refactor http hooks to be a wrapper
saartochner-lumigo 077f1be
refactor http hooks to be a wrapper
saartochner-lumigo 1c0d3ec
increase coverage
saartochner-lumigo 1764e73
refactor wrap once
saartochner-lumigo 838e077
support pymongo
saartochner-lumigo feb6abd
Merge branch 'master' of github.com:lumigo-io/python_tracer into RD-3…
saartochner-lumigo 846712f
Merge branch 'RD-3761-pymongo' of github.com:lumigo-io/python_tracer …
saartochner-lumigo fd8a27d
wip
saartochner-lumigo 8f1114c
Merge branch 'RD-3761-pymongo' of github.com:lumigo-io/python_tracer …
saartochner-lumigo 5a9c402
Merge branch 'master' into RD-3761-wrap-pymongo
saartochner-lumigo c5caf09
fix Dori's CR
saartochner-lumigo df61e65
fix Dori's CR
saartochner-lumigo 865fcee
Merge branch 'RD-3761-wrap-pymongo' into RD-3690-redis-sqlalchemy-ela…
saartochner-lumigo 2e83944
wip
saartochner-lumigo 4d0a2fa
Merge branch 'RD-3761-wrap-pymongo' into RD-3690-redis-sqlalchemy-ela…
saartochner-lumigo 3f0bf5c
Merge branch 'master' into RD-3761-wrap-pymongo
saartochner-lumigo f06a1c3
support redis
saartochner-lumigo 188bf41
wip
saartochner-lumigo 2809c3a
Merge branch 'RD-3761-wrap-pymongo' of github.com:lumigo-io/python_tr…
saartochner-lumigo 4520a2e
wip
saartochner-lumigo 03713d8
times
saartochner-lumigo b738fc5
Merge branch 'RD-3761-wrap-pymongo' of github.com:lumigo-io/python_tr…
saartochner-lumigo f837d38
IT
saartochner-lumigo a0f8ab0
Merge branch 'master' of github.com:lumigo-io/python_tracer into RD-3…
saartochner-lumigo 1a81a51
wip
saartochner-lumigo dc34b79
wip
saartochner-lumigo 3511adc
fix Nirhod's CR
saartochner-lumigo 009afa1
fix Nirhod's CR
saartochner-lumigo 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 hidden or 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 |
|---|---|---|
|
|
@@ -6,4 +6,5 @@ pytest-cov==2.6.1 | |
| capturer==2.4 | ||
| attrs==19.1.0 | ||
| requests==2.24.0 | ||
| pymongo==3.11.0 | ||
| pymongo==3.11.0 | ||
| redispy==3.0.0 | ||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,13 +1,17 @@ | ||
| from .http.sync_http_wrappers import wrap_http_calls | ||
| from .pymongo.pymongo_wrapper import wrap_pymongo | ||
| from .redis.redis_wrapper import wrap_redis | ||
|
|
||
|
|
||
| already_wrapped = False | ||
|
|
||
|
|
||
| def wrap(): | ||
| def wrap(force: bool = False): | ||
| global already_wrapped | ||
| if not already_wrapped: | ||
| # Never wrap http calls twice - it will create duplicate body | ||
| wrap_http_calls() | ||
| if force or not already_wrapped: | ||
saartochner-lumigo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| wrap_pymongo() | ||
| wrap_redis() | ||
| already_wrapped = True | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Empty file.
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import importlib | ||
| from typing import Optional, Dict, List, Union | ||
| import uuid | ||
|
|
||
| from lumigo_tracer.libs.wrapt import wrap_function_wrapper | ||
| from lumigo_tracer.lumigo_utils import ( | ||
| lumigo_safe_execute, | ||
| get_logger, | ||
| lumigo_dumps, | ||
| get_current_ms_time, | ||
| ) | ||
| from lumigo_tracer.spans_container import SpansContainer | ||
|
|
||
|
|
||
| REDIS_SPAN = "redis" | ||
|
|
||
|
|
||
| def command_started( | ||
| command: str, request_args: Union[Dict, List[Dict]], connection_options: Optional[Dict] | ||
| ): | ||
| span_id = str(uuid.uuid4()) | ||
| host = (connection_options or {}).get("host") | ||
| port = (connection_options or {}).get("port") | ||
| SpansContainer.get_span().add_span( | ||
| { | ||
| "id": span_id, | ||
| "type": REDIS_SPAN, | ||
| "started": get_current_ms_time(), | ||
| "requestCommand": command, | ||
| "requestArgs": lumigo_dumps(request_args), | ||
| "connectionOptions": {"host": host, "port": port}, | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| def command_finished(ret_val: Dict): | ||
| span = SpansContainer.get_span().get_last_span() | ||
| if not span: | ||
| get_logger().warning("Redis span ended without a record on its start") | ||
| return | ||
| span.update({"ended": get_current_ms_time(), "response": lumigo_dumps(ret_val)}) | ||
|
|
||
|
|
||
| def command_failed(exception: Exception): | ||
| span = SpansContainer.get_span().get_last_span() | ||
| if not span: | ||
| get_logger().warning("Redis span ended without a record on its start") | ||
| return | ||
| span.update( | ||
| {"ended": get_current_ms_time(), "error": exception.args[0] if exception.args else None} | ||
| ) | ||
|
|
||
|
|
||
| def execute_command_wrapper(func, instance, args, kwargs): | ||
nirLumigo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| with lumigo_safe_execute("redis start"): | ||
| command = args[0] if args else None | ||
| request_args = args[1:] if args and len(args) > 1 else None | ||
| connection_options = instance.connection_pool.connection_kwargs | ||
| command_started(command, request_args, connection_options) | ||
| try: | ||
| ret_val = func(*args, **kwargs) | ||
| command_finished(ret_val) | ||
| except Exception as e: | ||
| command_failed(e) | ||
| raise | ||
|
|
||
|
|
||
| def execute_wrapper(func, instance, args, kwargs): | ||
nirLumigo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| with lumigo_safe_execute("redis start"): | ||
| commands = instance.command_stack | ||
| command = [cmd[0] for cmd in commands if cmd] or None | ||
|
Contributor
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. Also here, the value here might be both list or None, is this ok?
Contributor
Author
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. here yes :-) |
||
| request_args = [cmd[1:] for cmd in commands if cmd and len(cmd) > 1] | ||
| connection_options = instance.connection_pool.connection_kwargs | ||
| command_started(lumigo_dumps(command), request_args, connection_options) | ||
| try: | ||
| ret_val = func(*args, **kwargs) | ||
| command_finished(ret_val) | ||
| except Exception as e: | ||
| command_failed(e) | ||
| raise | ||
|
|
||
|
|
||
| def wrap_redis(): | ||
| with lumigo_safe_execute("wrap redis"): | ||
| if importlib.util.find_spec("redis"): | ||
| get_logger().debug("wrapping redis") | ||
| wrap_function_wrapper("redis.client", "Redis.execute_command", execute_command_wrapper) | ||
| wrap_function_wrapper("redis.client", "Pipeline.execute", execute_wrapper) | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
|
|
||
| from lumigo_tracer.spans_container import SpansContainer | ||
| from lumigo_tracer.wrappers.redis.redis_wrapper import execute_command_wrapper, execute_wrapper | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def instance(): | ||
| return SimpleNamespace( | ||
| connection_pool=SimpleNamespace(connection_kwargs={"host": "lumigo"}), command_stack=None | ||
| ) | ||
|
|
||
|
|
||
| def func(*args, **kwargs): | ||
| return True | ||
|
|
||
|
|
||
| def test_execute_command_wrapper_happy_flow(instance): | ||
| execute_command_wrapper(func, instance, ["SET", {"a": 1}, "b"], {}) | ||
|
|
||
| spans = SpansContainer.get_span().spans | ||
| assert len(spans) == 1 | ||
| assert spans[0]["requestCommand"] == "SET" | ||
| assert spans[0]["requestArgs"] == '[{"a": 1}, "b"]' | ||
| assert spans[0]["connectionOptions"] == {"host": "lumigo", "port": None} | ||
| assert spans[0]["ended"] >= spans[0]["started"] | ||
| assert spans[0]["response"] == "true" | ||
| assert "error" not in spans[0] | ||
|
|
||
|
|
||
| def test_execute_command_wrapper_failing_command(instance): | ||
| with pytest.raises(ZeroDivisionError): | ||
| execute_command_wrapper(lambda *args, **kwargs: 1 / 0, instance, ["SET", {"a": 1}], {}) | ||
|
|
||
| spans = SpansContainer.get_span().spans | ||
| assert len(spans) == 1 | ||
| assert spans[0]["requestCommand"] == "SET" | ||
| assert spans[0]["ended"] >= spans[0]["started"] | ||
| assert spans[0]["error"] == "division by zero" | ||
| assert "response" not in spans[0] | ||
|
|
||
|
|
||
| def test_execute_command_wrapper_unexpected_params(instance): | ||
| execute_command_wrapper(func, instance, {"not": "list"}, {}) | ||
|
|
||
| spans = SpansContainer.get_span().spans | ||
| assert len(spans) == 0 | ||
|
|
||
|
|
||
| def test_execute_wrapper_happy_flow(instance, monkeypatch): | ||
| monkeypatch.setattr(instance, "command_stack", [["SET", {"a": 1}], ["GET", "a"]]) | ||
| execute_wrapper(func, instance, [], {}) | ||
|
|
||
| spans = SpansContainer.get_span().spans | ||
| assert len(spans) == 1 | ||
| assert spans[0]["requestCommand"] == '["SET", "GET"]' | ||
| assert spans[0]["requestArgs"] == '[[{"a": 1}], ["a"]]' | ||
| assert spans[0]["ended"] >= spans[0]["started"] | ||
| assert spans[0]["response"] == "true" | ||
| assert "error" not in spans[0] | ||
|
|
||
|
|
||
| def test_execute_wrapper_failing_command(instance, monkeypatch): | ||
| monkeypatch.setattr(instance, "command_stack", [["SET", {"a": 1}], ["GET", "a"]]) | ||
| with pytest.raises(ZeroDivisionError): | ||
| execute_wrapper(lambda *args, **kwargs: 1 / 0, instance, [], {}) | ||
|
|
||
| spans = SpansContainer.get_span().spans | ||
| assert len(spans) == 1 | ||
| assert spans[0]["requestCommand"] == '["SET", "GET"]' | ||
| assert spans[0]["ended"] >= spans[0]["started"] | ||
| assert spans[0]["error"] == "division by zero" | ||
| assert "response" not in spans[0] | ||
|
|
||
|
|
||
| def test_execute_wrapper_unexpected_params(instance, monkeypatch): | ||
| monkeypatch.setattr(instance, "command_stack", [{"not": "list"}]) | ||
| execute_wrapper(func, instance, [], {}) | ||
|
|
||
| spans = SpansContainer.get_span().spans | ||
| assert len(spans) == 0 |
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.