Browse Source

Feature #TASK_QT-29889 【印尼-Telkom-POC】可观测-https请求的协议和端口识别不准确问题处理

Tom 7 months ago
parent
commit
0de0129ab4

+ 19 - 19
containers/container_apm.go

@@ -56,23 +56,23 @@ func (c *Container) getOrInitTrace(traceId uint64) (*tracing.Trace, error) {
 }
 
 // Deprecated: InitTrace not used
-func (c *Container) InitTrace(traceId uint64, r *l7.RequestData) error {
-	method, path, hostIp, port := l7.ParseHttpHost(r.Payload)
-	ip, err := netaddr.ParseIP(hostIp)
-	if err != nil {
-		//fmt.Println("host ip error")
-		hostIp = "127.0.0.1"
-	}
-	addr := netaddr.IPPortFrom(ip, port)
-	trace := tracing.NewTrace(string(c.id), addr)
-	if trace == nil {
-		return fmt.Errorf("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT is null")
-	}
-	c.traceMap[traceId] = trace
-
-	trace.TraceStart(method, path, r.Status, r.Duration)
-	return nil
-}
+//func (c *Container) InitTrace(traceId uint64, r *l7.RequestData) error {
+//	method, path, hostIp, port := l7.ParseHttpHost(r.Payload)
+//	ip, err := netaddr.ParseIP(hostIp)
+//	if err != nil {
+//		//fmt.Println("host ip error")
+//		hostIp = "127.0.0.1"
+//	}
+//	addr := netaddr.IPPortFrom(ip, port)
+//	trace := tracing.NewTrace(string(c.id), addr)
+//	if trace == nil {
+//		return fmt.Errorf("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT is null")
+//	}
+//	c.traceMap[traceId] = trace
+//
+//	trace.TraceStart(method, path, r.Status, r.Duration)
+//	return nil
+//}
 
 // 在任意阶段,r.TraceId 不等于0 则创建 traceMap && createParentSpan
 // 更新 createTraceSpan 机制,更新触发traceEnd机制,当事件个数满足时,任意event均可触发end
