Skip to content

Commit

Permalink
feat: send request/response body size (#80)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?

- Closes #77 

## Short description of the changes
- Use `Content-Length` header to send request / response body size

## How to verify that this has the expected result
<img width="971" alt="Screenshot 2023-08-17 at 9 50 12 AM"
src="https://github.com/honeycombio/honeycomb-ebpf-agent/assets/8810222/152966eb-9df4-4000-9cf1-238994f52422">
  • Loading branch information
pkanal committed Aug 17, 2023
1 parent 2b20759 commit 97ddeed
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"fmt"
"io"
"os"
"os/signal"
"strconv"
"syscall"

"github.com/honeycombio/ebpf-agent/assemblers"
Expand Down Expand Up @@ -121,22 +121,30 @@ func handleHttpEvents(events chan assemblers.HttpEvent, client *kubernetes.Clien

// request attributes
if event.Request != nil {
bodySizeString := event.Request.Header.Get("Content-Length")
bodySize, _ := strconv.ParseInt(bodySizeString, 10, 64)
ev.AddField("name", fmt.Sprintf("HTTP %s", event.Request.Method))
ev.AddField(string(semconv.HTTPMethodKey), event.Request.Method)
ev.AddField(string(semconv.HTTPURLKey), event.Request.RequestURI)
ev.AddField("http.request.body", fmt.Sprintf("%v", event.Request.Body))
ev.AddField("http.request.headers", fmt.Sprintf("%v", event.Request.Header))
ev.AddField(string(semconv.UserAgentOriginalKey), event.Request.Header.Get("User-Agent"))
ev.AddField("http.request.body.size", bodySize)
} else {
ev.AddField("name", "HTTP")
ev.AddField("http.request.missing", "no request on this event")
}

// response attributes
if event.Response != nil {
bodySizeString := event.Response.Header.Get("Content-Length")
bodySize, _ := strconv.ParseInt(bodySizeString, 10, 64)

ev.AddField(string(semconv.HTTPStatusCodeKey), event.Response.StatusCode)
ev.AddField("http.response.body", event.Response.Body)
ev.AddField("http.response.headers", event.Response.Header)
ev.AddField("http.response.body.size", bodySize)

} else {
ev.AddField("http.response.missing", "no response on this event")
}
Expand All @@ -145,12 +153,6 @@ func handleHttpEvents(events chan assemblers.HttpEvent, client *kubernetes.Clien
k8sEventAttrs := utils.GetK8sEventAttrs(client, event.SrcIp, event.DstIp)
ev.Add(k8sEventAttrs)

//TODO: Body size produces a runtime error, commenting out for now.
// requestSize := getBodySize(event.request.Body)
// ev.AddField("http.request.body.size", requestSize)
// responseSize := getBodySize(event.response.Body)
// ev.AddField("http.response.body.size", responseSize)

err := ev.Send()
if err != nil {
log.Debug().
Expand All @@ -161,17 +163,6 @@ func handleHttpEvents(events chan assemblers.HttpEvent, client *kubernetes.Clien
}
}

func getBodySize(r io.ReadCloser) int {
length := 0
b, err := io.ReadAll(r)
if err == nil {
length = len(b)
r.Close()
}

return length
}

func getEnvOrDefault(key string, defaultValue string) string {
if value, ok := os.LookupEnv(key); ok {
return value
Expand Down

0 comments on commit 97ddeed

Please sign in to comment.