fix(middleware/tracing): replace strings.TrimLeft with strings.CutPrefix for correct fullMethod parsing#3668
Open
1911860538 wants to merge 4 commits intogo-kratos:mainfrom
Open
fix(middleware/tracing): replace strings.TrimLeft with strings.CutPrefix for correct fullMethod parsing#36681911860538 wants to merge 4 commits intogo-kratos:mainfrom
1911860538 wants to merge 4 commits intogo-kratos:mainfrom
Conversation
…fix for correct fullMethod parsing
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR refines the parsing of gRPC fullMethod strings by using strings.CutPrefix instead of strings.TrimLeft, enabling detection of invalid formats and ensuring only a leading slash is removed.
- Use
strings.CutPrefixto remove exactly one leading/and detect when it’s missing - Introduce an early return with an attribute for inputs not following
/package.service/method - Keep downstream splitting logic unchanged
Comments suppressed due to low confidence (2)
middleware/tracing/span.go:105
- [nitpick] The boolean variable
cutis ambiguous. Consider renaming it to something likehasPrefixorremovedPrefixto clarify its purpose.
name, cut := strings.CutPrefix(fullMethod, "/")
middleware/tracing/span.go:106
- The new branch handling inputs without a leading slash should be covered by unit tests to ensure correct behavior when
fullMethoddoesn't start with/.
if !cut {
Collaborator
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Replaced strings.TrimLeft with strings.CutPrefix to correctly handle fullMethod input.
strings.CutPrefix not only removes the leading '/', but also allows us to check whether fullMethod starts with a '/'.
This ensures that only the correct prefix is removed and provides a clear indication when the input doesn't follow the expected
/package.service/methodformat.