Skip to content

Commit

Permalink
Use strings.Cut() instead of string.SplitN() (#3822)
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 21, 2023
1 parent e8a2a70 commit b800830
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Add the new `go.opentelemetry.io/contrib/instrgen` package to provide auto-generated source code instrumentation. (#3068, #3108)

### Changed

- Use `strings.Cut()` instead of `string.SplitN()` for better readability and memory use. (#3822)

## [1.17.0-rc.1/0.42.0-rc.1/0.11.0-rc.1] - 2023-05-17

### Changed
Expand Down
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 b800830

Please sign in to comment.