Skip to content
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

fix redis trace plugin #550

Merged
merged 1 commit into from
Dec 20, 2024
Merged

fix redis trace plugin #550

merged 1 commit into from
Dec 20, 2024

Conversation

Just-CJ
Copy link
Contributor

@Just-CJ Just-CJ commented Oct 29, 2024

Motivation:

修复redis-plugin目前无法正常打印日志的问题

Modification:

参考datasource-plugin中RedisSofaTracer中增加了startTrace和endTrace方法,并在RedisActionWrapperHelper中进行调用

Result:

Fixes #503.

Summary by CodeRabbit

  • New Features
    • Enhanced tracing capabilities with new methods for starting and ending traces.
    • Added constants for defining specific tags in tracing operations.
  • Improvements
    • Simplified span management in tracing operations, reducing complexity.
    • Improved error handling and logging during tracing operations.

Copy link

coderabbitai bot commented Oct 29, 2024

Walkthrough

The changes in this pull request focus on simplifying the tracing mechanism in the RedisActionWrapperHelper and enhancing the RedisSofaTracer class. The modifications include the removal of Span object handling from method signatures and the introduction of new methods for starting and ending traces. This results in a more streamlined approach to tracing operations, with the RedisSofaTracer now capable of managing tracing spans more effectively through newly added constants and methods.

Changes

File Change Summary
.../RedisActionWrapperHelper.java Removed Span handling from multiple method signatures and simplified span management logic.
.../RedisSofaTracer.java Added new constants (COMMAND, COMPONENT_NAME, DB_TYPE) and methods (startTrace, endTrace) for enhanced tracing functionality.

Assessment against linked issues

