Skip to content

Commit

Permalink
Split port from host in gRPC client attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Oct 24, 2024
1 parent eec1740 commit 16c9aa8
Showing 1 changed file with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package grpc
import (
"fmt"
"log/slog"
"net"
"strconv"
"strings"

"github.com/cilium/ebpf"
"go.opentelemetry.io/collector/pdata/pcommon"
Expand Down Expand Up @@ -118,19 +118,25 @@ func processFn(pkg, ver, schemaURL string) func(*event) ptrace.ScopeSpans {
ss.SetSchemaUrl(schemaURL)

method := unix.ByteSliceToString(e.Method[:])
target := unix.ByteSliceToString(e.Target[:])
address := unix.ByteSliceToString(e.Target[:])

var port int
host, portStr, err := net.SplitHostPort(address)
if err == nil {
port, _ = strconv.Atoi(portStr)
} else {
host = address
}

attrs := []attribute.KeyValue{
semconv.RPCSystemKey.String("grpc"),
semconv.RPCServiceKey.String(method),
semconv.ServerAddress(target),
semconv.ServerAddress(host),
semconv.RPCGRPCStatusCodeKey.Int(int(e.StatusCode)),
}
// remove port
if parts := strings.Split(target, ":"); len(parts) > 1 {
if remotePeerPortInt, err := strconv.Atoi(parts[1]); err == nil {
attrs = append(attrs, semconv.NetworkPeerPort(remotePeerPortInt))
}

if port > 0 {
attrs = append(attrs, semconv.NetworkPeerPort(port))
}

span := ss.Spans().AppendEmpty()
Expand Down

0 comments on commit 16c9aa8

Please sign in to comment.