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 SofaRpcMetricsCollector npe #1460

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Just-CJ
Copy link

@Just-CJ Just-CJ commented Nov 12, 2024

Motivation:

修复SofaRpcMetricsCollector中的npe问题

Modification:

getLongAvoidNull方法取不到时返回0L,不返回null

Result:

Fixes #1459.

Summary by CodeRabbit

  • Bug Fixes
    • Updated the method for handling null values, ensuring a primitive long value is returned instead of null.

@sofastack-cla sofastack-cla bot added bug Something isn't working cla:yes CLA is ok First-time contributor First-time contributor size/XS labels Nov 12, 2024
Copy link
Contributor

coderabbitai bot commented Nov 12, 2024

Walkthrough

The pull request modifies the getLongAvoidNull method in the SofaRpcMetricsCollector class by changing its return type from Long to long. This adjustment ensures that the method returns a primitive long value, specifically 0L, when the input object is null, thereby preventing potential null pointer exceptions (NPE) in the context of metrics collection.

Changes

File Change Summary
metrics/metrics-prometheus/src/main/java/com/alipay/sofa/rpc/metrics/prometheus/SofaRpcMetricsCollector.java Updated method signature from private static Long getLongAvoidNull(Object object) to private static long getLongAvoidNull(Object object).

Assessment against linked issues

Objective Addressed Explanation
Prevent NPE in SofaRpcMetricsCollector (#1459)

Suggested labels

size/L

Suggested reviewers

  • EvenLjj

Poem

In the meadow where metrics play,
A rabbit hops, brightening the day.
No more nulls to cause a fright,
Just zeros now, all feels right!
With every hop, we gather cheer,
Metrics safe, let’s all draw near! 🐇✨


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 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
Contributor

@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: 0

🧹 Outside diff range and nitpick comments (1)
metrics/metrics-prometheus/src/main/java/com/alipay/sofa/rpc/metrics/prometheus/SofaRpcMetricsCollector.java (1)

Line range hint 203-212: Consider enhancing type safety

While the current implementation handles null, Integer, and Long cases, consider adding support for other number types and better error handling.

Here's a more robust implementation:

 private static long getLongAvoidNull(Object object) {
     if (object == null) {
         return 0L;
     }
-    if (object instanceof Integer) {
-        return Long.parseLong(object.toString());
+    if (object instanceof Number) {
+        return ((Number) object).longValue();
     }
-    return (Long) object;
+    try {
+        return Long.parseLong(object.toString());
+    } catch (NumberFormatException e) {
+        return 0L;
+    }
 }

This change:

  1. Handles all Number types (Integer, Long, Double, etc.)
  2. Safely parses string representations
  3. Falls back to 0L for unparseable values
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between be93251 and 9982615.

📒 Files selected for processing (1)
  • metrics/metrics-prometheus/src/main/java/com/alipay/sofa/rpc/metrics/prometheus/SofaRpcMetricsCollector.java (1 hunks)
🔇 Additional comments (2)
metrics/metrics-prometheus/src/main/java/com/alipay/sofa/rpc/metrics/prometheus/SofaRpcMetricsCollector.java (2)

203-205: LGTM! The NPE fix looks good.

The change from Long to long with 0L default effectively prevents NPE in metrics collection, which aligns with the PR objective. This is a safe change since zero is a reasonable default value for missing metrics data.


Line range hint 203-212: Verify metrics collection behavior

Let's verify that all callers properly handle the primitive long return type and that the change doesn't affect metrics collection.

Copy link
Contributor

@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: 0

🧹 Outside diff range and nitpick comments (2)
metrics/metrics-prometheus/src/main/java/com/alipay/sofa/rpc/metrics/prometheus/SofaRpcMetricsCollector.java (2)

Line range hint 203-211: LGTM! The NPE fix looks good with a minor improvement suggestion.

The change from Long to long effectively prevents NPEs by returning 0L for null inputs, which is appropriate for metrics collection. However, the type handling could be more robust.

Consider making the number handling more robust:

 private static long getLongAvoidNull(Object object) {
     if (object == null) {
         return 0L;
     }
-    if (object instanceof Integer) {
+    if (object instanceof Number) {
-        return Long.parseLong(object.toString());
+        return ((Number) object).longValue();
     }
-    return (Long) object;
+    try {
+        return Long.parseLong(object.toString());
+    } catch (NumberFormatException e) {
+        return 0L;
+    }
 }

This change:

  1. Handles all Number types (Integer, Long, Double, etc.)
  2. Safely parses string representations
  3. Provides a fallback for unparseable values

Inconsistent null handling in getLongAvoidNull

  • SofaRpcMetricsCollector.java returns 0L when null
  • SofaRpcMetrics.java returns null when null
    Review and standardize the null handling behavior to ensure consistent metrics collection.
🔗 Analysis chain

Line range hint 203-211: Verify metrics collection behavior with the new changes

The change affects how metrics are collected when null values are encountered. Let's verify the impact on metrics collection and reporting.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for potential issues with metrics collection

# Look for other metric collectors that might expect null values
rg -l "implements.*Collector" --type java

# Check for other methods that might be affected by getLongAvoidNull
rg "getLongAvoidNull" --type java -B 2 -A 2

# Look for test cases involving metrics collection
rg -l "Test.*Metrics|Metrics.*Test" --type java

Length of output: 6953

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between be93251 and 9982615.

📒 Files selected for processing (1)
  • metrics/metrics-prometheus/src/main/java/com/alipay/sofa/rpc/metrics/prometheus/SofaRpcMetricsCollector.java (1 hunks)

Choose a reason for hiding this comment

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cla:yes CLA is ok First-time contributor First-time contributor size/XS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SofaRpcMetricsCollector可能产生npe
1 participant