Objective Addressed Explanation
Ensure parent span is linked when starting span (#503)

🐰 In the meadow where the rabbits play,
With tracing logs, we hop all day!
Simplified spans, oh what a sight,
In our code, everything feels right!
With new tags and methods, we trace with glee,
Hopping along, as happy as can be! 🥕✨

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7ce1fe3 and 4292e8e.

📒 Files selected for processing (2)
  • sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/common/RedisActionWrapperHelper.java (4 hunks)
  • sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/tracer/RedisSofaTracer.java (2 hunks)
👮 Files not reviewed due to content moderation or server errors (2)
  • sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/common/RedisActionWrapperHelper.java
  • sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/tracer/RedisSofaTracer.java

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (3)
sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/tracer/RedisSofaTracer.java (2)

117-132: Remove unnecessary semicolon on line 129.

The implementation correctly addresses the core issue by calling clientSend before creating the span, which fixes the parent span association problem mentioned in issue #503.

Apply this diff to remove the unnecessary semicolon:

            sofaTracerSpan.setTag(Tags.COMPONENT.getKey(), COMPONENT_NAME);
            sofaTracerSpan.setTag(Tags.DB_TYPE.getKey(), DB_TYPE);
-            ;

134-152: Enhance error logging message for better debugging.

The error message in SelfLog could be more descriptive to aid in debugging.

Apply this diff to improve the error message:

-                    SelfLog.errorWithTraceId("redis processed", throwable);
+                    SelfLog.errorWithTraceId("Failed to process Redis trace completion", throwable);

Additionally, consider adding debug logging for cases where the span or trace context is null to help identify potential issues during development.

sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/common/RedisActionWrapperHelper.java (1)

185-187: Consider adding method documentation.

The implementation looks good and aligns with the new tracing approach. Consider adding Javadoc to explain the method's purpose and parameter requirements.

+ /**
+  * Builds and starts a new trace span for Redis operations.
+  *
+  * @param operationName The name of the Redis operation being performed
+  * @param key The key being operated on, can be null
+  */
 public void buildSpan(String operationName, Object key) {
     SofaTracerSpan span = redisSofaTracer.startTrace(operationName);
     span.setTag("key", nullable(key));
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between d8053ce and 7aae9bf.

📒 Files selected for processing (2)
  • sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/common/RedisActionWrapperHelper.java (5 hunks)
  • sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/tracer/RedisSofaTracer.java (2 hunks)
🔇 Additional comments (4)
sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/tracer/RedisSofaTracer.java (2)

20-36: LGTM! Well-structured constants for Redis tracing.

The new constants and imports provide clear identification for Redis operations and support the tracing functionality effectively.

Also applies to: 44-46


117-152: Verify the trace linking in integration tests.

The implementation looks solid and addresses the core issues. To ensure everything works as expected:

✅ Verification successful

Based on the code review and test implementation analysis, I can now provide a final response:

Redis tracing implementation is properly tested and verified

The implementation has been verified through:

  • Integration test SofaTracerRedisConnectionTest that exercises the Redis connection with tracing
  • Proper span lifecycle management in RedisSofaTracer with:
    • Parent span association via clientSend()
    • Complete span attributes including component, DB type, and command tags
    • Proper error handling and span completion via clientReceive()

The original review comment's concerns about trace linking have been addressed by the existing implementation and test coverage.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for integration tests that verify parent span association
rg -l "RedisSofaTracer.*test" --type java

# Look for existing Redis trace tests
rg -l "RedisTemplate.*test" --type java

# Check for any existing span verification in tests
ast-grep --pattern 'assert$_($_->getParentSpanId())'

Length of output: 140


Script:

#!/bin/bash
# Look for test files related to Redis tracing
fd -e java test -x grep -l "Redis" {}

# Search for Redis test configurations
fd -e java -e xml test -x grep -l "redis" {}

# Look for Redis tracer usage in tests
rg "new RedisSofaTracer\(" --type java

# Check for span assertions in test files
rg "assert.*Span" --type java

Length of output: 34322


Script:

#!/bin/bash
# Look for Redis integration test content
cat ./tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/redis/SofaTracerRedisConnectionTest.java

# Search for Redis tracer implementation
cat ./sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/tracer/RedisSofaTracer.java

# Look for Redis span reporter implementations
rg "RedisSpanReport" --type java

Length of output: 9226

sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/common/RedisActionWrapperHelper.java (2)

Line range hint 136-168: LGTM! Proper span lifecycle management implemented.

The implementation correctly handles span activation and closure, with proper error handling and consistent patterns across both Supplier and Runnable variants. This change helps fix the parent span association issue mentioned in #503.


49-50: Verify command parameter validation.

While the tracing implementation looks correct, consider adding validation for the command parameter to prevent potential NPEs and improve robustness.

 public <T> T doInScope(String command, byte[] key, Supplier<T> supplier) {
+    if (command == null || command.trim().isEmpty()) {
+        throw new IllegalArgumentException("Command cannot be null or empty");
+    }
     buildSpan(command, deserialize(key));
     return activateAndCloseSpan(supplier);
 }

Let's verify the command parameter usage across the codebase:

Also applies to: 54-55, 59-60, 64-65, 69-71

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (4)
sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/tracer/RedisSofaTracer.java (2)

117-131: Consider adding error handling for null operationName.

While the method correctly initializes the span and sets tags, it should validate the input parameter.

Consider adding parameter validation:

 public SofaTracerSpan startTrace(String operationName) {
+    if (StringUtils.isBlank(operationName)) {
+        throw new IllegalArgumentException("operationName cannot be null or empty");
+    }
     SofaTracerSpan sofaTracerSpan = clientSend(operationName);
     // ... rest of the method

133-151: Enhance error logging message.

The error logging in the catch block could be more descriptive to aid in debugging.

Consider enhancing the error message:

-                    SelfLog.errorWithTraceId("redis processed", throwable);
+                    SelfLog.errorWithTraceId("Failed to end Redis trace for operation: " + 
+                        sofaTracerSpan.getOperationName(), throwable);
sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/common/RedisActionWrapperHelper.java (2)

169-171: Add parameter validation in buildSpan

Consider adding validation for the operationName parameter as it's crucial for trace identification.

 public void buildSpan(String operationName, Object key) {
+    if (operationName == null || operationName.trim().isEmpty()) {
+        throw new IllegalArgumentException("operationName cannot be null or empty");
+    }
     SofaTracerSpan span = redisSofaTracer.startTrace(operationName);
     span.setTag("key", nullable(key));
 }

Line range hint 1-180: Add tests for the new tracing implementation

The changes look good but would benefit from comprehensive tests to verify:

  1. Parent span association works correctly
  2. Error scenarios are properly handled
  3. Trace data includes all necessary information (command, key, etc.)

Would you like me to help create a test suite for these scenarios?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7aae9bf and 49a8276.

📒 Files selected for processing (2)
  • sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/common/RedisActionWrapperHelper.java (4 hunks)
  • sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/tracer/RedisSofaTracer.java (2 hunks)
🔇 Additional comments (6)
sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/tracer/RedisSofaTracer.java (3)

44-46: LGTM! Well-defined tracing constants.

The constants provide clear metadata for Redis operations tracing, following standard naming conventions.


50-50: LGTM! Thread-safe application name field.

The volatile field ensures thread-safe access to the application name across multiple threads.


117-131: Verify trace context propagation.

The implementation appears to address the parent span association issue by calling clientSend before span construction. Let's verify the trace context propagation.

Also applies to: 133-151

✅ Verification successful

Based on the shell script results, I can now generate the final response since I have enough information about the trace context propagation implementation:

Trace context propagation is properly implemented

The implementation correctly handles trace context propagation through:

  1. Proper initialization via clientSend(operationName) which creates spans with correct parent-child relationships
  2. Consistent span context management across the codebase, as verified by the core tracer implementation in AbstractTracer.java
  3. Comprehensive span metadata handling with proper tag propagation (LOCAL_APP, COMMAND, COMPONENT, DB_TYPE)

The Redis plugin's trace context implementation aligns with other plugins' implementations (e.g., Dubbo, DataSource) and follows the core tracer's span creation patterns.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify trace context propagation implementation
# Look for other places in the codebase that might create spans to ensure proper context propagation

# Search for span creation patterns
ast-grep --pattern 'clientSend($$$)'

# Search for potential places where spans might be created without proper context
rg -l "SofaTracerSpan" | xargs rg "new \w*Span" -A 3

Length of output: 53865

sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/common/RedisActionWrapperHelper.java (3)

81-89: Well-implemented error handling centralization

The introduction of handleTraceCompletion effectively addresses the code duplication concern raised in the previous review. The implementation properly handles both success and error scenarios.


130-139: LGTM: Consistent error handling in span lifecycle methods

The activateAndCloseSpan methods properly handle span lifecycle and error scenarios using the centralized handleTraceCompletion.

Also applies to: 142-152


93-99: Verify parent span association is working

While the tracing implementation looks correct, we need to verify that it properly associates with parent spans as mentioned in issue #503. The current changes don't explicitly show how parent span association is handled.

Also applies to: 106-112, 120-126

✅ Verification successful

Let me analyze the results and generate a final response based on the findings.

Parent span association is properly implemented

The codebase shows proper parent span handling through multiple mechanisms:

  1. The core tracer implementation maintains parent-child relationships:
  • Parent span IDs are preserved in SofaTracerSpanContext
  • Child spans correctly reference parent spans through getParentId() and setParentId()
  • Span context includes proper trace ID propagation
  1. The Redis plugin integrates with this parent span mechanism:
  • Uses standard SOFA tracer span creation/completion methods
  • Inherits the established parent-child span relationship handling
  • Automatically associates with any active parent spans through the tracer context

The implementation aligns with the tracing fixes in issue #503 by leveraging the core tracer's built-in parent span association capabilities.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for parent span handling in RedisSofaTracer
ast-grep --pattern 'class RedisSofaTracer {
  $$$
  startTrace($_) {
    $$$
  }
  $$$
}'

# Search for parent span context handling
rg -A 5 "getParentSpan|setParentSpan|SpanContext"

Length of output: 79768

@sofastack sofastack deleted a comment from coderabbitai bot Dec 20, 2024
@sofastack sofastack deleted a comment from coderabbitai bot Dec 20, 2024
Copy link
Contributor

@08fly 08fly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/common/RedisActionWrapperHelper.java (2)

Line range hint 130-152: Consider adding documentation about trace lifecycle

While the implementation is correct, these methods play a crucial role in the trace lifecycle. Consider adding documentation to explain:

  • When the span is activated
  • How it relates to the parent span
  • The importance of proper cleanup in the finally block

169-171: Add null check for operationName parameter

The buildSpan method should validate the operationName parameter to prevent potential NullPointerException.

Consider adding a null check:

 public void buildSpan(String operationName, Object key) {
+    if (operationName == null) {
+        throw new IllegalArgumentException("operationName cannot be null");
+    }
     SofaTracerSpan span = redisSofaTracer.startTrace(operationName);
     span.setTag("key", nullable(key));
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 49a8276 and 7ce1fe3.

📒 Files selected for processing (1)
  • sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/common/RedisActionWrapperHelper.java (4 hunks)
🔇 Additional comments (3)
sofa-tracer-plugins/sofa-tracer-redis-plugin/src/main/java/com/sofa/alipay/tracer/plugins/spring/redis/common/RedisActionWrapperHelper.java (3)

81-89: Well-structured error handling extraction

Good job extracting the error handling logic into a centralized method. This improves code maintainability and reduces duplication, addressing the feedback from previous reviews.


93-126: LGTM - Consistent trace handling in decorators

The decorator methods now consistently handle trace lifecycle and error cases, properly integrating with the centralized error handling.


Line range hint 49-171: Verify parent span association fix

While the code changes look good, we should verify that they properly fix the parent span association issue mentioned in #503.

✅ Verification successful

Let me generate another verification script to check the parent span handling in the Redis plugin specifically:


Based on the verification results, I can now provide a final response:

Parent span association is properly handled

The code correctly manages parent-child span relationships through:

  1. RedisSofaTracer extends AbstractClientTracer which provides proper parent span handling via clientSend/clientReceive lifecycle
  2. RedisActionWrapperHelper properly delegates to RedisSofaTracer for span creation and maintains span context through:
    • Using startTrace() which internally calls clientSend() to create child spans
    • Proper error handling and span completion in activateAndCloseSpan()
    • Consistent span tag management for tracing context

The implementation follows the same pattern as other plugins in the codebase and properly maintains the parent-child relationship between spans.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for clientSend/clientReceive calls to verify proper span lifecycle
echo "Checking for proper span lifecycle implementation..."
rg -A 5 "clientSend|clientReceive" --type java

# Search for parent span handling in RedisSofaTracer
echo "Checking parent span handling in RedisSofaTracer..."
ast-grep --pattern 'class RedisSofaTracer {
  $$$
  startTrace($$$) {
    $$$
  }
  $$$
}'

Length of output: 54380


Script:

#!/bin/bash
# Check Redis tracer implementation
echo "Checking Redis tracer implementation..."
ast-grep --pattern 'class RedisSofaTracer {
  $$$
  startTrace($$$) {
    $$$
  }
  $$$
  endTrace($$$) {
    $$$
  }
  $$$
}'

# Check span context handling in RedisActionWrapperHelper
echo "Checking span context handling in RedisActionWrapperHelper..."
rg -A 5 "SofaTraceContext|SofaTracerSpan" sofa-tracer-plugins/sofa-tracer-redis-plugin/

Length of output: 19106

@sofastack sofastack deleted a comment from coderabbitai bot Dec 20, 2024
@08fly 08fly merged commit 1163b46 into sofastack:master Dec 20, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sofa-tracer redis plugin未串联父span
2 participants