Skip to content

Commit

Permalink
Use strings.Cut() instead of string.SplitN()
Browse files Browse the repository at this point in the history
strings.Cut() generates less garbage as it does not allocate the slice to hold parts.
  • Loading branch information
ash2k committed May 19, 2023
1 parent e8a2a70 commit 3ece61b
Showing 1 changed file with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ import (
// on a gRPC's FullMethod.
func ParseFullMethod(fullMethod string) (string, []attribute.KeyValue) {
name := strings.TrimLeft(fullMethod, "/")
parts := strings.SplitN(name, "/", 2)
if len(parts) != 2 {
service, method, found := strings.Cut(name, "/")
if !found {
// Invalid format, does not follow `/package.service/method`.
return name, []attribute.KeyValue(nil)
return name, nil
}

var attrs []attribute.KeyValue
if service := parts[0]; service != "" {
if service != "" {
attrs = append(attrs, semconv.RPCService(service))
}
if method := parts[1]; method != "" {
if method != "" {
attrs = append(attrs, semconv.RPCMethod(method))
}
return name, attrs
Expand Down

0 comments on commit 3ece61b

Please sign in to comment.