Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion containers/l7.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package containers

import (
"log"
"time"

"github.com/coroot/coroot-node-agent/common"
Expand Down Expand Up @@ -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]
Expand Down
25 changes: 11 additions & 14 deletions ebpftracer/l7/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package l7
import (
"bytes"
"encoding/base64"
"errors"
"log"
"regexp"
"strings"
Expand Down Expand Up @@ -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
}

Expand Down