Skip to content
Closed
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
6 changes: 4 additions & 2 deletions bpf/common/trace_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ struct callback_ctx {
u8 _pad[4];
};

enum { k_tp_pos_not_found = 0xffffffffu };

static unsigned char *hex = (unsigned char *)"0123456789abcdef";
static unsigned char *reverse_hex =
(unsigned char *)"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
Expand Down Expand Up @@ -101,13 +103,13 @@ static __always_inline unsigned char *bpf_strstr_tp_loop(unsigned char *buf, con
return NULL;
}

struct callback_ctx data = {.buf = buf, .pos = 0};
struct callback_ctx data = {.buf = buf, .pos = k_tp_pos_not_found};

const u32 nr_loops = (u32)buf_len;

bpf_loop(nr_loops, tp_match, &data, 0);

if (data.pos) {
if (data.pos != k_tp_pos_not_found) {
return (data.pos > (TRACE_BUF_SIZE - TRACE_PARENT_HEADER_LEN)) ? NULL : &buf[data.pos];
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/ebpf/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ func FixupSpec(spec *ebpf.CollectionSpec, overrideKernelVersion bool) {
spec.Programs["obi_continue_protocol_http"] = spec.Programs["obi_continue_protocol_http_legacy"]
spec.Programs["obi_continue_protocol_http"].Name = "obi_continue_protocol_http"
}

dummy := &ebpf.ProgramSpec{
Name: "obi_dummy",
Type: ebpf.Kprobe,
Expand All @@ -530,10 +531,11 @@ func FixupSpec(spec *ebpf.CollectionSpec, overrideKernelVersion bool) {
},
License: "MIT",
}

// Hack: insert dummy unused programs in order to be able to use bpf2go generated struct to load
// the collection.
spec.Programs["obi_protocol_http_legacy"] = dummy
spec.Programs["obi_continue_protocol_http_legacy"] = dummy
spec.Programs["obi_protocol_http_legacy"] = dummy.Copy()
spec.Programs["obi_continue_protocol_http_legacy"] = dummy.Copy()
}

// Injectable for tests
Expand Down
27 changes: 27 additions & 0 deletions pkg/ebpf/common/http_transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,30 @@ func TestHTTPRequestResponseToSpan_OpenAIEmbeddingDetectedByOpenAISpan(t *testin
require.NotNil(t, span.GenAI.OpenAI, "span.GenAI.OpenAI should be set")
assert.Equal(t, request.EmbeddingOperationName, span.GenAI.OpenAI.OperationName)
}

func TestToRequestTraceMissingLargeBuffers(t *testing.T) {
fltr := TestPidsFilter{services: map[app.PID]svc.Attrs{}}
var record BPFHTTPInfo

record.Type = 1
record.ReqMonotimeNs = 123450
record.StartMonotimeNs = 123456
record.EndMonotimeNs = 789012
record.Status = 200
record.HasLargeBuffers = 1 // This will trigger the missing large buffers branch
record.ConnInfo.D_port = 1
record.ConnInfo.S_addr = [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 168, 0, 1}
record.ConnInfo.D_addr = [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 8, 8, 8, 8}
copy(record.Buf[:], "GET /hello HTTP/1.1\r\nHost: example.com\r\n\r\n")

buf := new(bytes.Buffer)
err := binary.Write(buf, binary.LittleEndian, &record)
require.NoError(t, err)

pctx := NewEBPFParseContext(nil, nil, nil)
result, _, err := ReadHTTPInfoIntoSpan(pctx, &ringbuf.Record{RawSample: buf.Bytes()}, &fltr)
require.NoError(t, err)

require.Equal(t, "/hello", result.Path)
require.Equal(t, 200, result.Status)
}