-
Notifications
You must be signed in to change notification settings - Fork 800
Fix missing attributes in RedisCluster spans #2626
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
Open
a-cid
wants to merge
11
commits into
open-telemetry:main
Choose a base branch
from
a-cid:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+131
−3
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
65e2c64
fix missing attributes in RedisCluster spans
a-cid 8ff71aa
Merge branch 'main' into patch-1
a-cid 7802ade
added redis cluster test
a-cid f3024bc
Merge branch 'main' into patch-1
a-cid 0c97cdf
fix: avoid workaround for async RedisCluster
a-cid a0f908a
linting and formatting
a-cid 848cc51
add CHANGELOG entry
a-cid 5e71011
Update CHANGELOG.md
xrmx 045c9c6
Merge branch 'main' into patch-1
xrmx ac8cc13
Merge branch 'main' into patch-1
a-cid f5c4bf4
rename conn to instance
a-cid 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
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 |
|---|---|---|
|
|
@@ -33,6 +33,65 @@ | |
| from opentelemetry.test.test_base import TestBase | ||
| from opentelemetry.trace import SpanKind | ||
|
|
||
| default_cluster_slots = [ | ||
| [0, 8191, ["1.1.1.1", 6380, "node_0"], ["1.1.1.1", 6383, "node_3"]], | ||
| [8192, 16383, ["1.1.1.1", 6381, "node_1"], ["1.1.1.1", 6382, "node_2"]], | ||
| ] | ||
|
|
||
|
|
||
| def get_mocked_redis_cluster_client( | ||
| *args, func=None, cluster_slots_raise_error=False, **kwargs | ||
| ): # noqa | ||
| """ | ||
| Return a stable RedisCluster object that have deterministic | ||
| nodes and slots setup to remove the problem of different IP addresses | ||
| on different installations and machines. | ||
| """ | ||
| cluster_slots = kwargs.pop("cluster_slots", default_cluster_slots) | ||
| coverage_res = kwargs.pop("coverage_result", "yes") | ||
| cluster_enabled = kwargs.pop("cluster_enabled", True) | ||
| with mock.patch.object( | ||
| redis.Redis, "execute_command" | ||
| ) as execute_command_mock: | ||
|
|
||
| def execute_command(*_args, **_kwargs): | ||
| if _args[0] == "CLUSTER SLOTS": | ||
| if cluster_slots_raise_error: | ||
| raise redis.exceptions.ResponseError() | ||
| mock_cluster_slots = cluster_slots | ||
| return mock_cluster_slots | ||
| if _args[0] == "COMMAND": | ||
| return {"get": [], "set": []} | ||
| if _args[0] == "INFO": | ||
| return {"cluster_enabled": cluster_enabled} | ||
| if len(_args) > 1 and _args[1] == "cluster-require-full-coverage": | ||
| return {"cluster-require-full-coverage": coverage_res} | ||
| if func is not None: | ||
| return func(*args, **kwargs) | ||
| return execute_command_mock(*_args, **_kwargs) | ||
|
|
||
| execute_command_mock.side_effect = execute_command | ||
|
|
||
| with mock.patch.object( | ||
| redis._parsers.CommandsParser, "initialize", autospec=True | ||
| ) as cmd_parser_initialize: | ||
|
|
||
| def cmd_init_mock(self, r): | ||
| self.commands = { | ||
| "get": { | ||
| "name": "get", | ||
| "arity": 2, | ||
| "flags": ["readonly", "fast"], | ||
| "first_key_pos": 1, | ||
| "last_key_pos": 1, | ||
| "step_count": 1, | ||
| } | ||
| } | ||
|
|
||
| cmd_parser_initialize.side_effect = cmd_init_mock | ||
|
|
||
| return redis.RedisCluster(*args, **kwargs) | ||
|
|
||
|
|
||
| class TestRedis(TestBase): | ||
| def setUp(self): | ||
|
|
@@ -317,6 +376,68 @@ def test_attributes_unix_socket(self): | |
| NetTransportValues.OTHER.value, | ||
| ) | ||
|
|
||
| def test_attributes_redis_cluster(self): | ||
| with mock.patch.object(redis.RedisCluster, "from_url") as from_url: | ||
|
|
||
| def from_url_mocked(_url, **_kwargs): | ||
| return get_mocked_redis_cluster_client(url=_url, **_kwargs) | ||
|
|
||
| from_url.side_effect = from_url_mocked | ||
| redis_client = redis.RedisCluster.from_url( | ||
| "redis://foo:[email protected]:6380/0" | ||
| ) | ||
|
|
||
| with mock.patch.object( | ||
| redis._parsers.CommandsParser, "initialize", autospec=True | ||
| ) as cmd_parser_initialize: | ||
|
|
||
| def cmd_init_mock(self, r): | ||
| self.commands = { | ||
| "get": { | ||
| "name": "get", | ||
| "arity": 2, | ||
| "flags": ["readonly", "fast"], | ||
| "first_key_pos": 1, | ||
| "last_key_pos": 1, | ||
| "step_count": 1, | ||
| }, | ||
| "set": { | ||
| "name": "set", | ||
| "arity": -3, | ||
| "flags": ["write", "denyoom"], | ||
| "first_key_pos": 1, | ||
| "last_key_pos": 1, | ||
| "step_count": 1, | ||
| }, | ||
| } | ||
|
|
||
| cmd_parser_initialize.side_effect = cmd_init_mock | ||
| with mock.patch.object( | ||
| redis.connection.ConnectionPool, "get_connection" | ||
| ) as get_connection: | ||
| get_connection.return_value = mock.MagicMock() | ||
| redis_client.set("key", "value") | ||
|
|
||
| spans = self.memory_exporter.get_finished_spans() | ||
| self.assertEqual(len(spans), 1) | ||
|
|
||
| span = spans[0] | ||
| self.assertEqual( | ||
| span.attributes[SpanAttributes.DB_SYSTEM], | ||
| DbSystemValues.REDIS.value, | ||
| ) | ||
| self.assertEqual( | ||
| span.attributes[SpanAttributes.DB_REDIS_DATABASE_INDEX], 0 | ||
| ) | ||
| self.assertEqual( | ||
| span.attributes[SpanAttributes.NET_PEER_NAME], "1.1.1.1" | ||
| ) | ||
| self.assertEqual(span.attributes[SpanAttributes.NET_PEER_PORT], 6380) | ||
| self.assertEqual( | ||
| span.attributes[SpanAttributes.NET_TRANSPORT], | ||
| NetTransportValues.IP_TCP.value, | ||
| ) | ||
|
|
||
| def test_connection_error(self): | ||
| server = fakeredis.FakeServer() | ||
| server.connected = False | ||
|
|
@@ -422,3 +543,4 @@ async def redis_operations(): | |
| self.assertEqual(span.attributes.get("db.statement"), "SET ? ?") | ||
| self.assertEqual(span.kind, SpanKind.CLIENT) | ||
| self.assertEqual(span.status.status_code, trace.StatusCode.UNSET) | ||
|
|
||
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.