Ver Fonte

Feature #TASK_QT-18250 1、增加grpc-server模式,获取sn和sport数据。

rock há 7 meses atrás
pai
commit
97c62e8d8d
2 ficheiros alterados com 48 adições e 6 exclusões
  1. 45 5
      containers/container_apm.go
  2. 3 1
      tracing/apm_tracing.go

+ 45 - 5
containers/container_apm.go

@@ -5,6 +5,7 @@ import (
 	"bytes"
 	"debug/elf"
 	"fmt"
+	"net"
 	"os"
 	"path"
 	"sort"
@@ -55,6 +56,48 @@ func (c *Container) getOrInitTrace(traceId uint64) (*tracing.Trace, error) {
 	return trace, nil
 }
 
+// getGrpcServerNetworkInfo 获取 gRPC server 的网络信息
+// 返回: IP地址, 端口号, 容器ID
+func (c *Container) getGrpcServerNetworkInfo() (string, uint16, string) {
+	containerID := ""
+	if c.cgroup != nil {
+		containerID = c.cgroup.ContainerId
+	}
+
+	ipAddr := ""
+	ifaces, err := net.Interfaces()
+	if err == nil {
+		for _, iface := range ifaces {
+			if iface.Name == "eth0" {
+				addrs, err := iface.Addrs()
+				if err == nil {
+					for _, addr := range addrs {
+						var ipnet *net.IPNet
+						switch v := addr.(type) {
+						case *net.IPNet:
+							ipnet = v
+						case *net.IPAddr:
+							ipnet = &net.IPNet{IP: v.IP, Mask: v.IP.DefaultMask()}
+						}
+						if ipnet != nil && ipnet.IP.To4() != nil {
+							ipAddr = ipnet.IP.String()
+							break
+						}
+					}
+				}
+				break
+			}
+		}
+	}
+	klog.Debugf("grpc server ip %s", ipAddr)
+
+	// 本地端口尝试从AppInfo.Sport获取
+	port := c.AppInfo.Sport
+	klog.Debugf("grpc server port %d", port)
+
+	return ipAddr, uint16(port), containerID
+}
+
 // Deprecated: InitTrace not used
 //func (c *Container) InitTrace(traceId uint64, r *l7.RequestData) error {
 //	method, path, hostIp, port := l7.ParseHttpHost(r.Payload)
@@ -131,11 +174,8 @@ func (c *Container) onL7RequestApm(pid uint32, fd uint64, timestamp uint64, r *l
 					trace.TraceStartEvent(method, requestURI, sn, sport, r.Status, netaddr.IPPortFrom(ip, sport), pid, c.GetAppInfo(), container_id)
 					c.SendEvent(trace, r.TraceId)
 				} else if r.TraceType == 1 {
-					container_id := ""
-					if c.cgroup != nil {
-						container_id = c.cgroup.ContainerId
-					}
-					trace.GrpcServerTraceStartEvent(r, c.GetAppInfo(), container_id)
+					ipAddr, port, containerID := c.getGrpcServerNetworkInfo()
+					trace.GrpcServerTraceStartEvent(ipAddr, port, r, c.GetAppInfo(), containerID)
 					c.SendEvent(trace, r.TraceId)
 
 					// apmTrace, err := c.getOrInitTrace(r.TraceId)

+ 3 - 1
tracing/apm_tracing.go

@@ -129,9 +129,11 @@ func (t *Trace) AllEventReady(traceID uint64) bool {
 	return t.startEventReady && t.endEventReady && *t.currenEventCount >= t.needEventCount
 }
 
-func (t *Trace) GrpcServerTraceStartEvent(r *l7.RequestData, appInfo AppInfo, container_id string) {
+func (t *Trace) GrpcServerTraceStartEvent(sn string, sport uint16,r *l7.RequestData, appInfo AppInfo, container_id string) {
 	t.span.SetAttributes(attribute.String("rpc.uri", string(r.Payload)))
 	t.commonAttrs = []attribute.KeyValue{
+		semconv.NetPeerName(sn),
+		semconv.NetPeerPort(int(sport)),
 		// buildAppMapFromEvent
 		attribute.Int("server.code_type", appInfo.CodeType.Int()),
 		attribute.String("server.app_name", appInfo.AppName),