@@ -120,7 +120,7 @@ func (c *Container) onL7RequestApm(pid uint32, fd uint64, timestamp uint64, r *l
 				klog.Debugf("->>> [%s] -> payload:[%s]", c.AppInfo.AppName, r.Payload)
 			}
 			if err == nil {
-				method, requestURI, sn, sport := l7.ParseHttpHost(r.Payload)
+				method, requestURI, sn, sport := l7.ParseHttpHost(r.Payload, r.IsTls)
 				ip, _ := netaddr.ParseIP(sn)
 				//codeType := c.GetCodeTypeFromCache(pid)
 				container_id := ""
@@ -145,7 +145,7 @@ func (c *Container) onL7RequestApm(pid uint32, fd uint64, timestamp uint64, r *l
 	}
 	if r.Protocol == l7.ProtocolHTTP {
 		if c.l7Attach && c.valuableTrace(r.TraceId) {
-			method, requestURI, sn, sport := l7.ParseHttpHost(r.Payload)
+			method, requestURI, sn, sport := l7.ParseHttpHost(r.Payload, r.IsTls)
 			apmTrace, err := c.getOrInitTrace(r.TraceId)
 			//fmt.Println("ProtocolHTTP-----", r.TraceId, err)
 			if err == nil {

+ 4 - 0
ebpftracer/ebpf/l7/l7.c

@@ -86,6 +86,7 @@ struct l7_event {
     __u8 daddr[16];
     __u16 component_sport;
     __u16 component_dport;
+    __u16 is_tls;
     __u8 component_saddr[16];
     __u8 component_daddr[16];
 	unsigned char assumed_app_id[APM_ASSUMED_APP_ID_SIZE];
@@ -463,6 +464,8 @@ int trace_enter_write(void *ctx, __u64 fd, __u16 is_tls, char *buf, __u64 size,
         e->payload_size = size;
         e->event_count = event_count;
         COPY_PAYLOAD(e->payload, size, payload);
+        e->is_tls = is_tls;
+
         bpf_map_delete_elem(&active_l7_requests, &k);
 		// 清除事件计数
 	    bpf_map_delete_elem(&trace_event_count_heap, &trace_id);
@@ -779,6 +782,7 @@ int trace_exit_read_common(void *ctx, __u64 id, __u32 pid, __u16 is_tls, long in
     e->statement_id = 0;
     e->payload_size = 0;
     e->trace_id = 0;
+    e->is_tls = is_tls;
 
 	__u8 b[8];
 //	bpf_read(payload, b);

+ 11 - 2
ebpftracer/l7/http.go

@@ -21,7 +21,7 @@ func ParseHttp(payload []byte) (string, string) {
 	return string(method), string(uri)
 }
 
-func ParseHttpHost(payload []byte) (string, string, string, uint16) {
+func ParseHttpHost(payload []byte, isTls bool) (string, string, string, uint16) {
 	method, rest, ok := bytes.Cut(payload, space)
 	if !ok {
 		return "", "", "", 0
@@ -52,7 +52,16 @@ func ParseHttpHost(payload []byte) (string, string, string, uint16) {
 	hostPort := string(bytes.TrimSpace(hostPortBts))
 	hostParts := strings.Split(hostPort, ":")
 	host := hostParts[0]
-	port := uint16(80) // Default port
+
+	// 根据 TLS 标识设置默认端口
+	var port uint16
+	if isTls {
+		port = 443 // HTTPS 默认端口
+	} else {
+		port = 80 // HTTP 默认端口
+	}
+
+	// 如果 Host 头部明确指定了端口,使用指定的端口
 	if len(hostParts) > 1 {
 		port64, err := strconv.ParseUint(hostParts[1], 10, 16)
 		if err == nil {

+ 1 - 0
ebpftracer/l7/l7.go

@@ -214,4 +214,5 @@ type RequestData struct {
 		TypeFrom       string
 	}
 	ErrorMsg string
+	IsTls    bool
 }

+ 2 - 0
ebpftracer/tracer.go

@@ -541,6 +541,7 @@ type l7Event struct {
 	DAddr               HashByte16
 	ComponentSport      uint16
 	ComponentDport      uint16
+	IsTls               uint16
 	ComponentSAddr      HashByte16
 	ComponentDAddr      HashByte16
 	AssumedAppId        HashByte
@@ -781,6 +782,7 @@ func runEventsReader(name string, r *perf.Reader, ch chan<- Event, typ perfMapTy
 				ComponentSAddr: ipPort(v.ComponentSAddr, v.ComponentSport),
 				ComponentDAddr: ipPort(v.ComponentDAddr, v.ComponentDport),
 				ErrorMsg:       strings.TrimRight(string(v.ErrorMsg[:]), "\x00"),
+				IsTls:          v.IsTls > 0,
 			}
 			if req.Protocol == l7.ProtocolHTTP {
 				klog.Debugf("runEventsReader ComponentSAddr.String %s", req.ComponentSAddr.String())

+ 9 - 4
pkg/go.opentelemetry.io/otel/exporters/otlp/otlptrace/apm_exporter.go

@@ -4,10 +4,6 @@ import (
 	"crypto/md5"
 	"encoding/json"
 	"fmt"
-	. "github.com/coroot/coroot-node-agent/ebpftracer"
-	"github.com/coroot/coroot-node-agent/ebpftracer/l7"
-	"github.com/coroot/coroot-node-agent/utils"
-	klog "github.com/sirupsen/logrus"
 	"math"
 	"net/url"
 	"sort"
@@ -15,6 +11,11 @@ import (
 	"strings"
 	"sync"
 
+	. "github.com/coroot/coroot-node-agent/ebpftracer"
+	"github.com/coroot/coroot-node-agent/ebpftracer/l7"
+	"github.com/coroot/coroot-node-agent/utils"
+	klog "github.com/sirupsen/logrus"
+
 	"go.opentelemetry.io/otel/exporters/otlp/otlptrace/internal/tracetransform"
 	tracesdk "go.opentelemetry.io/otel/sdk/trace"
 	tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
@@ -847,6 +848,10 @@ func buildHttpMapFromEvent(mNode *MapInfoT, event tracesdk.Event) {
 			mNode.SrcAddr = attr.Value.AsString()
 		case "http.destination_addr":
 			mNode.DestinationAddr = attr.Value.AsString()
+		case "http.is_tls":
+			if attr.Value.AsBool() {
+				mNode.Schema = "https"
+			}
 		}
 	}
 	mNode.MethodName = fmt.Sprintf("%s %s %s:%d%s", "HTTP", method, mNode.Ip, mNode.Port, mNode.Uri)

+ 1 - 0
tracing/apm_tracing.go

@@ -642,6 +642,7 @@ func (t *Trace) HttpTraceRequestEvent(method, path, ip string, port uint16, r *l
 		attribute.Int("http.port", int(port)),
 		attribute.String("http.src_addr", r.ComponentSAddr.String()),
 		attribute.String("http.destination_addr", r.ComponentDAddr.String()),
+		attribute.Bool("http.is_tls", r.IsTls),
 	)
 	t.appendTimestamp(&attr, r.StartAt, r.EndAt, r.Duration.Nanoseconds())
 	t.createTraceEvent(l7.ProtocolHTTP.String(), ebpftracer.EventTypeL7Request.Int(), l7.ProtocolHTTP.Int(), attr...)