diff --git a/containers/container.go b/containers/container.go index 9139fd44..01ed8a6f 100644 --- a/containers/container.go +++ b/containers/container.go @@ -609,6 +609,12 @@ func (c *Container) onL7Request(pid uint32, fd uint64, timestamp uint64, r *l7.R return } + if conn.dstWorkload.Namespace == "external" && (r.Protocol == l7.ProtocolHTTP || r.Protocol == l7.ProtocolHTTP2) { + request, error := l7.ParseHttpRequest(string(r.Payload)) + if error == nil { + conn.dstWorkload.Name = request.Host + } + } stats := c.l7Stats.get(r.Protocol, conn.Dest, conn.ActualDest, r, conn.srcWorkload, conn.dstWorkload, conn.actualDestWorkload) trace := tracing.NewTrace(string(c.id), conn.ActualDest, conn.srcWorkload, conn.dstWorkload, conn.actualDestWorkload) switch r.Protocol { @@ -616,7 +622,7 @@ func (c *Container) onL7Request(pid uint32, fd uint64, timestamp uint64, r *l7.R stats.observe(r.Status.Http(), "", r.Duration) method, path := l7.ParseHttp(r.Payload) payload := "" - if int(r.PayloadSize) < 2048 { + if int(r.PayloadSize) < payloadThreshold { payload = string(r.Payload) } trace.HttpRequest(method, path, r.Status, r.Duration, r.PayloadSize, payload) diff --git a/containers/l7.go b/containers/l7.go index f4782eea..2ec65278 100644 --- a/containers/l7.go +++ b/containers/l7.go @@ -71,6 +71,12 @@ func (s L7Stats) get(protocol l7.Protocol, destination, actualDestination netadd method, path := l7.ParseHttp(r.Payload) constLabels["path"] = path constLabels["method"] = method + if dstWorkload.Namespace == "external" { + request, error := l7.ParseHttpRequest(string(r.Payload)) + if error == nil { + constLabels["destination_workload_name"] = request.Host + } + } hOpts := L7Latency[protocol] m.Latency = prometheus.NewHistogram( prometheus.HistogramOpts{Name: hOpts.Name, Help: hOpts.Help, ConstLabels: constLabels}, diff --git a/containers/registry.go b/containers/registry.go index fcef96d6..89ab04a7 100644 --- a/containers/registry.go +++ b/containers/registry.go @@ -287,7 +287,6 @@ func (r *Registry) getOrCreateContainer(pid uint32) *Container { return nil } id := calcId(cg, md) - klog.Infof("calculated container id %d -> %s -> %s", pid, cg.Id, id) if id == "" { if cg.Id == "/init.scope" && pid != 1 { klog.InfoS("ignoring without persisting", "cg", cg.Id, "pid", pid) @@ -318,7 +317,6 @@ func (r *Registry) getOrCreateContainer(pid uint32) *Container { return nil } - klog.InfoS("detected a new container", "pid", pid, "cg", cg.Id, "id", id) if err := prometheus.WrapRegistererWith(prometheus.Labels{"container_id": string(id)}, r.reg).Register(c); err != nil { klog.Warningln("failed to register container:", err) return nil diff --git a/ebpftracer/l7/http.go b/ebpftracer/l7/http.go index 1ffa1156..a5f31be8 100644 --- a/ebpftracer/l7/http.go +++ b/ebpftracer/l7/http.go @@ -1,9 +1,11 @@ package l7 import ( + "bufio" "bytes" "encoding/base64" "log" + "net/http" "regexp" "strings" ) @@ -49,6 +51,14 @@ func ParseHttpAndRest(payload []byte) (string, string, string, string) { return string(method), string(uri), string(headers), string(d) } +func ParseHttpRequest(request string) (*http.Request, error) { + request = strings.ReplaceAll(request, "\\n", "\r\n") + req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(request))) + if err != nil { + return nil, err + } + return req, nil +} func SanitizeString(input string) string { // Regular expression patterns to match various sensitive data formats sensitivePatterns := []*regexp.Regexp{ diff --git a/ebpftracer/l7/l7_test.go b/ebpftracer/l7/l7_test.go index 6c1f60a7..aa47237b 100644 --- a/ebpftracer/l7/l7_test.go +++ b/ebpftracer/l7/l7_test.go @@ -88,3 +88,9 @@ func TestParseMongo(t *testing.T) { binary.LittleEndian.PutUint32(payload[mongoHeaderLength+mongoSectionKindLength:], dataSize+1) assert.Equal(t, ``, ParseMongo(payload)) } + +func TestParseRequest(t *testing.T) { + m, _ := ParseHttpRequest(`HEAD /1 HTTP/1.1\nHost: 127.0.0.1\nUser-Agent: curl/8.0.1\nAccept: */*\n\nxzxxxxxxzx`) + assert.Equal(t, "HEAD", m.Method) + assert.Equal(t, "127.0.0.1", m.Host) +} diff --git a/prom/agent.go b/prom/agent.go index 1b819c13..816de4a0 100644 --- a/prom/agent.go +++ b/prom/agent.go @@ -20,7 +20,7 @@ import ( const ( RemoteFlushDeadline = time.Minute - jobName = "coroot-node-agent" + jobName = "nudgebee-node-agent" RemoteWriteTimeout = 30 * time.Second ) diff --git a/tracing/tracing.go b/tracing/tracing.go index 17fd132b..2082adb5 100644 --- a/tracing/tracing.go +++ b/tracing/tracing.go @@ -66,7 +66,7 @@ func Init(machineId, hostname, version string) { semconv.ContainerID(containerId), )), ) - return provider.Tracer("coroot-node-agent", trace.WithInstrumentationVersion(version)) + return provider.Tracer("nudgebee-node-agent", trace.WithInstrumentationVersion(version)) } }