diff --git a/containers/l7.go b/containers/l7.go index 2fb5be74..d5b2d231 100644 --- a/containers/l7.go +++ b/containers/l7.go @@ -1,6 +1,7 @@ package containers import ( + "log" "time" "github.com/coroot/coroot-node-agent/common" @@ -73,8 +74,10 @@ func (s L7Stats) get(protocol l7.Protocol, destination, actualDestination netadd constLabels["method"] = method if dstWorkload.Namespace == "external" { host, err := l7.ParseHostFromHttpRequest(string(r.Payload)) - if err == nil { + if host != "" { constLabels["destination_workload_name"] = host + } else { + log.Printf("Failed to parse host %s , %q", host, err) } } hOpts := L7Latency[protocol] diff --git a/ebpftracer/l7/http.go b/ebpftracer/l7/http.go index 64ff3dfc..2b0623fd 100644 --- a/ebpftracer/l7/http.go +++ b/ebpftracer/l7/http.go @@ -3,7 +3,6 @@ package l7 import ( "bytes" "encoding/base64" - "errors" "log" "regexp" "strings" @@ -50,28 +49,26 @@ func ParseHttpAndRest(payload []byte) (string, string, string, string) { return string(method), string(uri), string(headers), string(d) } -func ParseHostFromHttpRequest(request string) (string, error) { - // Split the request string into lines - lines := strings.Split(request, "\n") +func ParseHostFromHttpRequest(input string) (string, error) { + // Split the input string by newline characters + lines := strings.Split(input, "\r\n") - // Search for the Host header within the lines + // Initialize host variable var host string + + // Iterate through each line for _, line := range lines { + // Check if the line starts with "Host:" if strings.HasPrefix(line, "Host:") { - parts := strings.SplitN(line, ": ", 2) + // Split the line by colon and trim the leading/trailing whitespace + parts := strings.Split(line, ":") if len(parts) == 2 { - host = parts[1] - break + host = strings.TrimSpace(parts[1]) } + break } } - // Check if the Host header is found - if host == "" { - err := errors.New("host header not found") - return "", err - } - return host, nil }