Skip to content

Commit

Permalink
Preallocate slice in spanInfo() (#4262)
Browse files Browse the repository at this point in the history
  • Loading branch information
ash2k committed Sep 5, 2023
1 parent b1ea1d9 commit 0c4e5a9
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions instrumentation/google.golang.org/grpc/otelgrpc/interceptor.go
Expand Up @@ -501,26 +501,29 @@ func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
// spanInfo returns a span name and all appropriate attributes from the gRPC
// method and peer address.
func spanInfo(fullMethod, peerAddress string) (string, []attribute.KeyValue) {
attrs := []attribute.KeyValue{RPCSystemGRPC}
name, mAttrs := internal.ParseFullMethod(fullMethod)
peerAttrs := peerAttr(peerAddress)

attrs := make([]attribute.KeyValue, 0, 1+len(mAttrs)+len(peerAttrs))
attrs = append(attrs, RPCSystemGRPC)
attrs = append(attrs, mAttrs...)
attrs = append(attrs, peerAttr(peerAddress)...)
attrs = append(attrs, peerAttrs...)
return name, attrs
}

// peerAttr returns attributes about the peer address.
func peerAttr(addr string) []attribute.KeyValue {
host, p, err := net.SplitHostPort(addr)
if err != nil {
return []attribute.KeyValue(nil)
return nil
}

if host == "" {
host = "127.0.0.1"
}
port, err := strconv.Atoi(p)
if err != nil {
return []attribute.KeyValue(nil)
return nil
}

var attr []attribute.KeyValue
Expand Down

0 comments on commit 0c4e5a9

Please sign in to comment.