Skip to content

Commit

Permalink
Change HTTP1.1's SpanName from URI to Path.
Browse files Browse the repository at this point in the history
  • Loading branch information
StLeoX committed Jan 10, 2025
1 parent 4f95b6b commit 93ad8d7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
4 changes: 2 additions & 2 deletions containers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,8 @@ func (c *Container) onL7Request(pid uint32, fd uint64, timestamp uint64, r *l7.R
switch r.Protocol {
case l7.ProtocolHTTP:
stats.observe(r.Status.Http(), "", r.Duration)
method, path := l7.ParseHttp(r.Payload)
trace.HttpRequest(method, path, r.Status, r.Duration)
method, uri, path := l7.ParseHttp(r.Payload)
trace.HttpRequest(method, uri, path, r.Status, r.Duration)
case l7.ProtocolHTTP2:
if conn.http2Parser == nil {
conn.http2Parser = l7.NewHttp2Parser()
Expand Down
10 changes: 6 additions & 4 deletions ebpftracer/l7/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import (
"bytes"
)

func ParseHttp(payload []byte) (string, string) {
// https://en.wikipedia.org/wiki/Uniform_Resource_Identifier#Syntax
func ParseHttp(payload []byte) (string, string, string) {
method, rest, ok := bytes.Cut(payload, space)
if !ok {
return "", ""
return "", "", ""
}
if !isHttpMethod(string(method)) {
return "", ""
return "", "", ""
}
uri, _, ok := bytes.Cut(rest, space)
if !ok {
uri = append(uri, []byte("...")...)
}
return string(method), string(uri)
path, _, _ := bytes.Cut(uri, []byte{'?'})
return string(method), string(uri), string(path)
}
18 changes: 13 additions & 5 deletions ebpftracer/l7/l7_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@ import (
)

func TestParseHttp(t *testing.T) {
m, p := ParseHttp([]byte(`HEAD /1 HTTP/1.1\nHost: 127.0.0.1\nUser-Agent: curl/8.0.1\nAccept: */*\n\nxzxxxxxxzx`))
var m, u, p string

m, u, p = ParseHttp([]byte(`HEAD /1 HTTP/1.1\nHost: 127.0.0.1\nUser-Agent: curl/8.0.1\nAccept: */*\n\nxzxxxxxxzx`))
assert.Equal(t, "HEAD", m)
assert.Equal(t, "/1", p)
assert.Equal(t, "/1", u)

m, p = ParseHttp([]byte(`GET /too-long-uri`))
m, u, p = ParseHttp([]byte(`GET /greeting?name=foo-svc HTTP/1.1\n\n`))
assert.Equal(t, "GET", m)
assert.Equal(t, "/too-long-uri...", p)
assert.Equal(t, "/greeting?name=foo-svc", u)
assert.Equal(t, "/greeting", p)

m, u, p = ParseHttp([]byte(`POST /v1/profiles?container.id=%2Fdocker%2Fcoroot-local-loo-svc-1&host.id=b0d275fb46fa48c6820be57edaa22cf5 HTTP/1.1\n{{}}\n`))
assert.Equal(t, "POST", m)
assert.Equal(t, "/v1/profiles", p)

}

func Test_parseMemcached(t *testing.T) {
func TestParseMemcached(t *testing.T) {
cmd, items := ParseMemcached(append([]byte(`incr 1111 2222`), '\r', '\n'))
assert.Equal(t, "incr", cmd)
assert.Equal(t, []string{"1111"}, items)
Expand Down
4 changes: 2 additions & 2 deletions tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ func (t *Trace) createSpan(name string, duration time.Duration, error bool, attr
span.End(trace.WithTimestamp(end))
}

func (t *Trace) HttpRequest(method, path string, status l7.Status, duration time.Duration) {
func (t *Trace) HttpRequest(method, uri, path string, status l7.Status, duration time.Duration) {
if t == nil || method == "" {
return
}
t.createSpan(method, duration, status >= 400,
semconv.HTTPURL(fmt.Sprintf("http://%s%s", t.destination.String(), path)),
semconv.HTTPURL(fmt.Sprintf("http://%s%s", t.destination.String(), uri)),
semconv.HTTPMethod(method),
semconv.HTTPStatusCode(int(status)),
)
Expand Down

0 comments on commit 93ad8d7

Please sign in to comment.