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
21 changes: 15 additions & 6 deletions containers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,9 +749,23 @@ 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())
// 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)
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 := ""
Comment thread
RamanKharchee marked this conversation as resolved.
if headers != nil {
traceId = trace.ExtractTraceId(headers)
}
stats := c.l7Stats.get(r.Protocol, conn.DestinationKey, r, conn.srcWorkload, conn.DestinationKey.GetDestinationWorkload(), conn.DestinationKey.GetActualDestinationWorkload(), traceId)
switch r.Protocol {
case l7.ProtocolHTTP:
stats.observe(r.Status.Http(), "", r.Duration)
Expand All @@ -760,8 +774,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))
method, uri = l7.ParseHttp(r.Payload)
Expand All @@ -779,9 +791,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)
Expand Down
5 changes: 4 additions & 1 deletion containers/l7.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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:
Expand Down
29 changes: 17 additions & 12 deletions tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)),
Expand Down