From ad0ce685627784e8fd247d5af06b23c69b96bec4 Mon Sep 17 00:00:00 2001 From: Raman Kumar Date: Tue, 4 Mar 2025 08:02:53 +0530 Subject: [PATCH 1/2] fix: add trace in alert label --- containers/container.go | 29 ++++++++++++++++++++++++----- containers/l7.go | 5 ++++- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/containers/container.go b/containers/container.go index a8541785..ed68665c 100644 --- a/containers/container.go +++ b/containers/container.go @@ -749,7 +749,30 @@ func (c *Container) onL7Request(pid uint32, fd uint64, timestamp uint64, r *l7.R } } - stats := c.l7Stats.get(r.Protocol, conn.DestinationKey, r, conn.srcWorkload, conn.DestinationKey.GetDestinationWorkload(), conn.DestinationKey.GetActualDestinationWorkload()) + headers := http.Header{} + if r.Protocol == l7.ProtocolHTTP { + req, err := l7.ParseHTTPRequest(r.Payload) + if err == nil && req != nil && req.Header != nil { + headers = req.Header + } + } + + traceId := "" + if headers != nil { + traceIdHeaders := strings.Split(*flags.TraceIdHeaders, ",") + for _, header := range traceIdHeaders { + if id := headers.Get(header); id != "" { + // check for lowercase header + traceId = id + break + } else if id := headers.Get(strings.ToLower(header)); id != "" { + traceId = id + break + } + } + } + + stats := c.l7Stats.get(r.Protocol, conn.DestinationKey, r, conn.srcWorkload, conn.DestinationKey.GetDestinationWorkload(), conn.DestinationKey.GetActualDestinationWorkload(), traceId) trace := c.tracer.NewTrace(conn.DestinationKey.ActualDestinationIfKnown(), conn.srcWorkload, conn.DestinationKey.GetDestinationWorkload(), conn.DestinationKey.GetActualDestinationWorkload()) switch r.Protocol { @@ -760,7 +783,6 @@ func (c *Container) onL7Request(pid uint32, fd uint64, timestamp uint64, r *l7.R uri := "" response := "" host := conn.dstWorkload.Name - headers := http.Header{} req, err := l7.ParseHTTPRequest(r.Payload) if err != nil { log.Printf("Failed to parse payload %s, %q", err, string(r.Payload)) @@ -779,9 +801,6 @@ func (c *Container) onL7Request(pid uint32, fd uint64, timestamp uint64, r *l7.R klog.Warningf("Non-utf8 characters in uri %q", req.URL.Path) uri = string([]rune(req.URL.Path)) } - if req.Header != nil { - headers = req.Header - } } if r.Response != nil { response = base64.StdEncoding.EncodeToString(r.Response) diff --git a/containers/l7.go b/containers/l7.go index 274bd093..9ab3d93b 100644 --- a/containers/l7.go +++ b/containers/l7.go @@ -38,7 +38,7 @@ func (m *L7Metrics) observe(status, method string, duration time.Duration) { type L7Stats map[l7.Protocol]map[common.DestinationKey]*L7Metrics // protocol -> dst:actual_dst -> metrics -func (s L7Stats) get(protocol l7.Protocol, key common.DestinationKey, r *l7.RequestData, srcWorkload common.Workload, dstWorkload common.Workload, actualDstWorkload common.Workload) *L7Metrics { +func (s L7Stats) get(protocol l7.Protocol, key common.DestinationKey, r *l7.RequestData, srcWorkload common.Workload, dstWorkload common.Workload, actualDstWorkload common.Workload, traceId string) *L7Metrics { if protocol == l7.ProtocolHTTP2 { protocol = l7.ProtocolHTTP } @@ -63,6 +63,9 @@ func (s L7Stats) get(protocol l7.Protocol, key common.DestinationKey, r *l7.Requ "actual_destination_workload_name": actualDstWorkload.Name, "actual_destination_workload_namespace": actualDstWorkload.Namespace, } + if traceId != "" { + constLabels["trace_id"] = traceId + } labels := []string{"status"} switch protocol { case l7.ProtocolRabbitmq, l7.ProtocolNats: From a867d9c199ce211ef785cc7b5637fc3a47b1fe6e Mon Sep 17 00:00:00 2001 From: Raman Kumar Date: Tue, 4 Mar 2025 11:43:03 +0530 Subject: [PATCH 2/2] fix: made ParseHTTPRequest http request one time --- containers/container.go | 24 +++++++----------------- tracing/tracing.go | 29 +++++++++++++++++------------ 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/containers/container.go b/containers/container.go index ed68665c..b0dadf06 100644 --- a/containers/container.go +++ b/containers/container.go @@ -749,32 +749,23 @@ func (c *Container) onL7Request(pid uint32, fd uint64, timestamp uint64, r *l7.R } } - headers := http.Header{} + // Parse HTTP request once if needed + var req *http.Request + var err error + var headers http.Header = http.Header{} if r.Protocol == l7.ProtocolHTTP { - req, err := l7.ParseHTTPRequest(r.Payload) + req, err = l7.ParseHTTPRequest(r.Payload) if err == nil && req != nil && req.Header != nil { headers = req.Header } } + trace := c.tracer.NewTrace(conn.DestinationKey.ActualDestinationIfKnown(), conn.srcWorkload, conn.DestinationKey.GetDestinationWorkload(), conn.DestinationKey.GetActualDestinationWorkload()) traceId := "" if headers != nil { - traceIdHeaders := strings.Split(*flags.TraceIdHeaders, ",") - for _, header := range traceIdHeaders { - if id := headers.Get(header); id != "" { - // check for lowercase header - traceId = id - break - } else if id := headers.Get(strings.ToLower(header)); id != "" { - traceId = id - break - } - } + traceId = trace.ExtractTraceId(headers) } - stats := c.l7Stats.get(r.Protocol, conn.DestinationKey, r, conn.srcWorkload, conn.DestinationKey.GetDestinationWorkload(), conn.DestinationKey.GetActualDestinationWorkload(), traceId) - - trace := c.tracer.NewTrace(conn.DestinationKey.ActualDestinationIfKnown(), conn.srcWorkload, conn.DestinationKey.GetDestinationWorkload(), conn.DestinationKey.GetActualDestinationWorkload()) switch r.Protocol { case l7.ProtocolHTTP: stats.observe(r.Status.Http(), "", r.Duration) @@ -783,7 +774,6 @@ func (c *Container) onL7Request(pid uint32, fd uint64, timestamp uint64, r *l7.R uri := "" response := "" host := conn.dstWorkload.Name - req, err := l7.ParseHTTPRequest(r.Payload) if err != nil { log.Printf("Failed to parse payload %s, %q", err, string(r.Payload)) method, uri = l7.ParseHttp(r.Payload) diff --git a/tracing/tracing.go b/tracing/tracing.go index 2fe68f31..e3d073da 100644 --- a/tracing/tracing.go +++ b/tracing/tracing.go @@ -195,6 +195,22 @@ func ParseTraceIdHeaders(traceId string) string { return traceId } +func (t *Trace) ExtractTraceId(headers http.Header) string { + if headers == nil { + return "" + } + + traceIdHeaders := strings.Split(*flags.TraceIdHeaders, ",") + for _, header := range traceIdHeaders { + if id := headers.Get(header); id != "" { + return id + } else if id := headers.Get(strings.ToLower(header)); id != "" { + return id + } + } + return "" +} + func (t *Trace) HttpRequest(method, path string, status l7.Status, duration time.Duration, requestSize uint64, payload string, headers http.Header, response string, host string) { if t == nil || method == "" { return @@ -220,22 +236,11 @@ func (t *Trace) HttpRequest(method, path string, status l7.Status, duration time requestHost = host } - traceId := "" if headers != nil { requestHeaders = l7.ConvertHeadersToBase64String(headers) - traceIdHeaders := strings.Split(*flags.TraceIdHeaders, ",") - for _, header := range traceIdHeaders { - if id := headers.Get(header); id != "" { - // check for lowercase header - traceId = id - break - } else if id := headers.Get(strings.ToLower(header)); id != "" { - traceId = id - break - } - } } + traceId := t.ExtractTraceId(headers) t.createSpan(method, duration, status >= 400, traceId, semconv.HTTPURL(fmt.Sprintf("http://%s%s", requestHost, requestPath)),