Skip to content

Commit b800830

Browse files
authored
Use strings.Cut() instead of string.SplitN() (#3822)
strings.Cut() generates less garbage as it does not allocate the slice to hold parts.
1 parent e8a2a70 commit b800830

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1212

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

15+
### Changed
16+
17+
- Use `strings.Cut()` instead of `string.SplitN()` for better readability and memory use. (#3822)
18+
1519
## [1.17.0-rc.1/0.42.0-rc.1/0.11.0-rc.1] - 2023-05-17
1620

1721
### Changed

instrumentation/google.golang.org/grpc/otelgrpc/internal/parse.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ import (
2626
// on a gRPC's FullMethod.
2727
func ParseFullMethod(fullMethod string) (string, []attribute.KeyValue) {
2828
name := strings.TrimLeft(fullMethod, "/")
29-
parts := strings.SplitN(name, "/", 2)
30-
if len(parts) != 2 {
29+
service, method, found := strings.Cut(name, "/")
30+
if !found {
3131
// Invalid format, does not follow `/package.service/method`.
32-
return name, []attribute.KeyValue(nil)
32+
return name, nil
3333
}
3434

3535
var attrs []attribute.KeyValue
36-
if service := parts[0]; service != "" {
36+
if service != "" {
3737
attrs = append(attrs, semconv.RPCService(service))
3838
}
39-
if method := parts[1]; method != "" {
39+
if method != "" {
4040
attrs = append(attrs, semconv.RPCMethod(method))
4141
}
4242
return name, attrs

0 commit comments

Comments
 (0)